Fix vswhere search for msbuild on Python 3, and demote pywin32 as optional when only enabling emsdk for current command prompt instance. Fixes #177

This commit is contained in:
Jukka Jylänki
2018-10-23 14:52:40 +03:00
parent 0d8576c0e8
commit 7a0e27441e

37
emsdk
View File

@@ -118,7 +118,7 @@ def vswhere(version):
# Visual Studio 2017 Express is not included in the above search, and it does not have the VC.Tools.x86.x64 tool, so do a catch-all attempt as a fallback, to detect Express version.
if len(output) == 0:
output = json.loads(subprocess.check_output([vswhere_path, '-latest', '-version', '[%s.0,%s.0)' % (version, version + 1), '-products', '*', '-property', 'installationPath', '-format', 'json']))
return output[0]['installationPath'].encode('ascii') if len(output) > 0 else ''
return str(output[0]['installationPath']) if len(output) > 0 else ''
except Exception as e:
return ''
@@ -174,6 +174,14 @@ def remove_tree(d):
if VERBOSE: print('remove_tree threw an exception, ignoring: ' + str(e))
pass
def import_pywin32():
if WINDOWS:
try:
import win32api, win32con
except Exception as e:
print('Failed to import Python Windows extensions win32api and win32con. Make sure you are using the version of python available in emsdk, or install PyWin extensions to the distribution of Python you are attempting to use. (This script was launched in python instance from "' + sys.executable + '")')
sys.exit(1)
def win_set_environment_variable_direct(key, value, system=True):
prev_path = os.environ['PATH']
try:
@@ -181,7 +189,7 @@ def win_set_environment_variable_direct(key, value, system=True):
if py:
py_path = to_native_path(py.expand_vars(py.activated_path))
os.environ['PATH'] = os.environ['PATH'] + ';' + py_path
import win32api, win32con
import_pywin32()
if system: # Read globally from ALL USERS section.
folder = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment', 0, win32con.KEY_ALL_ACCESS)
else: # Register locally from CURRENT USER section.
@@ -209,12 +217,17 @@ def win_get_environment_variable(key, system=True):
if py:
py_path = to_native_path(py.expand_vars(py.activated_path))
os.environ['PATH'] = os.environ['PATH'] + ';' + py_path
import win32api, win32con
if system: # Read globally from ALL USERS section.
folder = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment')
else: # Register locally from CURRENT USER section.
folder = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, 'Environment')
value = str(win32api.RegQueryValueEx(folder, key)[0])
try:
import win32api, win32con
if system: # Read globally from ALL USERS section.
folder = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment')
else: # Register locally from CURRENT USER section.
folder = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, 'Environment')
value = str(win32api.RegQueryValueEx(folder, key)[0])
except Exception as e:
# PyWin32 is not available - read via os.environ. This has the drawback that expansion items such as %PROGRAMFILES% will have been expanded, so
# need to be precise not to set these back to system registry, or expansion items would be lost.
return os.environ[key]
except Exception as e:
if e[0] != 2: # 'The system cannot find the file specified.'
print('Failed to read environment variable ' + key + ':', file=sys.stderr)
@@ -650,7 +663,7 @@ def build_env(generator):
elif 'Visual Studio 15' in generator:
path = vswhere(15)
build_env['VCTargetsPath'] = os.path.join(path, 'Common7\IDE\VC\VCTargets')
build_env['VCTargetsPath'] = os.path.join(path, 'Common7\\IDE\\VC\\VCTargets')
# CMake and VS2017 cl.exe needs to have mspdb140.dll et al. in its PATH.
vc_bin_paths = [vs_filewhere(path, 'amd64', 'cl.exe'),
@@ -700,6 +713,8 @@ def find_msbuild(sln_file):
os.path.join(os.environ['ProgramFiles(x86)'], 'MSBuild/12.0/Bin')]
search_paths_old = [os.path.join(os.environ["WINDIR"], 'Microsoft.NET/Framework/v4.0.30319')]
generator = get_generator_for_sln_file(sln_file)
if VERBOSE:
print('find_msbuild looking for generator ' + str(generator))
if generator == 'Visual Studio 15':
path = vswhere(15)
search_paths = [os.path.join(path, 'MSBuild/15.0/Bin/amd64'),
@@ -713,6 +728,8 @@ def find_msbuild(sln_file):
for path in search_paths:
p = os.path.join(path, 'MSBuild.exe')
if VERBOSE:
print('Searching for MSBuild.exe: ' + p)
if os.path.isfile(p): return p
if VERBOSE:
print('MSBuild.exe in PATH? ' + str(which('MSBuild.exe')))
@@ -730,7 +747,7 @@ def make_build(build_root, build_type, build_target_platform='x64'):
if WINDOWS:
if 'Visual Studio' in CMAKE_GENERATOR:
solution_name = subprocess.check_output(['dir', '/b', '*.sln'], shell=True, cwd=build_root).strip()
solution_name = str(subprocess.check_output(['dir', '/b', '*.sln'], shell=True, cwd=build_root).decode('utf-8').strip())
generator_to_use = get_generator_for_sln_file(os.path.join(build_root, solution_name))
# Disabled for now: Don't pass /maxcpucount argument to msbuild, since it looks like when building, msbuild already automatically spawns the full amount of logical
# cores the system has, and passing the number of logical cores here has been observed to give a quadratic N*N explosion on the number of spawned processes