- use Py_CLEAR for python internally referencing other PyObjects (supposed to be...
[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
21 #
22 # ***** END GPL LICENSE BLOCK *****
23
24 # <pep8 compliant>
25
26 import os
27 from os.path import join, dirname, normpath, abspath, splitext, relpath, exists
28
29 base = join(os.path.dirname(__file__), "..", "..")
30 base = normpath(base)
31 base = abspath(base)
32
33 SIMPLE_PROJECTFILE = False
34
35
36 def source_list(path, filename_check=None):
37     for dirpath, dirnames, filenames in os.walk(path):
38
39         # skip '.svn'
40         if dirpath.startswith("."):
41             continue
42
43         for filename in filenames:
44             filepath = join(dirpath, filename)
45             if filename_check is None or filename_check(filepath):
46                 yield filepath
47
48
49 # extension checking
50 def is_cmake(filename):
51     ext = splitext(filename)[1]
52     return (ext == ".cmake") or (filename == "CMakeLists.txt")
53
54
55 def is_c_header(filename):
56     ext = splitext(filename)[1]
57     return (ext in (".h", ".hpp", ".hxx"))
58
59
60 def is_c(filename):
61     ext = splitext(filename)[1]
62     return (ext in (".c", ".cpp", ".cxx", ".m", ".mm", ".rc"))
63
64
65 def is_c_any(filename):
66     return is_c(filename) or is_c_header(filename)
67
68
69 def is_svn_file(filename):
70     dn, fn = os.path.split(filename)
71     filename_svn = join(dn, ".svn", "text-base", "%s.svn-base" % fn)
72     return exists(filename_svn)
73
74
75 def is_project_file(filename):
76     return (is_c_any(filename) or is_cmake(filename)) and is_svn_file(filename)
77
78
79 def cmake_advanced_info():
80     """ Extracr includes and defines from cmake.
81     """
82
83     def create_eclipse_project(cmake_dir):
84         cmd = 'cmake %r -G"Eclipse CDT4 - Unix Makefiles"' % cmake_dir
85         os.system(cmd)
86
87     includes = []
88     defines = []
89
90     import os
91     import sys
92
93     cmake_dir = sys.argv[-1]
94
95     if not os.path.join(cmake_dir, "CMakeCache.txt"):
96         cmake_dir = os.getcwd()
97     if not os.path.join(cmake_dir, "CMakeCache.txt"):
98         print("CMakeCache.txt not found in %r or %r\n    Pass CMake build dir as an argument, or run from that dir, abording" % (cmake_dir, os.getcwd()))
99         sys.exit(1)
100
101     # create_eclipse_project(cmake_dir)
102
103     from xml.dom.minidom import parse
104     tree = parse(os.path.join(cmake_dir, ".cproject"))
105     '''
106     f = open(".cproject_pretty", 'w')
107     f.write(tree.toprettyxml(indent="    ", newl=""))
108     '''
109     ELEMENT_NODE = tree.ELEMENT_NODE
110
111     cproject, = tree.getElementsByTagName("cproject")
112     for storage in cproject.childNodes:
113         if storage.nodeType != ELEMENT_NODE:
114             continue
115
116         if storage.attributes["moduleId"].value == "org.eclipse.cdt.core.settings":
117             cconfig = storage.getElementsByTagName("cconfiguration")[0]
118             for substorage in cconfig.childNodes:
119                 if substorage.nodeType != ELEMENT_NODE:
120                     continue
121
122                 moduleId = substorage.attributes["moduleId"].value
123
124                 # org.eclipse.cdt.core.settings
125                 # org.eclipse.cdt.core.language.mapping
126                 # org.eclipse.cdt.core.externalSettings
127                 # org.eclipse.cdt.core.pathentry
128                 # org.eclipse.cdt.make.core.buildtargets
129
130                 if moduleId == "org.eclipse.cdt.core.pathentry":
131                     for path in substorage.childNodes:
132                         if path.nodeType != ELEMENT_NODE:
133                             continue
134                         kind = path.attributes["kind"].value
135
136                         if kind == "mac":
137                             # <pathentry kind="mac" name="PREFIX" path="" value="&quot;/opt/blender25&quot;"/>
138                             defines.append((path.attributes["name"].value, path.attributes["value"].value))
139                         elif kind == "inc":
140                             # <pathentry include="/data/src/blender/blender/source/blender/editors/include" kind="inc" path="" system="true"/>
141                             includes.append(path.attributes["include"].value)
142                         else:
143                             pass
144
145     return includes, defines
146
147
148 def main():
149     files = list(source_list(base, filename_check=is_project_file))
150     files_rel = [relpath(f, start=base) for f in files]
151     files_rel.sort()
152
153     # --- qtcreator spesific, simple format
154     if SIMPLE_PROJECTFILE:
155         # --- qtcreator spesific, simple format
156         PROJECT_NAME = "Blender"
157         f = open(join(base, "%s.files" % PROJECT_NAME), 'w')
158         f.write("\n".join(files_rel))
159
160         f = open(join(base, "%s.includes" % PROJECT_NAME), 'w')
161         f.write("\n".join(sorted(list(set(dirname(f) for f in files_rel if is_c_header(f))))))
162
163         qtc_prj = join(base, "%s.creator" % PROJECT_NAME)
164         f = open(qtc_prj, 'w')
165         f.write("[General]\n")
166
167         qtc_cfg = join(base, "%s.config" % PROJECT_NAME)
168         if not exists(qtc_cfg):
169             f = open(qtc_cfg, 'w')
170             f.write("// ADD PREDEFINED MACROS HERE!\n")
171     else:
172         includes, defines = cmake_advanced_info()
173
174         # for some reason it doesnt give all internal includes
175         includes = list(set(includes) | set(dirname(f) for f in files_rel if is_c_header(f)))
176         includes.sort()
177
178
179         PROJECT_NAME = "Blender"
180         f = open(join(base, "%s.files" % PROJECT_NAME), 'w')
181         f.write("\n".join(files_rel))
182
183         f = open(join(base, "%s.includes" % PROJECT_NAME), 'w')
184         f.write("\n".join(sorted(includes)))
185
186         qtc_prj = join(base, "%s.creator" % PROJECT_NAME)
187         f = open(qtc_prj, 'w')
188         f.write("[General]\n")
189
190         qtc_cfg = join(base, "%s.config" % PROJECT_NAME)
191         f = open(qtc_cfg, 'w')
192         f.write("// ADD PREDEFINED MACROS HERE!\n")
193         f.write("\n".join([("#define %s %s" % item) for item in defines]))
194
195     print("Project file written to: %s" % qtc_prj)
196     # --- end
197
198 if __name__ == "__main__":
199     main()