Unset emsdk-related environment variable from inactive tools (#801)

When we deactivate a tool we also want to remove its environment
variables.   One driver for this is that modern sdks don't set
`EM_CACHE` whereas old ones did and we want to make sure that
`EM_CACHE` gets unset when folks upgrade (and then re-set if
they downgrade).  See #797.
This commit is contained in:
Sam Clegg
2021-04-29 08:43:20 -07:00
committed by GitHub
parent 3510ab2697
commit b4c9194d81
2 changed files with 60 additions and 30 deletions

View File

@@ -2711,6 +2711,18 @@ def construct_env(tools_to_activate, system, user):
return construct_env_with_vars(get_env_vars_to_add(tools_to_activate, system, user)) return construct_env_with_vars(get_env_vars_to_add(tools_to_activate, system, user))
def unset_env(key):
if POWERSHELL:
return 'Remove-Item env:%s\n' % key
if CMD:
return 'set %s=\n' % key
if CSH:
return 'unsetenv %s;\n' % key
if BASH:
return 'unset %s;\n' % key
assert False
def construct_env_with_vars(env_vars_to_add): def construct_env_with_vars(env_vars_to_add):
env_string = '' env_string = ''
if env_vars_to_add: if env_vars_to_add:
@@ -2718,36 +2730,40 @@ def construct_env_with_vars(env_vars_to_add):
for key, value in env_vars_to_add: for key, value in env_vars_to_add:
# Don't set env vars which are already set to the correct value. # Don't set env vars which are already set to the correct value.
if key not in os.environ or to_unix_path(os.environ[key]) != to_unix_path(value): if key in os.environ and to_unix_path(os.environ[key]) == to_unix_path(value):
errlog(key + ' = ' + value) continue
if POWERSHELL: errlog(key + ' = ' + value)
env_string += '$env:' + key + '="' + value + '"\n' if POWERSHELL:
elif CMD: env_string += '$env:' + key + '="' + value + '"\n'
env_string += 'SET ' + key + '=' + value + '\n' elif CMD:
elif CSH: env_string += 'SET ' + key + '=' + value + '\n'
env_string += 'setenv ' + key + ' "' + value + '"\n' elif CSH:
elif BASH: env_string += 'setenv ' + key + ' "' + value + '";\n'
env_string += 'export ' + key + '="' + value + '"\n' elif BASH:
else: env_string += 'export ' + key + '="' + value + '";\n'
assert False else:
if 'EMSDK_PYTHON' in env_vars_to_add: assert False
# When using our bundled python we never want the user's
# PYTHONHOME or PYTHONPATH if 'EMSDK_PYTHON' in env_vars_to_add:
# See https://github.com/emscripten-core/emsdk/issues/598 # When using our bundled python we never want the user's
if POWERSHELL: # PYTHONHOME or PYTHONPATH
env_string += 'Remove-Item env:PYTHONHOME\n' # See https://github.com/emscripten-core/emsdk/issues/598
env_string += 'Remove-Item env:PYTHONPATH\n' env_string += unset_env('PYTHONHOME')
elif CMD: env_string += unset_env('PYTHONPATH')
env_string += 'set PYTHONHOME=\n'
env_string += 'set PYTHONPATH=\n' # Remove any environment variables that might have been set by old or
elif CSH: # inactive tools/sdks. For example, we set EM_CACHE for older versions
env_string += 'unsetenv PYTHONHOME\n' # of the SDK but we want to remove that from the current environment
env_string += 'unsetenv PYTHONPATH\n' # if no such tool is active.
elif BASH: # Ignore certain keys that are inputs to emsdk itself.
env_string += 'unset PYTHONHOME\n' ignore_keys = set(['EMSDK_POWERSHELL', 'EMSDK_CSH', 'EMSDK_CMD', 'EMSDK_BASH',
env_string += 'unset PYTHONPATH\n' 'EMSDK_NUM_CORES', 'EMSDK_TTY'])
else: env_keys_to_add = set(pair[0] for pair in env_vars_to_add)
assert False for key in os.environ:
if key.startswith('EMSDK_') or key.startswith('EM_'):
if key not in env_keys_to_add and key not in ignore_keys:
errlog('Clearing existing environment variable: %s' % key)
env_string += unset_env(key)
return env_string return env_string

View File

@@ -13,9 +13,23 @@ source ./emsdk_env.sh
which emcc which emcc
emcc -v emcc -v
# Install an older version of the SDK that requires EM_CACHE to be
# set in the environment, so that we can test it is later removed
./emsdk install sdk-fastcomp-3b8cff670e9233a6623563add831647e8689a86b
./emsdk activate sdk-fastcomp-3b8cff670e9233a6623563add831647e8689a86b
source ./emsdk_env.sh
which emcc
emcc -v
test -n "$EM_CACHE"
# Install the latest version of the SDK which is the expected precondition
# of test.py.
./emsdk install latest ./emsdk install latest
./emsdk activate latest ./emsdk activate latest
source ./emsdk_env.sh --build=Release source ./emsdk_env.sh --build=Release
# Test that EM_CACHE was unset
test -z "$EM_CACHE"
# On mac and windows python3 should be in the path and point to the # On mac and windows python3 should be in the path and point to the
# bundled version. # bundled version.
which python3 which python3