summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHarmen Stoppels <me@harmenstoppels.nl>2023-11-06 23:30:27 +0100
committerGitHub <noreply@github.com>2023-11-06 14:30:27 -0800
commit461eb944bdff103b8e347c272afb2bcbd31f9723 (patch)
tree8e54a9d7a3601b205e0784eeb44ac56899b02d49 /lib
parent4700108b5b91c3182c96c4b8468ecf3b536dfd0a (diff)
downloadspack-461eb944bdff103b8e347c272afb2bcbd31f9723.tar.gz
spack-461eb944bdff103b8e347c272afb2bcbd31f9723.tar.bz2
spack-461eb944bdff103b8e347c272afb2bcbd31f9723.tar.xz
spack-461eb944bdff103b8e347c272afb2bcbd31f9723.zip
Don't let runtime env variables of compiler like deps leak into the build environment (#40916)
* Test that setup_run_environment changes to CC/CXX/FC/F77 are dropped in build env * compilers set in run env shouldn't impact build Adds `drop` to EnvironmentModifications courtesy of @haampie, and uses it to clear modifications of CC, CXX, F77 and FC made by `setup_{,dependent_}run_environment` routines when producing an environment in BUILD context. * comment / style * comment --------- Co-authored-by: Tom Scogland <scogland1@llnl.gov>
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/build_environment.py11
-rw-r--r--lib/spack/spack/test/build_environment.py15
-rw-r--r--lib/spack/spack/util/environment.py8
3 files changed, 32 insertions, 2 deletions
diff --git a/lib/spack/spack/build_environment.py b/lib/spack/spack/build_environment.py
index 3f6830ad33..4c4eca6567 100644
--- a/lib/spack/spack/build_environment.py
+++ b/lib/spack/spack/build_environment.py
@@ -1016,10 +1016,17 @@ class SetupContext:
self._make_runnable(dspec, env)
if self.should_setup_run_env & flag:
+ run_env_mods = EnvironmentModifications()
for spec in dspec.dependents(deptype=dt.LINK | dt.RUN):
if id(spec) in self.nodes_in_subdag:
- pkg.setup_dependent_run_environment(env, spec)
- pkg.setup_run_environment(env)
+ pkg.setup_dependent_run_environment(run_env_mods, spec)
+ pkg.setup_run_environment(run_env_mods)
+ if self.context == Context.BUILD:
+ # Don't let the runtime environment of comiler like dependencies leak into the
+ # build env
+ run_env_mods.drop("CC", "CXX", "F77", "FC")
+ env.extend(run_env_mods)
+
return env
def _make_buildtime_detectable(self, dep: spack.spec.Spec, env: EnvironmentModifications):
diff --git a/lib/spack/spack/test/build_environment.py b/lib/spack/spack/test/build_environment.py
index f2bf740272..cbccbc429e 100644
--- a/lib/spack/spack/test/build_environment.py
+++ b/lib/spack/spack/test/build_environment.py
@@ -652,3 +652,18 @@ def test_monkey_patching_works_across_virtual(default_mock_concretization):
s["mpich"].foo = "foo"
assert s["mpich"].foo == "foo"
assert s["mpi"].foo == "foo"
+
+
+def test_clear_compiler_related_runtime_variables_of_build_deps(default_mock_concretization):
+ """Verify that Spack drops CC, CXX, FC and F77 from the dependencies related build environment
+ variable changes if they are set in setup_run_environment. Spack manages those variables
+ elsewhere."""
+ s = default_mock_concretization("build-env-compiler-var-a")
+ ctx = spack.build_environment.SetupContext(s, context=Context.BUILD)
+ result = {}
+ ctx.get_env_modifications().apply_modifications(result)
+ assert "CC" not in result
+ assert "CXX" not in result
+ assert "FC" not in result
+ assert "F77" not in result
+ assert result["ANOTHER_VAR"] == "this-should-be-present"
diff --git a/lib/spack/spack/util/environment.py b/lib/spack/spack/util/environment.py
index 246df65cb8..d4c352b993 100644
--- a/lib/spack/spack/util/environment.py
+++ b/lib/spack/spack/util/environment.py
@@ -596,6 +596,14 @@ class EnvironmentModifications:
modifications[item.name].append(item)
return modifications
+ def drop(self, *name) -> bool:
+ """Drop all modifications to the variable with the given name."""
+ old_mods = self.env_modifications
+ new_mods = [x for x in self.env_modifications if x.name not in name]
+ self.env_modifications = new_mods
+
+ return len(old_mods) != len(new_mods)
+
def is_unset(self, variable_name: str) -> bool:
"""Returns True if the last modification to a variable is to unset it, False otherwise."""
modifications = self.group_by_name()