See Install Bazel on Windows for installation instructions.
We mark Windows-related Bazel issues on GitHub with the “team-Windows” label. You can see the open issues here.
We recommend running Bazel from the Command Prompt (cmd.exe
) or from PowerShell.
As of 2020-01-15, we do not recommend running Bazel from bash
-- either from MSYS2 shell, or Git Bash, or Cygwin, or any other Bash variant. While Bazel may work for most use cases, some things are broken, like interrupting the build with Ctrl+C from MSYS2). Also, if you choose to run under MSYS2, you need to disable MSYS2's automatic path conversion, otherwise MSYS will convert command line arguments that look like Unix paths (e.g. //foo:bar
) into Windows paths. See this StackOverflow answer for details.
bazel build
without BashBazel versions before 1.0 used to require Bash to build some rules.
Starting with Bazel 1.0, you can build any rule without Bash unless it is a:
genrule
, because genrules execute Bash commandssh_binary
or sh_test
rule, because these inherently need Bashctx.actions.run_shell()
or ctx.resolve_command()
However, genrule
is often used for simple tasks like copying a file or writing a text file. Instead of using genrule
(and depending on Bash) you may find a suitable rule in the bazel-skylib repository. When built on Windows, these rules do not require Bash.
bazel test
without BashBazel versions before 1.0 used to require Bash to bazel test
anything.
Starting with Bazel 1.0, you can test any rule without Bash, except when:
--run_under
bazel run
without BashBazel versions before 1.0 used to require Bash to bazel run
anything.
Starting with Bazel 1.0, you can run any rule without Bash, except when:
--run_under
or --script_path
sh_binary
and sh_*
rules, and ctx.actions.run_shell()
without BashYou need Bash to build and test sh_*
rules, and to build and test Starlark rules that use ctx.actions.run_shell()
and ctx.resolve_command()
. This applies not only to rules in your project, but to rules in any of the external repositories your project depends on (even transitively).
We may explore the option to use Windows Subsystem for Linux (WSL) to build these rules, but as of 2020-01-15 it is not a priority for the Bazel-on-Windows subteam.
Environment variables you set in the Windows Command Prompt (cmd.exe
) are only set in that command prompt session. If you start a new cmd.exe
, you need to set the variables again. To always set the variables when cmd.exe
starts, you can add them to the User variables or System variables in the Control Panel > System Properties > Advanced > Environment Variables...
dialog box.
The first time you build any target, Bazel auto-configures the location of Python and the Visual C++ compiler. If you need to auto-configure again, run bazel clean
then build a target.
You can also tell Bazel where to find the Python binary and the C++ compiler:
--python_path=c:\path\to\python.exe
flag for PythonBAZEL_VC
or the BAZEL_VS
environment variable (they are not the same!). See the Build C++ section below.To build C++ targets with MSVC, you need:
The Visual C++ compiler.
You can install it in one of the following ways:
Install Visual Studio 2015 or later (Community Edition is enough) with Visual C++.
Make sure to also install the Visual C++ > Common Tools for Visual C++
and Visual C++ > Microsoft Foundation Classes for C++
features. These features are not installed by default.
Install the Visual C++ Build Tools 2015 or later.
If alwayslink doesn't work with VS 2017, that is due to a known issue, please upgrade your VS 2017 to the latest version.
The BAZEL_VS
, BAZEL_VC
and BAZEL_VC_FULL_VERSION
environment variable.
Bazel tries to locate the C++ compiler the first time you build any target. To tell Bazel where the compiler is, you can set the following environment variables:
For Visual Studio 2017 and 2019, set one of BAZEL_VC
or BAZEL_VS
. Additionally you may also set BAZEL_VC_FULL_VERSION
.
BAZEL_VS
the Visual Studio installation directory
set BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools
BAZEL_VC
the Visual C++ Build Tools installation directory
set BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC
BAZEL_VC_FULL_VERSION
(Optional) Only for Visual Studio 2017 and 2019, the full version number of your Visual C++ Build Tools. You can choose the exact Visual C++ Build Tools version via BAZEL_VC_FULL_VERSION
if more than one version are installed, otherwise Bazel will choose the latest version.
set BAZEL_VC_FULL_VERSION=14.16.27023
For Visual Studio 2015 or older, set BAZEL_VC
or BAZEL_VS
. (BAZEL_VC_FULL_VERSION
is not supported.)
BAZEL_VS
the Visual Studio installation directory
set BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio 14.0
BAZEL_VC
the Visual C++ Build Tools installation directory
set BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
The Windows SDK.
The Windows SDK contains header files and libraries you need when building Windows applications, including Bazel itself. By default, the latest Windows SDK installed will be used. You also can specify Windows SDK version by setting BAZEL_WINSDK_FULL_VERSION
. You can use a full Windows 10 SDK number such as 10.0.10240.0, or specify 8.1 to use the Windows 8.1 SDK (only one version of Windows 8.1 SDK is available). Please make sure you have the specified Windows SDK installed.
Requirement: This is supported with VC 2017 and 2019. The standalone VC 2015 Build Tools doesn‘t support selecting Windows SDK, you’ll need the full Visual Studio 2015 installation, otherwise BAZEL_WINSDK_FULL_VERSION
will be ignored.
set BAZEL_WINSDK_FULL_VERSION=10.0.10240.0
If everything is set up, you can build a C++ target now!
Try building a target from one of our sample projects:
C:\projects\bazel> bazel build //examples/cpp:hello-world C:\projects\bazel> bazel-bin\examples\cpp\hello-world.exe
To build and use Dynamically Linked Libraries (DLL files), see this example.
From 0.29.0, Bazel supports building with LLVM's MSVC-compatible compiler driver (clang-cl.exe
).
Requirement: To build with Clang, you have to install both LLVM and Visual C++ Build tools, because although we use clang-cl.exe
as compiler, we still need to link to Visual C++ libraries.
Bazel can automatically detect LLVM installation on your system, or you can explicitly tell Bazel where LLVM is installed by BAZEL_LLVM
.
BAZEL_LLVM
the LLVM installation directory
set BAZEL_LLVM=C:\Program Files\LLVM
To enable the Clang toolchain for building C++, there are several situations.
In bazel 0.28 and older: Clang is not supported.
Without --incompatible_enable_cc_toolchain_resolution
: You can enable the Clang toolchain by a build flag --compiler=clang-cl
.
With --incompatible_enable_cc_toolchain_resolution
: You have to add a platform target to your BUILD file (eg. the top level BUILD file):
platform( name = "x64_windows-clang-cl", constraint_values = [ "@platforms//cpu:x86_64", "@platforms//os:windows", "@bazel_tools//tools/cpp:clang-cl", ], )
Then you can enable the Clang toolchain by either of the following two ways:
--extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows-clang-cl --extra_execution_platforms=//:x64_windows-clang-cl
register_execution_platforms( ":x64_windows-clang-cl" ) register_toolchains( "@local_config_cc//:cc-toolchain-x64_windows-clang-cl", )
The --incompatible_enable_cc_toolchain_resolution flag is planned to be enabled by default in future Bazel release. Therefore, it is recommended to enable Clang support with the second approach.
There's no setup necessary.
On Windows, Bazel builds two output files for java_binary
rules:
.jar
file.exe
file that can set up the environment for the JVM and run the binaryTry building a target from one of our sample projects:
C:\projects\bazel> bazel build //examples/java-native/src/main/java/com/example/myproject:hello-world C:\projects\bazel> bazel-bin\examples\java-native\src\main\java\com\example\myproject\hello-world.exe
To build Python targets, you need:
Both Python 2 and Python 3 are supported.
To tell Bazel where Python is, you can use --python_path=<path/to/python>
. For example:
bazel build --python_path=C:/Python27/python.exe ...
If --python_path
is not specified, Bazel uses python.exe
as the interpreter and the binary looks for it in $PATH
during runtime. If it is not in $PATH
(for example, when you use py_binary
as an action's executable, Bazel will sanitize $PATH
), then the execution will fail.
On Windows, Bazel builds two output files for py_binary
rules:
You can either run the executable file (it has a .exe
extension) or you can run Python with the self-extracting zip file as the argument.
Try building a target from one of our sample projects:
C:\projects\bazel> bazel build //examples/py_native:bin C:\projects\bazel> bazel-bin\examples\py_native\bin.exe C:\projects\bazel> python bazel-bin\examples\py_native\bin.zip
If you are interested in details about how Bazel builds Python targets on Windows, check out this design doc.