Merged changes in the trunk up to revision 53729.
[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 ~/blenderSVN/blender/build_files/cmake/cmake_netbeans_project.py ~/blenderSVN/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 SVN if we can!
71             PROJECT_NAME = project_name_get(SOURCE_DIR)
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,mm</cpp-extensions>\n')
109         f.write('            <header-extensions>h,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('        </data>\n')
122         f.write('    </configuration>\n')
123         f.write('</project>\n')
124
125         f = open(join(PROJECT_DIR_NB, "configurations.xml"), 'w')
126
127         f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
128         f.write('<configurationDescriptor version="79">\n')
129         f.write('  <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">\n')
130         f.write('    <df name="blender" root="%s">\n' % SOURCE_DIR)  # base_root_rel
131
132         # write files!
133         files_rel_local = [normpath(relpath(join(CMAKE_DIR, path), SOURCE_DIR)) for path in files_rel]
134         files_rel_hierarchy = file_list_to_nested(files_rel_local)
135         # print(files_rel_hierarchy)
136
137         def write_df(hdir, ident):
138             dirs = []
139             files = []
140             for key, item in sorted(hdir.items()):
141                 if item is None:
142                     files.append(key)
143                 else:
144                     dirs.append((key, item))
145
146             for key, item in dirs:
147                 f.write('%s  <df name="%s">\n' % (ident, key))
148                 write_df(item, ident + "  ")
149                 f.write('%s  </df>\n' % ident)
150
151             for key in files:
152                 f.write('%s<in>%s</in>\n' % (ident, key))
153
154         write_df(files_rel_hierarchy, ident="    ")
155
156         f.write('    </df>\n')
157
158         f.write('    <logicalFolder name="ExternalFiles"\n')
159         f.write('                   displayName="Important Files"\n')
160         f.write('                   projectFiles="false"\n')
161         f.write('                   kind="IMPORTANT_FILES_FOLDER">\n')
162         # f.write('      <itemPath>../GNUmakefile</itemPath>\n')
163         f.write('    </logicalFolder>\n')
164
165         f.write('  </logicalFolder>\n')
166         # default, but this dir is infact not in blender dir so we can ignore it
167         # f.write('  <sourceFolderFilter>^(nbproject)$</sourceFolderFilter>\n')
168         f.write('  <sourceFolderFilter>^(nbproject|__pycache__|.*\.py|.*\.html|.*\.blend)$</sourceFolderFilter>\n')
169
170         f.write('  <sourceRootList>\n')
171         f.write('    <Elem>%s</Elem>\n' % SOURCE_DIR)  # base_root_rel
172         f.write('  </sourceRootList>\n')
173
174         f.write('  <projectmakefile>Makefile</projectmakefile>\n')
175
176         # paths again
177         f.write('  <confs>\n')
178         f.write('    <conf name="Default" type="0">\n')
179
180         f.write('      <toolsSet>\n')
181         f.write('        <remote-sources-mode>LOCAL_SOURCES</remote-sources-mode>\n')
182         f.write('        <compilerSet>default</compilerSet>\n')
183         f.write('      </toolsSet>\n')
184         f.write('      <makefileType>\n')
185
186         f.write('        <makeTool>\n')
187         f.write('          <buildCommandWorkingDir>.</buildCommandWorkingDir>\n')
188         f.write('          <buildCommand>${MAKE} -f Makefile</buildCommand>\n')
189         f.write('          <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>\n')
190         f.write('          <executablePath>./bin/blender</executablePath>\n')
191
192         def write_toolinfo():
193             f.write('            <incDir>\n')
194             for inc in includes:
195                 f.write('              <pElem>%s</pElem>\n' % inc)
196             f.write('            </incDir>\n')
197             f.write('            <preprocessorList>\n')
198             for cdef in defines:
199                 f.write('              <Elem>%s</Elem>\n' % cdef)
200             f.write('            </preprocessorList>\n')
201
202         f.write('          <cTool>\n')
203         write_toolinfo()
204         f.write('          </cTool>\n')
205
206         f.write('          <ccTool>\n')
207         write_toolinfo()
208         f.write('          </ccTool>\n')
209
210         f.write('        </makeTool>\n')
211         f.write('      </makefileType>\n')
212         # finishe makefle info
213
214         f.write('    \n')
215
216         for path in files_rel_local:
217             f.write('      <item path="%s"\n' % path)
218             f.write('            ex="false"\n')
219             f.write('            tool="1"\n')
220             f.write('            flavor="0">\n')
221             f.write('      </item>\n')
222
223         f.write('      <runprofile version="9">\n')
224         f.write('        <runcommandpicklist>\n')
225         f.write('        </runcommandpicklist>\n')
226         f.write('        <runcommand>%s</runcommand>\n' % os.path.join(CMAKE_DIR, "bin/blender"))
227         f.write('        <rundir>%s</rundir>\n' % SOURCE_DIR)
228         f.write('        <buildfirst>false</buildfirst>\n')
229         f.write('        <terminal-type>0</terminal-type>\n')
230         f.write('        <remove-instrumentation>0</remove-instrumentation>\n')
231         f.write('        <environment>\n')
232         f.write('        </environment>\n')
233         f.write('      </runprofile>\n')
234
235         f.write('    </conf>\n')
236         f.write('  </confs>\n')
237
238         # todo
239
240         f.write('</configurationDescriptor>\n')
241
242
243 def main():
244     create_nb_project_main()
245
246
247 if __name__ == "__main__":
248     main()