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