merge from trunk #37722
[blender.git] / build_files / cmake / cmake_netbeans_project.py
1 #!/usr/bin/env python
2
3 # $Id$
4 # ***** BEGIN GPL LICENSE BLOCK *****
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #
20 # Contributor(s): Campbell Barton, M.G. Kishalmi
21 #
22 # ***** END GPL LICENSE BLOCK *****
23
24 # <pep8 compliant>
25
26 """
27 Example linux usage
28  python .~/blenderSVN/blender/build_files/cmake/cmake_netbeans_project.py ~/blenderSVN/cmake
29
30 Windows not supported so far
31 """
32
33 from project_info import *
34
35 import os
36 from os.path import join, dirname, normpath, relpath, exists
37
38
39 def create_nb_project_main():
40     files = list(source_list(SOURCE_DIR, filename_check=is_project_file))
41     files_rel = [relpath(f, start=PROJECT_DIR) for f in files]
42     files_rel.sort()
43
44     if SIMPLE_PROJECTFILE:
45         pass
46     else:
47         includes, defines = cmake_advanced_info()
48         # for some reason it doesnt give all internal includes
49         includes = list(set(includes) | set(dirname(f) for f in files if is_c_header(f)))
50         includes.sort()
51
52         PROJECT_NAME = "Blender"
53
54         # --------------- NB spesific
55         defines = [("%s=%s" % cdef) if cdef[1] else cdef[0] for cdef in defines]
56         defines += [cdef.replace("#define", "").strip() for cdef in cmake_compiler_defines()]
57
58         def file_list_to_nested(files):
59             # convert paths to hierarchy
60             paths_nested = {}
61
62             def ensure_path(filepath):
63                 filepath_split = filepath.split(os.sep)
64
65                 pn = paths_nested
66                 for subdir in filepath_split[:-1]:
67                     pn = pn.setdefault(subdir, {})
68                 pn[filepath_split[-1]] = None
69
70             for path in files:
71                 ensure_path(path)
72             return paths_nested
73
74         PROJECT_DIR_NB = join(PROJECT_DIR, "nbproject")
75         if not exists(PROJECT_DIR_NB):
76             os.mkdir(PROJECT_DIR_NB)
77
78         # SOURCE_DIR_REL = relpath(SOURCE_DIR, PROJECT_DIR)
79
80         f = open(join(PROJECT_DIR_NB, "project.xml"), 'w')
81
82         f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
83         f.write('<project xmlns="http://www.netbeans.org/ns/project/1">\n')
84         f.write('    <type>org.netbeans.modules.cnd.makeproject</type>\n')
85         f.write('    <configuration>\n')
86         f.write('        <data xmlns="http://www.netbeans.org/ns/make-project/1">\n')
87         f.write('            <name>%s</name>\n' % PROJECT_NAME)
88         f.write('            <c-extensions>c,m</c-extensions>\n')
89         f.write('            <cpp-extensions>cpp,mm</cpp-extensions>\n')
90         f.write('            <header-extensions>h,hpp,inl</header-extensions>\n')
91         f.write('            <sourceEncoding>UTF-8</sourceEncoding>\n')
92         f.write('            <make-dep-projects/>\n')
93         f.write('            <sourceRootList>\n')
94         f.write('                <sourceRootElem>%s</sourceRootElem>\n' % SOURCE_DIR)  # base_root_rel
95         f.write('            </sourceRootList>\n')
96         f.write('            <confList>\n')
97         f.write('                <confElem>\n')
98         f.write('                    <name>Default</name>\n')
99         f.write('                    <type>0</type>\n')
100         f.write('                </confElem>\n')
101         f.write('            </confList>\n')
102         f.write('        </data>\n')
103         f.write('    </configuration>\n')
104         f.write('</project>\n')
105
106         f = open(join(PROJECT_DIR_NB, "configurations.xml"), 'w')
107
108         f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
109         f.write('<configurationDescriptor version="79">\n')
110         f.write('  <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">\n')
111         f.write('    <df name="blender" root="%s">\n' % SOURCE_DIR)  # base_root_rel
112
113         # write files!
114         files_rel_local = [normpath(relpath(join(CMAKE_DIR, path), SOURCE_DIR)) for path in files_rel]
115         files_rel_hierarchy = file_list_to_nested(files_rel_local)
116         # print(files_rel_hierarchy)
117
118         def write_df(hdir, ident):
119             dirs = []
120             files = []
121             for key, item in sorted(hdir.items()):
122                 if item is None:
123                     files.append(key)
124                 else:
125                     dirs.append((key, item))
126
127             for key, item in dirs:
128                 f.write('%s  <df name="%s">\n' % (ident, key))
129                 write_df(item, ident + "  ")
130                 f.write('%s  </df>\n' % ident)
131
132             for key in files:
133                 f.write('%s<in>%s</in>\n' % (ident, key))
134
135         write_df(files_rel_hierarchy, ident="    ")
136
137         f.write('    </df>\n')
138
139         f.write('    <logicalFolder name="ExternalFiles"\n')
140         f.write('                   displayName="Important Files"\n')
141         f.write('                   projectFiles="false"\n')
142         f.write('                   kind="IMPORTANT_FILES_FOLDER">\n')
143         # f.write('      <itemPath>../GNUmakefile</itemPath>\n')
144         f.write('    </logicalFolder>\n')
145
146         f.write('  </logicalFolder>\n')
147         # default, but this dir is infact not in blender dir so we can ignore it
148         # f.write('  <sourceFolderFilter>^(nbproject)$</sourceFolderFilter>\n')
149         f.write('  <sourceFolderFilter>^(nbproject|__pycache__|.*\.py|.*\.html|.*\.blend)$</sourceFolderFilter>\n')
150
151         f.write('  <sourceRootList>\n')
152         f.write('    <Elem>%s</Elem>\n' % SOURCE_DIR)  # base_root_rel
153         f.write('  </sourceRootList>\n')
154
155         f.write('  <projectmakefile>Makefile</projectmakefile>\n')
156
157         # paths again
158         f.write('  <confs>\n')
159         f.write('    <conf name="Default" type="0">\n')
160
161         f.write('      <toolsSet>\n')
162         f.write('        <remote-sources-mode>LOCAL_SOURCES</remote-sources-mode>\n')
163         f.write('        <compilerSet>default</compilerSet>\n')
164         f.write('      </toolsSet>\n')
165         f.write('      <makefileType>\n')
166
167         f.write('        <makeTool>\n')
168         f.write('          <buildCommandWorkingDir>.</buildCommandWorkingDir>\n')
169         f.write('          <buildCommand>${MAKE} -f Makefile</buildCommand>\n')
170         f.write('          <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>\n')
171         f.write('          <executablePath>./bin/blender</executablePath>\n')
172
173         def write_toolinfo():
174             f.write('            <incDir>\n')
175             for inc in includes:
176                 f.write('              <pElem>%s</pElem>\n' % inc)
177             f.write('            </incDir>\n')
178             f.write('            <preprocessorList>\n')
179             for cdef in defines:
180                 f.write('              <Elem>%s</Elem>\n' % cdef)
181             f.write('            </preprocessorList>\n')
182
183         f.write('          <cTool>\n')
184         write_toolinfo()
185         f.write('          </cTool>\n')
186
187         f.write('          <ccTool>\n')
188         write_toolinfo()
189         f.write('          </ccTool>\n')
190
191         f.write('        </makeTool>\n')
192         f.write('      </makefileType>\n')
193         # finishe makefle info
194
195         f.write('    \n')
196
197         for path in files_rel_local:
198             f.write('      <item path="%s"\n' % path)
199             f.write('            ex="false"\n')
200             f.write('            tool="1"\n')
201             f.write('            flavor="0">\n')
202             f.write('      </item>\n')
203
204         f.write('      <runprofile version="9">\n')
205         f.write('        <runcommandpicklist>\n')
206         f.write('        </runcommandpicklist>\n')
207         f.write('        <runcommand>%s</runcommand>\n' % os.path.join(CMAKE_DIR, "bin/blender"))
208         f.write('        <rundir>%s</rundir>\n' % SOURCE_DIR)
209         f.write('        <buildfirst>false</buildfirst>\n')
210         f.write('        <terminal-type>0</terminal-type>\n')
211         f.write('        <remove-instrumentation>0</remove-instrumentation>\n')
212         f.write('        <environment>\n')
213         f.write('        </environment>\n')
214         f.write('      </runprofile>\n')
215
216         f.write('    </conf>\n')
217         f.write('  </confs>\n')
218
219         # todo
220
221         f.write('</configurationDescriptor>\n')
222
223
224 def main():
225     create_nb_project_main()
226
227
228 if __name__ == "__main__":
229     main()