Replaced --dump_env with --dump and --dump_format that allow dumping environment and config, both as text or json. And added _info() and env.Info() function for printing that reacts to SCons' -s flag.

This commit is contained in:
Patrick 2025-06-19 13:33:21 +02:00
parent 88844ee5da
commit c3b5244eac

View File

@ -244,6 +244,11 @@ def _find_lib(env: Environment, name: str, paths: 'list[str]', type : str = 'sta
return None return None
raise Exception(f'Could not find library with name {name} in paths: "{", ".join(paths)}" filename: "{fname}".') raise Exception(f'Could not find library with name {name} in paths: "{", ".join(paths)}" filename: "{fname}".')
def _info(env: Environment, message: str) -> None:
if not GetOption('silent'):
print(message)
def _error(env: Environment, message: str): def _error(env: Environment, message: str):
print(message, file=sys.stderr) print(message, file=sys.stderr)
Exit(1) Exit(1)
@ -727,11 +732,30 @@ AddOption(
) )
AddOption( AddOption(
'--dump_env', '--disable_auto_update',
dest = 'dump_env', dest = 'disable_auto_update',
action = 'store_true' action = 'store_true'
) )
AddOption(
'--dump',
dest = 'dump',
type = 'choice',
choices = ('env', 'config'),
nargs = 1,
action = 'store'
)
AddOption(
'--dump_format',
dest = 'dump_format',
type = 'choice',
choices = ('text', 'json'),
nargs = 1,
action = 'store',
default = 'text'
)
AddOption( AddOption(
'--generate_project', '--generate_project',
dest = 'generate_project', dest = 'generate_project',
@ -750,7 +774,9 @@ enable_asan = GetOption('enable_asan')
config_file = GetOption('config_file') config_file = GetOption('config_file')
compiler = GetOption('compiler') compiler = GetOption('compiler')
update_repositories = GetOption('update_repositories') update_repositories = GetOption('update_repositories')
dump_env = GetOption('dump_env') disable_auto_update = GetOption('disable_auto_update')
dump = GetOption('dump')
dump_format = GetOption('dump_format')
generate_project = GetOption('generate_project') generate_project = GetOption('generate_project')
default_CC = { default_CC = {
@ -796,12 +822,12 @@ env['DEPS_CFLAGS'] = []
env['DEPS_CXXFLAGS'] = [] env['DEPS_CXXFLAGS'] = []
env['DEPS_LINKFLAGS'] = [] env['DEPS_LINKFLAGS'] = []
print(f'Detected system cache directory: {env["SYSTEM_CACHE_DIR"]}') _info(None, f'Detected system cache directory: {env["SYSTEM_CACHE_DIR"]}')
try: try:
os.makedirs(env['SYSTEM_CACHE_DIR'], exist_ok=True) os.makedirs(env['SYSTEM_CACHE_DIR'], exist_ok=True)
except: except:
env['SYSTEM_CACHE_DIR'] = os.path.join(_get_fallback_cache_dir(), 'spp_cache') env['SYSTEM_CACHE_DIR'] = os.path.join(_get_fallback_cache_dir(), 'spp_cache')
print(f'Creating spp cache dir failed, using fallback: {env["SYSTEM_CACHE_DIR"]}.') _info(None, f'Creating spp cache dir failed, using fallback: {env["SYSTEM_CACHE_DIR"]}.')
os.makedirs(env['SYSTEM_CACHE_DIR'], exist_ok=True) # no more safeguards! os.makedirs(env['SYSTEM_CACHE_DIR'], exist_ok=True) # no more safeguards!
env['CLONE_DIR'] = os.path.join(env['SYSTEM_CACHE_DIR'], 'cloned') env['CLONE_DIR'] = os.path.join(env['SYSTEM_CACHE_DIR'], 'cloned')
env['DOWNLOAD_DIR'] = os.path.join(env['SYSTEM_CACHE_DIR'], 'downloaded') env['DOWNLOAD_DIR'] = os.path.join(env['SYSTEM_CACHE_DIR'], 'downloaded')
@ -878,8 +904,8 @@ if os.path.exists(update_stamp_file):
except: except:
pass pass
boot_time = psutil.boot_time() boot_time = psutil.boot_time()
if boot_time > update_time: if not disable_auto_update and boot_time > update_time:
print('Didn\'t update repositories since last boot, doing it now...') _info(None, 'Didn\'t update repositories since last boot, doing it now...')
env['UPDATE_REPOSITORIES'] = True env['UPDATE_REPOSITORIES'] = True
if env['UPDATE_REPOSITORIES']: if env['UPDATE_REPOSITORIES']:
with open(update_stamp_file, 'w') as f: with open(update_stamp_file, 'w') as f:
@ -1008,6 +1034,7 @@ env.AddMethod(_make_interface, 'MakeInterface')
env.AddMethod(_lib_filename, 'LibFilename') env.AddMethod(_lib_filename, 'LibFilename')
env.AddMethod(_find_executable, 'FindExecutable') env.AddMethod(_find_executable, 'FindExecutable')
env.AddMethod(_find_lib, 'FindLib') env.AddMethod(_find_lib, 'FindLib')
env.AddMethod(_info, 'Info')
env.AddMethod(_error, 'Error') env.AddMethod(_error, 'Error')
env.AddMethod(_wrap_builder(env.Library, TargetType.STATIC_LIBRARY), 'Library') env.AddMethod(_wrap_builder(env.Library, TargetType.STATIC_LIBRARY), 'Library')
env.AddMethod(_wrap_builder(env.StaticLibrary, TargetType.STATIC_LIBRARY), 'StaticLibrary') env.AddMethod(_wrap_builder(env.StaticLibrary, TargetType.STATIC_LIBRARY), 'StaticLibrary')
@ -1025,15 +1052,33 @@ env.AddMethod(_finalize, 'Finalize')
env.AddMethod(_find_target, 'FindTarget') env.AddMethod(_find_target, 'FindTarget')
if hasattr(env, 'Gch'): if hasattr(env, 'Gch'):
env.AddMethod(_wrap_builder(env.Gch), 'Gch') env.AddMethod(_wrap_builder(env.Gch, TargetType.STATIC_LIBRARY), 'Gch')
for addon_file in env.Glob('addons/*.py'): for addon_file in env.Glob('addons/*.py'):
env = SConscript(addon_file, exports = 'env') env = SConscript(addon_file, exports = 'env')
if dump_env: if dump is not None:
print('==== Begin Environment Dump =====') def _dump_as_text(data: Any) -> str:
print(env.Dump()) from pprint import pformat
print('==== End Environment Dump =====') dump_name = {
'env': 'Environment',
'config': 'Configuration'
}[dump]
return '\n'.join((
f'==== Begin {dump_name} Dump ====',
pformat(data),
f'==== End {dump_name} Dump ===='
))
data = {
'env': env.Dictionary,
'config': lambda: config
}[dump]()
dump_fn = {
'text': _dump_as_text,
'json': json.dumps
}[dump_format]
print(dump_fn(data))
Exit(0) Exit(0)
Return('env') Return('env')