@@ -2711,43 +2711,59 @@ def construct_env(tools_to_activate, system, user):
2711
2711
return construct_env_with_vars (get_env_vars_to_add (tools_to_activate , system , user ))
2712
2712
2713
2713
2714
+ def unset_env (key ):
2715
+ if POWERSHELL :
2716
+ return 'Remove-Item env:%s\n ' % key
2717
+ if CMD :
2718
+ return 'set %s=\n ' % key
2719
+ if CSH :
2720
+ return 'unsetenv %s;\n ' % key
2721
+ if BASH :
2722
+ return 'unset %s;\n ' % key
2723
+ assert False
2724
+
2725
+
2714
2726
def construct_env_with_vars (env_vars_to_add ):
2715
2727
env_string = ''
2716
2728
if env_vars_to_add :
2717
2729
errlog ('Setting environment variables:' )
2718
2730
2719
2731
for key , value in env_vars_to_add :
2720
2732
# Don't set env vars which are already set to the correct value.
2721
- if key not in os .environ or to_unix_path (os .environ [key ]) != to_unix_path (value ):
2722
- errlog (key + ' = ' + value )
2723
- if POWERSHELL :
2724
- env_string += '$env:' + key + '="' + value + '"\n '
2725
- elif CMD :
2726
- env_string += 'SET ' + key + '=' + value + '\n '
2727
- elif CSH :
2728
- env_string += 'setenv ' + key + ' "' + value + '"\n '
2729
- elif BASH :
2730
- env_string += 'export ' + key + '="' + value + '"\n '
2731
- else :
2732
- assert False
2733
- if 'EMSDK_PYTHON' in env_vars_to_add :
2734
- # When using our bundled python we never want the user's
2735
- # PYTHONHOME or PYTHONPATH
2736
- # See https://github.com/emscripten-core/emsdk/issues/598
2737
- if POWERSHELL :
2738
- env_string += 'Remove-Item env:PYTHONHOME\n '
2739
- env_string += 'Remove-Item env:PYTHONPATH\n '
2740
- elif CMD :
2741
- env_string += 'set PYTHONHOME=\n '
2742
- env_string += 'set PYTHONPATH=\n '
2743
- elif CSH :
2744
- env_string += 'unsetenv PYTHONHOME\n '
2745
- env_string += 'unsetenv PYTHONPATH\n '
2746
- elif BASH :
2747
- env_string += 'unset PYTHONHOME\n '
2748
- env_string += 'unset PYTHONPATH\n '
2749
- else :
2750
- assert False
2733
+ if key in os .environ and to_unix_path (os .environ [key ]) == to_unix_path (value ):
2734
+ continue
2735
+ errlog (key + ' = ' + value )
2736
+ if POWERSHELL :
2737
+ env_string += '$env:' + key + '="' + value + '"\n '
2738
+ elif CMD :
2739
+ env_string += 'SET ' + key + '=' + value + '\n '
2740
+ elif CSH :
2741
+ env_string += 'setenv ' + key + ' "' + value + '";\n '
2742
+ elif BASH :
2743
+ env_string += 'export ' + key + '="' + value + '";\n '
2744
+ else :
2745
+ assert False
2746
+
2747
+ if 'EMSDK_PYTHON' in env_vars_to_add :
2748
+ # When using our bundled python we never want the user's
2749
+ # PYTHONHOME or PYTHONPATH
2750
+ # See https://github.com/emscripten-core/emsdk/issues/598
2751
+ env_string += unset_env ('PYTHONHOME' )
2752
+ env_string += unset_env ('PYTHONPATH' )
2753
+
2754
+ # Remove any environment variables that might have been set by old or
2755
+ # inactive tools/sdks. For example, we set EM_CACHE for older versions
2756
+ # of the SDK but we want to remove that from the current environment
2757
+ # if no such tool is active.
2758
+ # Ignore certain keys that are inputs to emsdk itself.
2759
+ ignore_keys = set (['EMSDK_POWERSHELL' , 'EMSDK_CSH' , 'EMSDK_CMD' , 'EMSDK_BASH' ,
2760
+ 'EMSDK_NUM_CORES' , 'EMSDK_TTY' ])
2761
+ env_keys_to_add = set (pair [0 ] for pair in env_vars_to_add )
2762
+ for key in os .environ :
2763
+ if key .startswith ('EMSDK_' ) or key .startswith ('EM_' ):
2764
+ if key not in env_keys_to_add and key not in ignore_keys :
2765
+ errlog ('Clearing existing environment variable: %s' % key )
2766
+ env_string += unset_env (key )
2751
2767
2752
2768
return env_string
2753
2769
0 commit comments