Avoid writing to file in emsdk_env.csh / emsdk_env.sh (#544)

This is an alternative fix for
https://github.com/emscripten-core/emscripten/issues/9090 which recently
came up again after #539.

Tested with bash, tcsh and fish.
This commit is contained in:
Sam Clegg
2020-07-10 17:49:16 -07:00
committed by GitHub
parent 833dfddcdc
commit d37abed2de
5 changed files with 35 additions and 43 deletions

3
.gitignore vendored
View File

@@ -9,9 +9,8 @@ __pycache__
/.emscripten_sanity /.emscripten_sanity
/.emscripten_sanity_wasm /.emscripten_sanity_wasm
# Auto-generated by `active` # Auto-generated by emsdk.py under windows by `construct_env` or `activate`
/emsdk_set_env.bat /emsdk_set_env.bat
/emsdk_set_env.sh
# Tags files that get generated at runtime # Tags files that get generated at runtime
/emscripten-releases-tot.txt /emscripten-releases-tot.txt

View File

@@ -96,7 +96,6 @@ if WINDOWS:
else: else:
ENVPATH_SEPARATOR = ':' ENVPATH_SEPARATOR = ':'
ARCH = 'unknown' ARCH = 'unknown'
# platform.machine() may return AMD64 on windows, so standardize the case. # platform.machine() may return AMD64 on windows, so standardize the case.
machine = platform.machine().lower() machine = platform.machine().lower()
@@ -170,18 +169,7 @@ emscripten_config_directory = os.path.expanduser("~/")
if os.path.exists(os.path.join(emsdk_path(), '.emscripten')): if os.path.exists(os.path.join(emsdk_path(), '.emscripten')):
emscripten_config_directory = emsdk_path() emscripten_config_directory = emsdk_path()
EMSDK_SET_ENV = os.path.join(emsdk_path(), 'emsdk_set_env.bat')
def get_set_env_script_name():
if POWERSHELL:
return 'emsdk_set_env.ps1'
if WINDOWS and not MSYS:
return 'emsdk_set_env.bat'
if CSH:
return 'emsdk_set_env.csh'
return 'emsdk_set_env.sh'
EMSDK_SET_ENV = os.path.join(emsdk_path(), get_set_env_script_name())
ARCHIVE_SUFFIXES = ('zip', '.tar', '.gz', '.xz', '.tbz2', '.bz2') ARCHIVE_SUFFIXES = ('zip', '.tar', '.gz', '.xz', '.tbz2', '.bz2')
@@ -2479,6 +2467,11 @@ def copy_pregenerated_cache(tools_to_activate):
os.path.join(out_cache, filename)) os.path.join(out_cache, filename))
def write_set_env_bat(env_string):
assert(WINDOWS)
open(EMSDK_SET_ENV, 'w').write(env_string)
# Reconfigure .emscripten to choose the currently activated toolset, set PATH # Reconfigure .emscripten to choose the currently activated toolset, set PATH
# and other environment variables. # and other environment variables.
# Returns the full list of deduced tools that are now active. # Returns the full list of deduced tools that are now active.
@@ -2503,7 +2496,7 @@ def set_active_tools(tools_to_activate, permanently_activate):
# required. # required.
if WINDOWS: if WINDOWS:
env_string = construct_env(tools_to_activate) env_string = construct_env(tools_to_activate)
open(EMSDK_SET_ENV, 'w').write(env_string) write_set_env_bat(env_string)
# Apply environment variables to global all users section. # Apply environment variables to global all users section.
if WINDOWS and permanently_activate: if WINDOWS and permanently_activate:
@@ -2599,6 +2592,10 @@ def adjusted_path(tools_to_activate, log_additions=False, system_path_only=False
return (separator.join(whole_path), new_emsdk_tools) return (separator.join(whole_path), new_emsdk_tools)
def log_stderr(msg):
sys.stderr.write(str(msg) + '\n')
def construct_env(tools_to_activate): def construct_env(tools_to_activate):
env_string = '' env_string = ''
newpath, added_path = adjusted_path(tools_to_activate) newpath, added_path = adjusted_path(tools_to_activate)
@@ -2610,17 +2607,17 @@ def construct_env(tools_to_activate):
elif CMD: elif CMD:
env_string += 'SET PATH=' + newpath + '\n' env_string += 'SET PATH=' + newpath + '\n'
elif CSH: elif CSH:
env_string += 'setenv PATH "' + newpath + '"\n' env_string += 'setenv PATH "' + newpath + '";\n'
elif BASH: elif BASH:
env_string += 'export PATH="' + newpath + '"\n' env_string += 'export PATH="' + newpath + '";\n'
else: else:
assert False assert False
if added_path: if added_path:
print('Adding directories to PATH:') log_stderr('Adding directories to PATH:')
for item in added_path: for item in added_path:
print('PATH += ' + item) log_stderr('PATH += ' + item)
print('') log_stderr('')
# A core variable EMSDK points to the root of Emscripten SDK directory. # A core variable EMSDK points to the root of Emscripten SDK directory.
env_vars = [('EMSDK', to_unix_path(emsdk_path()))] env_vars = [('EMSDK', to_unix_path(emsdk_path()))]
@@ -2649,19 +2646,19 @@ def construct_env(tools_to_activate):
env_vars_to_add.append((key, value)) env_vars_to_add.append((key, value))
if env_vars_to_add: if env_vars_to_add:
print('Setting environment variables:') log_stderr('Setting environment variables:')
for key, value in env_vars_to_add: for key, value in env_vars_to_add:
if POWERSHELL: if POWERSHELL:
env_string += '$env:' + key + '="' + value + '"\n' env_string += '$env:' + key + '="' + value + '"\n'
elif CMD: elif CMD:
env_string += 'SET ' + key + '=' + value + '\n' env_string += 'SET ' + key + '=' + value + '\n'
elif CSH: elif CSH:
env_string += 'setenv ' + key + ' "' + value + '"\n' env_string += 'setenv ' + key + ' "' + value + '";\n'
elif BASH: elif BASH:
env_string += 'export ' + key + '="' + value + '"\n' env_string += 'export ' + key + '="' + value + '";\n'
else: else:
assert False assert False
print(key + ' = ' + value) log_stderr(key + ' = ' + value)
return env_string return env_string
@@ -3023,19 +3020,20 @@ def main():
elif cmd == 'construct_env': elif cmd == 'construct_env':
# Clean up old temp file up front, in case of failure later before we get # Clean up old temp file up front, in case of failure later before we get
# to write out the new one. # to write out the new one.
silentremove(EMSDK_SET_ENV)
tools_to_activate = currently_active_tools() tools_to_activate = currently_active_tools()
tools_to_activate = process_tool_list(tools_to_activate, log_errors=True) tools_to_activate = process_tool_list(tools_to_activate, log_errors=True)
env_string = construct_env(tools_to_activate) env_string = construct_env(tools_to_activate)
open(EMSDK_SET_ENV, 'w').write(env_string) if WINDOWS and not BASH:
if UNIX: write_set_env_bat(env_string)
os.chmod(EMSDK_SET_ENV, 0o755) else:
sys.stdout.write(env_string)
return 0 return 0
elif cmd == 'update': elif cmd == 'update':
update_emsdk() update_emsdk()
# Clean up litter after old emsdk update which may have left this temp file if WINDOWS:
# around. # Clean up litter after old emsdk update which may have left this temp
silentremove(sdk_path(EMSDK_SET_ENV)) # file around.
silentremove(sdk_path(EMSDK_SET_ENV))
return 0 return 0
elif cmd == 'update-tags': elif cmd == 'update-tags':
fetch_emscripten_tags() fetch_emscripten_tags()

11
emsdk_env.csh Executable file → Normal file
View File

@@ -5,8 +5,6 @@
# because it won't have any effect then. # because it won't have any effect then.
# That is, always run this script with # That is, always run this script with
# #
# . ./emsdk_env.csh
# or
# source ./emsdk_env.csh # source ./emsdk_env.csh
# #
# instead of just plainly running with # instead of just plainly running with
@@ -18,16 +16,15 @@ set SRC=($_)
if ("$SRC" == "") then if ("$SRC" == "") then
set SRC="$0" set SRC="$0"
else else
set SRC="$SRC[2]" set SRC="$SRC[1]"
endif endif
set CURDIR=`pwd` set CURDIR=`pwd`
set DIR=`dirname "$SRC"` setenv DIR `dirname "$SRC"`
unset SRC unset SRC
setenv EMSDK_CSH 1 setenv EMSDK_CSH 1
$DIR/emsdk construct_env eval `$DIR/emsdk construct_env`
source $DIR/emsdk_set_env.csh unsetenv DIR
unset DIR
unsetenv EMSDK_CSH unsetenv EMSDK_CSH

3
emsdk_env.fish Executable file → Normal file
View File

@@ -6,8 +6,7 @@
set -l script (status -f) set -l script (status -f)
set -l dir (dirname $script) set -l dir (dirname $script)
$dir/emsdk construct_env eval ($dir/emsdk construct_env)
. $dir/emsdk_set_env.sh
set -e -l script set -e -l script
set -e -l dir set -e -l dir

3
emsdk_env.sh Executable file → Normal file
View File

@@ -22,6 +22,5 @@ fi
DIR="$(dirname "$DIR")" DIR="$(dirname "$DIR")"
# Force emsdk to use bash syntax so that this works in windows + bash too # Force emsdk to use bash syntax so that this works in windows + bash too
EMSDK_BASH=1 $DIR/emsdk construct_env eval `EMSDK_BASH=1 $DIR/emsdk construct_env`
. $DIR/emsdk_set_env.sh
unset DIR unset DIR