Skip to content

Commit

Permalink
Avoid writing to file in emsdk_env.csh / emsdk_env.sh (emscripten-cor…
Browse files Browse the repository at this point in the history
…e#544)

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

Tested with bash, tcsh and fish.
  • Loading branch information
sbc100 authored Jul 11, 2020
1 parent 833dfdd commit d37abed
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ __pycache__
/.emscripten_sanity
/.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.sh

# Tags files that get generated at runtime
/emscripten-releases-tot.txt
Expand Down
58 changes: 28 additions & 30 deletions emsdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
else:
ENVPATH_SEPARATOR = ':'


ARCH = 'unknown'
# platform.machine() may return AMD64 on windows, so standardize the case.
machine = platform.machine().lower()
Expand Down Expand Up @@ -170,18 +169,7 @@ def emsdk_path():
if os.path.exists(os.path.join(emsdk_path(), '.emscripten')):
emscripten_config_directory = emsdk_path()


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())
EMSDK_SET_ENV = os.path.join(emsdk_path(), 'emsdk_set_env.bat')

ARCHIVE_SUFFIXES = ('zip', '.tar', '.gz', '.xz', '.tbz2', '.bz2')

Expand Down Expand Up @@ -2479,6 +2467,11 @@ def copy_pregenerated_cache(tools_to_activate):
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
# and other environment variables.
# Returns the full list of deduced tools that are now active.
Expand All @@ -2503,7 +2496,7 @@ def set_active_tools(tools_to_activate, permanently_activate):
# required.
if WINDOWS:
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.
if WINDOWS and permanently_activate:
Expand Down Expand Up @@ -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)


def log_stderr(msg):
sys.stderr.write(str(msg) + '\n')


def construct_env(tools_to_activate):
env_string = ''
newpath, added_path = adjusted_path(tools_to_activate)
Expand All @@ -2610,17 +2607,17 @@ def construct_env(tools_to_activate):
elif CMD:
env_string += 'SET PATH=' + newpath + '\n'
elif CSH:
env_string += 'setenv PATH "' + newpath + '"\n'
env_string += 'setenv PATH "' + newpath + '";\n'
elif BASH:
env_string += 'export PATH="' + newpath + '"\n'
env_string += 'export PATH="' + newpath + '";\n'
else:
assert False

if added_path:
print('Adding directories to PATH:')
log_stderr('Adding directories to PATH:')
for item in added_path:
print('PATH += ' + item)
print('')
log_stderr('PATH += ' + item)
log_stderr('')

# A core variable EMSDK points to the root of Emscripten SDK directory.
env_vars = [('EMSDK', to_unix_path(emsdk_path()))]
Expand Down Expand Up @@ -2649,19 +2646,19 @@ def construct_env(tools_to_activate):
env_vars_to_add.append((key, value))

if env_vars_to_add:
print('Setting environment variables:')
log_stderr('Setting environment variables:')
for key, value in env_vars_to_add:
if POWERSHELL:
env_string += '$env:' + key + '="' + value + '"\n'
elif CMD:
env_string += 'SET ' + key + '=' + value + '\n'
elif CSH:
env_string += 'setenv ' + key + ' "' + value + '"\n'
env_string += 'setenv ' + key + ' "' + value + '";\n'
elif BASH:
env_string += 'export ' + key + '="' + value + '"\n'
env_string += 'export ' + key + '="' + value + '";\n'
else:
assert False
print(key + ' = ' + value)
log_stderr(key + ' = ' + value)
return env_string


Expand Down Expand Up @@ -3023,19 +3020,20 @@ def print_tools(t):
elif cmd == 'construct_env':
# Clean up old temp file up front, in case of failure later before we get
# to write out the new one.
silentremove(EMSDK_SET_ENV)
tools_to_activate = currently_active_tools()
tools_to_activate = process_tool_list(tools_to_activate, log_errors=True)
env_string = construct_env(tools_to_activate)
open(EMSDK_SET_ENV, 'w').write(env_string)
if UNIX:
os.chmod(EMSDK_SET_ENV, 0o755)
if WINDOWS and not BASH:
write_set_env_bat(env_string)
else:
sys.stdout.write(env_string)
return 0
elif cmd == 'update':
update_emsdk()
# Clean up litter after old emsdk update which may have left this temp file
# around.
silentremove(sdk_path(EMSDK_SET_ENV))
if WINDOWS:
# Clean up litter after old emsdk update which may have left this temp
# file around.
silentremove(sdk_path(EMSDK_SET_ENV))
return 0
elif cmd == 'update-tags':
fetch_emscripten_tags()
Expand Down
11 changes: 4 additions & 7 deletions emsdk_env.csh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
# because it won't have any effect then.
# That is, always run this script with
#
# . ./emsdk_env.csh
# or
# source ./emsdk_env.csh
#
# instead of just plainly running with
Expand All @@ -18,16 +16,15 @@ set SRC=($_)
if ("$SRC" == "") then
set SRC="$0"
else
set SRC="$SRC[2]"
set SRC="$SRC[1]"
endif
set CURDIR=`pwd`
set DIR=`dirname "$SRC"`
setenv DIR `dirname "$SRC"`
unset SRC

setenv EMSDK_CSH 1

$DIR/emsdk construct_env
source $DIR/emsdk_set_env.csh
unset DIR
eval `$DIR/emsdk construct_env`
unsetenv DIR

unsetenv EMSDK_CSH
3 changes: 1 addition & 2 deletions emsdk_env.fish
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
set -l script (status -f)
set -l dir (dirname $script)

$dir/emsdk construct_env
. $dir/emsdk_set_env.sh
eval ($dir/emsdk construct_env)

set -e -l script
set -e -l dir
3 changes: 1 addition & 2 deletions emsdk_env.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ fi
DIR="$(dirname "$DIR")"

# Force emsdk to use bash syntax so that this works in windows + bash too
EMSDK_BASH=1 $DIR/emsdk construct_env
. $DIR/emsdk_set_env.sh
eval `EMSDK_BASH=1 $DIR/emsdk construct_env`
unset DIR

0 comments on commit d37abed

Please sign in to comment.