add error check for qtcreator&netbeans project file generators to quit early if cmake...
[blender.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 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", ".hh"})
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", ".osl"})
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     make_exe = cmake_cache_var("CMAKE_MAKE_PROGRAM")
137     make_exe_basename = os.path.basename(make_exe)
138
139     def create_eclipse_project():
140         print("CMAKE_DIR %r" % CMAKE_DIR)
141         if sys.platform == "win32":
142             cmd = 'cmake "%s" -G"Eclipse CDT4 - MinGW Makefiles"' % CMAKE_DIR
143         else:
144             if make_exe_basename.startswith("make"):
145                 cmd = 'cmake "%s" -G"Eclipse CDT4 - Unix Makefiles"' % CMAKE_DIR
146             elif make_exe_basename.startswith("ninja"):
147                 cmd = 'cmake "%s" -G"Eclipse CDT4 - Ninja"' % CMAKE_DIR
148             else:
149                 raise Exception("Unknown make program %r" % make_exe)
150
151         os.system(cmd)
152         return join(CMAKE_DIR, ".cproject")
153         
154
155     includes = []
156     defines = []
157
158     project_path = create_eclipse_project()
159
160     if not exists(project_path):
161         print("Generating Eclipse Prokect File Failed: %r not found" % project_path)
162         return None, None
163
164     from xml.dom.minidom import parse
165     tree = parse(project_path)
166
167     # to check on nicer xml
168     # f = open(".cproject_pretty", 'w')
169     # f.write(tree.toprettyxml(indent="    ", newl=""))
170
171     ELEMENT_NODE = tree.ELEMENT_NODE
172
173     cproject, = tree.getElementsByTagName("cproject")
174     for storage in cproject.childNodes:
175         if storage.nodeType != ELEMENT_NODE:
176             continue
177
178         if storage.attributes["moduleId"].value == "org.eclipse.cdt.core.settings":
179             cconfig = storage.getElementsByTagName("cconfiguration")[0]
180             for substorage in cconfig.childNodes:
181                 if substorage.nodeType != ELEMENT_NODE:
182                     continue
183
184                 moduleId = substorage.attributes["moduleId"].value
185
186                 # org.eclipse.cdt.core.settings
187                 # org.eclipse.cdt.core.language.mapping
188                 # org.eclipse.cdt.core.externalSettings
189                 # org.eclipse.cdt.core.pathentry
190                 # org.eclipse.cdt.make.core.buildtargets
191
192                 if moduleId == "org.eclipse.cdt.core.pathentry":
193                     for path in substorage.childNodes:
194                         if path.nodeType != ELEMENT_NODE:
195                             continue
196                         kind = path.attributes["kind"].value
197
198                         if kind == "mac":
199                             # <pathentry kind="mac" name="PREFIX" path="" value="&quot;/opt/blender25&quot;"/>
200                             defines.append((path.attributes["name"].value, path.attributes["value"].value))
201                         elif kind == "inc":
202                             # <pathentry include="/data/src/blender/blender/source/blender/editors/include" kind="inc" path="" system="true"/>
203                             includes.append(path.attributes["include"].value)
204                         else:
205                             pass
206
207     return includes, defines
208
209
210 def cmake_cache_var(var):
211     cache_file = open(join(CMAKE_DIR, "CMakeCache.txt"), encoding='utf-8')
212     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("#")]
213     cache_file.close()
214
215     for l in lines:
216         if l.split(":")[0] == var:
217             return l.split("=", 1)[-1]
218     return None
219
220
221 def cmake_compiler_defines():
222     compiler = cmake_cache_var("CMAKE_C_COMPILER")  # could do CXX too
223
224     if compiler is None:
225         print("Couldn't find the compiler, os defines will be omitted...")
226         return
227
228     import tempfile
229     temp_c = tempfile.mkstemp(suffix=".c")[1]
230     temp_def = tempfile.mkstemp(suffix=".def")[1]
231
232     os.system("%s -dM -E %s > %s" % (compiler, temp_c, temp_def))
233
234     temp_def_file = open(temp_def)
235     lines = [l.strip() for l in temp_def_file if l.strip()]
236     temp_def_file.close()
237
238     os.remove(temp_c)
239     os.remove(temp_def)
240     return lines
241
242
243 def project_name_get(path, fallback="Blender", prefix="Blender_"):
244     if not os.path.isdir(os.path.join(path, ".svn")):
245         return fallback
246
247     import subprocess
248     try:
249         info = subprocess.Popen(["svn", "info", path],
250                                 stdout=subprocess.PIPE).communicate()[0]
251     except:
252         # possibly 'svn' isnt found/installed
253         return fallback
254
255     # string version, we only want the URL
256     info = info.decode(encoding="utf-8", errors="ignore")
257
258     for l in info.split("\n"):
259         l = l.strip()
260         if l.startswith("URL"):
261             # https://svn.blender.org/svnroot/bf-blender/branches/bmesh/blender
262             # --> bmesh
263             if "/branches/" in l:
264                 return prefix + l.rsplit("/branches/", 1)[-1].split("/", 1)[0]
265     return fallback