Avoid writing to file in emsdk_env.csh / emsdk_env.sh (#544)

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

Tested with bash, tcsh and fish.
diff --git a/.gitignore b/.gitignore
index 75afb97..db399b8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,9 +9,8 @@
 /.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
diff --git a/emsdk.py b/emsdk.py
index 61c7e39..197f370 100755
--- a/emsdk.py
+++ b/emsdk.py
@@ -96,7 +96,6 @@
 else:
   ENVPATH_SEPARATOR = ':'
 
-
 ARCH = 'unknown'
 # platform.machine() may return AMD64 on windows, so standardize the case.
 machine = platform.machine().lower()
@@ -170,18 +169,7 @@
 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')
 
@@ -2479,6 +2467,11 @@
                      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.
@@ -2503,7 +2496,7 @@
   # 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:
@@ -2599,6 +2592,10 @@
   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)
@@ -2610,17 +2607,17 @@
     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()))]
@@ -2649,19 +2646,19 @@
       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
 
 
@@ -3023,19 +3020,20 @@
   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()
diff --git a/emsdk_env.csh b/emsdk_env.csh
old mode 100755
new mode 100644
index 7987a3b..187d4c5
--- a/emsdk_env.csh
+++ b/emsdk_env.csh
@@ -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
@@ -18,16 +16,15 @@
 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
diff --git a/emsdk_env.fish b/emsdk_env.fish
old mode 100755
new mode 100644
index e4aea06..29f6f46
--- a/emsdk_env.fish
+++ b/emsdk_env.fish
@@ -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
diff --git a/emsdk_env.sh b/emsdk_env.sh
old mode 100755
new mode 100644
index 9c0a5c7..d78dfff
--- a/emsdk_env.sh
+++ b/emsdk_env.sh
@@ -22,6 +22,5 @@
 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