77from distutils .command .install_lib import install_lib
88from distutils .command .install_data import install_data
99from distutils .sysconfig import get_config_var
10+ from distutils import log
1011from platform import architecture
1112from subprocess import Popen , CalledProcessError , PIPE , check_call
1213from glob import glob
@@ -27,67 +28,72 @@ def _find_msbuild_tool(tool="msbuild.exe", use_windows_sdk=False):
2728 except ImportError :
2829 import winreg as _winreg
2930
31+ keys_to_check = []
3032 if use_windows_sdk :
31- if sys .version_info [:2 ] == (2 ,7 ):
32- locappdir = os .environ ["LOCALAPPDATA" ]
33- vcpy27 = (r"Programs\Common\Microsoft"
34- r"\Visual C++ for Python\9.0\WinSDK\Bin" )
35- if PLATFORM == "x86" :
36- mtpath = os .path .join (
37- locappdir , vcpy27 , r"mt.exe" )
38- elif PLATFORM == "x64" :
39- mtpath = os .path .join (
40- locappdir , vcpy27 , r"x64\mt.exe" )
41- if os .path .exists (mtpath ):
42- return mtpath
43- value_name = "InstallationFolder"
44- sdk_name = "Windows SDK"
45- keys_to_check = [
46- r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1A\WinSDK-Win32Tools" ,
47- r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKWin32Tools" ,
48- r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0A\WinSDK-Win32Tools" ,
49- r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0\WinSDKWin32Tools" ,
50- r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v6.0A\WinSDKWin32Tools" ,
51- ]
33+ sdks_root = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows"
34+ kits_root = r"SOFTWARE\Microsoft\Windows Kits\Installed Roots"
35+ kits_suffix = "bin"
36+ if PLATFORM == "x64" :
37+ kits_suffix += r"\x64"
38+ keys_to_check .extend ([
39+ ("Windows Kit 10.0" , kits_root , "KitsRoot10" , kits_suffix ),
40+ ("Windows Kit 8.1" , kits_root , "KitsRoot81" , kits_suffix ),
41+ ("Windows Kit 8.0" , kits_root , "KitsRoot" , kits_suffix ),
42+ ("Windows SDK 7.1A" , sdks_root + r"\v7.1A\WinSDK-Win32Tools" , "InstallationFolder" ),
43+ ("Windows SDK 7.1" , sdks_root + r"\v7.1\WinSDKWin32Tools" , "InstallationFolder" ),
44+ ("Windows SDK 7.0A" , sdks_root + r"\v7.0A\WinSDK-Win32Tools" , "InstallationFolder" ),
45+ ("Windows SDK 7.0" , sdks_root + r"\v7.0\WinSDKWin32Tools" , "InstallationFolder" ),
46+ ("Windows SDK 6.0A" , sdks_root + r"\v6.0A\WinSDKWin32Tools" , "InstallationFolder" )
47+ ])
5248 else :
53- value_name = "MSBuildToolsPath"
54- sdk_name = "MSBuild"
55- keys_to_check = [
56- r"SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" ,
57- r"SOFTWARE\Microsoft\MSBuild\ToolsVersions\12.0" ,
58- r"SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0" ,
59- r"SOFTWARE\Microsoft\MSBuild\ToolsVersions\3.5" ,
60- r"SOFTWARE\Microsoft\MSBuild\ToolsVersions\2.0"
61- ]
62-
49+ vs_root = r"SOFTWARE\Microsoft\MSBuild\ToolsVersions"
50+ keys_to_check .extend ([
51+ ("MSBuild 14" , vs_root + r"\14.0" , "MSBuildToolsPath" ),
52+ ("MSBuild 12" , vs_root + r"\12.0" , "MSBuildToolsPath" ),
53+ ("MSBuild 4" , vs_root + r"\4.0" , "MSBuildToolsPath" ),
54+ ("MSBuild 3.5" , vs_root + r"\3.5" , "MSBuildToolsPath" ),
55+ ("MSBuild 2.0" , vs_root + r"\2.0" , "MSBuildToolsPath" )
56+ ])
57+
58+ # read the possible tools paths from the various registry locations
59+ paths_to_check = []
6360 hreg = _winreg .ConnectRegistry (None , _winreg .HKEY_LOCAL_MACHINE )
6461 try :
6562 hkey = None
66- for key in keys_to_check :
63+ for key_to_check in keys_to_check :
64+ sdk_name , key , value_name = key_to_check [:3 ]
65+ suffix = key_to_check [3 ] if len (key_to_check ) > 3 else None
6766 try :
6867 hkey = _winreg .OpenKey (hreg , key )
69- break
68+ val , type_ = _winreg .QueryValueEx (hkey , value_name )
69+ if type_ != _winreg .REG_SZ :
70+ continue
71+ if suffix :
72+ val = os .path .join (val , suffix )
73+ paths_to_check .append ((sdk_name , val ))
7074 except WindowsError :
7175 pass
72-
73- if hkey is None :
74- raise RuntimeError ("%s could not be found" % sdk_name )
75-
76- try :
77- val , type_ = _winreg .QueryValueEx (hkey , value_name )
78- if type_ != _winreg .REG_SZ :
79- raise RuntimeError ("%s could not be found" % sdk_name )
80-
81- path = os .path .join (val , tool )
82- if os .path .exists (path ):
83- return path
84- finally :
85- hkey .Close ()
76+ finally :
77+ hkey .Close ()
8678 finally :
8779 hreg .Close ()
8880
81+ # Add Visual C++ for Python as a fallback in case one of the other Windows SDKs isn't installed
82+ if use_windows_sdk :
83+ localappdata = os .environ ["LOCALAPPDATA" ]
84+ pywinsdk = localappdata + r"\Programs\Common\Microsoft\Visual C++ for Python\9.0\WinSDK\Bin"
85+ if PLATFORM == "x64" :
86+ pywinsdk += r"\x64"
87+ paths_to_check .append (("Visual C++ for Python" , pywinsdk ))
88+
89+ for sdk_name , path in paths_to_check :
90+ path = os .path .join (path , tool )
91+ if os .path .exists (path ):
92+ log .info ("Using %s from %s" % (tool , sdk_name ))
93+ return path
94+
8995 raise RuntimeError ("%s could not be found" % tool )
90-
96+
9197
9298if DEVTOOLS == "MsDev" :
9399 _xbuild = "\" %s\" " % _find_msbuild_tool ("msbuild.exe" )
0 commit comments