Fix project-file generators (didn't close files)
[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 from project_info import (SIMPLE_PROJECTFILE,
33                           SOURCE_DIR,
34                           CMAKE_DIR,
35                           PROJECT_DIR,
36                           source_list,
37                           is_project_file,
38                           is_c_header,
39                           # is_py,
40                           cmake_advanced_info,
41                           cmake_compiler_defines,
42                           project_name_get,
43                           )
44
45
46 import os
47 from os.path import join, dirname, normpath, relpath, exists
48
49
50 def create_nb_project_main():
51     files = list(source_list(SOURCE_DIR, filename_check=is_project_file))
52     files_rel = [relpath(f, start=PROJECT_DIR) for f in files]
53     files_rel.sort()
54
55     if SIMPLE_PROJECTFILE:
56         pass
57     else:
58         includes, defines = cmake_advanced_info()
59
60         if (includes, defines) == (None, None):
61             return
62
63         # for some reason it doesnt give all internal includes
64         includes = list(set(includes) | set(dirname(f) for f in files if is_c_header(f)))
65         includes.sort()
66
67         if 0:
68             PROJECT_NAME = "Blender"
69         else:
70             # be tricky, get the project name from git if we can!
71             PROJECT_NAME = project_name_get()
72
73         # --------------- NB spesific
74         defines = [("%s=%s" % cdef) if cdef[1] else cdef[0] for cdef in defines]
75         defines += [cdef.replace("#define", "").strip() for cdef in cmake_compiler_defines()]
76
77         def file_list_to_nested(files):
78             # convert paths to hierarchy
79             paths_nested = {}
80
81             def ensure_path(filepath):
82                 filepath_split = filepath.split(os.sep)
83
84                 pn = paths_nested
85                 for subdir in filepath_split[:-1]:
86                     pn = pn.setdefault(subdir, {})
87                 pn[filepath_split[-1]] = None
88
89             for path in files:
90                 ensure_path(path)
91             return paths_nested
92
93         PROJECT_DIR_NB = join(PROJECT_DIR, "nbproject")
94         if not exists(PROJECT_DIR_NB):
95             os.mkdir(PROJECT_DIR_NB)
96
97         # SOURCE_DIR_REL = relpath(SOURCE_DIR, PROJECT_DIR)
98
99         f = open(join(PROJECT_DIR_NB, "project.xml"), 'w')
100
101         f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
102         f.write('<project xmlns="http://www.netbeans.org/ns/project/1">\n')
103         f.write('    <type>org.netbeans.modules.cnd.makeproject</type>\n')
104         f.write('    <configuration>\n')
105         f.write('        <data xmlns="http://www.netbeans.org/ns/make-project/1">\n')
106         f.write('            <name>%s</name>\n' % PROJECT_NAME)
107         f.write('            <c-extensions>c,m</c-extensions>\n')
108         f.write('            <cpp-extensions>cpp,cxx,cc,mm</cpp-extensions>\n')
109         f.write('            <header-extensions>h,hxx,hh,hpp,inl</header-extensions>\n')
110         f.write('            <sourceEncoding>UTF-8</sourceEncoding>\n')
111         f.write('            <make-dep-projects/>\n')
112         f.write('            <sourceRootList>\n')
113         f.write('                <sourceRootElem>%s</sourceRootElem>\n' % SOURCE_DIR)  # base_root_rel
114         f.write('            </sourceRootList>\n')
115         f.write('            <confList>\n')
116         f.write('                <confElem>\n')
117         f.write('                    <name>Default</name>\n')
118         f.write('                    <type>0</type>\n')
119         f.write('                </confElem>\n')
120         f.write('            </confList>\n')
121         f.write('            <formatting>\n')
122         f.write('                <project-formatting-style>false</project-formatting-style>\n')
123         f.write('            </formatting>\n')
124         f.write('        </data>\n')
125         f.write('    </configuration>\n')
126         f.write('</project>\n')
127
128         f.close()
129
130         f = open(join(PROJECT_DIR_NB, "configurations.xml"), 'w')
131
132         f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
133         f.write('<configurationDescriptor version="94">\n')
134         f.write('  <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">\n')
135         f.write('    <df name="0" root="%s">\n' % SOURCE_DIR)  # base_root_rel
136
137         # write files!
138         files_rel_local = [normpath(relpath(join(CMAKE_DIR, path), SOURCE_DIR)) for path in files_rel]
139         files_rel_hierarchy = file_list_to_nested(files_rel_local)
140         # print(files_rel_hierarchy)
141
142         def write_df(hdir, ident):
143             dirs = []
144             files = []
145             for key, item in sorted(hdir.items()):
146                 if item is None:
147                     files.append(key)
148                 else:
149                     dirs.append((key, item))
150
151             for key, item in dirs:
152                 f.write('%s  <df name="%s">\n' % (ident, key))
153                 write_df(item, ident + "    ")
154                 f.write('%s  </df>\n' % ident)
155
156             for key in files:
157                 f.write('%s<in>%s</in>\n' % (ident, key))
158
159         write_df(files_rel_hierarchy, ident="    ")
160
161         f.write('    </df>\n')
162
163         f.write('    <logicalFolder name="ExternalFiles"\n')
164         f.write('                   displayName="Important Files"\n')
165         f.write('                   projectFiles="false"\n')
166         f.write('                   kind="IMPORTANT_FILES_FOLDER">\n')
167         # f.write('      <itemPath>../GNUmakefile</itemPath>\n')
168         f.write('    </logicalFolder>\n')
169
170         f.write('  </logicalFolder>\n')
171         # default, but this dir is infact not in blender dir so we can ignore it
172         # f.write('  <sourceFolderFilter>^(nbproject)$</sourceFolderFilter>\n')
173         f.write('  <sourceFolderFilter>^(nbproject|__pycache__|.*\.py|.*\.html|.*\.blend)$</sourceFolderFilter>\n')
174
175         f.write('  <sourceRootList>\n')
176         f.write('    <Elem>%s</Elem>\n' % SOURCE_DIR)  # base_root_rel
177         f.write('  </sourceRootList>\n')
178
179         f.write('  <projectmakefile>Makefile</projectmakefile>\n')
180
181         # paths again
182         f.write('  <confs>\n')
183         f.write('    <conf name="Default" type="0">\n')
184
185         f.write('      <toolsSet>\n')
186         f.write('        <compilerSet>default</compilerSet>\n')
187         f.write('      </toolsSet>\n')
188         f.write('      <dependencyChecking>false</dependencyChecking>\n')
189         f.write('      <rebuildPropChanged>false</rebuildPropChanged>\n')
190         f.write('      <makefileType>\n')
191
192         f.write('        <makeTool>\n')
193         f.write('          <buildCommandWorkingDir>.</buildCommandWorkingDir>\n')
194         f.write('          <buildCommand>${MAKE} -f Makefile</buildCommand>\n')
195         f.write('          <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>\n')
196         f.write('          <executablePath>./bin/blender</executablePath>\n')
197
198         def write_toolinfo():
199             f.write('            <incDir>\n')
200             for inc in includes:
201                 f.write('              <pElem>%s</pElem>\n' % inc)
202             f.write('            </incDir>\n')
203             f.write('            <preprocessorList>\n')
204             for cdef in defines:
205                 f.write('              <Elem>%s</Elem>\n' % cdef)
206             f.write('            </preprocessorList>\n')
207
208         f.write('          <cTool>\n')
209         write_toolinfo()
210         f.write('          </cTool>\n')
211
212         f.write('          <ccTool>\n')
213         write_toolinfo()
214         f.write('          </ccTool>\n')
215
216         f.write('        </makeTool>\n')
217         f.write('      </makefileType>\n')
218         # finishe makefle info
219
220         f.write('    \n')
221
222         for path in files_rel_local:
223             f.write('      <item path="%s"\n' % path)
224             f.write('            ex="false"\n')
225             f.write('            tool="1"\n')
226             f.write('            flavor2="0">\n')
227             f.write('      </item>\n')
228
229         f.write('      <runprofile version="9">\n')
230         f.write('        <runcommandpicklist>\n')
231         f.write('        </runcommandpicklist>\n')
232         f.write('        <runcommand>%s</runcommand>\n' % os.path.join(CMAKE_DIR, "bin/blender"))
233         f.write('        <rundir>%s</rundir>\n' % SOURCE_DIR)
234         f.write('        <buildfirst>false</buildfirst>\n')
235         f.write('        <terminal-type>0</terminal-type>\n')
236         f.write('        <remove-instrumentation>0</remove-instrumentation>\n')
237         f.write('        <environment>\n')
238         f.write('        </environment>\n')
239         f.write('      </runprofile>\n')
240
241         f.write('    </conf>\n')
242         f.write('  </confs>\n')
243
244         # todo
245
246         f.write('</configurationDescriptor>\n')
247
248         f.close()
249
250
251 def main():
252     create_nb_project_main()
253
254
255 if __name__ == "__main__":
256     main()