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 https://github.com/emscripten-core/emscripten/pull/13954
diff --git a/emsdk.py b/emsdk.py
index fc55934..48ac2a9 100644
--- a/emsdk.py
+++ b/emsdk.py
@@ -2171,6 +2171,14 @@
   return 'sdk-releases-upstream-%s-64bit' % (extra_release_tag)
 
 
+def parse_emscripten_version(emscripten_root):
+  version_file = os.path.join(emscripten_root, 'emscripten-version.txt')
+  with open(version_file) as f:
+    version = f.read().strip()
+    version = version.strip('"').split('.')
+    return [int(v) for v in version]
+
+
 # Given a git hash in emscripten-releases, find the emscripten
 # version for it. There may not be one if this is not the hash of
 # a release, in which case we return None.
@@ -2643,10 +2651,26 @@
   for tool in tools_to_activate:
     config = tool.activated_config()
     if 'EMSCRIPTEN_ROOT' in config:
-      # For older emscripten versions that don't use this default we export
-      # EM_CACHE.
-      em_cache_dir = os.path.join(config['EMSCRIPTEN_ROOT'], 'cache')
-      env_vars_to_add += [('EM_CACHE', em_cache_dir)]
+      # For older emscripten versions that don't use an embedded cache by
+      # default we need to export EM_CACHE.
+      #
+      # Sadly, we can't put this in the config file since those older versions
+      # also didn't read the `CACHE` key from the config file:
+      #
+      # History:
+      # - 'CACHE' config started being honored in 1.39.16
+      #   https://github.com/emscripten-core/emscripten/pull/11091
+      # - Default to embedded cache also started in 1.39.16
+      #   https://github.com/emscripten-core/emscripten/pull/11126
+      #
+      # Since setting EM_CACHE in the environment effects the entire machine
+      # we want to avoid this except when installing these older emscripten
+      # versions that really need it.
+      version = parse_emscripten_version(config['EMSCRIPTEN_ROOT'])
+      if version < [1, 39, 16]:
+        em_cache_dir = os.path.join(config['EMSCRIPTEN_ROOT'], 'cache')
+        env_vars_to_add += [('EM_CACHE', em_cache_dir)]
+
     envs = tool.activated_environment()
     for env in envs:
       key, value = parse_key_value(env)
diff --git a/test/test.py b/test/test.py
index 8d26669..2824efc 100755
--- a/test/test.py
+++ b/test/test.py
@@ -12,12 +12,6 @@
 
 assert 'EM_CONFIG' in os.environ, "emsdk should be activated before running this script"
 
-# Remove the EM_CACHE environment variable.  It interferes with testing since
-# it would otherwise be fixed for the duration of the script and we expect
-# "emsdk activate" to be able switch between SDKs during the running of this
-# script.
-del os.environ['EM_CACHE']
-
 emconfig = os.environ['EM_CONFIG']
 upstream_emcc = os.path.join('upstream', 'emscripten', 'emcc')
 fastcomp_emcc = os.path.join('fastcomp', 'emscripten', 'emcc')
diff --git a/test/test_activation.ps1 b/test/test_activation.ps1
index 82907bb..1aa0a97 100644
--- a/test/test_activation.ps1
+++ b/test/test_activation.ps1
@@ -32,7 +32,6 @@
     $EMSDK_NODE = [System.Environment]::GetEnvironmentVariable("EMSDK_NODE", $env_type)
     $EMSDK_PYTHON = [System.Environment]::GetEnvironmentVariable("EMSDK_PYTHON", $env_type)
     $JAVA_HOME = [System.Environment]::GetEnvironmentVariable("JAVA_HOME", $env_type)
-    $EM_CACHE = [System.Environment]::GetEnvironmentVariable("EM_CACHE", $env_type)
     $PATH = [System.Environment]::GetEnvironmentVariable("PATH", $env_type)
 
     if (!$EMSDK) {
@@ -50,9 +49,6 @@
     if (!$EMSDK_PYTHON) {
         throw "EMSDK_PYTHON is not set for the user"
     }
-    if (!$EM_CACHE) {
-        throw "EM_CACHE is not set for the user"
-    }
 
 
     $path_split = $PATH.Split(';')
@@ -91,7 +87,6 @@
     [Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "User")
     [Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "User")
     [Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "User")
-    [Environment]::SetEnvironmentVariable("EM_CACHE", $null, "User")
 
     try {
         [Environment]::SetEnvironmentVariable("EMSDK", $null, "Machine")
@@ -99,7 +94,6 @@
         [Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "Machine")
         [Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "Machine")
         [Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Machine")
-        [Environment]::SetEnvironmentVariable("EM_CACHE", $null, "Machine")
     } catch {}
 
 
@@ -108,7 +102,6 @@
     [Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "Process")
     [Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "Process")
     [Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Process")
-    [Environment]::SetEnvironmentVariable("EM_CACHE", $null, "Process")
 
     refreshenv
 }
diff --git a/test/test_path_preservation.ps1 b/test/test_path_preservation.ps1
index 79a42d9..b894954 100644
--- a/test/test_path_preservation.ps1
+++ b/test/test_path_preservation.ps1
@@ -127,7 +127,6 @@
     [Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "User")
     [Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "User")
     [Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "User")
-    [Environment]::SetEnvironmentVariable("EM_CACHE", $null, "User")
 
     try {
         [Environment]::SetEnvironmentVariable("EMSDK", $null, "Machine")
@@ -135,7 +134,6 @@
         [Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "Machine")
         [Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "Machine")
         [Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Machine")
-        [Environment]::SetEnvironmentVariable("EM_CACHE", $null, "Machine")
     } catch {}
 
 
@@ -144,7 +142,6 @@
     [Environment]::SetEnvironmentVariable("EMSDK_NODE", $null, "Process")
     [Environment]::SetEnvironmentVariable("EMSDK_PYTHON", $null, "Process")
     [Environment]::SetEnvironmentVariable("JAVA_HOME", $null, "Process")
-    [Environment]::SetEnvironmentVariable("EM_CACHE", $null, "Process")
 
     refreshenv