251 lines
6.9 KiB
Python
251 lines
6.9 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 client_csproj(name):
|
|
return fr'{_workspace_root()}\src\client\{name}\{name}.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 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 build')
|
|
|
|
parser.add_argument(
|
|
'-l',
|
|
'--link-output-dir',
|
|
metavar='link_path',
|
|
type=str,
|
|
nargs='?',
|
|
help='The symbolink link to --output-dir')
|
|
|
|
parser.add_argument(
|
|
'-t',
|
|
'--target',
|
|
metavar='targets',
|
|
type=str,
|
|
action='append',
|
|
nargs='+',
|
|
help='The target(s) to build')
|
|
|
|
_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 _unlink(path):
|
|
if not os.path.exists(path):
|
|
return
|
|
if not os.path.islink(path):
|
|
raise ValueError('The specified path already exists and is not a symbolic link')
|
|
os.unlink(path)
|
|
|
|
|
|
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 _link_dir(source_path, link_path):
|
|
if source_path is None:
|
|
raise TypeError('source_path is None')
|
|
if not os.path.isdir(source_path):
|
|
raise ValueError('source_path is not a directory')
|
|
if link_path is None:
|
|
raise TypeError('link_path is None')
|
|
if source_path == link_path:
|
|
return
|
|
|
|
if not os.path.exists(source_path):
|
|
os.makedirs(source_path)
|
|
if os.path.exists(link_path):
|
|
if os.path.islink(link_path):
|
|
if os.readlink(link_path) == source_path:
|
|
return
|
|
os.unlink(link_path)
|
|
elif os.path.isdir(link_path):
|
|
shutil.rmtree(link_path)
|
|
elif os.path.isfile(link_path):
|
|
os.remove(link_path)
|
|
else:
|
|
raise ValueError('link_path is an unexpected filetype')
|
|
os.symlink(source_path, link_path)
|
|
|
|
|
|
def _create_build_dir(build_dir, clean):
|
|
if build_dir is None:
|
|
raise TypeError('build_dir is None')
|
|
|
|
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()
|
|
|
|
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')
|
|
targets = _get_arg(args_dict, 'target') or [['spark']]
|
|
targets = [SparkPaths.client_csproj(t) for grp in targets for t in grp]
|
|
clean = args.clean
|
|
restore = args.restore
|
|
if build_dir is None:
|
|
print(args)
|
|
exit(1)
|
|
|
|
try:
|
|
build_dir = os.path.realpath(build_dir)
|
|
if link_build_dir is not None:
|
|
_unlink(link_build_dir)
|
|
_create_build_dir(build_dir, clean)
|
|
_configure_libspark(build_dir)
|
|
except Exception as e:
|
|
_exit(1, e)
|
|
|
|
build_cmd_steps = [SparkPaths.vs_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)}')
|
|
for t in targets:
|
|
build_cmd_steps.append(
|
|
f'{restore_cmd_prefix} {base_output_path_arg} {t}')
|
|
|
|
# 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)}')
|
|
for t in targets:
|
|
build_cmd_steps.append(
|
|
f'{build_cmd_prefix} {base_output_path_arg} {t}')
|
|
|
|
build_cmd = ' && '.join(build_cmd_steps)
|
|
subprocess.run(build_cmd)
|
|
if link_build_dir is not None:
|
|
_link_dir(build_dir, link_build_dir)
|