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