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>
This commit is contained in:
Harmen Stoppels 2023-11-06 23:30:27 +01:00 committed by GitHub
parent 4700108b5b
commit 461eb944bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 2 deletions

View file

@ -1016,10 +1016,17 @@ def get_env_modifications(self) -> EnvironmentModifications:
self._make_runnable(dspec, env) self._make_runnable(dspec, env)
if self.should_setup_run_env & flag: if self.should_setup_run_env & flag:
run_env_mods = EnvironmentModifications()
for spec in dspec.dependents(deptype=dt.LINK | dt.RUN): for spec in dspec.dependents(deptype=dt.LINK | dt.RUN):
if id(spec) in self.nodes_in_subdag: if id(spec) in self.nodes_in_subdag:
pkg.setup_dependent_run_environment(env, spec) pkg.setup_dependent_run_environment(run_env_mods, spec)
pkg.setup_run_environment(env) 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 return env
def _make_buildtime_detectable(self, dep: spack.spec.Spec, env: EnvironmentModifications): def _make_buildtime_detectable(self, dep: spack.spec.Spec, env: EnvironmentModifications):

View file

@ -652,3 +652,18 @@ def test_monkey_patching_works_across_virtual(default_mock_concretization):
s["mpich"].foo = "foo" s["mpich"].foo = "foo"
assert s["mpich"].foo == "foo" assert s["mpich"].foo == "foo"
assert s["mpi"].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"

View file

@ -596,6 +596,14 @@ def group_by_name(self) -> Dict[str, ModificationList]:
modifications[item.name].append(item) modifications[item.name].append(item)
return modifications 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: def is_unset(self, variable_name: str) -> bool:
"""Returns True if the last modification to a variable is to unset it, False otherwise.""" """Returns True if the last modification to a variable is to unset it, False otherwise."""
modifications = self.group_by_name() modifications = self.group_by_name()

View file

@ -0,0 +1,14 @@
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
from spack.package import *
class BuildEnvCompilerVarA(Package):
"""Package with runtime variable that should be dropped in the parent's build environment."""
url = "https://www.example.com"
version("1.0", md5="0123456789abcdef0123456789abcdef")
depends_on("build-env-compiler-var-b", type="build")

View file

@ -0,0 +1,20 @@
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
from spack.package import *
class BuildEnvCompilerVarB(Package):
"""Package with runtime variable that should be dropped in the parent's build environment."""
url = "https://www.example.com"
version("1.0", md5="0123456789abcdef0123456789abcdef")
def setup_run_environment(self, env):
env.set("CC", "this-should-be-dropped")
env.set("CXX", "this-should-be-dropped")
env.set("FC", "this-should-be-dropped")
env.set("F77", "this-should-be-dropped")
env.set("ANOTHER_VAR", "this-should-be-present")