Files
spark/tools/buildspark.py
T
John Colvin 939f44b48c build: Refine build scripts so they work out of the box
* Automatically create python virtual envs
* Automatically actvate the vs dev prompt

Note: the python39 virtual env will remain active in the calling shell.
This behavior may be changed down the road.
2021-09-15 23:53:57 -04:00

186 lines
4.9 KiB
Python

import argparse
import os
import pygit2
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}')
class SparkPaths:
@staticmethod
def root():
return _workspace_root()
@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():
return r'"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"'
@staticmethod
def vs_prompt():
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" ..'
)
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')
parser.add_argument(
'--cache-branch',
help='Whether or not to build in a directory named ' +
"'build.<branch_name>' (which is then symlinked to the normal " +
"'build' directory)",
action='store_true',
required=False)
parser.add_argument(
'--clean',
help=
'Whether or not to delete and overwrite an existing build directory directory',
action='store_true',
required=False)
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, dbuild_dir, clean):
if not os.path.exists(build_dir):
os.makedirs(build_dir)
elif clean:
shutil.rmtree(build_dir)
os.makedirs(build_dir)
if build_dir != dbuild_dir:
if clean:
_rmdir_or_unlink(dbuild_dir)
elif os.path.islink(dbuild_dir):
os.unlink(dbuild_dir)
os.symlink(build_dir, dbuild_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
if __name__ == "__main__":
parser = _get_arg_parser()
args = parser.parse_args()
os.environ[
'spark_build_python_path'] = fr'{SparkPaths.tools()}\..\externals\windows\bin\python'
repo = pygit2.Repository(SparkPaths.root())
args_dict = vars(args)
build_dir = _get_arg(args_dict, 'output_dir')
if build_dir is None:
if _get_arg(args_dict, 'cache_branch'):
branch_name = os.path.basename(repo.head.name)
build_dir = f'build.{branch_name}'
else:
build_dir = SparkPaths.default_build()
clean = args.clean
if build_dir is None:
print(args)
exit(1)
try:
build_dir = os.path.realpath(build_dir)
dbuild_dir = SparkPaths.default_build()
_create_and_link_build_dir(build_dir, dbuild_dir, clean)
_configure_libspark(build_dir)
except Exception as e:
_exit(e)
msbuild_restore_cmd = (
f'{SparkPaths.msbuild()} /m /p:debugtype=portable /p:platform=x64 '
f'/clp:verbosity=minimal /clp:summary '
f'/p:Configuration={args.configuration} /t:restore spark_client.sln')
msbuild_cmd = (
f'{SparkPaths.msbuild()} /m /p:debugtype=portable /p:platform=x64 '
f'/p:GenerateFullPaths=true /clp:verbosity=minimal '
f'/p:Configuration={args.configuration} /clp:summary spark_client.sln')
spark_build_cmd = ' && '.join(
[
SparkPaths.vs_prompt(), SparkPaths.venv27_prompt(), msbuild_restore_cmd,
msbuild_cmd
])
subprocess.run(spark_build_cmd)