2011-09-16 08:58:20 +02:00
|
|
|
# ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
# ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
|
|
|
# <pep8 compliant>
|
|
|
|
|
|
|
|
__all__ = (
|
|
|
|
"build_info",
|
|
|
|
"SOURCE_DIR",
|
2016-07-30 10:15:16 +02:00
|
|
|
)
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2011-11-19 01:01:10 +01:00
|
|
|
|
|
|
|
import sys
|
2020-10-02 05:46:41 +02:00
|
|
|
if sys.version_info.major < 3:
|
2020-09-07 14:58:16 +02:00
|
|
|
print("\nPython3.x or newer needed, found %s.\nAborting!\n" %
|
2011-11-19 01:01:10 +01:00
|
|
|
sys.version.partition(" ")[0])
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
import os
|
|
|
|
from os.path import join, dirname, normpath, abspath
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Callable,
|
|
|
|
Generator,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Sequence,
|
|
|
|
Tuple,
|
|
|
|
Union,
|
|
|
|
cast,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
SOURCE_DIR = join(dirname(__file__), "..", "..")
|
|
|
|
SOURCE_DIR = normpath(SOURCE_DIR)
|
|
|
|
SOURCE_DIR = abspath(SOURCE_DIR)
|
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def is_c_header(filename: str) -> bool:
|
2011-09-16 08:58:20 +02:00
|
|
|
ext = os.path.splitext(filename)[1]
|
2012-12-31 08:06:51 +01:00
|
|
|
return (ext in {".h", ".hpp", ".hxx", ".hh"})
|
2011-09-16 08:58:20 +02:00
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def is_c(filename: str) -> bool:
|
2011-09-16 08:58:20 +02:00
|
|
|
ext = os.path.splitext(filename)[1]
|
2012-10-17 03:47:37 +02:00
|
|
|
return (ext in {".c", ".cpp", ".cxx", ".m", ".mm", ".rc", ".cc", ".inl", ".osl"})
|
2011-09-16 08:58:20 +02:00
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def is_c_any(filename: str) -> bool:
|
|
|
|
return is_c(filename) or is_c_header(filename)
|
2011-09-16 08:58:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
# copied from project_info.py
|
|
|
|
CMAKE_DIR = "."
|
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def cmake_cache_var_iter() -> Generator[Tuple[str, str, str], None, None]:
|
2015-07-13 12:03:15 +02:00
|
|
|
import re
|
|
|
|
re_cache = re.compile(r'([A-Za-z0-9_\-]+)?:?([A-Za-z0-9_\-]+)?=(.*)$')
|
|
|
|
with open(join(CMAKE_DIR, "CMakeCache.txt"), 'r', encoding='utf-8') as cache_file:
|
|
|
|
for l in cache_file:
|
|
|
|
match = re_cache.match(l.strip())
|
|
|
|
if match is not None:
|
|
|
|
var, type_, val = match.groups()
|
|
|
|
yield (var, type_ or "", val)
|
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def cmake_cache_var(var: str) -> Optional[str]:
|
2015-07-13 12:03:15 +02:00
|
|
|
for var_iter, type_iter, value_iter in cmake_cache_var_iter():
|
|
|
|
if var == var_iter:
|
|
|
|
return value_iter
|
2011-09-16 08:58:20 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def cmake_cache_var_or_exit(var: str) -> str:
|
|
|
|
value = cmake_cache_var(var)
|
|
|
|
if value is None:
|
|
|
|
print("Unable to find %r exiting!" % value)
|
|
|
|
sys.exit(1)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def do_ignore(filepath: str, ignore_prefix_list: Optional[Sequence[str]]) -> bool:
|
2011-09-16 08:58:20 +02:00
|
|
|
if ignore_prefix_list is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
relpath = os.path.relpath(filepath, SOURCE_DIR)
|
|
|
|
return any([relpath.startswith(prefix) for prefix in ignore_prefix_list])
|
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def makefile_log() -> List[str]:
|
2011-09-16 08:58:20 +02:00
|
|
|
import subprocess
|
2011-09-20 20:29:19 +02:00
|
|
|
import time
|
2011-11-17 05:05:54 +01:00
|
|
|
|
2012-10-15 16:11:30 +02:00
|
|
|
# support both make and ninja
|
2021-04-13 13:12:17 +02:00
|
|
|
make_exe = cmake_cache_var_or_exit("CMAKE_MAKE_PROGRAM")
|
|
|
|
|
2012-10-15 16:11:30 +02:00
|
|
|
make_exe_basename = os.path.basename(make_exe)
|
|
|
|
|
2013-01-24 10:36:22 +01:00
|
|
|
if make_exe_basename.startswith(("make", "gmake")):
|
2012-10-15 16:11:30 +02:00
|
|
|
print("running 'make' with --dry-run ...")
|
|
|
|
process = subprocess.Popen([make_exe, "--always-make", "--dry-run", "--keep-going", "VERBOSE=1"],
|
2013-01-16 00:17:45 +01:00
|
|
|
stdout=subprocess.PIPE,
|
2012-10-15 16:11:30 +02:00
|
|
|
)
|
|
|
|
elif make_exe_basename.startswith("ninja"):
|
|
|
|
print("running 'ninja' with -t commands ...")
|
|
|
|
process = subprocess.Popen([make_exe, "-t", "commands"],
|
2013-01-16 00:17:45 +01:00
|
|
|
stdout=subprocess.PIPE,
|
2012-10-15 16:11:30 +02:00
|
|
|
)
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
if process is None:
|
|
|
|
print("Can't execute process")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
while process.poll():
|
|
|
|
time.sleep(1)
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
# We know this is always true based on the input arguments to `Popen`.
|
|
|
|
stdout: IO[bytes] = process.stdout # type: ignore
|
2011-09-16 08:58:20 +02:00
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
out = stdout.read()
|
|
|
|
stdout.close()
|
|
|
|
print("done!", len(out), "bytes")
|
|
|
|
return cast(List[str], out.decode("utf-8", errors="ignore").split("\n"))
|
2011-09-16 08:58:20 +02:00
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def build_info(
|
|
|
|
use_c: bool = True,
|
|
|
|
use_cxx: bool = True,
|
|
|
|
ignore_prefix_list: Optional[List[str]] = None,
|
|
|
|
) -> List[Tuple[str, List[str], List[str]]]:
|
2011-09-16 08:58:20 +02:00
|
|
|
makelog = makefile_log()
|
|
|
|
|
|
|
|
source = []
|
|
|
|
|
|
|
|
compilers = []
|
|
|
|
if use_c:
|
2021-04-13 13:12:17 +02:00
|
|
|
compilers.append(cmake_cache_var_or_exit("CMAKE_C_COMPILER"))
|
2011-09-16 08:58:20 +02:00
|
|
|
if use_cxx:
|
2021-04-13 13:12:17 +02:00
|
|
|
compilers.append(cmake_cache_var_or_exit("CMAKE_CXX_COMPILER"))
|
2011-09-16 08:58:20 +02:00
|
|
|
|
|
|
|
print("compilers:", " ".join(compilers))
|
|
|
|
|
|
|
|
fake_compiler = "%COMPILER%"
|
|
|
|
|
|
|
|
print("parsing make log ...")
|
|
|
|
|
|
|
|
for line in makelog:
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
args: Union[str, List[str]] = line.split()
|
2011-09-16 08:58:20 +02:00
|
|
|
|
|
|
|
if not any([(c in args) for c in compilers]):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# join args incase they are not.
|
|
|
|
args = ' '.join(args)
|
|
|
|
args = args.replace(" -isystem", " -I")
|
|
|
|
args = args.replace(" -D ", " -D")
|
|
|
|
args = args.replace(" -I ", " -I")
|
|
|
|
|
|
|
|
for c in compilers:
|
|
|
|
args = args.replace(c, fake_compiler)
|
|
|
|
args = args.split()
|
|
|
|
# end
|
|
|
|
|
|
|
|
# remove compiler
|
|
|
|
args[:args.index(fake_compiler) + 1] = []
|
|
|
|
|
|
|
|
c_files = [f for f in args if is_c(f)]
|
|
|
|
inc_dirs = [f[2:].strip() for f in args if f.startswith('-I')]
|
|
|
|
defs = [f[2:].strip() for f in args if f.startswith('-D')]
|
|
|
|
for c in sorted(c_files):
|
|
|
|
|
|
|
|
if do_ignore(c, ignore_prefix_list):
|
|
|
|
continue
|
|
|
|
|
|
|
|
source.append((c, inc_dirs, defs))
|
|
|
|
|
2012-10-15 16:11:30 +02:00
|
|
|
# make relative includes absolute
|
|
|
|
# not totally essential but useful
|
|
|
|
for i, f in enumerate(inc_dirs):
|
|
|
|
if not os.path.isabs(f):
|
|
|
|
inc_dirs[i] = os.path.abspath(os.path.join(CMAKE_DIR, f))
|
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
# safety check that our includes are ok
|
|
|
|
for f in inc_dirs:
|
|
|
|
if not os.path.exists(f):
|
|
|
|
raise Exception("%s missing" % f)
|
|
|
|
|
|
|
|
print("done!")
|
|
|
|
|
|
|
|
return source
|
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def build_defines_as_source() -> str:
|
2013-08-08 03:53:02 +02:00
|
|
|
"""
|
|
|
|
Returns a string formatted as an include:
|
|
|
|
'#defines A=B\n#define....'
|
|
|
|
"""
|
|
|
|
import subprocess
|
|
|
|
# works for both gcc and clang
|
2021-04-13 13:12:17 +02:00
|
|
|
cmd = (cmake_cache_var_or_exit("CMAKE_C_COMPILER"), "-dM", "-E", "-")
|
|
|
|
process = subprocess.Popen(
|
|
|
|
cmd,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stdin=subprocess.DEVNULL,
|
|
|
|
)
|
|
|
|
|
|
|
|
# We know this is always true based on the input arguments to `Popen`.
|
|
|
|
stdout: IO[bytes] = process.stdout # type: ignore
|
|
|
|
|
|
|
|
return cast(str, stdout.read().strip().decode('ascii'))
|
2013-08-08 03:53:02 +02:00
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def build_defines_as_args() -> List[str]:
|
|
|
|
return [
|
|
|
|
("-D" + "=".join(l.split(maxsplit=2)[1:]))
|
|
|
|
for l in build_defines_as_source().split("\n")
|
|
|
|
if l.startswith('#define')
|
|
|
|
]
|
2013-08-08 03:53:02 +02:00
|
|
|
|
|
|
|
|
2011-09-22 06:41:12 +02:00
|
|
|
# could be moved elsewhere!, this just happens to be used by scripts that also
|
|
|
|
# use this module.
|
2021-04-13 13:12:17 +02:00
|
|
|
def queue_processes(
|
|
|
|
process_funcs: Sequence[Tuple[Callable[..., subprocess.Popen[Any]], Tuple[Any, ...]]],
|
|
|
|
job_total: int =-1,
|
|
|
|
) -> None:
|
2011-09-22 06:41:12 +02:00
|
|
|
""" Takes a list of function arg pairs, each function must return a process
|
|
|
|
"""
|
|
|
|
|
|
|
|
if job_total == -1:
|
|
|
|
import multiprocessing
|
|
|
|
job_total = multiprocessing.cpu_count()
|
|
|
|
del multiprocessing
|
|
|
|
|
|
|
|
if job_total == 1:
|
|
|
|
for func, args in process_funcs:
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
process = func(*args)
|
|
|
|
process.wait()
|
|
|
|
else:
|
|
|
|
import time
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
processes: List[subprocess.Popen[Any]] = []
|
2011-09-22 06:41:12 +02:00
|
|
|
for func, args in process_funcs:
|
|
|
|
# wait until a thread is free
|
|
|
|
while 1:
|
|
|
|
processes[:] = [p for p in processes if p.poll() is None]
|
2011-09-26 17:39:15 +02:00
|
|
|
|
2011-09-22 06:41:12 +02:00
|
|
|
if len(processes) <= job_total:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
processes.append(func(*args))
|
|
|
|
|
|
|
|
|
2021-04-13 13:12:17 +02:00
|
|
|
def main() -> None:
|
2011-09-16 08:58:20 +02:00
|
|
|
if not os.path.exists(join(CMAKE_DIR, "CMakeCache.txt")):
|
|
|
|
print("This script must run from the cmake build dir")
|
|
|
|
return
|
|
|
|
|
|
|
|
for s in build_info():
|
|
|
|
print(s)
|
|
|
|
|
2020-10-02 02:10:01 +02:00
|
|
|
|
2011-09-16 08:58:20 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|