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