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