-
Notifications
You must be signed in to change notification settings - Fork 63
/
build.py
137 lines (122 loc) · 4.28 KB
/
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
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import subprocess
import os
import tempfile
import argparse
import shutil
from typing import Optional
def check_call(*args):
print("Executing", args)
subprocess.check_call(args)
def get_build_root(build_root: str) -> str:
if build_root:
os.makedirs(build_root, exist_ok=True)
return build_root
base_tmp_dir = None
if os.path.isdir("/dev/shm"):
base_tmp_dir = "/dev/shm"
return tempfile.mkdtemp(prefix="securefs", dir=base_tmp_dir)
def _unchecked_get_vcpkg_cmake_file(vcpkg_root: Optional[str]) -> str:
subpath = "scripts/buildsystems/vcpkg.cmake"
vcpkg_root = vcpkg_root or os.environ.get("VCPKG_ROOT")
if vcpkg_root:
return os.path.join(vcpkg_root, subpath)
exe_path = shutil.which("vcpkg")
if exe_path:
vcpkg_root = os.path.dirname(exe_path)
return os.path.join(vcpkg_root, subpath)
vcpkg_root = os.path.expanduser("~/vcpkg")
return os.path.join(vcpkg_root, subpath)
def get_vcpkg_cmake_file(vcpkg_root: Optional[str]) -> str:
result = _unchecked_get_vcpkg_cmake_file(vcpkg_root)
if not os.path.isfile(result):
raise ValueError(
"Cannot find vcpkg installation by heuristic. Please specify --vcpkg_root explicitly."
)
return result
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--vcpkg_root",
help="The root of vcpkg repository",
default=None,
)
parser.add_argument(
"--build_root",
help="The root of build directory. If unset, a temporary directory will be used.",
default="",
)
parser.add_argument(
"--enable_test", default=False, help="Enable all tests", action="store_true"
)
parser.add_argument(
"--enable_unit_test",
default=False,
help="Run unit test after building to ensure correctness",
action="store_true",
)
parser.add_argument(
"--enable_integration_test",
default=False,
help="Run integration test after building to ensure correctness",
action="store_true",
)
parser.add_argument(
"--triplet",
default="" if os.name != "nt" else "x64-windows-static-md",
help="Override the default vcpkg triplet",
)
parser.add_argument(
"--cmake_defines",
default=[],
nargs="*",
help="Additional CMake definitions. Example: FOO=BAR",
)
parser.add_argument("--build_type", default="Release", help="CMake build type")
parser.add_argument(
"--clang_cl", help="Use clang-cl on Windows for building", action="store_true"
)
parser.add_argument(
"--lto",
help="Build with link time optimization. Only works on some platforms",
action="store_true",
)
args = parser.parse_args()
if args.enable_test: # For backwards compat
args.enable_unit_test = True
args.enable_integration_test = True
source_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(get_build_root(args.build_root))
configure_args = [
"cmake",
"-DCMAKE_BUILD_TYPE=" + args.build_type,
f"-DCMAKE_TOOLCHAIN_FILE={get_vcpkg_cmake_file(args.vcpkg_root)}",
]
if args.clang_cl:
configure_args += ["-T", "ClangCL"]
if args.triplet:
configure_args.append("-DVCPKG_TARGET_TRIPLET=" + args.triplet)
if not args.enable_unit_test:
configure_args.append("-DSECUREFS_ENABLE_UNIT_TEST=OFF")
if not args.enable_integration_test:
configure_args.append("-DSECUREFS_ENABLE_INTEGRATION_TEST=OFF")
if args.lto:
configure_args += [
"-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON",
f"-DVCPKG_OVERLAY_TRIPLETS={os.path.join(source_dir,'overlay_triplets')}",
]
for pair in args.cmake_defines:
configure_args.append("-D" + pair)
configure_args.append(source_dir)
check_call(*configure_args)
check_call(
"cmake", "--build", ".", "--config", args.build_type, "-j", str(os.cpu_count())
)
if args.enable_unit_test or args.enable_integration_test:
check_call("ctest", "-V", "-C", args.build_type)
print(
"Build succeeds. Please copy the binary somewhere in your PATH:",
os.path.realpath("./securefs"),
)
if __name__ == "__main__":
main()