Update to new recipe system (S++ 2.0).
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
|
||||
import os
|
||||
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')
|
||||
|
||||
@@ -11,11 +14,45 @@ def cmd_quote(s: str) -> str:
|
||||
escaped = s.replace('\\', '\\\\')
|
||||
return f'"{escaped}"'
|
||||
|
||||
def _cmake_project(env: Environment, project_root: str, generate_args: 'list[str]' = [], build_args : 'list[str]' = [], install_args : 'list[str]' = []) -> dict:
|
||||
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}')
|
||||
is_built = os.path.exists(os.path.join(install_dir, _BUILT_STAMPFILE))
|
||||
|
||||
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)
|
||||
@@ -30,10 +67,15 @@ def _cmake_project(env: Environment, project_root: str, generate_args: 'list[str
|
||||
# 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', *generate_args, project_root])
|
||||
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)])
|
||||
pathlib.Path(install_dir, _BUILT_STAMPFILE).touch()
|
||||
|
||||
with pathlib.Path(install_dir, _BUILT_STAMPFILE).open('w') as f:
|
||||
f.write(version_hash)
|
||||
|
||||
libpath = []
|
||||
for lib_folder in ('lib', 'lib64'):
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
from git import Repo
|
||||
from git.exc import GitError
|
||||
import hashlib
|
||||
import os
|
||||
import re
|
||||
from SCons.Script import *
|
||||
|
||||
Import('env')
|
||||
@@ -18,7 +18,7 @@ def _clone(env: Environment, repo_name: str, remote_url: str):
|
||||
origin = repo.create_remote('origin', remote_url)
|
||||
return repo, origin
|
||||
|
||||
def _gitbranch(env: Environment, repo_name: str, remote_url: str, git_ref: str = 'main') -> dict:
|
||||
def _git_branch(env: Environment, repo_name: str, remote_url: str, git_ref: str = 'main') -> dict:
|
||||
repo, origin = _clone(env, repo_name, remote_url)
|
||||
worktree_dir = os.path.join(env['CLONE_DIR'], 'git', repo_name, hashlib.shake_128(git_ref.encode('utf-8')).hexdigest(6)) # TODO: commit hash would be better, right? -> not if it's a branch!
|
||||
if not os.path.exists(worktree_dir):
|
||||
@@ -38,12 +38,58 @@ def _gitbranch(env: Environment, repo_name: str, remote_url: str, git_ref: str =
|
||||
'checkout_root': worktree_dir
|
||||
}
|
||||
|
||||
def _gittags(env: Environment, repo_name: str, remote_url: str, force_fetch: bool = False) -> 'list[str]':
|
||||
def _git_tags(env: Environment, repo_name: str, remote_url: str, force_fetch: bool = False) -> 'list[str]':
|
||||
repo, origin = _clone(env, repo_name, remote_url)
|
||||
if force_fetch or env['UPDATE_REPOSITORIES']:
|
||||
origin.fetch(tags=True)
|
||||
return [t.name for t in repo.tags]
|
||||
|
||||
env.AddMethod(_gitbranch, 'GitBranch')
|
||||
env.AddMethod(_gittags, 'GitTags')
|
||||
def _make_callable(val):
|
||||
if callable(val):
|
||||
return val
|
||||
else:
|
||||
return lambda env: val
|
||||
|
||||
def _git_recipe(env: Environment, globals: dict, repo_name, repo_url, cook_fn, versions = None, tag_pattern = None, tag_fn = None, ref_fn = None, dependencies: dict = {}) -> None:
|
||||
_repo_name = _make_callable(repo_name)
|
||||
_repo_url = _make_callable(repo_url)
|
||||
_tag_pattern = _make_callable(tag_pattern)
|
||||
versions_cb = versions and _make_callable(versions)
|
||||
|
||||
def _versions(env: Environment, update: bool = False):
|
||||
pattern = _tag_pattern(env)
|
||||
if pattern:
|
||||
tags = env.GitTags(repo_name = _repo_name(env), remote_url = _repo_url(env), force_fetch=update)
|
||||
result = []
|
||||
for tag in tags:
|
||||
match = pattern.match(tag)
|
||||
if match:
|
||||
result.append(tuple(int(part) for part in match.groups() if part is not None))
|
||||
if len(result) == 0 and not update:
|
||||
return _versions(env, update=True)
|
||||
return result
|
||||
elif versions_cb:
|
||||
return versions_cb(env)
|
||||
else:
|
||||
return [(0, 0, 0)]
|
||||
|
||||
def _dependencies(env: Environment, version) -> 'dict':
|
||||
return dependencies
|
||||
|
||||
def _cook(env: Environment, version) -> dict:
|
||||
if tag_fn:
|
||||
git_ref = f'refs/tags/{tag_fn(version)}'
|
||||
else:
|
||||
assert ref_fn
|
||||
git_ref = ref_fn(env, version)
|
||||
repo = env.GitBranch(repo_name = _repo_name(env), remote_url = _repo_url(env), git_ref = git_ref)
|
||||
return cook_fn(env, repo)
|
||||
|
||||
globals['versions'] = _versions
|
||||
globals['dependencies'] = _dependencies
|
||||
globals['cook'] = _cook
|
||||
|
||||
env.AddMethod(_git_branch, 'GitBranch')
|
||||
env.AddMethod(_git_tags, 'GitTags')
|
||||
env.AddMethod(_git_recipe, 'GitRecipe')
|
||||
Return('env')
|
||||
Reference in New Issue
Block a user