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