builtin: reduce boilerplate when accessing package attributes

This commit is contained in:
Massimiliano Culpo 2025-01-11 14:31:37 +01:00
parent 337ab120b3
commit fc683b87f1
No known key found for this signature in database
GPG Key ID: 3E52BB992233066C
24 changed files with 44 additions and 48 deletions

View File

@ -100,7 +100,7 @@ def cmake_py_shared(self):
def cmake_args(self): def cmake_args(self):
try: try:
self.spec["cxx"].package.standard_flag(language="cxx", standard="14") self["cxx"].standard_flag(language="cxx", standard="14")
except UnsupportedCompilerFlag: except UnsupportedCompilerFlag:
InstallError("clingo requires a C++14-compliant C++ compiler") InstallError("clingo requires a C++14-compliant C++ compiler")

View File

@ -76,14 +76,14 @@ def setup_run_environment(self, env):
return return
if self.spec.dependencies(virtuals=("c",)): if self.spec.dependencies(virtuals=("c",)):
env.set("MPICC", self.spec["c"].package.cc) env.set("MPICC", self["c"].cc)
if self.spec.dependencies(virtuals=("cxx",)): if self.spec.dependencies(virtuals=("cxx",)):
env.set("MPICXX", self.spec["cxx"].package.cxx) env.set("MPICXX", self["cxx"].cxx)
if self.spec.dependencies(virtuals=("fortran",)): if self.spec.dependencies(virtuals=("fortran",)):
env.set("MPIFC", self.spec["fortran"].package.fc) env.set("MPIFC", self["fortran"].fc)
env.set("MPIF77", self.spec["fortran"].package.fc) env.set("MPIF77", self["fortran"].fc)
def setup_dependent_package(self, module, dependent_spec): def setup_dependent_package(self, module, dependent_spec):
spec = self.spec spec = self.spec

View File

@ -30,14 +30,14 @@ class CrayMvapich2(MpichEnvironmentModifications, Package):
def setup_run_environment(self, env): def setup_run_environment(self, env):
if self.spec.dependencies(virtuals=("c",)): if self.spec.dependencies(virtuals=("c",)):
env.set("MPICC", self.spec["c"].package.cc) env.set("MPICC", self["c"].cc)
if self.spec.dependencies(virtuals=("cxx",)): if self.spec.dependencies(virtuals=("cxx",)):
env.set("MPICXX", self.spec["cxx"].package.cxx) env.set("MPICXX", self["cxx"].cxx)
if self.spec.dependencies(virtuals=("fortran",)): if self.spec.dependencies(virtuals=("fortran",)):
env.set("MPIFC", self.spec["fortran"].package.fc) env.set("MPIFC", self["fortran"].fc)
env.set("MPIF77", self.spec["fortran"].package.fc) env.set("MPIF77", self["fortran"].fc)
def install(self, spec, prefix): def install(self, spec, prefix):
raise InstallError( raise InstallError(

View File

@ -78,7 +78,7 @@ def install(self, spec, prefix):
def _get_libraries_macho(self): def _get_libraries_macho(self):
"""Same as _get_libraries_elf but for Mach-O binaries""" """Same as _get_libraries_elf but for Mach-O binaries"""
cc = Executable(self.spec["gcc"].package.cc) cc = Executable(self["gcc"].cc)
path_and_install_name = [] path_and_install_name = []
for name in self.LIBRARIES: for name in self.LIBRARIES:

View File

@ -104,8 +104,8 @@ def setup_build_environment(self, env):
env.set("GOROOT_FINAL", self.spec.prefix.go) env.set("GOROOT_FINAL", self.spec.prefix.go)
# We need to set CC/CXX_FOR_TARGET, otherwise cgo will use the # We need to set CC/CXX_FOR_TARGET, otherwise cgo will use the
# internal Spack wrappers and fail. # internal Spack wrappers and fail.
env.set("CC_FOR_TARGET", self.spec["c"].package.cc) env.set("CC_FOR_TARGET", self["c"].cc)
env.set("CXX_FOR_TARGET", self.spec["cxx"].package.cxx) env.set("CXX_FOR_TARGET", self["cxx"].cxx)
env.set("GOMAXPROCS", make_jobs) env.set("GOMAXPROCS", make_jobs)
def build(self, spec, prefix): def build(self, spec, prefix):

View File

@ -70,5 +70,5 @@ def url_for_version(self, version):
return url.format(version) return url.format(version)
def configure_args(self): def configure_args(self):
args = ["--with-xml-catalog={0}".format(self.spec["docbook-xml"].package.catalog)] args = ["--with-xml-catalog={0}".format(self["docbook-xml"].catalog)]
return args return args

View File

@ -88,7 +88,7 @@ def setup_build_environment(self, env):
# devenv is needed to convert ancient MSbuild project to modern # devenv is needed to convert ancient MSbuild project to modern
# msbuild project so MSBuild versions older than 2010 can build this # msbuild project so MSBuild versions older than 2010 can build this
# project # project
devenv_path = os.path.join(self.spec["msvc"].package.vs_root, "Common7", "IDE") devenv_path = os.path.join(self["msvc"].vs_root, "Common7", "IDE")
env.prepend_path("PATH", devenv_path) env.prepend_path("PATH", devenv_path)
@property @property

View File

@ -110,7 +110,7 @@ def url_for_version(self, version):
def flag_handler(self, name, flags): def flag_handler(self, name, flags):
if name == "cflags" and self.spec.satisfies("+pic"): if name == "cflags" and self.spec.satisfies("+pic"):
flags.append(self.spec["c"].package.pic_flag) flags.append(self["c"].pic_flag)
flags.append("-DPIC") flags.append("-DPIC")
return (flags, None, None) return (flags, None, None)

View File

@ -118,8 +118,7 @@ def cmake_python_hints(self):
"""Include the python include path to the """Include the python include path to the
CMake based on current spec CMake based on current spec
""" """
python = self.spec["python"] return [self.define("Python_INCLUDE_DIR", self["python"].config_vars["include"])]
return [self.define("Python_INCLUDE_DIR", python.package.config_vars["include"])]
@classmethod @classmethod
def determine_version(cls, lib): def determine_version(cls, lib):

View File

@ -106,9 +106,9 @@ def setup_build_environment(self, env):
def flag_handler(self, name, flags): def flag_handler(self, name, flags):
if name == "cflags": if name == "cflags":
flags.append(self.spec["c"].package.pic_flag) flags.append(self["c"].pic_flag)
elif name == "cxxflags": elif name == "cxxflags":
flags.append(self.spec["cxx"].package.pic_flag) flags.append(self["cxx"].pic_flag)
# ncurses@:6.0 fails in definition of macro 'mouse_trafo' without -P # ncurses@:6.0 fails in definition of macro 'mouse_trafo' without -P
if self.spec.satisfies("@:6.0 %gcc@5.0:"): if self.spec.satisfies("@:6.0 %gcc@5.0:"):
@ -118,7 +118,7 @@ def flag_handler(self, name, flags):
# ncurses@:6.0 uses dynamic exception specifications not allowed in c++17 # ncurses@:6.0 uses dynamic exception specifications not allowed in c++17
if self.spec.satisfies("@:5"): if self.spec.satisfies("@:5"):
if name == "cxxflags": if name == "cxxflags":
flags.append(self.spec["cxx"].package.standard_flag(language="cxx", standard="14")) flags.append(self["cxx"].standard_flag(language="cxx", standard="14"))
return (flags, None, None) return (flags, None, None)

View File

@ -521,11 +521,11 @@ def install(self, spec, prefix):
makelocalrc_args = [ makelocalrc_args = [
"-gcc", "-gcc",
spec["gcc"].package.cc, self["gcc"].cc,
"-gpp", "-gpp",
spec["gcc"].package.cxx, self["gcc"].cxx,
"-g77", "-g77",
spec["gcc"].package.fortran, self["gcc"].fortran,
"-x", "-x",
compilers_bin, compilers_bin,
] ]

View File

@ -156,7 +156,7 @@ def install(self, spec, prefix):
if spec.satisfies("@1.0"): if spec.satisfies("@1.0"):
options.append("no-krb5") options.append("no-krb5")
# clang does not support the .arch directive in assembly files. # clang does not support the .arch directive in assembly files.
if "clang" in self.spec["c"].package.cc and spec.target.family == "aarch64": if "clang" in self["c"].cc and spec.target.family == "aarch64":
options.append("no-asm") options.append("no-asm")
elif "%nvhpc" in spec: elif "%nvhpc" in spec:
# Last tested on nvidia@22.3 for x86_64: # Last tested on nvidia@22.3 for x86_64:

View File

@ -49,7 +49,7 @@ def edit(self, spec, prefix):
vars = [ vars = [
("VERSUFF", "-%s" % gcc_version), ("VERSUFF", "-%s" % gcc_version),
("CXX", compiler.cxx), ("CXX", compiler.cxx),
("GCJ", spec["eclipse-gcj-parser"].package.gcj), ("GCJ", self["eclipse-gcj-parser"].gcj),
("GCJH", join_path(gcc_base, "bin", "gcjh")), ("GCJH", join_path(gcc_base, "bin", "gcjh")),
("GJAR", join_path(gcc_base, "bin", "gjar")), ("GJAR", join_path(gcc_base, "bin", "gjar")),
("LIBGCJ", join_path(gcc_base, "share", "java", "libgcj-{0}.jar".format(gcc_version))), ("LIBGCJ", join_path(gcc_base, "share", "java", "libgcj-{0}.jar".format(gcc_version))),

View File

@ -67,9 +67,9 @@ def build_pl_args(self):
def setup_build_environment(self, env): def setup_build_environment(self, env):
# These variables are exected by by the Build.PL file # These variables are exected by by the Build.PL file
# even though we override the results via PERL_MB_OPT # even though we override the results via PERL_MB_OPT
kent = self.spec["kentutils"] kent_pkg = self["kentutils"]
env.set("KENT_SRC", kent.prefix) env.set("KENT_SRC", kent_pkg.prefix)
env.set("MACHTYPE", kent.package.machtype) env.set("MACHTYPE", kent_pkg.machtype)
# Overriding this explicitly as an environmental variable # Overriding this explicitly as an environmental variable
# as the Build.PL script doesn't honnor the command line # as the Build.PL script doesn't honnor the command line

View File

@ -337,7 +337,7 @@ def do_stage(self, mirror_only=False):
def nmake_arguments(self): def nmake_arguments(self):
args = [] args = []
if self.spec.satisfies("%msvc"): if self.spec.satisfies("%msvc"):
args.append("CCTYPE=%s" % self.spec["msvc"].package.short_msvc_version) args.append("CCTYPE=%s" % self["msvc"].short_msvc_version)
else: else:
raise RuntimeError("Perl unsupported for non MSVC compilers on Windows") raise RuntimeError("Perl unsupported for non MSVC compilers on Windows")
args.append("INST_TOP=%s" % windows_sfn(self.prefix.replace("/", "\\"))) args.append("INST_TOP=%s" % windows_sfn(self.prefix.replace("/", "\\")))
@ -384,7 +384,7 @@ def configure_args(self):
# https://github.com/spack/spack/pull/3081 and # https://github.com/spack/spack/pull/3081 and
# https://github.com/spack/spack/pull/4416 # https://github.com/spack/spack/pull/4416
if spec.satisfies("%intel"): if spec.satisfies("%intel"):
config_args.append("-Accflags={0}".format(self.spec["cc"].package.pic_flag)) config_args.append("-Accflags={0}".format(self["c"].pic_flag))
if "+shared" in spec: if "+shared" in spec:
config_args.append("-Duseshrplib") config_args.append("-Duseshrplib")
@ -546,7 +546,7 @@ def filter_config_dot_pm(self):
"-MModule::Loaded", "-MConfig", "-e", "print is_loaded(Config)", output=str "-MModule::Loaded", "-MConfig", "-e", "print is_loaded(Config)", output=str
) )
c_compiler = self.spec["c"].package.cc c_compiler = self["c"].cc
with self.make_briefly_writable(config_dot_pm): with self.make_briefly_writable(config_dot_pm):
match = "cc *=>.*" match = "cc *=>.*"
substitute = "cc => '{cc}',".format(cc=c_compiler) substitute = "cc => '{cc}',".format(cc=c_compiler)

View File

@ -120,7 +120,7 @@ def cmake_args(self):
options.append(self.define("{}_PROVIDER".format(lib), provider)) options.append(self.define("{}_PROVIDER".format(lib), provider))
if "cray-libsci" in self.spec: if "cray-libsci" in self.spec:
for lib in ("CBLAS", "LAPACKE"): for lib in ("CBLAS", "LAPACKE"):
libsci_prefix = self.spec["cray-libsci"].package.external_prefix libsci_prefix = self["cray-libsci"].external_prefix
options.append(self.define("{}_PROVIDER".format(lib), "generic")) options.append(self.define("{}_PROVIDER".format(lib), "generic"))
options.append( options.append(
self.define("{}_INCLUDE_DIRS".format(lib), join_path(libsci_prefix, "include")) self.define("{}_INCLUDE_DIRS".format(lib), join_path(libsci_prefix, "include"))

View File

@ -194,7 +194,7 @@ def cmake_args(self):
python_library = spec["python"].libs[0] python_library = spec["python"].libs[0]
python_include = spec["python"].headers.directories[0] python_include = spec["python"].headers.directories[0]
numpy_include = join_path( numpy_include = join_path(
spec["py-numpy"].package.module.python_platlib, "numpy", "core", "include" self["py-numpy"].module.python_platlib, "numpy", "core", "include"
) )
cmake_args.extend( cmake_args.extend(
[ [

View File

@ -60,7 +60,7 @@ def patch(self):
python_include = spec["python"].headers.directories[0] python_include = spec["python"].headers.directories[0]
numpy_include = join_path( numpy_include = join_path(
spec["py-numpy"].package.module.python_platlib, "numpy", "core", "include" self["py-numpy"].module.python_platlib, "numpy", "core", "include"
) )
libs = blas.libs + lapack.libs + libxc.libs libs = blas.libs + lapack.libs + libxc.libs

View File

@ -264,11 +264,11 @@ def setup_build_environment(self, env):
# Pick up BLAS/LAPACK from numpy # Pick up BLAS/LAPACK from numpy
if self.spec.satisfies("@:1.8"): if self.spec.satisfies("@:1.8"):
self.spec["py-numpy"].package.setup_build_environment(env) self["py-numpy"].setup_build_environment(env)
@when("@1.9:") @when("@1.9:")
def config_settings(self, spec, prefix): def config_settings(self, spec, prefix):
blas, lapack = self.spec["py-numpy"].package.blas_lapack_pkg_config() blas, lapack = self["py-numpy"].blas_lapack_pkg_config()
if spec.satisfies("%aocc") or spec.satisfies("%clang@18:"): if spec.satisfies("%aocc") or spec.satisfies("%clang@18:"):
fortran_std = "none" fortran_std = "none"
@ -286,10 +286,9 @@ def config_settings(self, spec, prefix):
}, },
} }
@when("@:1.8") @run_before("install", when="@:1.8")
@run_before("install")
def set_blas_lapack(self): def set_blas_lapack(self):
self.spec["py-numpy"].package.blas_lapack_site_cfg() self["py-numpy"].blas_lapack_site_cfg()
@run_after("install") @run_after("install")
@on_package_attributes(run_tests=True) @on_package_attributes(run_tests=True)

View File

@ -586,7 +586,7 @@ def configure_args(self):
config_args.append("--without-ensurepip") config_args.append("--without-ensurepip")
if "+pic" in spec: if "+pic" in spec:
cflags.append(self.spec["c"].package.pic_flag) cflags.append(self["c"].pic_flag)
if "+ssl" in spec: if "+ssl" in spec:
config_args.append("--with-openssl={0}".format(spec["openssl"].prefix)) config_args.append("--with-openssl={0}".format(spec["openssl"].prefix))
@ -704,9 +704,9 @@ def filter_compilers(self):
filenames = [self.get_sysconfigdata_name(), self.config_vars["makefile_filename"]] filenames = [self.get_sysconfigdata_name(), self.config_vars["makefile_filename"]]
filter_file(spack_cc, self.spec["c"].package.cc, *filenames, **kwargs) filter_file(spack_cc, self["c"].cc, *filenames, **kwargs)
if spack_cxx: if spack_cxx:
filter_file(spack_cxx, self.spec["cxx"].package.cxx, *filenames, **kwargs) filter_file(spack_cxx, self["cxx"].cxx, *filenames, **kwargs)
@run_after("install") @run_after("install")
def symlink(self): def symlink(self):

View File

@ -198,7 +198,7 @@ class Qgis(CMakePackage):
@run_before("cmake", when="^py-pyqt5") @run_before("cmake", when="^py-pyqt5")
def fix_pyqt5_cmake(self): def fix_pyqt5_cmake(self):
cmfile = FileFilter(join_path("cmake", "FindPyQt5.cmake")) cmfile = FileFilter(join_path("cmake", "FindPyQt5.cmake"))
pyqtpath = join_path(self.spec["py-pyqt5"].package.module.python_platlib, "PyQt5") pyqtpath = join_path(self["py-pyqt5"].module.python_platlib, "PyQt5")
cmfile.filter( cmfile.filter(
'SET(PYQT5_MOD_DIR "${Python_SITEARCH}/PyQt5")', 'SET(PYQT5_MOD_DIR "${Python_SITEARCH}/PyQt5")',
'SET(PYQT5_MOD_DIR "' + pyqtpath + '")', 'SET(PYQT5_MOD_DIR "' + pyqtpath + '")',

View File

@ -77,9 +77,7 @@ def with_torch(self):
@property @property
def torch_dir(self): def torch_dir(self):
return ( return (
join_path(self.spec["py-torch"].package.cmake_prefix_paths[0], "Torch") join_path(self["py-torch"].cmake_prefix_paths[0], "Torch") if self.with_torch else None
if self.with_torch
else None
) )
@property @property

View File

@ -63,7 +63,7 @@ def configure_args(self):
] ]
if self.spec.satisfies("^intel-oneapi-mpi"): if self.spec.satisfies("^intel-oneapi-mpi"):
args.append("--with-mpi=" + self.spec["intel-oneapi-mpi"].package.component_prefix) args.append("--with-mpi=" + self["intel-oneapi-mpi"].component_prefix)
else: else:
args.append("--with-mpi=" + self.spec["mpi"].prefix) args.append("--with-mpi=" + self.spec["mpi"].prefix)

View File

@ -71,7 +71,7 @@ def libs(self):
def flag_handler(self, name, flags): def flag_handler(self, name, flags):
if name == "cflags" and self.spec.satisfies("+pic build_system=autotools"): if name == "cflags" and self.spec.satisfies("+pic build_system=autotools"):
flags.append(self.spec["c"].package.pic_flag) flags.append(self["c"].pic_flag)
return (flags, None, None) return (flags, None, None)