Yun Peng has uploaded this change for review.
Detect Visual C++ build tools through vcvarsqueryregistry.bat Change-Id: I12b109af81e0eb8948fd1d7a7d2bbfcf6baa7ca4 --- M tools/cpp/cc_configure.bzl 1 file changed, 49 insertions(+), 20 deletions(-)
diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl
index f7eb191..f12ab84 100644
--- a/tools/cpp/cc_configure.bzl
+++ b/tools/cpp/cc_configure.bzl
@@ -424,13 +424,45 @@
auto_configure_warning("Python found at %s" % python_binary)
return python_binary
+def _add_system_root(repository_ctx, env):
+ """Running VCVARSALL.BAT and VCVARSQUERYREGISTRY.BAT need %SYSTEMROOT%\\system32 to be in PATH."""
+ if "PATH" not in env:
+ env["PATH"] = ""
+ env["PATH"] = env["PATH"] + ";" + _get_system_root(repository_ctx) + "\\system32"
+ return env
-def _find_vs_path(repository_ctx):
- """Find Visual Studio install path."""
+def _find_vc_path(repository_ctx):
+ """Find Visual C++ build tools install path."""
+ # 1. Check if BAZEL_VC or BAZEL_VS is already set by user.
+ if "BAZEL_VC" in repository_ctx.os.environ:
+ return repository_ctx.os.environ["BAZEL_VC"]
+
if "BAZEL_VS" in repository_ctx.os.environ:
- return repository_ctx.os.environ["BAZEL_VS"]
- auto_configure_warning("'BAZEL_VS' is not set, start looking for the latest Visual Studio installed.")
+ return repository_ctx.os.environ["BAZEL_VS"] + "\\VC\\"
+ auto_configure_warning("'BAZEL_VC' is not set, start looking for the latest Visual C++ installed.")
+ # 2. Check if VS%VS_VERSION%COMNTOOLS is set, if true then try to find and use vcvarsqueryregistry.bat to detect VC++.
+ auto_configure_warning("Looking for VS%VERSION%COMNTOOLS environment variables, eg. VS140COMNTOOLS")
+ for vscommontools_env in ["VS140COMNTOOLS", "VS120COMNTOOLS", "VS110COMNTOOLS", "VS100COMNTOOLS", "VS90COMNTOOLS"]:
+ if not vscommontools_env in repository_ctx.os.environ:
+ continue
+ vcvarsqueryregistry = repository_ctx.os.environ[vscommontools_env] + "\\" + "vcvarsqueryregistry.bat"
+ if not repository_ctx.path(vcvarsqueryregistry).exists:
+ continue
+ repository_ctx.file("wrapper/get_vc_dir.bat",
+ "@echo off\n" +
+ "call \"" + vcvarsqueryregistry + "\"\n" +
+ "echo %VCINSTALLDIR%", True)
+ env = _add_system_root(repository_ctx, repository_ctx.os.environ)
+ vc_dir = _execute(repository_ctx, ["wrapper/get_vc_dir.bat"], environment=env)
+
+ auto_configure_warning("Visual C++ build tools found at %s" % vc_dir)
+ return vc_dir
+
+ # 3. User might clean up all environment variables, if so looking for Visual C++ through registry.
+ # This method only works if Visual Studio is installed, and for some reason, is flaky.
+ # https://github.com/bazelbuild/bazel/issues/2434
+ auto_configure_warning("Looking for Visual C++ through registry")
# Query the installed visual stduios on the machine, should return something like:
# HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0
# HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\8.0
@@ -460,23 +492,20 @@
vs_dir = "\\".join(path_segments[:-2])
if not vs_dir:
- auto_configure_fail("Visual Studio not found on your machine.")
- auto_configure_warning("Visual Studio found at %s" % vs_dir)
- return vs_dir
+ auto_configure_fail("Visual C++ build tools not found on your machine.")
+ vc_dir = vs_dir + "\\VC\\"
+ auto_configure_warning("Visual C++ build tools found at %s" % vc_dir)
+ return vc_dir
-def _find_env_vars(repository_ctx, vs_path):
+def _find_env_vars(repository_ctx, vc_path):
"""Get environment variables set by VCVARSALL.BAT."""
- vsvars = vs_path + "/VC/VCVARSALL.BAT"
+ vsvars = vc_path + "/VCVARSALL.BAT"
repository_ctx.file("wrapper/get_env.bat",
"@echo off\n" +
"call \"" + vsvars + "\" amd64 \n" +
"echo PATH=%PATH%,INCLUDE=%INCLUDE%,LIB=%LIB% \n", True)
- env = repository_ctx.os.environ
- if "PATH" not in env:
- env["PATH"]=""
- # Running VCVARSALL.BAT needs %SYSTEMROOT%\\system32 to be in PATH
- env["PATH"] = env["PATH"] + ";" + _get_system_root(repository_ctx) + "\\system32"
+ env = _add_system_root(repository_ctx, repository_ctx.os.environ)
envs = _execute(repository_ctx, ["wrapper/get_env.bat"], environment=env).split(",")
env_map = {}
for env in envs:
@@ -485,12 +514,12 @@
return env_map
-def _is_support_whole_archive(repository_ctx, vs_dir):
+def _is_support_whole_archive(repository_ctx, vc_dir):
"""Run MSVC linker alone to see if it supports /WHOLEARCHIVE."""
env = repository_ctx.os.environ
if "NO_WHOLE_ARCHIVE_OPTION" in env and env["NO_WHOLE_ARCHIVE_OPTION"] == "1":
return False
- result = _execute(repository_ctx, [vs_dir + "/VC/BIN/amd64/link"])
+ result = _execute(repository_ctx, [vc_dir + "/BIN/amd64/link"])
return result.find("/WHOLEARCHIVE") != -1
@@ -554,13 +583,13 @@
python_binary = _find_python(repository_ctx)
_tpl(repository_ctx, "wrapper/bin/call_python.bat", {"%{python_binary}": python_binary})
- vs_path = _find_vs_path(repository_ctx)
- env = _find_env_vars(repository_ctx, vs_path)
+ vc_path = _find_vc_path(repository_ctx)
+ env = _find_env_vars(repository_ctx, vc_path)
python_dir = python_binary[0:-10].replace("\\", "\\\\")
include_paths = env["INCLUDE"] + (python_dir + "include")
lib_paths = env["LIB"] + (python_dir + "libs")
- lib_tool = vs_path.replace("\\", "\\\\") + "/VC/bin/amd64/lib.exe"
- if _is_support_whole_archive(repository_ctx, vs_path):
+ lib_tool = vc_path.replace("\\", "\\\\") + "/bin/amd64/lib.exe"
+ if _is_support_whole_archive(repository_ctx, vc_path):
support_whole_archive = "True"
else:
support_whole_archive = "False"
To view, visit change 8693. To unsubscribe, visit settings.
Yun Peng uploaded patch set #2 to this change.
Detect Visual C++ build tools through vcvarsqueryregistry.bat See: https://github.com/bazelbuild/bazel/issues/2434 Fixed https://github.com/bazelbuild/bazel/issues/2448
Change-Id: I12b109af81e0eb8948fd1d7a7d2bbfcf6baa7ca4 --- M tools/cpp/cc_configure.bzl 1 file changed, 49 insertions(+), 20 deletions(-)
To view, visit change 8693. To unsubscribe, visit settings.
Dmitry Lomov posted comments on this change.
Patch set 2:Verified +1Code-Review +2
László Csomor posted comments on this change.
Patch set 2:
(2 comments)
File tools/cpp/cc_configure.bzl:
Patch Set #2, Line 427: def _add_system_root(repository_ctx, env):
nit: could you align the lines to 80 chars?
`exists` a method, isn't it?
To view, visit change 8693. To unsubscribe, visit settings.
Yun Peng uploaded patch set #3 to this change.
Detect Visual C++ build tools through vcvarsqueryregistry.bat See: https://github.com/bazelbuild/bazel/issues/2434 Fixed https://github.com/bazelbuild/bazel/issues/2448 Change-Id: I12b109af81e0eb8948fd1d7a7d2bbfcf6baa7ca4 --- M tools/cpp/cc_configure.bzl 1 file changed, 49 insertions(+), 20 deletions(-)
To view, visit change 8693. To unsubscribe, visit settings.
Yun Peng uploaded patch set #4 to this change.
Detect Visual C++ build tools through vcvarsqueryregistry.bat See: https://github.com/bazelbuild/bazel/issues/2434 Fixed https://github.com/bazelbuild/bazel/issues/2448 Change-Id: I12b109af81e0eb8948fd1d7a7d2bbfcf6baa7ca4 --- M tools/cpp/cc_configure.bzl 1 file changed, 53 insertions(+), 20 deletions(-)
To view, visit change 8693. To unsubscribe, visit settings.
Yun Peng posted comments on this change.
Patch Set #2, Line 427: def _add_system_root(repository_ctx, env):
nit: could you align the lines to 80 chars?
Looks like the limit is 100 chars, updated.
`exists` a method, isn't it?
According to https://bazel.build/versions/master/docs/skylark/lib/path.html#exists
It's surprisingly an attribute.
To view, visit change 8693. To unsubscribe, visit settings.
László Csomor posted comments on this change.
Patch set 4:Verified +1Code-Review +2
Just nits, thanks for your efforts on this change! Looks good.
(2 comments)
File tools/cpp/cc_configure.bzl:
nit: unnecessary "r" ?
Patch Set #4, Line 463: build tools
nit: here and below: maybe let's avoid the term "build tools" and use "compiler" instead, because both VS and the VC++ Build Tools are fine for us, but "build tools" may suggest we can only use the latter.
To view, visit change 8693. To unsubscribe, visit settings.
Yun Peng posted comments on this change.
Patch set 4:
(2 comments)
nit: unnecessary "r" ?
The internal lint checker suggest to add r because the comment contains backslash.
Patch Set #4, Line 463: build tools
nit: here and below: maybe let's avoid the term "build tools" and use "comp
Hmm.. VC++ build tools can be a part of VS, but users can actually install VS without the build tools. I think using the term "build tools" sort of remind them to install it.
To view, visit change 8693. To unsubscribe, visit settings.
László Csomor posted comments on this change.
Patch set 4:
Patch Set 4:
(2 comments)
Cool, thanks!
Kristina Chodorow merged this change.
Detect Visual C++ build tools through vcvarsqueryregistry.bat See: https://github.com/bazelbuild/bazel/issues/2434 Fixed https://github.com/bazelbuild/bazel/issues/2448 -- Change-Id: I12b109af81e0eb8948fd1d7a7d2bbfcf6baa7ca4 Reviewed-on: https://cr.bazel.build/8693 PiperOrigin-RevId: 146886981 MOS_MIGRATED_REVID=146886981 --- M tools/cpp/cc_configure.bzl 1 file changed, 53 insertions(+), 20 deletions(-)
diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl
index f7eb191..7ee9356 100644
--- a/tools/cpp/cc_configure.bzl
+++ b/tools/cpp/cc_configure.bzl
@@ -424,13 +424,49 @@
auto_configure_warning("Python found at %s" % python_binary)
return python_binary
+def _add_system_root(repository_ctx, env):
+ r"""Running VCVARSALL.BAT and VCVARSQUERYREGISTRY.BAT need %SYSTEMROOT%\\system32 in PATH."""
+ if "PATH" not in env:
+ env["PATH"] = ""
+ env["PATH"] = env["PATH"] + ";" + _get_system_root(repository_ctx) + "\\system32"
+ return env
-def _find_vs_path(repository_ctx):
- """Find Visual Studio install path."""
+def _find_vc_path(repository_ctx):
+ """Find Visual C++ build tools install path."""
+ # 1. Check if BAZEL_VC or BAZEL_VS is already set by user.
+ if "BAZEL_VC" in repository_ctx.os.environ:
+ return repository_ctx.os.environ["BAZEL_VC"]
+
if "BAZEL_VS" in repository_ctx.os.environ:
- return repository_ctx.os.environ["BAZEL_VS"]
- auto_configure_warning("'BAZEL_VS' is not set, start looking for the latest Visual Studio installed.")
+ return repository_ctx.os.environ["BAZEL_VS"] + "\\VC\\"
+ auto_configure_warning("'BAZEL_VC' is not set, " +
+ "start looking for the latest Visual C++ installed.")
+ # 2. Check if VS%VS_VERSION%COMNTOOLS is set, if true then try to find and use
+ # vcvarsqueryregistry.bat to detect VC++.
+ auto_configure_warning("Looking for VS%VERSION%COMNTOOLS environment variables," +
+ "eg. VS140COMNTOOLS")
+ for vscommontools_env in ["VS140COMNTOOLS", "VS120COMNTOOLS",
+ "VS110COMNTOOLS", "VS100COMNTOOLS", "VS90COMNTOOLS"]:
+ if vscommontools_env not in repository_ctx.os.environ:
+ continue
+ vcvarsqueryregistry = repository_ctx.os.environ[vscommontools_env] + "\\vcvarsqueryregistry.bat"
+ if not repository_ctx.path(vcvarsqueryregistry).exists:
+ continue
+ repository_ctx.file("wrapper/get_vc_dir.bat",
+ "@echo off\n" +
+ "call \"" + vcvarsqueryregistry + "\"\n" +
+ "echo %VCINSTALLDIR%", True)
+ env = _add_system_root(repository_ctx, repository_ctx.os.environ)
+ vc_dir = _execute(repository_ctx, ["wrapper/get_vc_dir.bat"], environment=env)
+
+ auto_configure_warning("Visual C++ build tools found at %s" % vc_dir)
+ return vc_dir
+
+ # 3. User might clean up all environment variables, if so looking for Visual C++ through registry.
+ # This method only works if Visual Studio is installed, and for some reason, is flaky.
+ # https://github.com/bazelbuild/bazel/issues/2434
+ auto_configure_warning("Looking for Visual C++ through registry")
# Query the installed visual stduios on the machine, should return something like:
# HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0
# HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\8.0
@@ -460,23 +496,20 @@
vs_dir = "\\".join(path_segments[:-2])
if not vs_dir:
- auto_configure_fail("Visual Studio not found on your machine.")
- auto_configure_warning("Visual Studio found at %s" % vs_dir)
- return vs_dir
+ auto_configure_fail("Visual C++ build tools not found on your machine.")
+ vc_dir = vs_dir + "\\VC\\"
+ auto_configure_warning("Visual C++ build tools found at %s" % vc_dir)
+ return vc_dir
-def _find_env_vars(repository_ctx, vs_path):
+def _find_env_vars(repository_ctx, vc_path):
"""Get environment variables set by VCVARSALL.BAT."""
- vsvars = vs_path + "/VC/VCVARSALL.BAT"
+ vsvars = vc_path + "/VCVARSALL.BAT"
repository_ctx.file("wrapper/get_env.bat",
"@echo off\n" +
"call \"" + vsvars + "\" amd64 \n" +
"echo PATH=%PATH%,INCLUDE=%INCLUDE%,LIB=%LIB% \n", True)
- env = repository_ctx.os.environ
- if "PATH" not in env:
- env["PATH"]=""
- # Running VCVARSALL.BAT needs %SYSTEMROOT%\\system32 to be in PATH
- env["PATH"] = env["PATH"] + ";" + _get_system_root(repository_ctx) + "\\system32"
+ env = _add_system_root(repository_ctx, repository_ctx.os.environ)
envs = _execute(repository_ctx, ["wrapper/get_env.bat"], environment=env).split(",")
env_map = {}
for env in envs:
@@ -485,12 +518,12 @@
return env_map
-def _is_support_whole_archive(repository_ctx, vs_dir):
+def _is_support_whole_archive(repository_ctx, vc_dir):
"""Run MSVC linker alone to see if it supports /WHOLEARCHIVE."""
env = repository_ctx.os.environ
if "NO_WHOLE_ARCHIVE_OPTION" in env and env["NO_WHOLE_ARCHIVE_OPTION"] == "1":
return False
- result = _execute(repository_ctx, [vs_dir + "/VC/BIN/amd64/link"])
+ result = _execute(repository_ctx, [vc_dir + "/BIN/amd64/link"])
return result.find("/WHOLEARCHIVE") != -1
@@ -554,13 +587,13 @@
python_binary = _find_python(repository_ctx)
_tpl(repository_ctx, "wrapper/bin/call_python.bat", {"%{python_binary}": python_binary})
- vs_path = _find_vs_path(repository_ctx)
- env = _find_env_vars(repository_ctx, vs_path)
+ vc_path = _find_vc_path(repository_ctx)
+ env = _find_env_vars(repository_ctx, vc_path)
python_dir = python_binary[0:-10].replace("\\", "\\\\")
include_paths = env["INCLUDE"] + (python_dir + "include")
lib_paths = env["LIB"] + (python_dir + "libs")
- lib_tool = vs_path.replace("\\", "\\\\") + "/VC/bin/amd64/lib.exe"
- if _is_support_whole_archive(repository_ctx, vs_path):
+ lib_tool = vc_path.replace("\\", "\\\\") + "/bin/amd64/lib.exe"
+ if _is_support_whole_archive(repository_ctx, vc_path):
support_whole_archive = "True"
else:
support_whole_archive = "False"
To view, visit change 8693. To unsubscribe, visit settings.
Kristina Chodorow uploaded patch set #5 to this change.
Detect Visual C++ build tools through vcvarsqueryregistry.bat See: https://github.com/bazelbuild/bazel/issues/2434
Fixed https://github.com/bazelbuild/bazel/issues/2448 -- Change-Id: I12b109af81e0eb8948fd1d7a7d2bbfcf6baa7ca4 Reviewed-on: https://cr.bazel.build/8693 PiperOrigin-RevId
: 146886981 MOS_MIGRATED_REVID=146886981 --- M tools/cpp/cc_configure.bzl 1 file changed, 53 insertions(+), 20 deletions(-)
To view, visit change 8693. To unsubscribe, visit settings.