Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use vswhere to find Visual C++ in Windows #1057

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions src/build_tools/vs_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,23 @@
import pathlib
import subprocess
import sys
from typing import Union
from typing import Union, Optional

def find_vswhere() -> Optional[pathlib.Path]:
path = pathlib.Path(r'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s use %ProgramFiles(x86)%.

return path if path.exists() else None

def get_vcvarsall_from_vswhere(vswhere_path: pathlib.Path) -> Optional[pathlib.Path]:
result = subprocess.run([str(vswhere_path), '-find', 'VC/Auxiliary/Build/vcvarsall.bat', '-utf8'], stdout=subprocess.PIPE)
first = result.stdout.splitlines()[0].decode()
if first == '':
return None
return pathlib.Path(first)

def get_display_name_from_vswhere(vswhere_path: pathlib.Path) -> Optional[str]:
result = subprocess.run([str(vswhere_path), '-property', 'displayName', '-utf8'], stdout=subprocess.PIPE)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless it’s absolutely necessary, I prefer to set shell=False to avoid any unnecessary complexities coming from cmd.exe.

first = result.stdout.splitlines()[0].decode()
return None if first == '' else first

def get_vcvarsall(path_hint: Union[str, None] = None) -> pathlib.Path:
"""Returns the path of 'vcvarsall.bat'.
Expand All @@ -52,14 +67,24 @@ def get_vcvarsall(path_hint: Union[str, None] = None) -> pathlib.Path:
path = pathlib.Path(path_hint).resolve()
if path.exists():
return path

for program_files in ['Program Files', 'Program Files (x86)']:
for edition in ['Community', 'Professional', 'Enterprise', 'BuildTools']:
vcvarsall = pathlib.Path('C:\\', program_files, 'Microsoft Visual Studio',
'2022', edition, 'VC', 'Auxiliary', 'Build',
'vcvarsall.bat')
if vcvarsall.exists():
return vcvarsall

if (vswhere_path := find_vswhere()) is not None:
if (vcvarsall_from_vswhere := get_vcvarsall_from_vswhere(vswhere_path)) is not None:
return vcvarsall_from_vswhere

raise FileNotFoundError(
'Could not find vcvarsall.bat. '
'Install "Desktop development with C++" Workload on' + (get_display_name_from_vswhere(vswhere_path) or 'Visual Studio')
+ ' in Visual Studio Installer first.'
)
else:
for program_files in ['Program Files', 'Program Files (x86)']:
for edition in ['Community', 'Professional', 'Enterprise', 'BuildTools']:
vcvarsall = pathlib.Path('C:\\', program_files, 'Microsoft Visual Studio',
'2022', edition, 'VC', 'Auxiliary', 'Build',
'vcvarsall.bat')
if vcvarsall.exists():
return vcvarsall

raise FileNotFoundError(
'Could not find vcvarsall.bat. '
Expand Down