Allow different target flags for different compilers

This commit is contained in:
Massimiliano Culpo 2024-11-15 18:35:26 +01:00
parent 7f093d129b
commit ec97e7e6fe
No known key found for this signature in database
GPG Key ID: 3E52BB992233066C
4 changed files with 40 additions and 21 deletions

10
lib/spack/env/cc vendored
View File

@ -402,7 +402,7 @@ dtags_to_strip="${SPACK_DTAGS_TO_STRIP}"
linker_arg="${SPACK_LINKER_ARG}"
# Set up rpath variable according to language.
rpath="ERROR: RPATH ARG WAS NOT SET"
rpath="ERROR: RPATH ARG WAS NOT SET, MAYBE THE PACKAGE DOES NOT DEPEND ON ${comp}?"
eval "rpath=\${SPACK_${comp}_RPATH_ARG:?${rpath}}"
# Dump the mode and exit if the command is dump-mode.
@ -787,15 +787,17 @@ case "$mode" in
C)
extend spack_flags_list SPACK_ALWAYS_CFLAGS
extend spack_flags_list SPACK_CFLAGS
preextend flags_list SPACK_TARGET_ARGS_CC
;;
CXX)
extend spack_flags_list SPACK_ALWAYS_CXXFLAGS
extend spack_flags_list SPACK_CXXFLAGS
preextend flags_list SPACK_TARGET_ARGS_CXX
;;
F)
preextend flags_list SPACK_TARGET_ARGS_FORTRAN
;;
esac
# prepend target args
preextend flags_list SPACK_TARGET_ARGS
;;
esac

View File

@ -193,6 +193,13 @@ def enable_new_dtags(self) -> str:
def setup_dependent_build_environment(self, env, dependent_spec):
# FIXME (compiler as nodes): check if this is good enough or should be made more general
# The package is not used as a compiler, so skip this setup
if not any(
lang in dependent_spec and dependent_spec[lang].name == self.spec.name
for lang in ("c", "cxx", "fortran")
):
return
# Populate an object with the list of environment modifications and return it
link_dir = pathlib.Path(spack.paths.build_env_path)
@ -218,6 +225,19 @@ def setup_dependent_build_environment(self, env, dependent_spec):
env.set(wrapper_var_name, str(wrapper_path))
env.set(f"SPACK_{wrapper_var_name}_RPATH_ARG", self.rpath_arg)
uarch = dependent_spec.architecture.target
version_number, _ = archspec.cpu.version_components(
self.spec.version.dotted_numeric_string
)
try:
isa_arg = uarch.optimization_flags(self.archspec_name(), version_number)
except (ValueError, archspec.cpu.UnsupportedMicroarchitecture):
isa_arg = ""
if isa_arg:
env.set(f"SPACK_TARGET_ARGS_{attr_name.upper()}", isa_arg)
# FIXME (compiler as nodes): make these paths language specific
env.set("SPACK_LINKER_ARG", self.linker_arg)
paths = _implicit_rpaths(pkg=self)
@ -233,16 +253,6 @@ def setup_dependent_build_environment(self, env, dependent_spec):
env.set("SPACK_DTAGS_TO_ADD", self.enable_new_dtags)
spec = self.spec
uarch = dependent_spec.architecture.target
version_number, _ = archspec.cpu.version_components(spec.version.dotted_numeric_string)
try:
isa_arg = uarch.optimization_flags(spec.name, version_number)
except (ValueError, archspec.cpu.UnsupportedMicroarchitecture):
isa_arg = ""
if isa_arg:
env.set("SPACK_TARGET_ARGS", isa_arg)
env.set("SPACK_COMPILER_SPEC", spec.format("{name}{@version}{variants}{/hash:7}"))
if spec.extra_attributes:
@ -275,6 +285,10 @@ def setup_dependent_build_environment(self, env, dependent_spec):
env.prepend_path("PATH", item)
env.set_path("SPACK_ENV_PATH", env_paths)
def archspec_name(self) -> str:
"""Name that archspec uses to refer to this compiler"""
return self.spec.name
def _implicit_rpaths(pkg: spack.package_base.PackageBase) -> List[str]:
detector = spack.compilers.libraries.CompilerPropertyDetector(pkg.spec)

View File

@ -791,11 +791,11 @@ def test_optimization_flags_are_using_node_target(default_mock_concretization, m
"""
monkeypatch.setattr(spack.build_systems.compiler, "_implicit_rpaths", lambda pkg: [])
gcc = default_mock_concretization("gcc target=core2")
mpileaks = default_mock_concretization("gcc target=x86_64")
mpileaks = default_mock_concretization("mpileaks target=x86_64")
env = EnvironmentModifications()
gcc.package.setup_dependent_build_environment(env, mpileaks)
actions = env.group_by_name()["SPACK_TARGET_ARGS"]
actions = env.group_by_name()["SPACK_TARGET_ARGS_CC"]
assert len(actions) == 1 and isinstance(actions[0], spack.util.environment.SetEnv)
assert actions[0].value == "-march=x86-64 -mtune=generic"

View File

@ -132,6 +132,7 @@
headerpad = ["-headerpad_max_install_names"]
target_args = ["-march=znver2", "-mtune=znver2"]
target_args_fc = ["-march=znver4", "-mtune=znver4"]
# common compile arguments: includes, libs, -Wl linker args, other args
common_compile_args = (
@ -166,7 +167,9 @@ def wrapper_environment(working_env):
SPACK_LINK_DIRS=None,
SPACK_INCLUDE_DIRS=None,
SPACK_RPATH_DIRS=None,
SPACK_TARGET_ARGS="-march=znver2 -mtune=znver2",
SPACK_TARGET_ARGS_CC="-march=znver2 -mtune=znver2",
SPACK_TARGET_ARGS_CXX="-march=znver2 -mtune=znver2",
SPACK_TARGET_ARGS_FORTRAN="-march=znver4 -mtune=znver4",
SPACK_LINKER_ARG="-Wl,",
SPACK_DTAGS_TO_ADD="--disable-new-dtags",
SPACK_DTAGS_TO_STRIP="--enable-new-dtags",
@ -376,7 +379,7 @@ def test_fc_flags(wrapper_environment, wrapper_flags):
fc,
test_args,
[real_cc]
+ target_args
+ target_args_fc
+ test_include_paths
+ ["-Lfoo"]
+ test_library_paths
@ -423,7 +426,7 @@ def test_Wl_parsing_NAG_is_ignored(wrapper_environment):
check_args(
fc,
["-Wl,-Wl,,x,,y,,z"],
[real_cc] + target_args + ["-Wl,--disable-new-dtags", "-Wl,-Wl,,x,,y,,z"],
[real_cc] + target_args_fc + ["-Wl,--disable-new-dtags", "-Wl,-Wl,,x,,y,,z"],
)
@ -832,14 +835,14 @@ def test_no_ccache_prepend_for_fc(wrapper_environment):
fc,
test_args,
# no ccache for Fortran
[real_cc] + target_args + common_compile_args,
[real_cc] + target_args_fc + common_compile_args,
)
os.environ["SPACK_SHORT_SPEC"] = "foo@1.2=darwin-x86_64"
check_args(
fc,
test_args,
# no ccache for Fortran
[real_cc] + target_args + lheaderpad + common_compile_args,
[real_cc] + target_args_fc + lheaderpad + common_compile_args,
)