Merging r44109 through r44128 from trunk into soc-2011-tomato
[blender-staging.git] / build_files / cmake / project_info.py
1 #!/usr/bin/env python
2
3 # ***** BEGIN GPL LICENSE BLOCK *****
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19 # Contributor(s): Campbell Barton, M.G. Kishalmi
20 #
21 # ***** END GPL LICENSE BLOCK *****
22
23 # <pep8 compliant>
24
25 """
26 Example Win32 usage:
27  c:\Python32\python.exe c:\blender_dev\blender\build_files\cmake\cmake_qtcreator_project.py c:\blender_dev\cmake_build
28
29 example linux usage
30  python .~/blenderSVN/blender/build_files/cmake/cmake_qtcreator_project.py ~/blenderSVN/cmake
31 """
32
33 __all__ = (
34     "SIMPLE_PROJECTFILE",
35     "SOURCE_DIR",
36     "CMAKE_DIR",
37     "PROJECT_DIR",
38     "source_list",
39     "is_project_file",
40     "is_c_header",
41     "is_py",
42     "cmake_advanced_info",
43     "cmake_compiler_defines",
44     "project_name_get"
45 )
46
47
48 import sys
49 if not sys.version.startswith("3"):
50     print("\nPython3.x needed, found %s.\nAborting!\n" %
51           sys.version.partition(" ")[0])
52     sys.exit(1)
53
54
55 import os
56 from os.path import join, dirname, normpath, abspath, splitext, exists
57
58 SOURCE_DIR = join(dirname(__file__), "..", "..")
59 SOURCE_DIR = normpath(SOURCE_DIR)
60 SOURCE_DIR = abspath(SOURCE_DIR)
61
62 SIMPLE_PROJECTFILE = False
63
64 # get cmake path
65 CMAKE_DIR = sys.argv[-1]
66
67 if not exists(join(CMAKE_DIR, "CMakeCache.txt")):
68     CMAKE_DIR = os.getcwd()
69 if not exists(join(CMAKE_DIR, "CMakeCache.txt")):
70     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()))
71     sys.exit(1)
72
73
74 # could be either.
75 # PROJECT_DIR = SOURCE_DIR
76 PROJECT_DIR = CMAKE_DIR
77
78
79 def source_list(path, filename_check=None):
80     for dirpath, dirnames, filenames in os.walk(path):
81
82         # skip '.svn'
83         if dirpath.startswith("."):
84             continue
85
86         for filename in filenames:
87             filepath = join(dirpath, filename)
88             if filename_check is None or filename_check(filepath):
89                 yield filepath
90
91
92 # extension checking
93 def is_cmake(filename):
94     ext = splitext(filename)[1]
95     return (ext == ".cmake") or (filename.endswith("CMakeLists.txt"))
96
97
98 def is_c_header(filename):
99     ext = splitext(filename)[1]
100     return (ext in (".h", ".hpp", ".hxx"))
101
102
103 def is_py(filename):
104     ext = splitext(filename)[1]
105     return (ext == ".py")
106
107
108 def is_glsl(filename):
109     ext = splitext(filename)[1]
110     return (ext == ".glsl")
111
112
113 def is_c(filename):
114     ext = splitext(filename)[1]
115     return (ext in (".c", ".cpp", ".cxx", ".m", ".mm", ".rc", ".cc", ".inl"))
116
117
118 def is_c_any(filename):
119     return is_c(filename) or is_c_header(filename)
120
121
122 def is_svn_file(filename):
123     dn, fn = os.path.split(filename)
124     filename_svn = join(dn, ".svn", "text-base", "%s.svn-base" % fn)
125     return exists(filename_svn)
126
127
128 def is_project_file(filename):
129     return (is_c_any(filename) or is_cmake(filename) or is_glsl(filename))  # and is_svn_file(filename)
130
131
132 def cmake_advanced_info():
133     """ Extracr includes and defines from cmake.
134     """
135
136     def create_eclipse_project():
137         print("CMAKE_DIR %r" % CMAKE_DIR)
138         if sys.platform == "win32":
139             cmd = 'cmake "%s" -G"Eclipse CDT4 - MinGW Makefiles"' % CMAKE_DIR
140         else:
141             cmd = 'cmake "%s" -G"Eclipse CDT4 - Unix Makefiles"' % CMAKE_DIR
142
143         os.system(cmd)
144
145     includes = []
146     defines = []
147
148     create_eclipse_project()
149
150     from xml.dom.minidom import parse
151     tree = parse(join(CMAKE_DIR, ".cproject"))
152
153     # to check on nicer xml
154     # f = open(".cproject_pretty", 'w')
155     # f.write(tree.toprettyxml(indent="    ", newl=""))
156
157     ELEMENT_NODE = tree.ELEMENT_NODE
158
159     cproject, = tree.getElementsByTagName("cproject")
160     for storage in cproject.childNodes:
161         if storage.nodeType != ELEMENT_NODE:
162             continue
163
164         if storage.attributes["moduleId"].value == "org.eclipse.cdt.core.settings":
165             cconfig = storage.getElementsByTagName("cconfiguration")[0]
166             for substorage in cconfig.childNodes:
167                 if substorage.nodeType != ELEMENT_NODE:
168                     continue
169
170                 moduleId = substorage.attributes["moduleId"].value
171
172                 # org.eclipse.cdt.core.settings
173                 # org.eclipse.cdt.core.language.mapping
174                 # org.eclipse.cdt.core.externalSettings
175                 # org.eclipse.cdt.core.pathentry
176                 # org.eclipse.cdt.make.core.buildtargets
177
178                 if moduleId == "org.eclipse.cdt.core.pathentry":
179                     for path in substorage.childNodes:
180                         if path.nodeType != ELEMENT_NODE:
181                             continue
182                         kind = path.attributes["kind"].value
183
184                         if kind == "mac":
185                             # <pathentry kind="mac" name="PREFIX" path="" value="&quot;/opt/blender25&quot;"/>
186                             defines.append((path.attributes["name"].value, path.attributes["value"].value))
187                         elif kind == "inc":
188                             # <pathentry include="/data/src/blender/blender/source/blender/editors/include" kind="inc" path="" system="true"/>
189                             includes.append(path.attributes["include"].value)
190                         else:
191                             pass
192
193     return includes, defines
194
195
196 def cmake_cache_var(var):
197     cache_file = open(join(CMAKE_DIR, "CMakeCache.txt"), encoding='utf-8')
198     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("#")]
199     cache_file.close()
200
201     for l in lines:
202         if l.split(":")[0] == var:
203             return l.split("=", 1)[-1]
204     return None
205
206
207 def cmake_compiler_defines():
208     compiler = cmake_cache_var("CMAKE_C_COMPILER")  # could do CXX too
209
210     if compiler is None:
211         print("Couldn't find the compiler, os defines will be omitted...")
212         return
213
214     import tempfile
215     temp_c = tempfile.mkstemp(suffix=".c")[1]
216     temp_def = tempfile.mkstemp(suffix=".def")[1]
217
218     os.system("%s -dM -E %s > %s" % (compiler, temp_c, temp_def))
219
220     temp_def_file = open(temp_def)
221     lines = [l.strip() for l in temp_def_file if l.strip()]
222     temp_def_file.close()
223
224     os.remove(temp_c)
225     os.remove(temp_def)
226     return lines
227
228
229 def project_name_get(path, fallback="Blender", prefix="Blender_"):
230     if not os.path.isdir(os.path.join(path, ".svn")):
231         return fallback
232
233     import subprocess
234     info = subprocess.Popen(["svn", "info", path],
235                             stdout=subprocess.PIPE).communicate()[0]
236     # string version, we only want the URL
237     info = info.decode(encoding="utf-8", errors="ignore")
238
239     for l in info.split("\n"):
240         l = l.strip()
241         if l.startswith("URL"):
242             # https://svn.blender.org/svnroot/bf-blender/branches/bmesh/blender
243             # --> bmesh
244             if "/branches/" in l:
245                 return prefix + l.rsplit("/branches/", 1)[-1].split("/", 1)[0]
246     return fallback