| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
|
| 4 |
import ctypes.wintypes |
| 5 |
import os |
| 6 |
import re |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
from functools import lru_cache |
| 10 |
from glob import glob |
| 11 |
|
| 12 |
CSIDL_PROGRAM_FILES = 38 |
| 13 |
CSIDL_PROGRAM_FILESX86 = 42 |
| 14 |
|
| 15 |
|
| 16 |
@lru_cache |
| 17 |
def get_program_files_location(which=CSIDL_PROGRAM_FILESX86): |
| 18 |
SHGFP_TYPE_CURRENT = 0 |
| 19 |
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH) |
| 20 |
ctypes.windll.shell32.SHGetFolderPathW(0, which, 0, SHGFP_TYPE_CURRENT, buf) |
| 21 |
return buf.value |
| 22 |
|
| 23 |
|
| 24 |
@lru_cache |
| 25 |
def find_vswhere(): |
| 26 |
for which in (CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES): |
| 27 |
root = get_program_files_location(which) |
| 28 |
vswhere = os.path.join(root, 'Microsoft Visual Studio', 'Installer', 'vswhere.exe') |
| 29 |
if os.path.exists(vswhere): |
| 30 |
return vswhere |
| 31 |
raise SystemExit('Could not find vswhere.exe') |
| 32 |
|
| 33 |
|
| 34 |
def get_output(*cmd): |
| 35 |
return subprocess.check_output(cmd, encoding='mbcs', errors='strict') |
| 36 |
|
| 37 |
|
| 38 |
@lru_cache |
| 39 |
def find_visual_studio(): |
| 40 |
path = get_output( |
| 41 |
find_vswhere(), '-latest', '-requires', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64', '-property', 'installationPath', '-products', '*' |
| 42 |
).strip() |
| 43 |
return os.path.join(path, 'VC', 'Auxiliary', 'Build') |
| 44 |
|
| 45 |
|
| 46 |
@lru_cache |
| 47 |
def find_msbuild(): |
| 48 |
base_path = get_output(find_vswhere(), '-latest', '-requires', 'Microsoft.Component.MSBuild', '-property', 'installationPath').strip() |
| 49 |
return glob(os.path.join(base_path, 'MSBuild', '*', 'Bin', 'MSBuild.exe'))[0] |
| 50 |
|
| 51 |
|
| 52 |
def find_vcvarsall(): |
| 53 |
productdir = find_visual_studio() |
| 54 |
vcvarsall = os.path.join(productdir, 'vcvarsall.bat') |
| 55 |
if os.path.isfile(vcvarsall): |
| 56 |
return vcvarsall |
| 57 |
raise SystemExit('Unable to find vcvarsall.bat in productdir: ' + productdir) |
| 58 |
|
| 59 |
|
| 60 |
def remove_dups(variable): |
| 61 |
old_list = variable.split(os.pathsep) |
| 62 |
new_list = [] |
| 63 |
for i in old_list: |
| 64 |
if i not in new_list: |
| 65 |
new_list.append(i) |
| 66 |
return os.pathsep.join(new_list) |
| 67 |
|
| 68 |
|
| 69 |
def query_process(cmd, is64bit): |
| 70 |
if is64bit and 'PROGRAMFILES(x86)' not in os.environ: |
| 71 |
os.environ['PROGRAMFILES(x86)'] = get_program_files_location() |
| 72 |
result = {} |
| 73 |
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 74 |
try: |
| 75 |
stdout, stderr = popen.communicate() |
| 76 |
if popen.wait() != 0: |
| 77 |
raise RuntimeError(stderr.decode('mbcs')) |
| 78 |
|
| 79 |
stdout = stdout.decode('mbcs') |
| 80 |
for line in stdout.splitlines(): |
| 81 |
if '=' not in line: |
| 82 |
continue |
| 83 |
line = line.strip() |
| 84 |
key, value = line.split('=', 1) |
| 85 |
key = key.lower() |
| 86 |
if key == 'path': |
| 87 |
if value.endswith(os.pathsep): |
| 88 |
value = value[:-1] |
| 89 |
value = remove_dups(value) |
| 90 |
result[key] = value |
| 91 |
|
| 92 |
finally: |
| 93 |
popen.stdout.close() |
| 94 |
popen.stderr.close() |
| 95 |
return result |
| 96 |
|
| 97 |
|
| 98 |
@lru_cache |
| 99 |
def query_vcvarsall(is64bit=True): |
| 100 |
plat = 'amd64' if is64bit else 'amd64_x86' |
| 101 |
vcvarsall = find_vcvarsall() |
| 102 |
env = query_process(f'"{vcvarsall}" {plat} & set', is64bit) |
| 103 |
pat = re.compile(r'vs(\d+)comntools', re.I) |
| 104 |
|
| 105 |
comn_tools = {} |
| 106 |
|
| 107 |
for k in env: |
| 108 |
m = pat.match(k) |
| 109 |
if m is not None: |
| 110 |
comn_tools[k] = int(m.group(1)) |
| 111 |
comntools = max(comn_tools, key=comn_tools.__getitem__) |
| 112 |
|
| 113 |
def g(k): |
| 114 |
try: |
| 115 |
return env[k] |
| 116 |
except KeyError: |
| 117 |
try: |
| 118 |
return env[k.lower()] |
| 119 |
except KeyError: |
| 120 |
for k, v in env.items(): |
| 121 |
print(f'{k}={v}', file=sys.stderr) |
| 122 |
raise |
| 123 |
|
| 124 |
return { |
| 125 |
k: g(k) |
| 126 |
for k in ( |
| 127 |
'PATH LIB INCLUDE LIBPATH WINDOWSSDKDIR' |
| 128 |
f' {comntools} PLATFORM' |
| 129 |
' UCRTVERSION UNIVERSALCRTSDKDIR VCTOOLSVERSION WINDOWSSDKDIR' |
| 130 |
' WINDOWSSDKVERSION WINDOWSSDKVERBINPATH WINDOWSSDKBINPATH' |
| 131 |
' VISUALSTUDIOVERSION VSCMD_ARG_HOST_ARCH VSCMD_ARG_TGT_ARCH' |
| 132 |
).split() |
| 133 |
} |