diff options
author | Massimiliano Culpo <massimiliano.culpo@gmail.com> | 2024-08-27 14:01:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-27 14:01:50 +0200 |
commit | 9a91f021a75055a07449cc2ed6ae58a67c8bcb06 (patch) | |
tree | ec21f6f45e6e86970e09975368f38fe0ff592228 /lib | |
parent | 297e43b097580b1f704564d11b64de10a3347ed4 (diff) | |
download | spack-9a91f021a75055a07449cc2ed6ae58a67c8bcb06.tar.gz spack-9a91f021a75055a07449cc2ed6ae58a67c8bcb06.tar.bz2 spack-9a91f021a75055a07449cc2ed6ae58a67c8bcb06.tar.xz spack-9a91f021a75055a07449cc2ed6ae58a67c8bcb06.zip |
Move `spack.compilers._to_dict` to `Compiler` (#46051)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/compiler.py | 27 | ||||
-rw-r--r-- | lib/spack/spack/compilers/__init__.py | 38 |
2 files changed, 35 insertions, 30 deletions
diff --git a/lib/spack/spack/compiler.py b/lib/spack/spack/compiler.py index 341b77db05..f24ccf679f 100644 --- a/lib/spack/spack/compiler.py +++ b/lib/spack/spack/compiler.py @@ -29,6 +29,9 @@ from spack.util.environment import filter_system_paths __all__ = ["Compiler"] +PATH_INSTANCE_VARS = ["cc", "cxx", "f77", "fc"] +FLAG_INSTANCE_VARS = ["cflags", "cppflags", "cxxflags", "fflags"] + @llnl.util.lang.memoized def _get_compiler_version_output(compiler_path, version_arg, ignore_errors=()): @@ -700,6 +703,30 @@ class Compiler: os.environ.clear() os.environ.update(backup_env) + def to_dict(self): + flags_dict = {fname: " ".join(fvals) for fname, fvals in self.flags.items()} + flags_dict.update( + {attr: getattr(self, attr, None) for attr in FLAG_INSTANCE_VARS if hasattr(self, attr)} + ) + result = { + "spec": str(self.spec), + "paths": {attr: getattr(self, attr, None) for attr in PATH_INSTANCE_VARS}, + "flags": flags_dict, + "operating_system": str(self.operating_system), + "target": str(self.target), + "modules": self.modules or [], + "environment": self.environment or {}, + "extra_rpaths": self.extra_rpaths or [], + } + + if self.enable_implicit_rpaths is not None: + result["implicit_rpaths"] = self.enable_implicit_rpaths + + if self.alias: + result["alias"] = self.alias + + return result + class CompilerAccessError(spack.error.SpackError): def __init__(self, compiler, paths): diff --git a/lib/spack/spack/compilers/__init__.py b/lib/spack/spack/compilers/__init__.py index a03de45d7d..d15ce87b2d 100644 --- a/lib/spack/spack/compilers/__init__.py +++ b/lib/spack/spack/compilers/__init__.py @@ -31,8 +31,6 @@ from spack.operating_systems import windows_os from spack.util.environment import get_path from spack.util.naming import mod_to_class -_path_instance_vars = ["cc", "cxx", "f77", "fc"] -_flags_instance_vars = ["cflags", "cppflags", "cxxflags", "fflags"] _other_instance_vars = [ "modules", "operating_system", @@ -90,29 +88,7 @@ def _auto_compiler_spec(function): def _to_dict(compiler): """Return a dict version of compiler suitable to insert in YAML.""" - d = {} - d["spec"] = str(compiler.spec) - d["paths"] = dict((attr, getattr(compiler, attr, None)) for attr in _path_instance_vars) - d["flags"] = dict((fname, " ".join(fvals)) for fname, fvals in compiler.flags.items()) - d["flags"].update( - dict( - (attr, getattr(compiler, attr, None)) - for attr in _flags_instance_vars - if hasattr(compiler, attr) - ) - ) - d["operating_system"] = str(compiler.operating_system) - d["target"] = str(compiler.target) - d["modules"] = compiler.modules or [] - d["environment"] = compiler.environment or {} - d["extra_rpaths"] = compiler.extra_rpaths or [] - if compiler.enable_implicit_rpaths is not None: - d["implicit_rpaths"] = compiler.enable_implicit_rpaths - - if compiler.alias: - d["alias"] = compiler.alias - - return {"compiler": d} + return {"compiler": compiler.to_dict()} def get_compiler_config( @@ -488,13 +464,15 @@ def compiler_from_dict(items): os = items.get("operating_system", None) target = items.get("target", None) - if not ("paths" in items and all(n in items["paths"] for n in _path_instance_vars)): + if not ( + "paths" in items and all(n in items["paths"] for n in spack.compiler.PATH_INSTANCE_VARS) + ): raise InvalidCompilerConfigurationError(cspec) cls = class_for_compiler_name(cspec.name) compiler_paths = [] - for c in _path_instance_vars: + for c in spack.compiler.PATH_INSTANCE_VARS: compiler_path = items["paths"][c] if compiler_path != "None": compiler_paths.append(compiler_path) @@ -904,9 +882,9 @@ class CompilerConfigFactory: class InvalidCompilerConfigurationError(spack.error.SpackError): def __init__(self, compiler_spec): super().__init__( - 'Invalid configuration for [compiler "%s"]: ' % compiler_spec, - "Compiler configuration must contain entries for all compilers: %s" - % _path_instance_vars, + f'Invalid configuration for [compiler "{compiler_spec}"]: ', + f"Compiler configuration must contain entries for " + f"all compilers: {spack.compiler.PATH_INSTANCE_VARS}", ) |