Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 46 additions & 30 deletions emsdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2684,43 +2684,59 @@ def construct_env(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):
env_string = ''
if env_vars_to_add:
errlog('Setting environment variables:')

for key, value in env_vars_to_add:
# 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):
errlog(key + ' = ' + value)
if POWERSHELL:
env_string += '$env:' + key + '="' + value + '"\n'
elif CMD:
env_string += 'SET ' + key + '=' + value + '\n'
elif CSH:
env_string += 'setenv ' + key + ' "' + value + '"\n'
elif BASH:
env_string += 'export ' + key + '="' + value + '"\n'
else:
assert False
if 'EMSDK_PYTHON' in env_vars_to_add:
# When using our bundled python we never want the user's
# PYTHONHOME or PYTHONPATH
# See https://github.com/emscripten-core/emsdk/issues/598
if POWERSHELL:
env_string += 'Remove-Item env:PYTHONHOME\n'
env_string += 'Remove-Item env:PYTHONPATH\n'
elif CMD:
env_string += 'set PYTHONHOME=\n'
env_string += 'set PYTHONPATH=\n'
elif CSH:
env_string += 'unsetenv PYTHONHOME\n'
env_string += 'unsetenv PYTHONPATH\n'
elif BASH:
env_string += 'unset PYTHONHOME\n'
env_string += 'unset PYTHONPATH\n'
else:
assert False
if key in os.environ and to_unix_path(os.environ[key]) == to_unix_path(value):
continue
errlog(key + ' = ' + value)
if POWERSHELL:
env_string += '$env:' + key + '="' + value + '"\n'
elif CMD:
env_string += 'SET ' + key + '=' + value + '\n'
elif CSH:
env_string += 'setenv ' + key + ' "' + value + '";\n'
elif BASH:
env_string += 'export ' + key + '="' + value + '";\n'
else:
assert False

if 'EMSDK_PYTHON' in env_vars_to_add:
# When using our bundled python we never want the user's
# PYTHONHOME or PYTHONPATH
# See https://github.com/emscripten-core/emsdk/issues/598
env_string += unset_env('PYTHONHOME')
env_string += unset_env('PYTHONPATH')

# Remove any environment variables that might have been set by old or
# inactive tools/sdks. For example, we set EM_CACHE for older versions
# of the SDK but we want to remove that from the current environment
# if no such tool is active.
# Ignore certain keys that are inputs to emsdk itself.
ignore_keys = set(['EMSDK_POWERSHELL', 'EMSDK_CSH', 'EMSDK_CMD', 'EMSDK_BASH',
'EMSDK_NUM_CORES', 'EMSDK_TTY'])
env_keys_to_add = set(pair[0] for pair in env_vars_to_add)
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

Expand Down
14 changes: 14 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@ source ./emsdk_env.sh
which emcc
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 activate latest
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
# bundled version.
which python3
Expand Down