deea844034cd9eb5fe6b89897733b367d411ff65
[blender-staging.git] / build_files / cmake / project_info.py
1 #!/usr/bin/env python3
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 Module for accessing project file data for Blender.
27
28 Before use, call init(cmake_build_dir).
29 """
30
31 __all__ = (
32     "SIMPLE_PROJECTFILE",
33     "SOURCE_DIR",
34     "CMAKE_DIR",
35     "PROJECT_DIR",
36     "source_list",
37     "is_project_file",
38     "is_c_header",
39     "is_py",
40     "cmake_advanced_info",
41     "cmake_compiler_defines",
42     "project_name_get",
43     "init",
44 )
45
46
47 import sys
48 if not sys.version.startswith("3"):
49     print("\nPython3.x needed, found %s.\nAborting!\n" %
50           sys.version.partition(" ")[0])
51     sys.exit(1)
52
53
54 import os
55 from os.path import join, dirname, normpath, abspath, splitext, exists
56
57 SOURCE_DIR = join(dirname(__file__), "..", "..")
58 SOURCE_DIR = normpath(SOURCE_DIR)
59 SOURCE_DIR = abspath(SOURCE_DIR)
60
61 SIMPLE_PROJECTFILE = False
62
63 # must initialize from 'init'
64 CMAKE_DIR = None
65
66
67 def init(cmake_path):
68     global CMAKE_DIR, PROJECT_DIR
69
70     # get cmake path
71     cmake_path = cmake_path or ""
72
73     if (not cmake_path) or (not exists(join(cmake_path, "CMakeCache.txt"))):
74         cmake_path = os.getcwd()
75     if not exists(join(cmake_path, "CMakeCache.txt")):
76         print("CMakeCache.txt not found in %r or %r\n"
77               "    Pass CMake build dir as an argument, or run from that dir, aborting" %
78               (cmake_path, os.getcwd()))
79         return False
80
81     PROJECT_DIR = CMAKE_DIR = cmake_path
82     return True
83
84
85 def source_list(path, filename_check=None):
86     for dirpath, dirnames, filenames in os.walk(path):
87
88         # skip '.svn'
89         if dirpath.startswith("."):
90             continue
91
92         for filename in filenames:
93             filepath = join(dirpath, filename)
94             if filename_check is None or filename_check(filepath):
95                 yield filepath
96
97
98 # extension checking
99 def is_cmake(filename):
100     ext = splitext(filename)[1]
101     return (ext == ".cmake") or (filename.endswith("CMakeLists.txt"))
102
103
104 def is_c_header(filename):
105     ext = splitext(filename)[1]
106     return (ext in {".h", ".hpp", ".hxx", ".hh"})
107
108
109 def is_py(filename):
110     ext = splitext(filename)[1]
111     return (ext == ".py")
112
113
114 def is_glsl(filename):
115     ext = splitext(filename)[1]
116     return (ext == ".glsl")
117
118
119 def is_c(filename):
120     ext = splitext(filename)[1]
121     return (ext in {".c", ".cpp", ".cxx", ".m", ".mm", ".rc", ".cc", ".inl", ".osl"})
122
123
124 def is_c_any(filename):
125     return is_c(filename) or is_c_header(filename)
126
127
128 def is_svn_file(filename):
129     dn, fn = os.path.split(filename)
130     filename_svn = join(dn, ".svn", "text-base", "%s.svn-base" % fn)
131     return exists(filename_svn)
132
133
134 def is_project_file(filename):
135     return (is_c_any(filename) or is_cmake(filename) or is_glsl(filename))  # and is_svn_file(filename)
136
137
138 def cmake_advanced_info():
139     """ Extract includes and defines from cmake.
140     """
141
142     make_exe = cmake_cache_var("CMAKE_MAKE_PROGRAM")
143     make_exe_basename = os.path.basename(make_exe)
144
145     def create_eclipse_project():
146         print("CMAKE_DIR %r" % CMAKE_DIR)
147         if sys.platform == "win32":
148             cmd = 'cmake "%s" -G"Eclipse CDT4 - MinGW Makefiles"' % CMAKE_DIR
149         else:
150             if make_exe_basename.startswith(("make", "gmake")):
151                 cmd = 'cmake "%s" -G"Eclipse CDT4 - Unix Makefiles"' % CMAKE_DIR
152             elif make_exe_basename.startswith("ninja"):
153                 cmd = 'cmake "%s" -G"Eclipse CDT4 - Ninja"' % CMAKE_DIR
154             else:
155                 raise Exception("Unknown make program %r" % make_exe)
156
157         os.system(cmd)
158         return join(CMAKE_DIR, ".cproject")
159
160     includes = []
161     defines = []
162
163     project_path = create_eclipse_project()
164
165     if not exists(project_path):
166         print("Generating Eclipse Prokect File Failed: %r not found" % project_path)
167         return None, None
168
169     from xml.dom.minidom import parse
170     tree = parse(project_path)
171
172     # to check on nicer xml
173     # f = open(".cproject_pretty", 'w')
174     # f.write(tree.toprettyxml(indent="    ", newl=""))
175
176     ELEMENT_NODE = tree.ELEMENT_NODE
177
178     cproject, = tree.getElementsByTagName("cproject")
179     for storage in cproject.childNodes:
180         if storage.nodeType != ELEMENT_NODE:
181             continue
182
183         if storage.attributes["moduleId"].value == "org.eclipse.cdt.core.settings":
184             cconfig = storage.getElementsByTagName("cconfiguration")[0]
185             for substorage in cconfig.childNodes:
186                 if substorage.nodeType != ELEMENT_NODE:
187                     continue
188
189                 moduleId = substorage.attributes["moduleId"].value
190
191                 # org.eclipse.cdt.core.settings
192                 # org.eclipse.cdt.core.language.mapping
193                 # org.eclipse.cdt.core.externalSettings
194                 # org.eclipse.cdt.core.pathentry
195                 # org.eclipse.cdt.make.core.buildtargets
196
197                 if moduleId == "org.eclipse.cdt.core.pathentry":
198                     for path in substorage.childNodes:
199                         if path.nodeType != ELEMENT_NODE:
200                             continue
201                         kind = path.attributes["kind"].value
202
203                         if kind == "mac":
204                             # <pathentry kind="mac" name="PREFIX" path="" value="&quot;/opt/blender25&quot;"/>
205                             defines.append((path.attributes["name"].value, path.attributes["value"].value))
206                         elif kind == "inc":
207                             # <pathentry include="/data/src/blender/blender/source/blender/editors/include" kind="inc" path="" system="true"/>
208                             includes.append(path.attributes["include"].value)
209                         else:
210                             pass
211
212     return includes, defines
213
214
215 def cmake_cache_var(var):
216     cache_file = open(join(CMAKE_DIR, "CMakeCache.txt"), encoding='utf-8')
217     lines = [
218         l_strip for l in cache_file
219         for l_strip in (l.strip(),)
220         if l_strip
221         if not l_strip.startswith(("//", "#"))
222     ]
223     cache_file.close()
224
225     for l in lines:
226         if l.split(":")[0] == var:
227             return l.split("=", 1)[-1]
228     return None
229
230
231 def cmake_compiler_defines():
232     compiler = cmake_cache_var("CMAKE_C_COMPILER")  # could do CXX too
233
234     if compiler is None:
235         print("Couldn't find the compiler, os defines will be omitted...")
236         return
237
238     import tempfile
239     temp_c = tempfile.mkstemp(suffix=".c")[1]
240     temp_def = tempfile.mkstemp(suffix=".def")[1]
241
242     os.system("%s -dM -E %s > %s" % (compiler, temp_c, temp_def))
243
244     temp_def_file = open(temp_def)
245     lines = [l.strip() for l in temp_def_file if l.strip()]
246     temp_def_file.close()
247
248     os.remove(temp_c)
249     os.remove(temp_def)
250     return lines
251
252
253 def project_name_get():
254     return cmake_cache_var("CMAKE_PROJECT_NAME")