forked from vepadulano/build-root-recipe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaunch_build.py
129 lines (111 loc) · 4.58 KB
/
launch_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import argparse
import os
import re
import shlex
import subprocess
import textwrap
from typing import Iterable
def create_directories_if_necessary() -> Iterable[str]:
cwd = os.getcwd()
root_home = os.path.join(cwd, "root")
if not os.path.exists(root_home):
subprocess.run(shlex.split(
"git clone -o upstream https://github.com/root-project/root.git"), check=True)
if not os.path.exists(os.path.join(cwd, "roottest")):
subprocess.run(shlex.split(
"git clone -o upstream https://github.com/root-project/roottest.git"), check=True)
root_build = os.path.join(cwd, "rootbuild")
if not os.path.exists(root_build):
os.mkdir(root_build)
root_install = os.path.join(cwd, "rootinstall")
if not os.path.exists(root_install):
os.mkdir(root_install)
return root_home, root_build, root_install
# builtin_glew is necessary on latest CMake versions https://gitlab.kitware.com/cmake/cmake/-/issues/19662
build_config = {
"default": "-DCMAKE_BUILD_TYPE=RelWithDebInfo -Dbuiltin_glew=ON",
"debug": (
"-DCMAKE_BUILD_TYPE=Debug -Dtesting=ON -Droottest=ON "
"-Dtest_distrdf_pyspark=ON -Dtest_distrdf_dask=ON -Dbuiltin_glew=ON"
),
"relwithdebinfo": (
"-DCMAKE_BUILD_TYPE=RelWithDebInfo -Dtesting=ON -Droottest=ON "
"-Dtest_distrdf_pyspark=ON -Dtest_distrdf_dask=ON -Dbuiltin_glew=ON"
),
"minimal": (
"-DCMAKE_BUILD_TYPE=RelWithDebInfo -Dminimal=ON"
),
}
parser = argparse.ArgumentParser()
parser.add_argument("-j", help="As in 'cmake -jNJOBS'",
default=os.cpu_count(), type=int, dest="njobs")
parser.add_argument(
"-n",
help=("The name of this build. If specified, it takes "
"precedence over the automatic choice for a name"),
dest="name")
group = parser.add_argument_group(
"CMake configuration [required]",
textwrap.dedent("""
The 'mode' option allows to choose one of the predefined CMake configuration
strings. Otherwise, specify a custom string via the 'config' option
"""))
exclusive_group = group.add_mutually_exclusive_group(required=True)
exclusive_group.add_argument(
"-m",
help="One of the predefined CMake configuration modes",
choices=list(build_config.keys()),
default="default",
dest="mode")
exclusive_group.add_argument(
"-c",
help=(textwrap.dedent("""
Custom list of CMake options. Specify this option with an equal sign and
quoted, as in: '-c=\"-DOpt1=ON -DOpt2=OFF\"'
""")),
nargs="*",
dest="config")
args = parser.parse_args()
def launch_build():
root_home, root_build, root_install = create_directories_if_necessary()
if args.name is None:
# Figure out a sensible name to give to the build/install directories
p = subprocess.run(
["git", "status"], cwd=root_home,
check=True, capture_output=True)
gitstatus = p.stdout.decode()
pattern = re.compile("^On branch (?P<branch>.*)")
branch = pattern.match(gitstatus).groupdict()["branch"]
if branch == "master":
# Further specify builds on the master branch with the commit SHA
p = subprocess.run(
["git", "rev-parse", "--short", "HEAD"],
cwd=root_home, check=True, capture_output=True)
sha = p.stdout.decode().rstrip()
branch += "-" + sha
dirname = branch + "-" + args.mode if args.mode else branch + "-custom"
else:
dirname = args.name
# Check if we are using a conda environment
dirname = dirname + \
(f"-conda-{os.environ['CONDA_DEFAULT_ENV']}" if os.environ.get(
'CONDA_DEFAULT_ENV', "") else "")
build_dir = os.path.join(root_build, dirname)
if not os.path.exists(build_dir):
os.mkdir(build_dir)
install_dir = os.path.join(root_install, dirname)
if not os.path.exists(install_dir):
os.mkdir(install_dir)
if not os.path.exists(os.path.join(build_dir, "CMakeCache.txt")):
base_config = shlex.split("cmake -GNinja -Dccache=ON")
mode_config = args.config[0] if args.config else build_config[args.mode]
mode_config = shlex.split(mode_config)
dirs_config = shlex.split(
f"-DCMAKE_INSTALL_PREFIX={install_dir} -B {build_dir} -S {root_home}")
configure_command = base_config + mode_config + dirs_config
subprocess.run(configure_command, check=True)
njobs = args.njobs
build_command = f"cmake --build {build_dir} --target install -j{njobs}"
subprocess.run(shlex.split(build_command), check=True)
if __name__ == "__main__":
launch_build()