Files
spark/tools/py/build_sparkd.py
T
John Colvin e64dc49a64 fix build: Respect cfg path order in gen'd defaults
The path order represents the value-inheritance hierarchy. This way,
values from files earlier in the path take precedence over those from
later files.
2023-06-07 13:16:21 -04:00

288 lines
8.9 KiB
Python

import argparse
from typing import cast
import subprocess
from cmake.cmake_utils import CMakeCompilerPair, CMakeConfigureCommand
from core.argparse_utils import add_toggle_opt, get_arg
from core.cmake_utils import CMakeCompilerPair, CMakeConfigureCommand
from core.file_utils import CWD, SparkPaths, as_path, create_dir
from core.iter_utils import distinct, flatten
from core.logging import exit_msg
from core.str_utils import wrap_dquotes
def _parse_compiler(arg: str or None):
'''Parses compiler input (from cli) and returns a tuple of c, cxx compilers respectively'''
known_compilers = {
'gcc': CMakeCompilerPair('gcc', 'gcc-11', 'g++-11'),
'clang': CMakeCompilerPair('clang', 'clang', 'clang++'),
'clang-13': CMakeCompilerPair('clang', 'clang-13', 'clang++-13'),
}
if arg in known_compilers:
return known_compilers[arg]
raise ValueError(
f"Unexpected compiler '{arg}'. Please use one of [{' '.join(known_compilers)}]"
)
def _parse_target(t, targets, aliases) -> list:
if t in targets:
return [targets[t]]
elif aliases is not None and t in aliases:
alias_ts = aliases[t]
for a in alias_ts:
if a not in targets:
msg = f"Failed to find target for '{t}' (alias '{a}'). known targets: {targets}"
exit_msg(1, msg)
return [targets[alias_t] for alias_t in aliases[t]]
return []
def _parse_targets(targets, candidates, aliases=[]):
'''Parses target input (from cli)'''
if targets is None:
return None
# Always try to parse `t` as both cpp and csharp targets, since it
# could be an alias for a combination of csharp and cpp targets.
return flatten([_parse_target(t, candidates, aliases) for t in targets])
def _parse_build_dir(build_dir, build_type, compiler):
return build_dir or SparkPaths.root().joinpath('build', compiler.name, build_type.lower())
class BuildSparkdArgs:
def __init__(self, args):
args_dict = vars(args)
self.compiler = _parse_compiler(get_arg(args_dict, 'compiler', 'gcc'))
# Forcibly correct case (so user doesn't have to worry abt it)
self.build_type = str(args.build_type).lower()
if self.build_type not in ['debug', 'release']:
raise ValueError("Unexpected build_type '{args.build_type}'")
self.build_dir = _parse_build_dir(
args_dict.get('output_dir'), self.build_type, self.compiler)
self.clean = cast(bool, args.clean)
self.jobs=args.jobs
self.targets = _parse_targets(args_dict.get('targets'), ['all', 'install'])
self.config_profiles = args.config_profiles
def _get_arg_parser():
parser = argparse.ArgumentParser(description='Build spark_client.sln')
parser.add_argument(
'--build-type',
metavar='debug|release',
type=str,
nargs='?',
help='The build type')
parser.add_argument(
'--compiler',
metavar='gcc|clang',
type=str,
nargs='?',
help='The compiler')
parser.add_argument(
'-o',
'--output-dir',
metavar='path',
type=str,
nargs='?',
help=
'The directory in which to build (defaults to [repo_root]/build/<compiler>/<configuration>)'
)
parser.add_argument(
'--config-profiles',
nargs='+',
help='Configuration profiles (can be used to override the generated config defaults)'
)
parser.add_argument(
'-t',
'--target',
metavar='targets',
type=str,
action='append',
nargs='+',
help='The target(s) to build')
parser.add_argument(
'-j',
'--jobs',
type=int,
help='[CMake] The number of parallel jobs to run')
add_toggle_opt(
parser,
'clean',
help=
'Whether or not to delete and overwrite an existing build directory')
parser.set_defaults(build_type='Debug', compiler='gcc')
return parser
def _get_cmake_build_type(build_type: str):
'''
Converts a case-insensitive build-type str (from cli) to a case-sensitive CMAKE_BUILD_TYPE var
'''
# The 'build_type' arg is case-sensitive because 'CMAKE_BUILD_TYPE' is substitued for a folder
# in the externals-libs path. In order to match a real path, it must be 'Debug' or 'Release'.
# Keep the orig_build_type around for potential use in err msg.
orig_build_type = build_type
build_type = build_type or 'debug'
build_type = build_type.title()
if build_type not in ['Debug', 'Release']:
raise ValueError(f"Unexpected build_type '{orig_build_type}'")
return build_type
def _get_cmake_opts(build_type, compiler, config_profiles: list):
opts = {}
cxx_flags_common = [
'-fPIC',
'-Wall',
'-pedantic',
'-Wextra',
'-Werror',
'-Wno-missing-braces',
'-Wno-ignored-qualifiers',
'--std=c++17',
'-fdiagnostics-color',
]
if compiler == 'gcc':
cxx_flags_common.append('-Wno-stringop-overread')
if build_type == 'debug':
cxx_flags_debug = cxx_flags_common + [
'-g',
'-DDEBUG',
]
opts['CMAKE_CXX_FLAGS_DEBUG'] = f'"{" ".join(cxx_flags_debug)}"'
opts['CMAKE_EXPORT_COMPILE_COMMANDS'] = 'ON'
else:
cxx_flags_release = cxx_flags_common + [
'-fno-omit-frame-pointer',
'-DNDEBUG',
'-O3',
]
if compiler == 'clang':
cxx_flags_release.extend([
'-Wno-uninitialized',
])
else:
cxx_flags_release.extend([
'-Wno-maybe-uninitialized',
])
opts['CMAKE_CXX_FLAGS_RELEASE'] = f'"{" ".join(cxx_flags_release)}"'
# Ensure 'defaults' is the first item profile (so all other profiles
# override the defaults). Remove duplicates to guard against bad input.
opts['CONFIG_DEFAULTS_PATH'] = wrap_dquotes(
';'.join([f'config/profiles/{p}/*.conf' for p in config_profiles]))
return opts
def _configure(
src_dir,
build_dir,
build_type,
compiler: CMakeCompilerPair,
config_profiles=[]) -> subprocess.CompletedProcess:
'''Configures sparkd (server) build'''
cmake_build_type = _get_cmake_build_type(build_type)
cmd = CMakeConfigureCommand(
source_dir=src_dir,
build_dir=build_dir,
generator='Ninja',
build_type=cmake_build_type,
compiler=compiler)
opts = _get_cmake_opts(build_type, compiler.name, config_profiles)
for name, value in opts.items():
cmd.add_opt(name, value)
print(f'configure_cmd: {cmd}')
with CWD(build_dir):
return subprocess.run(str(cmd), shell=True)
def _check_results(proc_results):
for res in proc_results:
if res.returncode != 0:
exit_msg(1, f'The following build subprocess failed: {res}')
def _build(
src_dir,
build_dir,
build_type=None,
compiler=None,
clean=False,
targets=None,
config_profiles=None,
jobs=None) -> list:
'''Builds sparkd (server)'''
# check if explicit no-op
if targets is not None and len(targets) == 0:
return []
compiler = compiler or CMakeCompilerPair('gcc', 'gcc', 'g++')
config_profiles = config_profiles or []
build_type = build_type or 'release'
build_dir = as_path(build_dir).value()
proc_results = []
try:
print(f'creating build_dir (clean: {clean}): {build_dir}')
create_dir(build_dir, clean)
print(f'configuring build (clean: {clean}): {build_dir}')
cfg_res = _configure(
src_dir=src_dir,
build_dir=build_dir,
build_type=build_type,
compiler=compiler,
config_profiles=config_profiles)
proc_results.append(cfg_res)
if cfg_res.returncode != 0:
# Don't bother executing build step since configure failed
return proc_results
except Exception as e:
exit_msg(1, e)
build_cmd_parts = [f'cmake --build {build_dir}']
if jobs is not None:
build_cmd_parts.append(f'-j {jobs}')
targets = targets or ['all', 'server_layout']
build_cmd_parts.append(f"--target {' '.join([str(t) for t in targets])}")
build_cmd = ' '.join(build_cmd_parts)
print(f'build_cmd: {build_cmd}')
build_res = subprocess.run(build_cmd, shell=True)
proc_results.append(build_res)
return proc_results
def _main():
parser = _get_arg_parser()
args = BuildSparkdArgs(parser.parse_args())
proc_results = _build(
src_dir=SparkPaths.root(),
build_dir=args.build_dir,
build_type=args.build_type,
compiler=args.compiler,
clean=args.clean,
targets=args.targets,
config_profiles=args.config_profiles,
jobs=args.jobs)
_check_results(proc_results)
if __name__ == '__main__':
_main()