-
Notifications
You must be signed in to change notification settings - Fork 18
/
mach
executable file
·512 lines (451 loc) · 15.5 KB
/
mach
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/env python3
#
# Copyright 2022 Cryspen Sarl
#
# Licensed under the Apache License, Version 2.0 or MIT.
# * http://www.apache.org/licenses/LICENSE-2.0
# * http://opensource.org/licenses/MIT
#
# The mach driver for HACL.
import os
import pathlib
import platform
import re
import shutil
import subprocess
import sys
from ast import arg
from tools.benchmark import run_benchmarks
from tools.configure import Config
from tools.doc import doc
from tools.macos import ios_sysroot
from tools.ocaml import build_ocaml, clean_ocaml
from tools.openssl import find_openssl_home
from tools.rust import rust
from tools.js import build_js
from tools.test import run_tests
from tools.update import update
from tools.utils import (
argument,
check_cmd,
cli,
cmake_config,
config_cache,
coverage_dependency_check,
dep_config,
json_config,
mprint as print,
subcommand,
subparsers,
)
# === SUBCOMMANDS === #
def _install(prefix=None, config="Release"):
cmake_cmd = ["cmake", "--install", "build", "--config", config]
if prefix:
cmake_cmd.extend(["--prefix", prefix])
print(cmake_cmd)
if subprocess.call(cmake_cmd) == 1:
print("`cmake --install` returned with an error.")
if config == "Debug":
print(
"Please make sure to run `./mach build` before `./mach install --debug`."
)
else:
print(
"Please make sure to run `./mach build --release` before `./mach install`."
)
@subcommand(
[
argument("-p", "--prefix",
help="The path prefix to install into.", type=str),
argument("--debug", help="Install a debug build.", action="store_true"),
]
)
def install(args):
# "Release" is the default in `_install`.
if args.debug:
# Thus, we only pass a "Debug" config when asked for ...
print("Note: You are installing a debug build.")
_install(prefix=args.prefix, config="Debug")
else:
# ... and stick to the default when not.
_install(prefix=args.prefix)
@subcommand(
[
argument("-c", "--clean", help="Clean before building.",
action="store_true"),
argument("--tests", help="Build tests.", action="store_true"),
argument("--test", help="Build and run tests.", action="store_true"),
argument("--benchmarks", help="Build benchmarks.", action="store_true"),
argument("--benchmark", help="Build and run benchmarks.",
action="store_true"),
argument(
"--no-openssl",
help="Don't build and run OpenSSL benchmarks.",
action="store_true",
),
argument(
"--libtomcrypt",
help="Build and run LibTomCrypt benchmarks.",
action="store_true",
),
argument("-r", "--release", help="Build in release mode.",
action="store_true"),
argument(
"-a",
"--algorithms",
help="A list of algorithms to enable. Defaults to all.",
type=str,
),
argument(
"-p",
"--target",
help="Define compile target for cross compilation.",
type=str,
),
argument(
"-d",
"--disable",
help="Disable (hardware) features even if available.",
type=str,
),
argument("-s", "--sanitizer", help="Enable sanitizers.", type=str),
argument("--ndk", help="Path to the Android NDK.", type=str),
argument(
"--msvc",
help="Use MSVC on Windows (default is clang-cl).",
action="store_true",
),
argument("-e", "--edition",
help="Choose a different HACL* edition.", type=str),
argument(
"-l",
"--language",
help="Build language bindings for the given language.",
type=str,
),
argument("-v", "--verbose", help="Make builds verbose.",
action="store_true"),
argument(
"-m32", help="Build for 32-bit (even when on 64-bit).", action="store_true"
),
argument(
"--no-build",
help="Don't actually build (don't run ninja).",
action="store_true",
),
argument(
"--coverage",
help="Build with coverage instrumentation.",
action="store_true",
),
]
)
def build(args):
"""Main entry point for building HACL
For convenience it is possible to run tests right after building using --test.
Supported cross compilation targets:
- x86_64-apple-darwin (macOS aarch64 only)
- s390x-linux-gnu
- aarch64-apple-ios (macOS only)
- aarch64-apple-darwin (macOS x64 only)
- aarch64-linux-android
Features that can be disabled:
- vec128 (avx/neon)
- vec256 (avx2)
- vale (x64 assembly)
Supported sanitizers:
- asan
- ubsan
Use an edition if you want a different build. Note that this build will
use the MSVC version by default on Windows.
Supported editions:
- c89
HACL can be built for another language than C.
Note that bindings will always require the full C library such that the
algorithm flag will be ignored.
- rust
- ocaml
- wasm (TBD)
! Windows builds are limited. The following arguments are not supported:
- algorithms
- sanitizer
- edition
- disable
- coverage
"""
# Create the build folder.
if not os.path.exists("build"):
os.mkdir("build")
cmake_args = []
# Verbosity
verbose = False
if args.verbose:
verbose = True
def vprint(*args, **kwargs):
print(args, kwargs)
else:
vprint = lambda *a, **k: None
# Set config
build_config = "Debug"
if args.release:
build_config = "Release"
# Clean if requested
if args.clean:
_clean()
try:
os.mkdir("build")
except:
pass # We ignore the error if the directory exists already
# Check if the config has been run before.
# In future we might want to put content in there.
cache = False
if os.path.exists(config_cache()):
cache = True
bindings = args.language is not None
if bindings and args.language == "ocaml":
# OCaml always gets release builds for now
build_config = "Release"
cflags = []
cxxflags = []
# We want to build for a 32-bit platform.
m32 = False
if args.m32:
cflags.append("-m32")
cxxflags.append("-m32")
m32 = True
# Our default compiler is clang.
compiler = os.getenv("CC", "clang")
windows = False
# Select the source folder to use (regular, c89, msvc)
source_dir = "src"
include_dir = "include"
if args.edition == "c89":
source_dir = os.path.join(source_dir, "c89")
include_dir = os.path.join(include_dir, "c89")
cmake_args.append("-DCMAKE_C_STANDARD=90")
# Set MSVC if detecting Windows.
if sys.platform == "win32":
windows = True
if args.msvc:
source_dir = os.path.join(source_dir, "msvc")
include_dir = os.path.join(include_dir, "msvc")
# get msvc in the path
vs_arch = "64"
if m32:
vs_arch = "32"
vswhere_cmd = ["tools\\vcbuild.cmd", vs_arch]
subprocess.run(vswhere_cmd, check=True)
# We use the ninja multi config generator.
cmake_args.append("-GNinja Multi-Config")
# Use MSVC on Windows if requested.
if windows and args.msvc:
cmake_args.append("-DUSE_MSVC=1")
# Enrich the environment for all calls.
env = os.environ
# Set target toolchain if cross compiling
target = args.target
if args.target:
if windows:
print("! Cross-compilation is not supported on Windows.")
exit(1)
if m32:
print("! Cross-compilation is not supported when -m32 is set.")
exit(1)
if args.target == "x86_64-apple-darwin":
cmake_args.extend(
["-DCMAKE_TOOLCHAIN_FILE=config/x64-darwin.cmake"])
elif args.target == "s390x-linux-gnu":
cmake_args.extend(
[
"-DCMAKE_TOOLCHAIN_FILE=config/s390x.cmake",
"-DCMAKE_C_COMPILER=s390x-linux-gnu-gcc-10",
"-DCMAKE_CXX_COMPILER=s390x-linux-gnu-g++-10",
]
)
elif args.target == "aarch64-apple-ios":
cmake_args.extend(
["-DCMAKE_TOOLCHAIN_FILE=config/aarch64-ios.cmake"])
cmake_args.extend(["-DCMAKE_OSX_SYSROOT=" + ios_sysroot()])
elif args.target == "aarch64-apple-darwin":
cmake_args.extend(
["-DCMAKE_TOOLCHAIN_FILE=config/aarch64-darwin.cmake"])
elif args.target == "aarch64-linux-android":
if args.ndk:
cmake_args.append("-DANDROID_NDK_PATH=" + args.ndk)
else:
print(
'! Compiling for "%s" requires an NDK. \n\t Use --ndk to specify the path.'
% args.target
)
print(" See help for more information.")
exit(1)
cmake_args.extend(
["-DCMAKE_TOOLCHAIN_FILE=config/aarch64-android.cmake"])
else:
print('! Unknown cross-compilation target "%s"' % args.target)
print(" See help for available targets.")
exit(1)
if args.disable:
if windows:
print("! Disabling features is not supported on Windows.")
exit(1)
features_to_disable = list(
map(
lambda f: "-DDISABLE_" + f.upper() + "=ON",
re.split(r"\W+", args.disable),
)
)
cmake_args.extend(features_to_disable)
if args.tests or args.test:
cmake_args.append("-DENABLE_TESTS=ON")
if args.benchmarks or args.benchmark:
if not args.release:
print(
"! Benchmarks need to be run on release for now. Please add --release"
)
exit(1)
cmake_args.append("-DENABLE_BENCHMARKS=ON")
if not args.no_openssl:
cmake_args.append("-DENABLE_OPENSSL_BENCHMARKS=ON")
if platform.system() == "Darwin":
openssl_home = find_openssl_home()
if openssl_home is not None:
env = {**env, "OPENSSL_HOME": openssl_home}
if args.libtomcrypt:
cmake_args.append("-DENABLE_LIBTOMCRYPT_BENCHMARKS=ON")
if args.sanitizer:
if windows:
print("! Sanitizers are not supported on Windows.")
exit(1)
sanitizers = list(
map(
lambda f: "-DENABLE_" + f.upper() + "=ON",
re.split(r"\W+", args.sanitizer),
)
)
cmake_args.extend(sanitizers)
# if verbose:
# cmake_args.extend(["--debug-output", "--trace"])
# Check if we want to compile with coverage information.
if args.coverage:
if windows:
print("The `--coverage` flag is not supported on Windows.")
exit(1)
# Check that `lcov` and `genhtml` are installed (and callable).
coverage_dependency_check()
cmake_args.append("-DENABLE_COVERAGE=ON")
if len(cflags) != 0:
cmake_args.append("-DCMAKE_C_FLAGS=" + " ".join(cflags))
if len(cxxflags) != 0:
cmake_args.append("-DCMAKE_CXX_FLAGS=" + " ".join(cxxflags))
# In order to perform correct dependency analysis we have to first get a
# correct config.h. The config.h is generated by cmake, which requires the
# config.cmake generated by this script.
# We therefore have to
# - run the mach configuration to generate a (incorrect) config.cmake
# - run cmake to generate config.h
# - run the mach configuration again to generate the correct config.cmake
# - run cmake to generate the ninja build files
#
# If this has been run on this system before, only the last cmake invocation
# is performed.
# '--debug-trycompile'
if not cache:
print("Running config to write config.cmake and config.h ...")
config = Config(
json_config(), source_dir, include_dir, compiler=compiler, target=target
)
config.write_cmake_config(cmake_config())
config.write_dep_config(dep_config())
cmake_cmd = ["cmake", "-B", "build"]
cmake_cmd.extend(cmake_args)
vprint(str(cmake_cmd))
subprocess.run(cmake_cmd, check=True, env=env)
pathlib.Path(config_cache()).touch()
if not cache or (args.algorithms and not bindings) or args.test or args.benchmark:
algorithms = []
if args.algorithms and not bindings:
algorithms = re.split(r"\W+", args.algorithms)
config = Config(
json_config(),
source_dir,
include_dir,
algorithms=algorithms,
compiler=compiler,
target=target,
)
config.write_cmake_config(cmake_config())
config.write_dep_config(dep_config())
cmake_cmd = ["cmake", "-B", "build"]
cmake_cmd.extend(cmake_args)
vprint(str(cmake_cmd))
subprocess.run(cmake_cmd, check=True, env=env)
if args.no_build:
print("Finished configuration, exiting because you didn't want me to build.")
exit(0)
# Set ninja arguments
ninja_args = []
if verbose:
ninja_args.append("-v")
# build C library
ninja_cmd = ["ninja", "-f", "build-%s.ninja" % build_config, "-C", "build"]
ninja_cmd.extend(ninja_args)
vprint(str(ninja_cmd))
subprocess.run(ninja_cmd, check=True)
# build bindings if requested
if bindings:
if args.language == "rust":
check_cmd("cargo")
_install(prefix="build/installed", config=build_config)
cargo_cmd = "cargo build --manifest-path rust/Cargo.toml"
if verbose:
cargo_cmd += " -v"
env = {**os.environ, "MACH_BUILD": "1"}
if windows:
subprocess.Popen("setx MACH_BUILD 1", shell=True).wait()
subprocess.run(cargo_cmd, check=True, shell=True, env=env)
elif args.language == "ocaml":
check_cmd("make")
check_cmd("ocaml")
print()
build_ocaml()
elif args.language == "js":
check_cmd("make")
check_cmd("node")
print()
build_js()
else:
print(
"Unknown language binding %s. Please see --help for supported bindings"
% (args.language)
)
exit(1)
print("Build finished.")
# test if requested
if args.test:
run_tests(config.tests, build_config, coverage=args.coverage)
# benchmark if requested
if args.benchmark:
run_benchmarks(config.benchmarks, build_config)
def _clean():
print("Cleaning ...")
# These might fail if not present. That's ok.
shutil.rmtree("build", ignore_errors=True)
try:
clean_ocaml()
except:
pass # We don't really care
@subcommand()
def clean(args):
"""Remove all build and config artifacts"""
_clean()
# === Boiler plate === #
def main():
args = cli.parse_args()
if args.subcommand is None:
cli.print_help()
else:
args.func(args)
if __name__ == "__main__":
main()