Skip to content

Commit 4282d5d

Browse files
authored
Avoid setting EM_CACHE unless we really need to (#797)
This avoid polluting the global environment which makes side-by-side installational of different emscripten version harder. See emscripten-core/emscripten#13954
1 parent cee96f8 commit 4282d5d

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

emsdk.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,14 @@ def find_tot_sdk():
21712171
return 'sdk-releases-upstream-%s-64bit' % (extra_release_tag)
21722172

21732173

2174+
def parse_emscripten_version(emscripten_root):
2175+
version_file = os.path.join(emscripten_root, 'emscripten-version.txt')
2176+
with open(version_file) as f:
2177+
version = f.read().strip()
2178+
version = version.strip('"').split('.')
2179+
return [int(v) for v in version]
2180+
2181+
21742182
# Given a git hash in emscripten-releases, find the emscripten
21752183
# version for it. There may not be one if this is not the hash of
21762184
# a release, in which case we return None.
@@ -2643,10 +2651,26 @@ def get_env_vars_to_add(tools_to_activate, system, user):
26432651
for tool in tools_to_activate:
26442652
config = tool.activated_config()
26452653
if 'EMSCRIPTEN_ROOT' in config:
2646-
# For older emscripten versions that don't use this default we export
2647-
# EM_CACHE.
2648-
em_cache_dir = os.path.join(config['EMSCRIPTEN_ROOT'], 'cache')
2649-
env_vars_to_add += [('EM_CACHE', em_cache_dir)]
2654+
# For older emscripten versions that don't use an embedded cache by
2655+
# default we need to export EM_CACHE.
2656+
#
2657+
# Sadly, we can't put this in the config file since those older versions
2658+
# also didn't read the `CACHE` key from the config file:
2659+
#
2660+
# History:
2661+
# - 'CACHE' config started being honored in 1.39.16
2662+
# https://github.com/emscripten-core/emscripten/pull/11091
2663+
# - Default to embedded cache also started in 1.39.16
2664+
# https://github.com/emscripten-core/emscripten/pull/11126
2665+
#
2666+
# Since setting EM_CACHE in the environment effects the entire machine
2667+
# we want to avoid this except when installing these older emscripten
2668+
# versions that really need it.
2669+
version = parse_emscripten_version(config['EMSCRIPTEN_ROOT'])
2670+
if version < [1, 39, 16]:
2671+
em_cache_dir = os.path.join(config['EMSCRIPTEN_ROOT'], 'cache')
2672+
env_vars_to_add += [('EM_CACHE', em_cache_dir)]
2673+
26502674
envs = tool.activated_environment()
26512675
for env in envs:
26522676
key, value = parse_key_value(env)

test/test.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212

1313
assert 'EM_CONFIG' in os.environ, "emsdk should be activated before running this script"
1414

15-
# Remove the EM_CACHE environment variable. It interferes with testing since
16-
# it would otherwise be fixed for the duration of the script and we expect
17-
# "emsdk activate" to be able switch between SDKs during the running of this
18-
# script.
19-
del os.environ['EM_CACHE']
20-
2115
emconfig = os.environ['EM_CONFIG']
2216
upstream_emcc = os.path.join('upstream', 'emscripten', 'emcc')
2317
fastcomp_emcc = os.path.join('fastcomp', 'emscripten', 'emcc')

test/test_activation.ps1

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ try {
3232
$EMSDK_NODE = [System.Environment]::GetEnvironmentVariable("EMSDK_NODE", $env_type)
3333
$EMSDK_PYTHON = [System.Environment]::GetEnvironmentVariable("EMSDK_PYTHON", $env_type)
3434
$JAVA_HOME = [System.Environment]::GetEnvironmentVariable("JAVA_HOME", $env_type)
35-
$EM_CACHE = [System.Environment]::GetEnvironmentVariable("EM_CACHE", $env_type)
3635
$PATH = [System.Environment]::GetEnvironmentVariable("PATH", $env_type)
3736

3837
if (!$EMSDK) {
@@ -50,9 +49,6 @@ try {
5049
if (!$EMSDK_PYTHON) {
5150
throw "EMSDK_PYTHON is not set for the user"
5251
}
53-
if (!$EM_CACHE) {
54-
throw "EM_CACHE is not set for the user"
55-
}
5652

5753

5854
$path_split = $PATH.Split(';')
@@ -91,15 +87,13 @@ finally {
9187
[Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "User")
9288
[Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "User")
9389
[Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "User")
94-
[Environment]::SetEnvironmentVariable("EM_CACHE", $null, "User")
9590

9691
try {
9792
[Environment]::SetEnvironmentVariable("EMSDK", $null, "Machine")
9893
[Environment]::SetEnvironmentVariable("EM_CONFIG", $null, "Machine")
9994
[Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "Machine")
10095
[Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "Machine")
10196
[Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Machine")
102-
[Environment]::SetEnvironmentVariable("EM_CACHE", $null, "Machine")
10397
} catch {}
10498

10599

@@ -108,7 +102,6 @@ finally {
108102
[Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "Process")
109103
[Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "Process")
110104
[Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Process")
111-
[Environment]::SetEnvironmentVariable("EM_CACHE", $null, "Process")
112105

113106
refreshenv
114107
}

test/test_path_preservation.ps1

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,13 @@ finally {
127127
[Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "User")
128128
[Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "User")
129129
[Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "User")
130-
[Environment]::SetEnvironmentVariable("EM_CACHE", $null, "User")
131130

132131
try {
133132
[Environment]::SetEnvironmentVariable("EMSDK", $null, "Machine")
134133
[Environment]::SetEnvironmentVariable("EM_CONFIG", $null, "Machine")
135134
[Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "Machine")
136135
[Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "Machine")
137136
[Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Machine")
138-
[Environment]::SetEnvironmentVariable("EM_CACHE", $null, "Machine")
139137
} catch {}
140138

141139

@@ -144,7 +142,6 @@ finally {
144142
[Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "Process")
145143
[Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "Process")
146144
[Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Process")
147-
[Environment]::SetEnvironmentVariable("EM_CACHE", $null, "Process")
148145

149146
refreshenv
150147

0 commit comments

Comments
 (0)