import argparse import os import subprocess from core.file_utils import SparkPaths from core.iter_utils import at_bounded class BuildArgs: def __init__( self, configuration: str = 'debug', clean: bool = False, verbosity: int = 1, dry_run: bool = False, publish: bool = False, ): self.configuration = configuration self.clean = clean self.verbosity = verbosity self.dry_run = dry_run self.dotnet_verbosity = at_bounded( ['quiet', 'minimal', 'normal', 'detailed', 'diagnostic'], verbosity) # Leave out WARNING and NOTICE to normalize cmake log level and dotnet verbosity # (so both can be expressed as an int 1-5) self.cmake_log_level = at_bounded( [ 'ERROR', # 'WARNING', # 'NOTICE', 'STATUS', 'VERBOSE', 'DEBUG', 'TRACE', ], verbosity) self.publish = publish def _get_arg_parser(): parser = argparse.ArgumentParser(description='Build spark_client.sln') parser.add_argument( '--configuration', metavar='debug|release', help='The build configuration', ) parser.add_argument( '--clean', action='store_true', help='Perform a clean build (remove artifacts from previous build)', ) parser.add_argument( '--verbosity', metavar='{0-5}', choices=range(0, 6), type=int, help='The output verbosity level', ) parser.add_argument( '--dry-run', action='store_true', help='Preview the build commands without running them', ) defaults = BuildArgs() parser.set_defaults( configuration=defaults.configuration, clean=defaults.clean, verbosity=defaults.verbosity, dry_run=defaults.dry_run, ) return parser def build(args: BuildArgs): def run(cmd): print(cmd) if args.dry_run: return subprocess.run(cmd).check_returncode() def dotnet_cmd(action='build'): return ' '.join( [ f'dotnet {action} spark_client.sln', f'--configuration {args.configuration.title()}', f'--verbosity {args.dotnet_verbosity}', ]) os.chdir(SparkPaths.root()) cmake_configure_cmd = ' '.join( [ f'cmake', f'--preset spark', f'-DCMAKE_MESSAGE_LOG_LEVEL={args.cmake_log_level}', ]) cmake_build_cmd = ' '.join( [ f'cmake', f'--build', f'--preset spark-{args.configuration.lower()}', ]) if args.clean: run(f'{cmake_build_cmd} --target clean') run(dotnet_cmd('clean')) run(cmake_configure_cmd) run(cmake_build_cmd) run(dotnet_cmd('publish' if args.publish else 'build')) def _main(): parser = _get_arg_parser() cli_args = parser.parse_args() args = BuildArgs( configuration=cli_args.configuration, clean=cli_args.clean, verbosity=cli_args.verbosity, dry_run=cli_args.dry_run, ) build(args) if __name__ == "__main__": try: _main() except Exception as e: print(f'build_spark.py failed: {e}') exit(1)