Skip to content

Commit d37abed

Browse files
authored
Avoid writing to file in emsdk_env.csh / emsdk_env.sh (#544)
This is an alternative fix for emscripten-core/emscripten#9090 which recently came up again after #539. Tested with bash, tcsh and fish.
1 parent 833dfdd commit d37abed

File tree

5 files changed

+35
-43
lines changed

5 files changed

+35
-43
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ __pycache__
99
/.emscripten_sanity
1010
/.emscripten_sanity_wasm
1111

12-
# Auto-generated by `active`
12+
# Auto-generated by emsdk.py under windows by `construct_env` or `activate`
1313
/emsdk_set_env.bat
14-
/emsdk_set_env.sh
1514

1615
# Tags files that get generated at runtime
1716
/emscripten-releases-tot.txt

emsdk.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
else:
9797
ENVPATH_SEPARATOR = ':'
9898

99-
10099
ARCH = 'unknown'
101100
# platform.machine() may return AMD64 on windows, so standardize the case.
102101
machine = platform.machine().lower()
@@ -170,18 +169,7 @@ def emsdk_path():
170169
if os.path.exists(os.path.join(emsdk_path(), '.emscripten')):
171170
emscripten_config_directory = emsdk_path()
172171

173-
174-
def get_set_env_script_name():
175-
if POWERSHELL:
176-
return 'emsdk_set_env.ps1'
177-
if WINDOWS and not MSYS:
178-
return 'emsdk_set_env.bat'
179-
if CSH:
180-
return 'emsdk_set_env.csh'
181-
return 'emsdk_set_env.sh'
182-
183-
184-
EMSDK_SET_ENV = os.path.join(emsdk_path(), get_set_env_script_name())
172+
EMSDK_SET_ENV = os.path.join(emsdk_path(), 'emsdk_set_env.bat')
185173

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

@@ -2479,6 +2467,11 @@ def copy_pregenerated_cache(tools_to_activate):
24792467
os.path.join(out_cache, filename))
24802468

24812469

2470+
def write_set_env_bat(env_string):
2471+
assert(WINDOWS)
2472+
open(EMSDK_SET_ENV, 'w').write(env_string)
2473+
2474+
24822475
# Reconfigure .emscripten to choose the currently activated toolset, set PATH
24832476
# and other environment variables.
24842477
# Returns the full list of deduced tools that are now active.
@@ -2503,7 +2496,7 @@ def set_active_tools(tools_to_activate, permanently_activate):
25032496
# required.
25042497
if WINDOWS:
25052498
env_string = construct_env(tools_to_activate)
2506-
open(EMSDK_SET_ENV, 'w').write(env_string)
2499+
write_set_env_bat(env_string)
25072500

25082501
# Apply environment variables to global all users section.
25092502
if WINDOWS and permanently_activate:
@@ -2599,6 +2592,10 @@ def adjusted_path(tools_to_activate, log_additions=False, system_path_only=False
25992592
return (separator.join(whole_path), new_emsdk_tools)
26002593

26012594

2595+
def log_stderr(msg):
2596+
sys.stderr.write(str(msg) + '\n')
2597+
2598+
26022599
def construct_env(tools_to_activate):
26032600
env_string = ''
26042601
newpath, added_path = adjusted_path(tools_to_activate)
@@ -2610,17 +2607,17 @@ def construct_env(tools_to_activate):
26102607
elif CMD:
26112608
env_string += 'SET PATH=' + newpath + '\n'
26122609
elif CSH:
2613-
env_string += 'setenv PATH "' + newpath + '"\n'
2610+
env_string += 'setenv PATH "' + newpath + '";\n'
26142611
elif BASH:
2615-
env_string += 'export PATH="' + newpath + '"\n'
2612+
env_string += 'export PATH="' + newpath + '";\n'
26162613
else:
26172614
assert False
26182615

26192616
if added_path:
2620-
print('Adding directories to PATH:')
2617+
log_stderr('Adding directories to PATH:')
26212618
for item in added_path:
2622-
print('PATH += ' + item)
2623-
print('')
2619+
log_stderr('PATH += ' + item)
2620+
log_stderr('')
26242621

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

26512648
if env_vars_to_add:
2652-
print('Setting environment variables:')
2649+
log_stderr('Setting environment variables:')
26532650
for key, value in env_vars_to_add:
26542651
if POWERSHELL:
26552652
env_string += '$env:' + key + '="' + value + '"\n'
26562653
elif CMD:
26572654
env_string += 'SET ' + key + '=' + value + '\n'
26582655
elif CSH:
2659-
env_string += 'setenv ' + key + ' "' + value + '"\n'
2656+
env_string += 'setenv ' + key + ' "' + value + '";\n'
26602657
elif BASH:
2661-
env_string += 'export ' + key + '="' + value + '"\n'
2658+
env_string += 'export ' + key + '="' + value + '";\n'
26622659
else:
26632660
assert False
2664-
print(key + ' = ' + value)
2661+
log_stderr(key + ' = ' + value)
26652662
return env_string
26662663

26672664

@@ -3023,19 +3020,20 @@ def print_tools(t):
30233020
elif cmd == 'construct_env':
30243021
# Clean up old temp file up front, in case of failure later before we get
30253022
# to write out the new one.
3026-
silentremove(EMSDK_SET_ENV)
30273023
tools_to_activate = currently_active_tools()
30283024
tools_to_activate = process_tool_list(tools_to_activate, log_errors=True)
30293025
env_string = construct_env(tools_to_activate)
3030-
open(EMSDK_SET_ENV, 'w').write(env_string)
3031-
if UNIX:
3032-
os.chmod(EMSDK_SET_ENV, 0o755)
3026+
if WINDOWS and not BASH:
3027+
write_set_env_bat(env_string)
3028+
else:
3029+
sys.stdout.write(env_string)
30333030
return 0
30343031
elif cmd == 'update':
30353032
update_emsdk()
3036-
# Clean up litter after old emsdk update which may have left this temp file
3037-
# around.
3038-
silentremove(sdk_path(EMSDK_SET_ENV))
3033+
if WINDOWS:
3034+
# Clean up litter after old emsdk update which may have left this temp
3035+
# file around.
3036+
silentremove(sdk_path(EMSDK_SET_ENV))
30393037
return 0
30403038
elif cmd == 'update-tags':
30413039
fetch_emscripten_tags()

emsdk_env.csh

100755100644
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
# because it won't have any effect then.
66
# That is, always run this script with
77
#
8-
# . ./emsdk_env.csh
9-
# or
108
# source ./emsdk_env.csh
119
#
1210
# instead of just plainly running with
@@ -18,16 +16,15 @@ set SRC=($_)
1816
if ("$SRC" == "") then
1917
set SRC="$0"
2018
else
21-
set SRC="$SRC[2]"
19+
set SRC="$SRC[1]"
2220
endif
2321
set CURDIR=`pwd`
24-
set DIR=`dirname "$SRC"`
22+
setenv DIR `dirname "$SRC"`
2523
unset SRC
2624

2725
setenv EMSDK_CSH 1
2826

29-
$DIR/emsdk construct_env
30-
source $DIR/emsdk_set_env.csh
31-
unset DIR
27+
eval `$DIR/emsdk construct_env`
28+
unsetenv DIR
3229

3330
unsetenv EMSDK_CSH

emsdk_env.fish

100755100644
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
set -l script (status -f)
77
set -l dir (dirname $script)
88

9-
$dir/emsdk construct_env
10-
. $dir/emsdk_set_env.sh
9+
eval ($dir/emsdk construct_env)
1110

1211
set -e -l script
1312
set -e -l dir

emsdk_env.sh

100755100644
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ fi
2222
DIR="$(dirname "$DIR")"
2323

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

0 commit comments

Comments
 (0)