2017-10-03 05:24:00 +08:00
|
|
|
import os
|
2017-10-25 04:47:27 +08:00
|
|
|
import platform
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2020-03-30 14:28:32 +08:00
|
|
|
if os.name == "nt":
|
2017-10-05 05:21:32 +08:00
|
|
|
import sys
|
2020-03-25 21:36:03 +08:00
|
|
|
import winreg
|
2017-10-03 05:24:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _reg_open_key(key, subkey):
|
|
|
|
try:
|
|
|
|
return winreg.OpenKey(key, subkey)
|
2020-09-02 22:58:07 +08:00
|
|
|
except OSError:
|
2020-03-30 14:28:32 +08:00
|
|
|
if platform.architecture()[0] == "32bit":
|
2017-10-03 05:24:00 +08:00
|
|
|
bitness_sam = winreg.KEY_WOW64_64KEY
|
|
|
|
else:
|
|
|
|
bitness_sam = winreg.KEY_WOW64_32KEY
|
|
|
|
return winreg.OpenKey(key, subkey, 0, winreg.KEY_READ | bitness_sam)
|
|
|
|
|
|
|
|
|
2017-10-25 04:47:27 +08:00
|
|
|
def _reg_open_key_bits(key, subkey, bits):
|
|
|
|
sam = winreg.KEY_READ
|
|
|
|
|
2020-03-30 14:28:32 +08:00
|
|
|
if platform.architecture()[0] == "32bit":
|
|
|
|
if bits == "64":
|
2017-10-25 04:47:27 +08:00
|
|
|
# Force 32bit process to search in 64bit registry
|
|
|
|
sam |= winreg.KEY_WOW64_64KEY
|
|
|
|
else:
|
2020-03-30 14:28:32 +08:00
|
|
|
if bits == "32":
|
2017-10-25 04:47:27 +08:00
|
|
|
# Force 64bit process to search in 32bit registry
|
|
|
|
sam |= winreg.KEY_WOW64_32KEY
|
|
|
|
|
|
|
|
return winreg.OpenKey(key, subkey, 0, sam)
|
|
|
|
|
|
|
|
|
|
|
|
def _find_mono_in_reg(subkey, bits):
|
2017-10-03 05:24:00 +08:00
|
|
|
try:
|
2017-10-25 04:47:27 +08:00
|
|
|
with _reg_open_key_bits(winreg.HKEY_LOCAL_MACHINE, subkey, bits) as hKey:
|
2020-03-30 14:28:32 +08:00
|
|
|
value = winreg.QueryValueEx(hKey, "SdkInstallRoot")[0]
|
2017-10-03 05:24:00 +08:00
|
|
|
return value
|
2020-09-02 22:58:07 +08:00
|
|
|
except OSError:
|
2017-10-03 05:24:00 +08:00
|
|
|
return None
|
|
|
|
|
2017-10-25 04:47:27 +08:00
|
|
|
|
|
|
|
def _find_mono_in_reg_old(subkey, bits):
|
2017-10-03 05:24:00 +08:00
|
|
|
try:
|
2017-10-25 04:47:27 +08:00
|
|
|
with _reg_open_key_bits(winreg.HKEY_LOCAL_MACHINE, subkey, bits) as hKey:
|
2020-03-30 14:28:32 +08:00
|
|
|
default_clr = winreg.QueryValueEx(hKey, "DefaultCLR")[0]
|
2017-10-03 05:24:00 +08:00
|
|
|
if default_clr:
|
2020-03-30 14:28:32 +08:00
|
|
|
return _find_mono_in_reg(subkey + "\\" + default_clr, bits)
|
2017-10-03 05:24:00 +08:00
|
|
|
return None
|
2020-09-02 22:58:07 +08:00
|
|
|
except OSError:
|
2017-10-03 05:24:00 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2017-10-25 04:47:27 +08:00
|
|
|
def find_mono_root_dir(bits):
|
2020-03-30 14:28:32 +08:00
|
|
|
root_dir = _find_mono_in_reg(r"SOFTWARE\Mono", bits)
|
2017-10-25 04:47:27 +08:00
|
|
|
if root_dir is not None:
|
2018-06-26 00:38:03 +08:00
|
|
|
return str(root_dir)
|
2020-03-30 14:28:32 +08:00
|
|
|
root_dir = _find_mono_in_reg_old(r"SOFTWARE\Novell\Mono", bits)
|
2017-10-25 04:47:27 +08:00
|
|
|
if root_dir is not None:
|
2018-06-26 00:38:03 +08:00
|
|
|
return str(root_dir)
|
2020-03-30 14:28:32 +08:00
|
|
|
return ""
|
2017-10-03 05:24:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
def find_msbuild_tools_path_reg():
|
2017-10-25 04:47:27 +08:00
|
|
|
import subprocess
|
|
|
|
|
2020-03-30 14:28:32 +08:00
|
|
|
vswhere = os.getenv("PROGRAMFILES(X86)")
|
2017-10-25 04:47:27 +08:00
|
|
|
if not vswhere:
|
2020-03-30 14:28:32 +08:00
|
|
|
vswhere = os.getenv("PROGRAMFILES")
|
|
|
|
vswhere += r"\Microsoft Visual Studio\Installer\vswhere.exe"
|
2017-10-25 04:47:27 +08:00
|
|
|
|
2020-03-30 14:28:32 +08:00
|
|
|
vswhere_args = ["-latest", "-products", "*", "-requires", "Microsoft.Component.MSBuild"]
|
2017-10-25 04:47:27 +08:00
|
|
|
|
2017-10-03 05:24:00 +08:00
|
|
|
try:
|
2017-10-25 04:47:27 +08:00
|
|
|
lines = subprocess.check_output([vswhere] + vswhere_args).splitlines()
|
|
|
|
|
|
|
|
for line in lines:
|
2020-03-30 14:28:32 +08:00
|
|
|
parts = line.decode("utf-8").split(":", 1)
|
2017-10-25 04:47:27 +08:00
|
|
|
|
2020-03-30 14:28:32 +08:00
|
|
|
if len(parts) < 2 or parts[0] != "installationPath":
|
2017-10-25 04:47:27 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
val = parts[1].strip()
|
|
|
|
|
|
|
|
if not val:
|
2020-03-30 14:28:32 +08:00
|
|
|
raise ValueError("Value of `installationPath` entry is empty")
|
2017-10-25 04:47:27 +08:00
|
|
|
|
2019-04-06 07:32:41 +08:00
|
|
|
# Since VS2019, the directory is simply named "Current"
|
2020-03-30 14:28:32 +08:00
|
|
|
msbuild_dir = os.path.join(val, "MSBuild\\Current\\Bin")
|
2019-04-06 07:32:41 +08:00
|
|
|
if os.path.isdir(msbuild_dir):
|
|
|
|
return msbuild_dir
|
|
|
|
|
|
|
|
# Directory name "15.0" is used in VS 2017
|
2020-03-30 14:28:32 +08:00
|
|
|
return os.path.join(val, "MSBuild\\15.0\\Bin")
|
2017-10-25 04:47:27 +08:00
|
|
|
|
2020-03-30 14:28:32 +08:00
|
|
|
raise ValueError("Cannot find `installationPath` entry")
|
2017-10-25 04:47:27 +08:00
|
|
|
except ValueError as e:
|
2020-03-30 14:28:32 +08:00
|
|
|
print("Error reading output from vswhere: " + e.message)
|
2020-09-02 22:58:07 +08:00
|
|
|
except OSError:
|
2020-03-30 14:28:32 +08:00
|
|
|
pass # Fine, vswhere not found
|
2017-10-30 00:55:05 +08:00
|
|
|
except (subprocess.CalledProcessError, OSError):
|
2017-10-25 04:47:27 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
# Try to find 14.0 in the Registry
|
|
|
|
|
|
|
|
try:
|
2020-03-30 14:28:32 +08:00
|
|
|
subkey = r"SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0"
|
2017-10-25 04:47:27 +08:00
|
|
|
with _reg_open_key(winreg.HKEY_LOCAL_MACHINE, subkey) as hKey:
|
2020-03-30 14:28:32 +08:00
|
|
|
value = winreg.QueryValueEx(hKey, "MSBuildToolsPath")[0]
|
2017-10-03 05:24:00 +08:00
|
|
|
return value
|
2020-09-02 22:58:07 +08:00
|
|
|
except OSError:
|
2020-03-30 14:28:32 +08:00
|
|
|
return ""
|