Skip to content

Commit cd33d14

Browse files
Vertexwahncopybara-github
authored andcommitted
Fix VS 2022 autodetection
This is the same PR as #18608 but extended by the modification proposed by @meteorcloudy This should fix #18592 Should also be picked to 6.3.0 -> #18799 Closes #18945. PiperOrigin-RevId: 548725707 Change-Id: Iff0f972c9fc23491c8070ee2b12ec600a3d1ead9
1 parent 40e6f11 commit cd33d14

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

tools/cpp/windows_cc_configure.bzl

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,10 @@ def find_vc_path(repository_ctx):
232232
# 5. Check default directories for VC installation
233233
auto_configure_warning_maybe(repository_ctx, "Looking for default Visual C++ installation directory")
234234
for path in [
235-
"Microsoft Visual Studio\\2019\\Preview\\VC",
236-
"Microsoft Visual Studio\\2019\\BuildTools\\VC",
237-
"Microsoft Visual Studio\\2019\\Community\\VC",
238-
"Microsoft Visual Studio\\2019\\Professional\\VC",
239-
"Microsoft Visual Studio\\2019\\Enterprise\\VC",
240-
"Microsoft Visual Studio\\2017\\BuildTools\\VC",
241-
"Microsoft Visual Studio\\2017\\Community\\VC",
242-
"Microsoft Visual Studio\\2017\\Professional\\VC",
243-
"Microsoft Visual Studio\\2017\\Enterprise\\VC",
235+
"Microsoft Visual Studio\\%s\\%s\\VC" % (year, edition)
236+
for year in (2022, 2019, 2017)
237+
for edition in ("Preview", "BuildTools", "Community", "Professional", "Enterprise")
238+
] + [
244239
"Microsoft Visual Studio 14.0\\VC",
245240
]:
246241
path = program_files_dir + "\\" + path
@@ -254,17 +249,11 @@ def find_vc_path(repository_ctx):
254249
auto_configure_warning_maybe(repository_ctx, "Visual C++ build tools found at %s" % vc_dir)
255250
return vc_dir
256251

257-
def _is_vs_2017_or_2019(repository_ctx, vc_path):
258-
"""Check if the installed VS version is Visual Studio 2017 or 2019."""
252+
def _is_vs_2017_or_newer(repository_ctx, vc_path):
253+
"""Check if the installed VS version is Visual Studio 2017 or newer."""
259254

260-
# The layout of VC folder in VS 2017 and 2019 is different from that in VS 2015 and older versions.
261-
# In VS 2017 and 2019, it contains only three directories:
262-
# "Auxiliary", "Redist", "Tools"
263-
264-
vc_2017_or_2019_contents = ["auxiliary", "redist", "tools"]
265-
vc_path_contents = [d.basename.lower() for d in repository_ctx.path(vc_path).readdir()]
266-
vc_path_contents = sorted(vc_path_contents)
267-
return vc_path_contents == vc_2017_or_2019_contents
255+
# For VS 2017 and later, a `Tools` directory should exist under `BAZEL_VC`
256+
return repository_ctx.path(vc_path).get_child("Tools").exists
268257

269258
def _is_msbuildtools(vc_path):
270259
"""Check if the installed VC version is from MSBuildTools."""
@@ -275,7 +264,7 @@ def _is_msbuildtools(vc_path):
275264

276265
def _find_vcvars_bat_script(repository_ctx, vc_path):
277266
"""Find batch script to set up environment variables for VC. Doesn't %-escape the result."""
278-
if _is_vs_2017_or_2019(repository_ctx, vc_path):
267+
if _is_vs_2017_or_newer(repository_ctx, vc_path):
279268
vcvars_script = vc_path + "\\Auxiliary\\Build\\VCVARSALL.BAT"
280269
else:
281270
vcvars_script = vc_path + "\\VCVARSALL.BAT"
@@ -293,7 +282,7 @@ def _is_support_vcvars_ver(vc_full_version):
293282

294283
def _is_support_winsdk_selection(repository_ctx, vc_path):
295284
"""Windows SDK selection is supported with VC 2017 / 2019 or with full VS 2015 installation."""
296-
if _is_vs_2017_or_2019(repository_ctx, vc_path):
285+
if _is_vs_2017_or_newer(repository_ctx, vc_path):
297286
return True
298287

299288
# By checking the source code of VCVARSALL.BAT in VC 2015, we know that
@@ -319,7 +308,7 @@ def _get_vc_env_vars(repository_ctx, vc_path, msvc_vars_x64, target_arch):
319308
dictionary of envvars
320309
"""
321310
env = {}
322-
if _is_vs_2017_or_2019(repository_ctx, vc_path):
311+
if _is_vs_2017_or_newer(repository_ctx, vc_path):
323312
lib = msvc_vars_x64["%{msvc_env_lib_x64}"]
324313
full_version = _get_vc_full_version(repository_ctx, vc_path)
325314
tools_path = "%s\\Tools\\MSVC\\%s\\bin\\HostX64\\%s" % (vc_path, full_version, target_arch)
@@ -367,7 +356,7 @@ def setup_vc_env_vars(repository_ctx, vc_path, envvars = [], allow_empty = False
367356

368357
# Get VC version set by user. Only supports VC 2017 & 2019.
369358
vcvars_ver = ""
370-
if _is_vs_2017_or_2019(repository_ctx, vc_path):
359+
if _is_vs_2017_or_newer(repository_ctx, vc_path):
371360
full_version = _get_vc_full_version(repository_ctx, vc_path)
372361

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

0 commit comments

Comments
 (0)