Files
spark/tools/buildspark.py
T

200 lines
5.4 KiB
Python

import argparse
import os
import subprocess
import shutil
def _workspace_root():
tools_dir = os.path.dirname(os.path.realpath(__file__))
return os.path.abspath(f'{tools_dir}\\{os.path.pardir}')
def _add_toggle_opt(parser, name, **kwargs):
parser.add_argument(f'--{name}', action=f'store_true', dest=name, **kwargs)
parser.add_argument(f'--no-{name}', action=f'store_false', dest=name)
return parser
class SparkPaths:
@staticmethod
def root():
return _workspace_root()
@staticmethod
def spark_proj():
return fr'{_workspace_root()}\src\client\spark\Spark.csproj'
@staticmethod
def libspark_proj(build_dir):
return fr'{build_dir}\ALL_BUILD.vcxproj'
@staticmethod
def tools():
return fr'{_workspace_root()}\tools'
@staticmethod
def default_build():
return fr'{_workspace_root()}\build'
@staticmethod
def externals():
return fr'{_workspace_root()}\externals'
@staticmethod
def venv27():
return fr'{_workspace_root()}\.venv27'
@staticmethod
def venv27_prompt():
return fr'{_workspace_root()}\.venv27\Scripts\activate.bat'
@staticmethod
def msbuild():
# Wrap in quotes so spaces don't need to be escaped
return r'"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"'
@staticmethod
def vs_prompt():
# Wrap in quotes so spaces don't need to be escaped
return r'"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"'
class CWD:
def __init__(self, push_path, pop_path=os.getcwd()):
self.push_path = push_path
self.pop_path = pop_path
def __enter__(self):
if self.push_path is not None:
os.chdir(self.push_path)
def __exit__(self, exc_type, exc_val, exc_tb):
if self.pop_path is not None:
os.chdir(self.pop_path)
def _configure_libspark(build_dir):
with CWD(build_dir):
subprocess.call(
f'{SparkPaths.vs_prompt()} && cmake.exe -G "Visual Studio 16 2019" {SparkPaths.root()}'
)
shutil.copy(
f'{SparkPaths.tools()}\\cpp.build.props', 'Directory.Build.Props')
def _get_arg_parser():
parser = argparse.ArgumentParser(description='Build spark_client.sln')
parser.add_argument(
'--configuration',
metavar='debug|release',
type=str,
nargs='?',
help='The build configuration',
default='Debug')
parser.add_argument(
'-o',
'--output-dir',
metavar='path',
type=str,
nargs='?',
help='The directory in which to put build files')
_add_toggle_opt(
parser, 'restore', help='Whether or not to run msbuild /t:restore')
_add_toggle_opt(
parser,
'clean',
help=
'Whether or not to delete and overwrite an existing build directory')
parser.set_defaults(restore=True)
return parser
def _rmdir_or_unlink(dir):
if not os.path.exists(dir):
return
if os.path.islink(dir):
os.unlink(dir)
else:
shutil.rmtree(dir)
def _create_and_link_build_dir(build_dir, clean):
if not os.path.exists(build_dir):
os.makedirs(build_dir)
elif clean:
_rmdir_or_unlink(build_dir)
os.makedirs(build_dir)
def _exit(code=0, msg=None):
if msg is not None:
print(msg)
exit(code)
def _get_arg(args, name, value_or=None):
if name in args:
return args[name]
return value_or
def _msbuild_prop_arg(key, value):
return f'/property:{key}={value}'
if __name__ == "__main__":
parser = _get_arg_parser()
args = parser.parse_args()
os.environ[
'spark_build_python_path'] = fr'{SparkPaths.tools()}\..\externals\windows\bin\python'
args_dict = vars(args)
build_dir = _get_arg(args_dict, 'output_dir') or SparkPaths.default_build()
clean = args.clean
restore = args.restore
if build_dir is None:
print(args)
exit(1)
try:
build_dir = os.path.realpath(build_dir)
_create_and_link_build_dir(build_dir, clean)
_configure_libspark(build_dir)
except Exception as e:
_exit(e)
build_cmd_steps = [SparkPaths.vs_prompt(), SparkPaths.venv27_prompt()]
base_output_path_arg = _msbuild_prop_arg(
'BaseOutputPath', f'{build_dir}\\{args.configuration}')
# restore build (downloads nuget package deps)
if restore or clean:
restore_cmd_prefix = (
f'{SparkPaths.msbuild()} /m /p:debugtype=portable /p:platform=x64 '
f'/clp:verbosity=minimal /clp:summary '
f'/p:Configuration={args.configuration} /t:restore')
build_cmd_steps.append(
f'{restore_cmd_prefix} {SparkPaths.libspark_proj(build_dir)}')
build_cmd_steps.append(
f'{restore_cmd_prefix} {base_output_path_arg} {SparkPaths.spark_proj()}'
)
# main build
build_cmd_prefix = (
f'{SparkPaths.msbuild()} /m /p:debugtype=portable /p:platform=x64 '
f'/p:GenerateFullPaths=true /clp:verbosity=minimal '
f'/p:Configuration={args.configuration} /clp:summary')
build_cmd_steps.append(
f'{build_cmd_prefix} {SparkPaths.libspark_proj(build_dir)}')
build_cmd_steps.append(
f'{build_cmd_prefix} {base_output_path_arg} {SparkPaths.spark_proj()}')
build_cmd = ' && '.join(build_cmd_steps)
subprocess.run(build_cmd)