Skip to content

Commit

Permalink
Merge pull request #1753 from dkaszews/fix-vswhere-build-tools
Browse files Browse the repository at this point in the history
Fix vswhere for Build Tools
  • Loading branch information
mergify[bot] authored Aug 12, 2024
2 parents 74a5e71 + 6f1f4f8 commit 18af82a
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,39 @@ def Exit( self ):
'or use your system Clangd. '
'See the YCM docs for details on how to use a custom Clangd.' )

ACCEPTABLE_MSVC_VERSIONS = [ 17, 16, 15 ]

def FindLatestMSVC( quiet, preview=False ):
ACCEPTABLE_VERSIONS = [ 17, 16, 15 ]

def UseVsWhere( quiet, vswhere_args ):
if not quiet:
print( "Calling", *vswhere_args )
latest_full_v = subprocess.check_output( vswhere_args ).strip().decode()
if '.' in latest_full_v:
try:
latest_v = int( latest_full_v.split( '.' )[ 0 ] )
except ValueError:
raise ValueError( f"{ latest_full_v } is not a version number." )

if not quiet:
print( f'vswhere -latest returned version { latest_full_v }' )

if latest_v not in ACCEPTABLE_MSVC_VERSIONS:
if latest_v > 17:
if not quiet:
print( f'MSVC Version { latest_full_v } is newer than expected.' )
else:
raise ValueError(
f'vswhere returned { latest_full_v } which is unexpected.'
'Pass --msvc <version> argument.' )
return latest_v
else:
if not quiet:
print( f'vswhere returned nothing usable: "{ latest_full_v }"' )

return None


def FindLatestMSVC( quiet, preview=False ):
VSWHERE_EXE = os.path.join( os.environ[ 'ProgramFiles(x86)' ],
'Microsoft Visual Studio',
'Installer', 'vswhere.exe' )
Expand All @@ -127,30 +156,16 @@ def FindLatestMSVC( quiet, preview=False ):
'-latest', '-property', 'installationVersion' ]
if preview:
vswhere_args.append( '-prerelease' )

if msvc := UseVsWhere( quiet, vswhere_args ):
return msvc

if not quiet:
print( "Calling", *vswhere_args )
latest_full_v = subprocess.check_output( vswhere_args ).strip().decode()
if '.' in latest_full_v:
try:
latest_v = int( latest_full_v.split( '.' )[ 0 ] )
except ValueError:
raise ValueError( f"{ latest_full_v } is not a version number." )
print( 'Retrying vswhere for Build Tools' )

if not quiet:
print( f'vswhere -latest returned version { latest_full_v }' )

if latest_v not in ACCEPTABLE_VERSIONS:
if latest_v > 17:
if not quiet:
print( f'MSVC Version { latest_full_v } is newer than expected.' )
else:
raise ValueError(
f'vswhere returned { latest_full_v } which is unexpected.'
'Pass --msvc <version> argument.' )
return latest_v
else:
if not quiet:
print( f'vswhere returned nothing usable, { latest_full_v }' )
vswhere_args += [ '-products', 'Microsoft.VisualStudio.Product.BuildTools' ]
if msvc := UseVsWhere( quiet, vswhere_args ):
return msvc

# Fall back to registry parsing, which works at least until MSVC 2019 (16)
# but is likely failing on MSVC 2022 (17)
Expand All @@ -160,7 +175,7 @@ def FindLatestMSVC( quiet, preview=False ):
import winreg
handle = winreg.ConnectRegistry( None, winreg.HKEY_LOCAL_MACHINE )
msvc = None
for i in ACCEPTABLE_VERSIONS:
for i in ACCEPTABLE_MSVC_VERSIONS:
if not quiet:
print( 'Trying to find '
rf'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\{ i }.0' )
Expand Down

0 comments on commit 18af82a

Please sign in to comment.