cf1da2f666f1209e16f38c519800a47d533a23bb
[blender-staging.git] / build_files / cmake / cmake_qtcreator_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 Win32 usage:
28  c:\Python32\python.exe c:\blender_dev\blender\build_files\cmake\cmake_qtcreator_project.py c:\blender_dev\cmake_build
29
30 example linux usage
31  python .~/blenderSVN/blender/build_files/cmake/cmake_qtcreator_project.py ~/blenderSVN/cmake
32 """
33
34 import sys
35 import os
36 from os.path import join, dirname, normpath, abspath, splitext, relpath, exists
37
38 base = join(os.path.dirname(__file__), "..", "..")
39 base = normpath(base)
40 base = abspath(base)
41
42 SIMPLE_PROJECTFILE = False
43
44 # get cmake path
45 CMAKE_DIR = sys.argv[-1]
46
47 if not os.path.exists(os.path.join(CMAKE_DIR, "CMakeCache.txt")):
48     CMAKE_DIR = os.getcwd()
49 if not os.path.exists(os.path.join(CMAKE_DIR, "CMakeCache.txt")):
50     print("CMakeCache.txt not found in %r or %r\n    Pass CMake build dir as an argument, or run from that dir, aborting" % (CMAKE_DIR, os.getcwd()))
51     sys.exit(1)
52
53
54 # could be either.
55 # PROJECT_DIR = base
56 PROJECT_DIR = CMAKE_DIR
57
58
59 def source_list(path, filename_check=None):
60     for dirpath, dirnames, filenames in os.walk(path):
61
62         # skip '.svn'
63         if dirpath.startswith("."):
64             continue
65
66         for filename in filenames:
67             filepath = join(dirpath, filename)
68             if filename_check is None or filename_check(filepath):
69                 yield filepath
70
71
72 # extension checking
73 def is_cmake(filename):
74     ext = splitext(filename)[1]
75     return (ext == ".cmake") or (filename.endswith("CMakeLists.txt"))
76
77
78 def is_c_header(filename):
79     ext = splitext(filename)[1]
80     return (ext in (".h", ".hpp", ".hxx"))
81
82
83 def is_py(filename):
84     ext = splitext(filename)[1]
85     return (ext == ".py")
86
87
88 def is_glsl(filename):
89     ext = splitext(filename)[1]
90     return (ext == ".glsl")
91
92
93 def is_c(filename):
94     ext = splitext(filename)[1]
95     return (ext in (".c", ".cpp", ".cxx", ".m", ".mm", ".rc"))
96
97
98 def is_c_any(filename):
99     return is_c(filename) or is_c_header(filename)
100
101
102 def is_svn_file(filename):
103     dn, fn = os.path.split(filename)
104     filename_svn = join(dn, ".svn", "text-base", "%s.svn-base" % fn)
105     return exists(filename_svn)
106
107
108 def is_project_file(filename):
109     return (is_c_any(filename) or is_cmake(filename) or is_glsl(filename))  # and is_svn_file(filename)
110
111
112 def cmake_advanced_info():
113     """ Extracr includes and defines from cmake.
114     """
115
116     def create_eclipse_project(CMAKE_DIR):
117         import sys
118         if sys.platform == "win32":
119             cmd = 'cmake %r -G"Eclipse CDT4 - MinGW Makefiles"' % CMAKE_DIR
120         else:
121             cmd = 'cmake %r -G"Eclipse CDT4 - Unix Makefiles"' % CMAKE_DIR
122
123         os.system(cmd)
124
125     includes = []
126     defines = []
127
128     import os
129     import sys
130
131     create_eclipse_project(CMAKE_DIR)
132
133     from xml.dom.minidom import parse
134     tree = parse(os.path.join(CMAKE_DIR, ".cproject"))
135     '''
136     f = open(".cproject_pretty", 'w')
137     f.write(tree.toprettyxml(indent="    ", newl=""))
138     '''
139     ELEMENT_NODE = tree.ELEMENT_NODE
140
141     cproject, = tree.getElementsByTagName("cproject")
142     for storage in cproject.childNodes:
143         if storage.nodeType != ELEMENT_NODE:
144             continue
145
146         if storage.attributes["moduleId"].value == "org.eclipse.cdt.core.settings":
147             cconfig = storage.getElementsByTagName("cconfiguration")[0]
148             for substorage in cconfig.childNodes:
149                 if substorage.nodeType != ELEMENT_NODE:
150                     continue
151
152                 moduleId = substorage.attributes["moduleId"].value
153
154                 # org.eclipse.cdt.core.settings
155                 # org.eclipse.cdt.core.language.mapping
156                 # org.eclipse.cdt.core.externalSettings
157                 # org.eclipse.cdt.core.pathentry
158                 # org.eclipse.cdt.make.core.buildtargets
159
160                 if moduleId == "org.eclipse.cdt.core.pathentry":
161                     for path in substorage.childNodes:
162                         if path.nodeType != ELEMENT_NODE:
163                             continue
164                         kind = path.attributes["kind"].value
165
166                         if kind == "mac":
167                             # <pathentry kind="mac" name="PREFIX" path="" value="&quot;/opt/blender25&quot;"/>
168                             defines.append((path.attributes["name"].value, path.attributes["value"].value))
169                         elif kind == "inc":
170                             # <pathentry include="/data/src/blender/blender/source/blender/editors/include" kind="inc" path="" system="true"/>
171                             includes.append(path.attributes["include"].value)
172                         else:
173                             pass
174
175     return includes, defines
176
177
178 def cmake_cache_var(var):
179     cache_file = open(os.path.join(CMAKE_DIR, "CMakeCache.txt"))
180     lines = [l_strip for l in cache_file for l_strip in (l.strip(),) if l_strip if not l_strip.startswith("//") if not l_strip.startswith("#")]
181     cache_file.close()
182
183     for l in lines:
184         if l.split(":")[0] == var:
185             return l.split("=", 1)[-1]
186     return None
187
188
189 def cmake_compiler_defines():
190     compiler = cmake_cache_var("CMAKE_C_COMPILER")  # could do CXX too
191
192     if compiler is None:
193         print("Couldn't find the compiler, os defines will be omitted...")
194         return
195
196     import tempfile
197     temp_c = tempfile.mkstemp(suffix=".c")[1]
198     temp_def = tempfile.mkstemp(suffix=".def")[1]
199
200     os.system("%s -dM -E %s > %s" % (compiler, temp_c, temp_def))
201
202     temp_def_file = open(temp_def)
203     lines = [l.strip() for l in temp_def_file if l.strip()]
204     temp_def_file.close()
205
206     os.remove(temp_c)
207     os.remove(temp_def)
208
209     return lines
210
211
212 def create_qtc_project_main():
213     files = list(source_list(base, filename_check=is_project_file))
214     files_rel = [relpath(f, start=PROJECT_DIR) for f in files]
215     files_rel.sort()
216
217     # --- qtcreator specific, simple format
218     if SIMPLE_PROJECTFILE:
219         # --- qtcreator specific, simple format
220         PROJECT_NAME = "Blender"
221         f = open(join(PROJECT_DIR, "%s.files" % PROJECT_NAME), 'w')
222         f.write("\n".join(files_rel))
223
224         f = open(join(PROJECT_DIR, "%s.includes" % PROJECT_NAME), 'w')
225         f.write("\n".join(sorted(list(set(dirname(f) for f in files_rel if is_c_header(f))))))
226
227         qtc_prj = join(PROJECT_DIR, "%s.creator" % PROJECT_NAME)
228         f = open(qtc_prj, 'w')
229         f.write("[General]\n")
230
231         qtc_cfg = join(PROJECT_DIR, "%s.config" % PROJECT_NAME)
232         if not exists(qtc_cfg):
233             f = open(qtc_cfg, 'w')
234             f.write("// ADD PREDEFINED MACROS HERE!\n")
235     else:
236         includes, defines = cmake_advanced_info()
237
238         # for some reason it doesnt give all internal includes
239         includes = list(set(includes) | set(dirname(f) for f in files_rel if is_c_header(f)))
240         includes.sort()
241
242         PROJECT_NAME = "Blender"
243         FILE_NAME = PROJECT_NAME.lower()
244         f = open(join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w')
245         f.write("\n".join(files_rel))
246
247         f = open(join(PROJECT_DIR, "%s.includes" % FILE_NAME), 'w')
248         f.write("\n".join(sorted(includes)))
249
250         qtc_prj = join(PROJECT_DIR, "%s.creator" % FILE_NAME)
251         f = open(qtc_prj, 'w')
252         f.write("[General]\n")
253
254         qtc_cfg = join(PROJECT_DIR, "%s.config" % FILE_NAME)
255         f = open(qtc_cfg, 'w')
256         f.write("// ADD PREDEFINED MACROS HERE!\n")
257         defines_final = [("#define %s %s" % item) for item in defines]
258         defines_final += cmake_compiler_defines()  # defines from the compiler
259         f.write("\n".join(defines_final))
260
261     print("Blender project file written to: %s" % qtc_prj)
262     # --- end
263
264
265 def create_qtc_project_python():
266     files = list(source_list(base, filename_check=is_py))
267     files_rel = [relpath(f, start=PROJECT_DIR) for f in files]
268     files_rel.sort()
269
270     # --- qtcreator specific, simple format
271     PROJECT_NAME = "Blender_Python"
272     FILE_NAME = PROJECT_NAME.lower()
273     f = open(join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w')
274     f.write("\n".join(files_rel))
275
276     qtc_prj = join(PROJECT_DIR, "%s.creator" % FILE_NAME)
277     f = open(qtc_prj, 'w')
278     f.write("[General]\n")
279
280     qtc_cfg = join(PROJECT_DIR, "%s.config" % FILE_NAME)
281     if not exists(qtc_cfg):
282         f = open(qtc_cfg, 'w')
283         f.write("// ADD PREDEFINED MACROS HERE!\n")
284
285     print("Python project file written to:  %s" % qtc_prj)
286
287
288 def main():
289     create_qtc_project_main()
290     create_qtc_project_python()
291
292
293 if __name__ == "__main__":
294     main()