diff --git a/tools/buildspark.py b/tools/buildspark.py index 0ea963e5c..363460493 100644 --- a/tools/buildspark.py +++ b/tools/buildspark.py @@ -2,6 +2,7 @@ import argparse import os import subprocess import shutil +from typing import Tuple class VisualStudioContext: @@ -73,6 +74,7 @@ def _add_toggle_opt(parser, name, **kwargs): return parser + class SparkPaths: @staticmethod def root(): @@ -83,8 +85,8 @@ class SparkPaths: return fr'{_workspace_root()}\src\client\{name}\{name}.csproj' @staticmethod - def libspark_proj(build_dir): - return fr'{build_dir}\ALL_BUILD.vcxproj' + def client_sln(): + return fr'{_workspace_root()}\spark_client.sln' @staticmethod def tools(): @@ -99,6 +101,97 @@ class SparkPaths: return fr'{_workspace_root()}\externals' +class BuildTarget: + def __init__(self, name, path): + self.name = name + self.path = path + + +class BuildContext: + def __init__(self, workspace_root = None, build_dir = None): + workspace_root = workspace_root or _workspace_root() + build_dir = build_dir or f'{workspace_root}\\build' + + self.workspace_root = workspace_root + self.build_dir = build_dir + + def get_csharp_targets() -> dict: + targets = {} + + def add_target(target: BuildTarget): + targets[target.name] = target + + add_target(BuildTarget('spark_client', f'{workspace_root}\\spark_client.sln')) + + def add_csproj_target(name: str): + add_target(BuildTarget(name, f'{workspace_root}\\src\\client\\{name}\\{name}.csproj')) + + # TODO: rename 'Spark.csproj' -> 'spark.csproj' to be consistent + # with all other csproj filenames, which are snake_case + add_target(BuildTarget('spark', f'{workspace_root}\\src\\client\\spark\\Spark.csproj')) + add_csproj_target('spark_api') + add_csproj_target('spark_core') + add_csproj_target('spark_sdk') + add_csproj_target('spark_test') + add_csproj_target('sparkcmd') + return targets + + self.csharp_targets = get_csharp_targets() + self.csharp_aliases = { + 'all': ['spark_client'], + 'api': ['spark_api'], + 'core': ['spark_core'], + 'cmd': ['sparkcmd'], + 'test': ['test'], + 'sdk': ['spark_sdk'] + } + + def get_cpp_targets() -> dict: + targets = {} + + def add_target(target: BuildTarget): + targets[target.name] = target + + add_target(BuildTarget('libspark', f'{build_dir}\\ALL_BUILD.vcxproj')) + return targets + + self.cpp_targets = get_cpp_targets() + self.cpp_aliases = { } + + def parse_targets(self, targets) -> Tuple[list, list]: + '''Parses target input (from cli) and returns a tuple whose items are a + list of csharp targets, and cpp targets, respectively''' + + csharp_ts = [] + cpp_ts = [] + for t in targets: + # 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. + csharp_ts.extend(self._parse_target(t, self.csharp_targets, self.csharp_aliases)) + cpp_ts.extend(self._parse_target(t, self.cpp_targets, self.cpp_aliases)) + + return (csharp_ts, cpp_ts) + + def _parse_target(self, t, targets, aliases) -> list: + if t in targets: + return [targets[t]] + elif t in aliases: + alias_ts = aliases[t] + for a in alias_ts: + if a not in targets: + _exit(f"Failed to find target for '{t}' (alias '{a}'). known targets: {targets}") + return [targets[alias_t] for alias_t in aliases[t]] + return [] + + def _parse_targets(self, targets_in, targets, aliases): + rv = [] + for t in targets_in: + res = self._parse_target(t, targets, aliases) + if res is not None: + rv[res.name] = res + return rv + + class CWD: def __init__(self, push_path, pop_path=os.getcwd()): self.push_path = push_path @@ -149,6 +242,10 @@ def _get_arg_parser(): nargs='+', help='The target(s) to build') + _add_toggle_opt(parser, 'cpp', help='Whether or not to build cpp') + + _add_toggle_opt(parser, 'csharp', help='Whether or not to build csharp') + _add_toggle_opt( parser, 'restore', help='Whether or not to run msbuild /t:restore') @@ -234,19 +331,14 @@ if __name__ == "__main__": args = parser.parse_args() args_dict = vars(args) - build_dir = _get_arg(args_dict, 'output_dir') or SparkPaths.default_build() - link_build_dir = _get_arg(args_dict, 'link_output_dir') - build_csharp = args.csharp - build_cpp = args.cpp clean = args.clean restore = args.restore - - targets = _get_arg(args_dict, 'target') or [['spark']] - targets = [SparkPaths.client_csproj(t) for grp in targets for t in grp] - - if build_dir is None: - print(args) - exit(1) + build_dir = _get_arg(args_dict, 'output_dir') or SparkPaths.default_build() + link_build_dir = _get_arg(args_dict, 'link_output_dir') + targets = _get_arg(args_dict, 'target') or [] + targets = [t for grp in targets for t in grp] + if len(targets) == 0: + targets.append('all') vs_context = _deduce_visual_studio_context() @@ -259,6 +351,8 @@ if __name__ == "__main__": except Exception as e: _exit(1, e) + build_context = BuildContext(_workspace_root(), build_dir) + csharp_targets, cpp_targets = build_context.parse_targets(targets) build_cmd_steps = [vs_context.prompt_cmd()] base_output_path_arg = vs_context.msbuild_prop( 'BaseOutputPath', f'{build_dir}\\{args.configuration}') @@ -274,11 +368,14 @@ if __name__ == "__main__": f"{vs_context.msbuild_target('restore')}", f"{vs_context.msbuild_clp('verbosity', 'minimal')}", f"{vs_context.msbuild_clp('summary')}"]) - build_cmd_steps.append( - f'{restore_cmd_prefix} {SparkPaths.libspark_proj(build_dir)}') - for t in targets: + + for t in cpp_targets: build_cmd_steps.append( - f'{restore_cmd_prefix} {base_output_path_arg} {t}') + f'{restore_cmd_prefix} {t.path}') + + for t in csharp_targets: + build_cmd_steps.append( + f'{restore_cmd_prefix} {base_output_path_arg} {t.path}') # main build build_cmd_prefix = ' '.join([ @@ -290,11 +387,12 @@ if __name__ == "__main__": f"{vs_context.msbuild_prop('configuration', args.configuration)}", f"{vs_context.msbuild_clp('verbosity', 'minimal')}", f"{vs_context.msbuild_clp('summary')}"]) - build_cmd_steps.append( - f'{build_cmd_prefix} {SparkPaths.libspark_proj(build_dir)}') - for t in targets: + for t in cpp_targets: build_cmd_steps.append( - f'{build_cmd_prefix} {base_output_path_arg} {t}') + f'{build_cmd_prefix} {t.path}') + for t in csharp_targets: + build_cmd_steps.append( + f'{build_cmd_prefix} {base_output_path_arg} {t.path}') build_cmd = ' && '.join(build_cmd_steps) subprocess.run(build_cmd)