From 08f23f48024c454428260d75429c8b8753942fe0 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Tue, 4 Jul 2023 10:54:13 +0200 Subject: macos sip: apply on macos only, dont store LD_LIBRARY_PATH (#38687) --- lib/spack/spack/main.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/main.py b/lib/spack/spack/main.py index 00131114f6..8d4e73bc65 100644 --- a/lib/spack/spack/main.py +++ b/lib/spack/spack/main.py @@ -855,6 +855,23 @@ def print_setup_info(*info): shell_set("_sp_module_prefix", "not_installed") +def restore_macos_dyld_vars(): + """ + Spack mutates DYLD_* variables in `spack load` and `spack env activate`. + Unlike Linux, macOS SIP clears these variables in new processes, meaning + that os.environ["DYLD_*"] in our Python process is not the same as the user's + shell. Therefore, we store the user's DYLD_* variables in SPACK_DYLD_* and + restore them here. + """ + if not sys.platform == "darwin": + return + + for dyld_var in ("DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH"): + stored_var_name = f"SPACK_{dyld_var}" + if stored_var_name in os.environ: + os.environ[dyld_var] = os.environ[stored_var_name] + + def _main(argv=None): """Logic for the main entry point for the Spack command. @@ -887,18 +904,6 @@ def _main(argv=None): parser.add_argument("command", nargs=argparse.REMAINDER) args, unknown = parser.parse_known_args(argv) - # Recover stored LD_LIBRARY_PATH variables from spack shell function - # This is necessary because MacOS System Integrity Protection clears - # (DY?)LD_LIBRARY_PATH variables on process start. - # Spack clears these variables before building and installing packages, - # but needs to know the prior state for commands like `spack load` and - # `spack env activate that modify the user environment. - recovered_vars = ("LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH", "DYLD_FALLBACK_LIBRARY_PATH") - for var in recovered_vars: - stored_var_name = "SPACK_%s" % var - if stored_var_name in os.environ: - os.environ[var] = os.environ[stored_var_name] - # Just print help and exit if run with no arguments at all no_args = (len(sys.argv) == 1) if argv is None else (len(argv) == 0) if no_args: @@ -921,6 +926,9 @@ def _main(argv=None): # scopes, then environment configuration here. # ------------------------------------------------------------------------ + # Make spack load / env activate work on macOS + restore_macos_dyld_vars() + # make spack.config aware of any command line configuration scopes if args.config_scopes: spack.config.command_line_scopes = args.config_scopes -- cgit v1.2.3-60-g2f50