3 # ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
19 # Contributor(s): Campbell Barton, M.G. Kishalmi
21 # ***** END GPL LICENSE BLOCK *****
26 Module for accessing project file data for Blender.
28 Before use, call init(cmake_build_dir).
40 "cmake_advanced_info",
41 "cmake_compiler_defines",
48 if not sys.version.startswith("3"):
49 print("\nPython3.x needed, found %s.\nAborting!\n" %
50 sys.version.partition(" ")[0])
55 from os.path import join, dirname, normpath, abspath, splitext, exists
57 SOURCE_DIR = join(dirname(__file__), "..", "..")
58 SOURCE_DIR = normpath(SOURCE_DIR)
59 SOURCE_DIR = abspath(SOURCE_DIR)
61 SIMPLE_PROJECTFILE = False
63 # must initialize from 'init'
68 global CMAKE_DIR, PROJECT_DIR
71 cmake_path = cmake_path or ""
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()))
81 PROJECT_DIR = CMAKE_DIR = cmake_path
85 def source_list(path, filename_check=None):
86 for dirpath, dirnames, filenames in os.walk(path):
88 dirnames[:] = [d for d in dirnames if not d.startswith(".")]
90 for filename in filenames:
91 filepath = join(dirpath, filename)
92 if filename_check is None or filename_check(filepath):
97 def is_cmake(filename):
98 ext = splitext(filename)[1]
99 return (ext == ".cmake") or (filename.endswith("CMakeLists.txt"))
102 def is_c_header(filename):
103 ext = splitext(filename)[1]
104 return (ext in {".h", ".hpp", ".hxx", ".hh"})
108 ext = splitext(filename)[1]
109 return (ext == ".py")
112 def is_glsl(filename):
113 ext = splitext(filename)[1]
114 return (ext == ".glsl")
118 ext = splitext(filename)[1]
119 return (ext in {".c", ".cpp", ".cxx", ".m", ".mm", ".rc", ".cc", ".inl", ".osl"})
122 def is_c_any(filename):
123 return is_c(filename) or is_c_header(filename)
126 def is_svn_file(filename):
127 dn, fn = os.path.split(filename)
128 filename_svn = join(dn, ".svn", "text-base", "%s.svn-base" % fn)
129 return exists(filename_svn)
132 def is_project_file(filename):
133 return (is_c_any(filename) or is_cmake(filename) or is_glsl(filename)) # and is_svn_file(filename)
136 def cmake_advanced_info():
137 """ Extract includes and defines from cmake.
140 make_exe = cmake_cache_var("CMAKE_MAKE_PROGRAM")
141 make_exe_basename = os.path.basename(make_exe)
143 def create_eclipse_project():
144 print("CMAKE_DIR %r" % CMAKE_DIR)
145 if sys.platform == "win32":
146 raise Exception("Error: win32 is not supported")
148 if make_exe_basename.startswith(("make", "gmake")):
149 cmd = 'cmake "%s" -G"Eclipse CDT4 - Unix Makefiles"' % CMAKE_DIR
150 elif make_exe_basename.startswith("ninja"):
151 cmd = 'cmake "%s" -G"Eclipse CDT4 - Ninja"' % CMAKE_DIR
153 raise Exception("Unknown make program %r" % make_exe)
156 return join(CMAKE_DIR, ".cproject")
161 project_path = create_eclipse_project()
163 if not exists(project_path):
164 print("Generating Eclipse Prokect File Failed: %r not found" % project_path)
167 from xml.dom.minidom import parse
168 tree = parse(project_path)
170 # to check on nicer xml
171 # f = open(".cproject_pretty", 'w')
172 # f.write(tree.toprettyxml(indent=" ", newl=""))
174 ELEMENT_NODE = tree.ELEMENT_NODE
176 cproject, = tree.getElementsByTagName("cproject")
177 for storage in cproject.childNodes:
178 if storage.nodeType != ELEMENT_NODE:
181 if storage.attributes["moduleId"].value == "org.eclipse.cdt.core.settings":
182 cconfig = storage.getElementsByTagName("cconfiguration")[0]
183 for substorage in cconfig.childNodes:
184 if substorage.nodeType != ELEMENT_NODE:
187 moduleId = substorage.attributes["moduleId"].value
189 # org.eclipse.cdt.core.settings
190 # org.eclipse.cdt.core.language.mapping
191 # org.eclipse.cdt.core.externalSettings
192 # org.eclipse.cdt.core.pathentry
193 # org.eclipse.cdt.make.core.buildtargets
195 if moduleId == "org.eclipse.cdt.core.pathentry":
196 for path in substorage.childNodes:
197 if path.nodeType != ELEMENT_NODE:
199 kind = path.attributes["kind"].value
202 # <pathentry kind="mac" name="PREFIX" path="" value=""/opt/blender25""/>
203 defines.append((path.attributes["name"].value, path.attributes["value"].value))
205 # <pathentry include="/data/src/blender/blender/source/blender/editors/include" kind="inc" path="" system="true"/>
206 includes.append(path.attributes["include"].value)
210 return includes, defines
213 def cmake_cache_var(var):
214 cache_file = open(join(CMAKE_DIR, "CMakeCache.txt"), encoding='utf-8')
216 l_strip for l in cache_file
217 for l_strip in (l.strip(),)
219 if not l_strip.startswith(("//", "#"))
224 if l.split(":")[0] == var:
225 return l.split("=", 1)[-1]
229 def cmake_compiler_defines():
230 compiler = cmake_cache_var("CMAKE_C_COMPILER") # could do CXX too
233 print("Couldn't find the compiler, os defines will be omitted...")
237 temp_c = tempfile.mkstemp(suffix=".c")[1]
238 temp_def = tempfile.mkstemp(suffix=".def")[1]
240 os.system("%s -dM -E %s > %s" % (compiler, temp_c, temp_def))
242 temp_def_file = open(temp_def)
243 lines = [l.strip() for l in temp_def_file if l.strip()]
244 temp_def_file.close()
251 def project_name_get():
252 return cmake_cache_var("CMAKE_PROJECT_NAME")