93 lines
3.4 KiB
Python
93 lines
3.4 KiB
Python
|
|
import json
|
|
import pathlib
|
|
import shutil
|
|
|
|
from SCons.Script import *
|
|
|
|
_BUILT_STAMPFILE = '.spp_built'
|
|
_VERSION = 0 # bump if you change how the projects are build to trigger a clean build
|
|
|
|
Import('env')
|
|
|
|
def cmd_quote(s: str) -> str:
|
|
escaped = s.replace('\\', '\\\\')
|
|
return f'"{escaped}"'
|
|
|
|
def _generate_cmake_c_flags(dependencies: 'list[dict]') -> str:
|
|
parts = []
|
|
for dependency in dependencies:
|
|
for path in dependency.get('CPPPATH', []):
|
|
parts.append(cmd_quote(f'-I{path}'))
|
|
return ' '.join(parts)
|
|
|
|
def _generate_cmake_cxx_flags(dependencies: 'list[dict]') -> str:
|
|
parts = []
|
|
for dependency in dependencies:
|
|
for path in dependency.get('CPPPATH', []):
|
|
parts.append(cmd_quote(f'-I{path}'))
|
|
return ' '.join(parts)
|
|
|
|
def _calc_version_hash(dependencies: 'list[dict]') -> str:
|
|
return json.dumps({
|
|
'version': _VERSION,
|
|
'dependencies': dependencies
|
|
})
|
|
|
|
def _cmake_project(env: Environment, project_root: str, generate_args: 'list[str]' = [], build_args : 'list[str]' = [], install_args : 'list[str]' = [], dependencies: 'list[dict]' = []) -> dict:
|
|
config = env['BUILD_TYPE']
|
|
build_dir = os.path.join(project_root, f'build_{config}')
|
|
install_dir = os.path.join(project_root, f'install_{config}')
|
|
|
|
version_hash = _calc_version_hash(dependencies)
|
|
stamp_file = pathlib.Path(install_dir, _BUILT_STAMPFILE)
|
|
is_built = stamp_file.exists()
|
|
|
|
if is_built:
|
|
with stamp_file.open('r') as f:
|
|
build_version = f.read()
|
|
if build_version != version_hash:
|
|
print(f'Rebuilding CMake project at {project_root} as the script version changed.')
|
|
is_built = False
|
|
if not is_built:
|
|
shutil.rmtree(build_dir)
|
|
shutil.rmtree(install_dir)
|
|
|
|
if not is_built or env['UPDATE_REPOSITORIES']:
|
|
print(f'Building {project_root}, config {config}')
|
|
os.makedirs(build_dir, exist_ok=True)
|
|
build_type = {
|
|
'debug': 'Debug',
|
|
'release_debug': 'RelWithDebInfo',
|
|
'release': 'Release',
|
|
'profile': 'RelWithDebInfo'
|
|
}.get(env['BUILD_TYPE'], 'RelWithDebInfo')
|
|
def run_cmd(args):
|
|
env.Execute(' '.join([str(s) for s in args]))
|
|
# TODO: is this a problem?
|
|
# environ = os.environ.copy()
|
|
# environ['CXXFLAGS'] = ' '.join(f'-D{define}' for define in env['CPPDEFINES']) # TODO: who cares about windows?
|
|
run_cmd(['cmake', '-G', 'Ninja', '-B', build_dir, f'-DCMAKE_BUILD_TYPE={build_type}',
|
|
f'-DCMAKE_INSTALL_PREFIX={cmd_quote(install_dir)}', '-DBUILD_TESTING=OFF',
|
|
f'-DCMAKE_C_FLAGS={_generate_cmake_c_flags(dependencies)}',
|
|
f'-DCMAKE_CXX_FLAGS={_generate_cmake_cxx_flags(dependencies)}', *generate_args, project_root])
|
|
run_cmd(['cmake', '--build', *build_args, cmd_quote(build_dir)])
|
|
run_cmd(['cmake', '--install', *install_args, cmd_quote(build_dir)])
|
|
|
|
with pathlib.Path(install_dir, _BUILT_STAMPFILE).open('w') as f:
|
|
f.write(version_hash)
|
|
|
|
libpath = []
|
|
for lib_folder in ('lib', 'lib64'):
|
|
full_path = os.path.join(install_dir, lib_folder)
|
|
if os.path.exists(full_path):
|
|
libpath.append(full_path)
|
|
|
|
return {
|
|
'install_dir': install_dir,
|
|
'LIBPATH': libpath,
|
|
'CPPPATH': [os.path.join(install_dir, 'include')]
|
|
}
|
|
|
|
env.AddMethod(_cmake_project, 'CMakeProject')
|
|
Return('env') |