pep8 checker, mostly pedantic style changes but also found an error in mesh_utils...
[blender.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         if sys.platform == "win32":
118             cmd = 'cmake %r -G"Eclipse CDT4 - MinGW Makefiles"' % CMAKE_DIR
119         else:
120             cmd = 'cmake %r -G"Eclipse CDT4 - Unix Makefiles"' % CMAKE_DIR
121
122         os.system(cmd)
123
124     includes = []
125     defines = []
126
127
128
129     create_eclipse_project(CMAKE_DIR)
130
131     from xml.dom.minidom import parse
132     tree = parse(os.path.join(CMAKE_DIR, ".cproject"))
133     '''
134     f = open(".cproject_pretty", 'w')
135     f.write(tree.toprettyxml(indent="    ", newl=""))
136     '''
137     ELEMENT_NODE = tree.ELEMENT_NODE
138
139     cproject, = tree.getElementsByTagName("cproject")
140     for storage in cproject.childNodes:
141         if storage.nodeType != ELEMENT_NODE:
142             continue
143
144         if storage.attributes["moduleId"].value == "org.eclipse.cdt.core.settings":
145             cconfig = storage.getElementsByTagName("cconfiguration")[0]
146             for substorage in cconfig.childNodes:
147                 if substorage.nodeType != ELEMENT_NODE:
148                     continue
149
150                 moduleId = substorage.attributes["moduleId"].value
151
152                 # org.eclipse.cdt.core.settings
153                 # org.eclipse.cdt.core.language.mapping
154                 # org.eclipse.cdt.core.externalSettings
155                 # org.eclipse.cdt.core.pathentry
156                 # org.eclipse.cdt.make.core.buildtargets
157
158                 if moduleId == "org.eclipse.cdt.core.pathentry":
159                     for path in substorage.childNodes:
160                         if path.nodeType != ELEMENT_NODE:
161                             continue
162                         kind = path.attributes["kind"].value
163
164                         if kind == "mac":
165                             # <pathentry kind="mac" name="PREFIX" path="" value="&quot;/opt/blender25&quot;"/>
166                             defines.append((path.attributes["name"].value, path.attributes["value"].value))
167                         elif kind == "inc":
168                             # <pathentry include="/data/src/blender/blender/source/blender/editors/include" kind="inc" path="" system="true"/>
169                             includes.append(path.attributes["include"].value)
170                         else:
171                             pass
172
173     return includes, defines
174
175
176 def cmake_cache_var(var):
177     cache_file = open(os.path.join(CMAKE_DIR, "CMakeCache.txt"))
178     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("#")]
179     cache_file.close()
180
181     for l in lines:
182         if l.split(":")[0] == var:
183             return l.split("=", 1)[-1]
184     return None
185
186
187 def cmake_compiler_defines():
188     compiler = cmake_cache_var("CMAKE_C_COMPILER")  # could do CXX too
189
190     if compiler is None:
191         print("Couldn't find the compiler, os defines will be omitted...")
192         return
193
194     import tempfile
195     temp_c = tempfile.mkstemp(suffix=".c")[1]
196     temp_def = tempfile.mkstemp(suffix=".def")[1]
197
198     os.system("%s -dM -E %s > %s" % (compiler, temp_c, temp_def))
199
200     temp_def_file = open(temp_def)
201     lines = [l.strip() for l in temp_def_file if l.strip()]
202     temp_def_file.close()
203
204     os.remove(temp_c)
205     os.remove(temp_def)
206
207     return lines
208
209
210 def create_qtc_project_main():
211     files = list(source_list(base, filename_check=is_project_file))
212     files_rel = [relpath(f, start=PROJECT_DIR) for f in files]
213     files_rel.sort()
214
215     # --- qtcreator specific, simple format
216     if SIMPLE_PROJECTFILE:
217         # --- qtcreator specific, simple format
218         PROJECT_NAME = "Blender"
219         f = open(join(PROJECT_DIR, "%s.files" % PROJECT_NAME), 'w')
220         f.write("\n".join(files_rel))
221
222         f = open(join(PROJECT_DIR, "%s.includes" % PROJECT_NAME), 'w')
223         f.write("\n".join(sorted(list(set(dirname(f) for f in files_rel if is_c_header(f))))))
224
225         qtc_prj = join(PROJECT_DIR, "%s.creator" % PROJECT_NAME)
226         f = open(qtc_prj, 'w')
227         f.write("[General]\n")
228
229         qtc_cfg = join(PROJECT_DIR, "%s.config" % PROJECT_NAME)
230         if not exists(qtc_cfg):
231             f = open(qtc_cfg, 'w')
232             f.write("// ADD PREDEFINED MACROS HERE!\n")
233     else:
234         includes, defines = cmake_advanced_info()
235
236         # for some reason it doesnt give all internal includes
237         includes = list(set(includes) | set(dirname(f) for f in files_rel if is_c_header(f)))
238         includes.sort()
239
240         PROJECT_NAME = "Blender"
241         FILE_NAME = PROJECT_NAME.lower()
242         f = open(join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w')
243         f.write("\n".join(files_rel))
244
245         f = open(join(PROJECT_DIR, "%s.includes" % FILE_NAME), 'w')
246         f.write("\n".join(sorted(includes)))
247
248         qtc_prj = join(PROJECT_DIR, "%s.creator" % FILE_NAME)
249         f = open(qtc_prj, 'w')
250         f.write("[General]\n")
251
252         qtc_cfg = join(PROJECT_DIR, "%s.config" % FILE_NAME)
253         f = open(qtc_cfg, 'w')
254         f.write("// ADD PREDEFINED MACROS HERE!\n")
255         defines_final = [("#define %s %s" % item) for item in defines]
256         defines_final += cmake_compiler_defines()  # defines from the compiler
257         f.write("\n".join(defines_final))
258
259     print("Blender project file written to: %s" % qtc_prj)
260     # --- end
261
262
263 def create_qtc_project_python():
264     files = list(source_list(base, filename_check=is_py))
265     files_rel = [relpath(f, start=PROJECT_DIR) for f in files]
266     files_rel.sort()
267
268     # --- qtcreator specific, simple format
269     PROJECT_NAME = "Blender_Python"
270     FILE_NAME = PROJECT_NAME.lower()
271     f = open(join(PROJECT_DIR, "%s.files" % FILE_NAME), 'w')
272     f.write("\n".join(files_rel))
273
274     qtc_prj = join(PROJECT_DIR, "%s.creator" % FILE_NAME)
275     f = open(qtc_prj, 'w')
276     f.write("[General]\n")
277
278     qtc_cfg = join(PROJECT_DIR, "%s.config" % FILE_NAME)
279     if not exists(qtc_cfg):
280         f = open(qtc_cfg, 'w')
281         f.write("// ADD PREDEFINED MACROS HERE!\n")
282
283     print("Python project file written to:  %s" % qtc_prj)
284
285
286 def main():
287     create_qtc_project_main()
288     create_qtc_project_python()
289
290
291 if __name__ == "__main__":
292     main()