|
5 | 5 | import sys |
6 | 6 | import stat |
7 | 7 | import shutil |
| 8 | +import argparse |
8 | 9 | import subprocess |
9 | 10 | from pathlib import Path |
| 11 | +from collections import namedtuple |
10 | 12 |
|
11 | 13 | PROJECT_DIR = Path(__file__).parent.resolve() |
12 | 14 | SBUILD_DIR = PROJECT_DIR/"sbuild" |
13 | 15 | GN_DIR = SBUILD_DIR/"gn" |
14 | 16 | GN_REV = "3357c4f51b1a9e676378c695dd9c7e9911c35ee6" |
15 | 17 |
|
| 18 | + |
16 | 19 | def log(*args, **kwargs): |
17 | 20 | print(*args, **kwargs, file=sys.stderr) |
18 | 21 |
|
19 | 22 | def run_cmd(command, cwd, **kwargs): |
20 | 23 | log(f"{command} (cwd={cwd})") |
21 | 24 | return subprocess.run(command, cwd=cwd, check=True, **kwargs) |
22 | 25 |
|
23 | | -def make_executable(path): |
| 26 | +def _make_executable(path): |
24 | 27 | path.chmod(path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
25 | 28 |
|
26 | | -def build_gn(): |
| 29 | +def _env_prepend(key, value, sep): |
| 30 | + tail = os.environ.get(key, "") |
| 31 | + if tail: |
| 32 | + tail = sep + tail |
| 33 | + os.environ[key] = value + tail |
| 34 | + |
| 35 | + |
| 36 | +CompilerToolset = namedtuple("CompilerToolset", ("CXX", "AR", "LD"), defaults=(None, )) |
| 37 | + |
| 38 | +def build(toolset): |
| 39 | + |
27 | 40 | # GN's build system runs git commands that assume a full checkout |
28 | 41 | # --depth 1 checkout would need a manually created sbuild/gn/out/last_commit_position.h and passing "--no-last-commit-position" to build/gen.py - probably not worth the trouble for now. |
29 | 42 | SBUILD_DIR.mkdir(exist_ok=True) |
30 | 43 | if not GN_DIR.exists(): |
31 | 44 | run_cmd(["git", "clone", "https://gn.googlesource.com/gn/"], cwd=SBUILD_DIR) |
32 | 45 | run_cmd(["git", "checkout", GN_REV], cwd=GN_DIR) |
33 | | - env = os.environ.copy() |
34 | | - env["CXX"] = os.environ.get("CXX", "g++") |
35 | | - run_cmd([sys.executable, "build/gen.py", "--no-static-libstdc++", "--allow-warnings"], cwd=GN_DIR, env=env) |
| 46 | + |
| 47 | + env = os.environ |
| 48 | + if toolset: |
| 49 | + env = env.copy() |
| 50 | + env["CXX"] = toolset.CXX |
| 51 | + env["AR"] = toolset.AR |
| 52 | + env["LD"] = toolset.LD or toolset.CXX |
| 53 | + |
| 54 | + args = [sys.executable, "build/gen.py", "--allow-warnings", "--no-static-libstdc++"] |
| 55 | + run_cmd(args, cwd=GN_DIR, env=env) |
36 | 56 | # TODO on CI, build and run unittests |
37 | 57 | run_cmd(["ninja", "-C", "out", "gn"], cwd=GN_DIR) |
38 | 58 | target_path = PROJECT_DIR/"src"/"gn_dist"/"gn" |
39 | 59 | shutil.copyfile(GN_DIR/"out"/"gn", target_path) |
40 | | - make_executable(target_path) |
| 60 | + _make_executable(target_path) |
| 61 | + |
| 62 | + |
| 63 | +def assisted_build(compiler=None, toolprefix="", clang_path=None): |
| 64 | + |
| 65 | + if compiler is None: |
| 66 | + compiler = "gcc" |
| 67 | + |
| 68 | + if compiler == "gcc": |
| 69 | + toolset = CompilerToolset(CXX=toolprefix+"g++", AR=toolprefix+"ar") |
| 70 | + elif compiler == "clang": |
| 71 | + toolset = CompilerToolset(CXX="clang++", AR="llvm-ar") |
| 72 | + if clang_path: |
| 73 | + _env_prepend("PATH", str(clang_path/"bin"), os.pathsep) |
| 74 | + elif compiler == "custom": |
| 75 | + toolset = CompilerToolset( |
| 76 | + CXX=os.environ["CXX"], AR=os.environ["AR"], LD=os.environ.get("LD"), |
| 77 | + ) |
| 78 | + else: |
| 79 | + assert False, compiler |
| 80 | + |
| 81 | + build(toolset) |
| 82 | + |
| 83 | + |
| 84 | +def parse_args(argv): |
| 85 | + parser = argparse.ArgumentParser( |
| 86 | + description="GN build script", |
| 87 | + ) |
| 88 | + parser.add_argument( |
| 89 | + "-c", "--compiler", |
| 90 | + choices=("gcc", "clang", "custom"), |
| 91 | + help = "The compiler to use.", |
| 92 | + ) |
| 93 | + parser.add_argument( |
| 94 | + "--toolprefix", |
| 95 | + default = "", |
| 96 | + help = "(gcc only) Toolprefix to use.", |
| 97 | + ) |
| 98 | + parser.add_argument( |
| 99 | + "--clang-path", |
| 100 | + type = lambda p: Path(p).expanduser().resolve(), |
| 101 | + help = "(clang only) Clang root dir to use.", |
| 102 | + ) |
| 103 | + return parser.parse_args(argv) |
| 104 | + |
| 105 | + |
| 106 | +def main(): |
| 107 | + args = parse_args(sys.argv[1:]) |
| 108 | + assisted_build(**vars(args)) |
| 109 | + |
41 | 110 |
|
42 | 111 | if __name__ == "__main__": |
43 | | - build_gn() |
| 112 | + main() |
0 commit comments