From ed0fc609009d8f12f3473d5cf3ab1ef2e4bac48a Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 7 Apr 2023 00:57:13 -0400 Subject: [PATCH] build: Clean up build_spark.py a bit --- tools/py/build_spark.py | 91 ++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 33 deletions(-) diff --git a/tools/py/build_spark.py b/tools/py/build_spark.py index e0da29f7f..33c47eef5 100644 --- a/tools/py/build_spark.py +++ b/tools/py/build_spark.py @@ -1,11 +1,9 @@ import argparse -import os +from os import PathLike from pathlib import Path import subprocess import shutil -from os import PathLike - from core.argparse_utils import add_toggle_opt, get_arg from core.cmake_utils import CMakeConfigureCommand from core.file_utils import CWD, SparkPaths, create_dir, link_dir, unlink @@ -49,16 +47,27 @@ def _get_cmake_opts(): '/MT', ] opts = { - 'CMAKE_CXX_FLAGS': wrap_dquotes(' '.join(cxx_flags)), - 'CMAKE_CXX_FLAGS_DEBUG': wrap_dquotes(' '.join(cxx_flags_debug)), - 'CMAKE_CXX_FLAGS_RELEASE': wrap_dquotes(' '.join(cxx_flags_release)), - 'CMAKE_SHARED_LINKER_FLAGS_RELEASE': wrap_dquotes(' '.join(linker_flags)) + 'CMAKE_CXX_FLAGS': + wrap_dquotes(' '.join(cxx_flags)), + 'CMAKE_CXX_FLAGS_DEBUG': + wrap_dquotes(' '.join(cxx_flags_debug)), + 'CMAKE_CXX_FLAGS_RELEASE': + wrap_dquotes(' '.join(cxx_flags_release)), + 'CMAKE_SHARED_LINKER_FLAGS_RELEASE': + wrap_dquotes(' '.join(linker_flags)) } return opts class VisualStudioContext: - def __init__(self, version, year, prompt_path, msbuild_path, msbuild_arg_prefix): + def __init__( + self, + version, + year, + prompt_path: Path, + msbuild_path: Path, + msbuild_arg_prefix, + ): self.version = version self.year = year self.prompt_path = prompt_path @@ -105,26 +114,41 @@ class VisualStudioContext: return f'{self._msbuild_arg_prefix}p:{key}={value}' -def _deduce_visual_studio_context(): - # Wrap paths in quotes so spaces don't need to be escaped - # promp_path values are tuples with fields 'version, year, prompt_path', respectively - vs_contexts = [ - VisualStudioContext( - 17, 2022, - r'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat', - r'"C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Current\Bin\amd64\MSBuild.exe"', - '-'), - VisualStudioContext( - 16, 2019, - r'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat', - r'"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"', - '/') - ] +def _get_supported_vs_contexts(): + '''Gets a list of supported Visual Studio contexts (versions)''' + # vs 17 + vs17_path = Path('C:/Program Files/Microsoft Visual Studio/2022/Community') + vs17 = VisualStudioContext( + 17, + 2022, + vs17_path.joinpath('Common7/Tools/VsDevCmd.bat'), + vs17_path.joinpath('Msbuild/Current/Bin/amd64/MSBuild.exe'), + '-', + ) - for vs_context in vs_contexts: - if os.path.exists(f'{vs_context.prompt_path}'): + # vs 16 + vs16_path = Path( + 'C:/Program Files (x86)/Microsoft Visual Studio/2019/Community') + vs16 = VisualStudioContext( + 16, + 2019, + vs16_path.joinpath('Common7/Tools/VsDevCmd.bat'), + vs16_path.joinpath('MSBuild/Current/Bin/MSBuild.exe'), + '/', + ) + return [vs17, vs16] + + +def _get_preferred_vs_context(): + '''Gets the most preferred Visual Studio context (version) on this computer''' + vs_ctxs = _get_supported_vs_contexts() + for vs_context in vs_ctxs: + if vs_context.prompt_path.exists(): return vs_context - raise ValueError('Failed to deduce visual studio context') + supported_versions = ', '.join([str(v.version) for v in vs_ctxs]) + raise ValueError( + f'Failed to find supported Visual Studio version (works with {supported_versions})' + ) class BuildTarget: @@ -331,7 +355,7 @@ def build( if verbosity is None: verbosity = 'minimal' - vs_context = _deduce_visual_studio_context() + vs_context = _get_preferred_vs_context() try: build_dir = build_dir.absolute() @@ -348,19 +372,19 @@ def build( csharp_tgts, cpp_tgts, publish_tgts = build_ctx.parse_targets(targets) build_cmd_steps = [vs_context.prompt_cmd()] base_output_path_arg = vs_context.msbuild_prop( - 'BaseOutputPath', f'{build_dir}\\{configuration}') + 'BaseOutputPath', Path(build_dir.joinpath(configuration))) # restore build (downloads nuget package deps) if restore or clean: restore_cmd_prefix = ' '.join( [ - f"{vs_context.msbuild_path}", + f'"{vs_context.msbuild_path}"', f"{vs_context.msbuild_flag('m')}", f"{vs_context.msbuild_prop('platform', 'x64')}", f"{vs_context.msbuild_prop('configuration', configuration)}", f"{vs_context.msbuild_target('restore')}", f"{vs_context.msbuild_clp('verbosity', verbosity)}", - f"{vs_context.msbuild_clp('summary')}" + f"{vs_context.msbuild_clp('summary')}", ]) for t in cpp_tgts: @@ -374,7 +398,7 @@ def build( debugtype = 'portable' if configuration.lower() == 'debug' else 'none' build_cmd_prefix = ' '.join( [ - f"{vs_context.msbuild_path}", f"{vs_context.msbuild_flag('m')}", + f'"{vs_context.msbuild_path}"', f"{vs_context.msbuild_flag('m')}", f"{vs_context.msbuild_prop('platform', 'x64')}", f"{vs_context.msbuild_prop('GenerateFullPaths', 'true')}", f"{vs_context.msbuild_prop('configuration', configuration)}", @@ -393,10 +417,11 @@ def build( build_cmd_steps.append(f'cd {Path(t.path).parent}') build_cmd_steps.append('dotnet publish -c Release -r win-x64 --sc') build_cmd_steps.append( - f'copy {bins.joinpath("libspark.dll")} {bins.joinpath("win-x64", "publish")}') + f'copy {bins.joinpath("libspark.dll")} {bins.joinpath("win-x64", "publish")}' + ) build_cmd = ' && '.join(build_cmd_steps) - proc_results.append(subprocess.run(build_cmd)) + proc_results.append(subprocess.run(f'{build_cmd}')) if link_build_dir is not None: link_dir(build_dir, link_build_dir) return proc_results