Skip to content

Commit 3b6c6b8

Browse files
committed
Make emsdk work on Windows with MSYS shell and native Windows Python 2.7
1 parent b5dd78e commit 3b6c6b8

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

emsdk

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ binaryen_git_repo = 'https://github.com/WebAssembly/binaryen.git'
3232
VERBOSE = bool(os.getenv('EMSDK_VERBOSE')) if os.getenv('EMSDK_VERBOSE') != None else False
3333

3434
WINDOWS = False
35-
if os.name == 'nt':
35+
if os.name == 'nt' or 'WINDOWS' in os.getenv('SYSTEMROOT') or 'WINDOWS' in os.getenv('COMSPEC'):
3636
WINDOWS = True
3737
ENVPATH_SEPARATOR = ';'
3838

39+
MSYS = False
40+
if os.getenv('MSYSTEM'):
41+
if os.getenv('MSYSTEM') == 'MSYS':
42+
MSYS = True
43+
else:
44+
print('Warning: MSYSTEM environment variable is present, and is set to "' + os.getenv('MSYSTEM') + '". This shell has not been tested with emsdk and may not work.') # https://stackoverflow.com/questions/37460073/msys-vs-mingw-internal-environment-variables
45+
3946
OSX = False
4047
if platform.mac_ver()[0] != '':
4148
OSX = True
@@ -73,7 +80,7 @@ emscripten_config_directory = os.path.expanduser("~/")
7380
if os.path.exists(os.path.join(emsdk_path(), '.emscripten')):
7481
emscripten_config_directory = emsdk_path()
7582

76-
EMSDK_SET_ENV = 'emsdk_set_env.bat' if WINDOWS else 'emsdk_set_env.sh'
83+
EMSDK_SET_ENV = 'emsdk_set_env.bat' if (WINDOWS and not MSYS) else 'emsdk_set_env.sh'
7784

7885
# Finds the given executable 'program' in PATH. Operates like the Unix tool 'which'.
7986
def which(program):
@@ -910,7 +917,7 @@ def download_and_unzip(zipfile, dest_dir, download_even_if_exists=False, filenam
910917
return untargz(dst_file, dest_dir, unpack_even_if_exists=download_even_if_exists)
911918

912919
def to_native_path(p):
913-
if WINDOWS:
920+
if WINDOWS and not MSYS:
914921
return to_unix_path(p).replace('/', '\\')
915922
else:
916923
return to_unix_path(p)
@@ -1208,11 +1215,14 @@ class Tool:
12081215
for cfg in activated_cfg:
12091216
cfg = cfg.strip()
12101217
(key, value) = parse_key_value(cfg)
1211-
if not key in dot_emscripten: return False
1218+
if not key in dot_emscripten:
1219+
if VERBOSE: print(str(self) + ' is not active, because key="' + key + '" does not exist in .emscripten')
1220+
return False
12121221

12131222
# If running in embedded mode, all paths are stored dynamically relative to the emsdk root, so normalize those first.
12141223
dot_emscripten_key = dot_emscripten[key].replace("' + emsdk_path + '", emsdk_path())
12151224
if dot_emscripten_key != value:
1225+
if VERBOSE: print(str(self) + ' is not active, because key="' + key + '" has value "' + dot_emscripten_key + '" but should have value "' + value + '"')
12161226
return False
12171227
return True
12181228

@@ -1221,6 +1231,7 @@ class Tool:
12211231
if hasattr(self, 'activated_env'):
12221232
(key, value) = parse_key_value(self.activated_environment())
12231233
if not key in os.environ or to_unix_path(os.environ[key]) != to_unix_path(value):
1234+
if VERBOSE: print(str(self) + ' is not active, because environment variable key="' + key + '" has value "' + str(os.getenv(key)) + '" but should have value "' + value + '"')
12241235
return False
12251236
# if WINDOWS:
12261237
# env_var = win_get_active_environment_variable(key)
@@ -1232,6 +1243,7 @@ class Tool:
12321243
for p in path:
12331244
path_items = os.environ['PATH'].replace('\\', '/').split(ENVPATH_SEPARATOR)
12341245
if not normalized_contains(path_items, p):
1246+
if VERBOSE: print(str(self) + ' is not active, because environment variable PATH item "' + p + '" is not present (PATH=' + os.environ['PATH'] + ')')
12351247
return False
12361248
return True
12371249

@@ -1808,13 +1820,20 @@ def normalized_contains(lst, elem):
18081820
return True
18091821
return False
18101822

1823+
def to_msys_path(p):
1824+
p = to_unix_path(p)
1825+
new_path = re.sub(r'([a-zA-Z]):/(.*)', r'/\1/\2', p)
1826+
if len(new_path) > 3 and new_path[0] == '/' and new_path[2] == '/':
1827+
new_path = new_path[0] + new_path[1].lower() + new_path[2:]
1828+
return new_path
1829+
18111830
# Looks at the current PATH and adds and removes entries so that the PATH reflects
18121831
# the set of given active tools.
18131832
def adjusted_path(tools_to_activate, log_additions=False, system_path_only=False):
18141833
# These directories should be added to PATH
18151834
path_add = get_required_path(tools_to_activate)
18161835
# These already exist.
1817-
if WINDOWS:
1836+
if WINDOWS and not MSYS:
18181837
existing_path = win_get_environment_variable('PATH', system=True)
18191838
if not system_path_only:
18201839
current_user_path = win_get_environment_variable('PATH', system=False)
@@ -1830,17 +1849,24 @@ def adjusted_path(tools_to_activate, log_additions=False, system_path_only=False
18301849
elif (system_root + '\\system32\\wbem') in p.lower(): p = '%SystemRoot%\\System32\\Wbem'
18311850
elif (system_root + '\\system32\\windowspowershell\v1.0') in p.lower(): p = '%SystemRoot%\\System32\\WindowsPowerShell\v1.0\\'
18321851
existing_path[i] = p
1833-
18341852
else:
18351853
existing_path = os.environ['PATH'].split(ENVPATH_SEPARATOR)
18361854
emsdk_root_path = to_unix_path(emsdk_path())
18371855

18381856
existing_emsdk_tools = [item for item in existing_path if to_unix_path(item).startswith(emsdk_root_path)]
18391857
new_emsdk_tools = [item for item in path_add if not normalized_contains(existing_emsdk_tools, item)]
1858+
18401859
# Existing non-emsdk tools
18411860
existing_path = [item for item in existing_path if not to_unix_path(item).startswith(emsdk_root_path)]
18421861
new_path = [item for item in path_add if not normalized_contains(existing_path, item)]
1843-
return (ENVPATH_SEPARATOR.join(unique_items(new_path + existing_path)), new_emsdk_tools)
1862+
whole_path = unique_items(new_path + existing_path)
1863+
if MSYS:
1864+
# XXX Hack: If running native Windows Python in MSYS prompt where PATH entries look like "/c/Windows/System32", os.environ['PATH']
1865+
# in Python will transform to show them as "C:\\Windows\\System32", so need to reconvert path delimiter back to forward slashes.
1866+
whole_path = map(to_msys_path, whole_path)
1867+
new_emsdk_tools = map(to_msys_path, new_emsdk_tools)
1868+
1869+
return ((':' if MSYS else ENVPATH_SEPARATOR).join(whole_path), new_emsdk_tools)
18441870

18451871
def construct_env(tools_to_activate, permanent):
18461872
global emscripten_config_directory
@@ -1854,7 +1880,7 @@ def construct_env(tools_to_activate, permanent):
18541880
# else:
18551881

18561882
if os.environ['PATH'] != newpath: # Don't bother setting the path if there are no changes.
1857-
if WINDOWS: env_string += 'SET PATH=' + newpath + '\n'
1883+
if WINDOWS and not MSYS: env_string += 'SET PATH=' + newpath + '\n'
18581884
else: env_string += 'export PATH="' + newpath + '"\n'
18591885
if len(added_path) > 0:
18601886
print('Adding directories to PATH:')
@@ -1886,7 +1912,7 @@ def construct_env(tools_to_activate, permanent):
18861912
if len(env_vars_to_add) > 0:
18871913
print('Setting environment variables:')
18881914
for key, value in env_vars_to_add:
1889-
if WINDOWS:
1915+
if WINDOWS and not MSYS:
18901916
if permanent:
18911917
env_string += 'SETX ' + key + ' "' + value + '"\n'
18921918
else:

emsdk_env.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fi
2222
pushd `dirname "$SRC"` > /dev/null
2323
unset SRC
2424

25-
./emsdk construct_env
25+
./emsdk construct_env "$@"
2626
. ./emsdk_set_env.sh
2727

2828
popd > /dev/null

0 commit comments

Comments
 (0)