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