#! /usr/bin/env python3 import os import argparse import platform import subprocess import sys def shell(command, expect=0, cwd=None, env={}): subprocess_stdout = subprocess.DEVNULL print("Env:", env) print("Command: ", end="") for i, word in enumerate(command): if i == 4: print("'{}' ".format(word), end="") else: print("{} ".format(word), end="") print("\nDirectory: {}".format(cwd)) os_env = os.environ os_env.update(env) ret = subprocess.run(command, cwd=cwd, env=os_env) if ret.returncode != expect: raise Exception("Error {}. Expected {}.".format(ret, expect)) class buildAction(argparse.Action): def __call__(self, parser, args, values, option_string=None) -> None: if platform.system() == "Windows": # On Windows we use MSVC etc. by default. # There's no multi config here. The type needs to be set when configuring. cmake_args = [] if args.release: cmake_args.append("-DCMAKE_BUILD_TYPE=Release") shell(["cmake", "-B", "build"] + cmake_args) else: # By default we use ninja with a multi config and set the build type # during the build. cmake_build_args = [] if args.release: cmake_build_args.append("--config Release") shell(["cmake", "-B", "build", "-GNinja Multi-Config"]) shell(["cmake", "--build", "build"] + cmake_build_args) return None def parse_arguments(): parser = argparse.ArgumentParser(description="Libcrux C build helper.") subparsers = parser.add_subparsers() build_parser = subparsers.add_parser("build", help="Build the C code.") build_parser.add_argument("build", nargs="*", action=buildAction) build_parser.add_argument( "--release", action="store_true", help="Build in relase mode" ) if len(sys.argv) == 1: parser.print_help(sys.stderr) sys.exit(1) return parser.parse_args() def main(): # Don't print unnecessary Python stack traces. sys.tracebacklimit = 0 parse_arguments() if __name__ == "__main__": main()