Skip to content

Commit

Permalink
Make cc_configure on Windows more robust
Browse files Browse the repository at this point in the history
1. Throw an error if Bash-on-Windows is found instead of MSYS bash
2. Introduce BAZEL_VS environment variable
3. Output more information

Fix bazelbuild#1847

--
Change-Id: Ic4571c6c792d9b81df4cd800b8f19d121cc44c33
Reviewed-on: https://bazel-review.googlesource.com/#/c/6330
MOS_MIGRATED_REVID=134531295
  • Loading branch information
meteorcloudy committed Sep 28, 2016
1 parent 8b88f64 commit eb87208
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions site/versions/master/docs/windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ To build C++ targets, you will need:
<br/>We are using MSVC as the native C++ toolchain, so please ensure you have Visual
Studio installed with the Visual C++ components
(which is NOT the default installation type of Visual Studio).
You can set BAZEL\_VS environment variable to tell Bazel
where Visual Studio is, otherwise Bazel will try to find the latest version installed.
<br/>For example: `export BAZEL_VS="C:/Program Files (x86)/Microsoft Visual Studio 14.0"`

* [Python 2.7](https://www.python.org/downloads/)
<br/>Currently, we use python wrapper scripts to call the actual MSVC compiler, so
Expand Down
13 changes: 8 additions & 5 deletions src/main/native/build_windows_jni.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ trap "rm -fr \"$VSTEMP\"" EXIT

# Find Visual Studio. We don't have any regular environment variables available
# so this is the best we can do.
VSVERSION="$(ls "C:/Program Files (x86)" | grep -E "Microsoft Visual Studio [0-9]+" | sort | tail -n 1)"
if [[ "$VSVERSION" == "" ]]; then
echo "Visual Studio not found"
exit 1
if [ -z "${BAZEL_VS+set}" ]; then
VSVERSION="$(ls "C:/Program Files (x86)" | grep -E "Microsoft Visual Studio [0-9]+" | sort --version-sort | tail -n 1)"
if [[ "$VSVERSION" == "" ]]; then
echo "Visual Studio not found"
exit 1
fi
BAZEL_VS="C:/Program Files (x86)/$VSVERSION"
fi
VSVARS="C:/Program Files (x86)/$VSVERSION/VC/VCVARSALL.BAT"
VSVARS="${BAZEL_VS}/VC/VCVARSALL.BAT"

# Find Java. $(JAVA) in the BUILD file points to external/local_jdk/..., which
# is not very useful for anything not MSYS-based.
Expand Down
20 changes: 17 additions & 3 deletions tools/cpp/cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ def _find_python(repository_ctx):
if "BAZEL_PYTHON" in repository_ctx.os.environ:
return repository_ctx.os.environ["BAZEL_PYTHON"]
auto_configure_warning("'BAZEL_PYTHON' is not set, start looking for python in PATH.")
python_binary = _which_cmd(repository_ctx, "python.exe", "C:\\Python27\\python.exe")
python_binary = _which_cmd(repository_ctx, "python.exe")
auto_configure_warning("Python found at %s" % python_binary)
return python_binary

def _find_bash(repository_ctx):
Expand All @@ -383,14 +384,27 @@ def _find_bash(repository_ctx):
return repository_ctx.os.environ["BAZEL_SH"]
auto_configure_warning("'BAZEL_SH' is not set, start looking for bash in PATH.")
bash_binary = _which_cmd(repository_ctx, "bash.exe")
auto_configure_warning("Bash binary found at %s" % bash_binary)
if bash_binary.lower() == "c:/windows/system32/bash.exe":
auto_configure_fail("Bash on Windows currently doesn't work with Bazel, please set BAZEL_SH or make sure 'which bash' returns MSYS bash binary.")
return bash_binary

def _find_vs_path(repository_ctx):
"""Find Visual Studio install path."""
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.")
bash_bin = _find_bash(repository_ctx)
program_files_dir = _get_env_var(repository_ctx, "ProgramFiles(x86)", "C:\\Program Files (x86)")
vs_version = _execute(repository_ctx, [bash_bin, "-c", "ls '%s' | grep -E 'Microsoft Visual Studio [0-9]+' | sort | tail -n 1" % program_files_dir])
return program_files_dir + "/" + vs_version
# --version-sort let us find the latest version of Visual Studio
# Make sure we are using msys sort, the Windows one doesn't support --version-sort.
sort_binary = bash_bin[0:-8].replace("\\", "/") + "sort.exe"
vs_version = _execute(repository_ctx, [bash_bin, "-c", "ls '%s' | grep -E 'Microsoft Visual Studio [0-9]+' | %s --version-sort | tail -n 1" % (program_files_dir, sort_binary)])
if not vs_version:
auto_configure_fail("Visual Studio not found under %s" % program_files_dir)
vs_dir = program_files_dir + "/" + vs_version
auto_configure_warning("Visual Studio found at %s" % vs_dir)
return vs_dir


def _find_env_vars(repository_ctx, vs_path):
Expand Down

0 comments on commit eb87208

Please sign in to comment.