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

Update windows VS detection code to account for new directory #18608

Closed
wants to merge 7 commits into from
39 changes: 18 additions & 21 deletions tools/cpp/windows_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,10 @@ def find_vc_path(repository_ctx):
# 5. Check default directories for VC installation
auto_configure_warning_maybe(repository_ctx, "Looking for default Visual C++ installation directory")
for path in [
"Microsoft Visual Studio\\2019\\Preview\\VC",
"Microsoft Visual Studio\\2019\\BuildTools\\VC",
"Microsoft Visual Studio\\2019\\Community\\VC",
"Microsoft Visual Studio\\2019\\Professional\\VC",
"Microsoft Visual Studio\\2019\\Enterprise\\VC",
"Microsoft Visual Studio\\2017\\BuildTools\\VC",
"Microsoft Visual Studio\\2017\\Community\\VC",
"Microsoft Visual Studio\\2017\\Professional\\VC",
"Microsoft Visual Studio\\2017\\Enterprise\\VC",
"Microsoft Visual Studio\\%s\\%s\\VC" % (year, edition)
for year in (2022, 2019, 2017)
for edition in ("Preview", "BuildTools", "Community", "Professional", "Enterprise")
] + [
"Microsoft Visual Studio 14.0\\VC",
]:
path = program_files_dir + "\\" + path
Expand All @@ -254,17 +249,19 @@ def find_vc_path(repository_ctx):
auto_configure_warning_maybe(repository_ctx, "Visual C++ build tools found at %s" % vc_dir)
return vc_dir

def _is_vs_2017_or_2019(repository_ctx, vc_path):
"""Check if the installed VS version is Visual Studio 2017 or 2019."""
def _is_vs_2017_or_newer(repository_ctx, vc_path):
"""Check if the installed VS version is Visual Studio 2017 or newer."""

# The layout of VC folder in VS 2017 and 2019 is different from that in VS 2015 and older versions.
# In VS 2017 and 2019, it contains only three directories:
# The layout of VC folder in VS 2017 and newer versions is different from that in VS 2015 and older versions.
# From VS 2017 it contains three directories:
# "Auxiliary", "Redist", "Tools"
# From VS 2022 17.6, a fourth "vcpkg" directory is also present, but we only check presence of the three
# directories above in case other directories pop up in the future

vc_2017_or_2019_contents = ["auxiliary", "redist", "tools"]
vc_2017_or_newer_contents = ["auxiliary", "redist", "tools"]
vc_path_contents = [d.basename.lower() for d in repository_ctx.path(vc_path).readdir()]
redsun82 marked this conversation as resolved.
Show resolved Hide resolved
vc_path_contents = sorted(vc_path_contents)
return vc_path_contents == vc_2017_or_2019_contents
vc_path_contents = sorted([d for d in vc_path_contents if d in vc_2017_or_newer_contents])
Copy link
Member

Choose a reason for hiding this comment

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

I guess we can simplify the logic here:

vc_2017_or_newer_expected_dirs = ["auxiliary", "redist", "tools"]
for dir in vc_2017_or_newer_expected_dirs:
    if not repository_ctx.path(vc_path).get_child(dir).exists:
        return False
return True

Copy link

Choose a reason for hiding this comment

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

From a comment above, VS2015 has this directory structure

C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\VC
├───bin
├───crt
├───include
├───lib
└───redist

So I think anything that checks for tools and auxiliary will work to differentiate VS2017+ from VS2015. The check for tools is the important one, since that is where the binaries are found.

Copy link
Member

@meteorcloudy meteorcloudy Jun 28, 2023

Choose a reason for hiding this comment

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

Thanks, then this could be further simplified to

return repository_ctx.path(vc_path).get_child("tools").exists

Copy link
Contributor

Choose a reason for hiding this comment

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

@redsun82 Can you overtake the suggested change - would be nice if we could soon merge this in bring it to Bazel 6.3.0

Copy link
Contributor

Choose a reason for hiding this comment

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

I can fork this and add @meteorcloudy suggestion if @redsun82 does not have time/resources to work on this further...

Copy link
Contributor

Choose a reason for hiding this comment

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

I create a PR with this change -> #18945 @meteorcloudy, @redsun82

return vc_path_contents == vc_2017_or_newer_contents

def _is_msbuildtools(vc_path):
"""Check if the installed VC version is from MSBuildTools."""
Expand All @@ -275,7 +272,7 @@ def _is_msbuildtools(vc_path):

def _find_vcvars_bat_script(repository_ctx, vc_path):
"""Find batch script to set up environment variables for VC. Doesn't %-escape the result."""
if _is_vs_2017_or_2019(repository_ctx, vc_path):
if _is_vs_2017_or_newer(repository_ctx, vc_path):
vcvars_script = vc_path + "\\Auxiliary\\Build\\VCVARSALL.BAT"
else:
vcvars_script = vc_path + "\\VCVARSALL.BAT"
Expand All @@ -293,7 +290,7 @@ def _is_support_vcvars_ver(vc_full_version):

def _is_support_winsdk_selection(repository_ctx, vc_path):
"""Windows SDK selection is supported with VC 2017 / 2019 or with full VS 2015 installation."""
if _is_vs_2017_or_2019(repository_ctx, vc_path):
if _is_vs_2017_or_newer(repository_ctx, vc_path):
return True

# By checking the source code of VCVARSALL.BAT in VC 2015, we know that
Expand All @@ -319,7 +316,7 @@ def _get_vc_env_vars(repository_ctx, vc_path, msvc_vars_x64, target_arch):
dictionary of envvars
"""
env = {}
if _is_vs_2017_or_2019(repository_ctx, vc_path):
if _is_vs_2017_or_newer(repository_ctx, vc_path):
lib = msvc_vars_x64["%{msvc_env_lib_x64}"]
full_version = _get_vc_full_version(repository_ctx, vc_path)
tools_path = "%s\\Tools\\MSVC\\%s\\bin\\HostX64\\%s" % (vc_path, full_version, target_arch)
Expand Down Expand Up @@ -367,7 +364,7 @@ def setup_vc_env_vars(repository_ctx, vc_path, envvars = [], allow_empty = False

# Get VC version set by user. Only supports VC 2017 & 2019.
vcvars_ver = ""
if _is_vs_2017_or_2019(repository_ctx, vc_path):
if _is_vs_2017_or_newer(repository_ctx, vc_path):
full_version = _get_vc_full_version(repository_ctx, vc_path)

# Because VCVARSALL.BAT is from the latest VC installed, so we check if the latest
Expand Down Expand Up @@ -448,7 +445,7 @@ def _find_msvc_tools(repository_ctx, vc_path, target_arch = "x64"):
def find_msvc_tool(repository_ctx, vc_path, tool, target_arch = "x64"):
"""Find the exact path of a specific build tool in MSVC. Doesn't %-escape the result."""
tool_path = None
if _is_vs_2017_or_2019(repository_ctx, vc_path) or _is_msbuildtools(vc_path):
if _is_vs_2017_or_newer(repository_ctx, vc_path) or _is_msbuildtools(vc_path):
full_version = _get_vc_full_version(repository_ctx, vc_path)
if full_version:
tool_path = "%s\\Tools\\MSVC\\%s\\bin\\HostX64\\%s\\%s" % (vc_path, full_version, target_arch, tool)
Expand Down
Loading