Install bazel on windows

Using Bazel on Windows

This page covers Best Practices for using Bazel on Windows. For installation instructions, see Install Bazel on Windows.

Known issues

Windows-related Bazel issues are marked with the “team-Windows” label on GitHub. You can see the open issues here.

Best practices

Avoid long path issues

Some tools have the Maximum Path Length Limitation on Windows, including the MSVC compiler. To avoid hitting this issue, you can specify a short output directory for Bazel by the —output_user_root flag. For example, add the following line to your bazelrc file:

Enable 8.3 filename support

Bazel attempts to create a short name version for long file paths. But to do so the 8.3 filename support needs to be enabled for the volume in which the file with the long path resides. You can enable 8.3 name creation in all volumes by running the following command:

Some features require Bazel to create file symlink on Windows, you can allow Bazel to do that by enabling Developer Mode on Windows (Only works for Windows 10, version 1703 or newer). After enabling the Developer Mode, you should be able to use the following features:

To make it easier, add the following lines to your bazelrc file:

Note: Creating symlinks on Windows is an expensive operation. The —enable_runfiles flag can potentially create a large amount of file symlinks. Only enable this feature when you need it.

Running Bazel: MSYS2 shell vs. command prompt vs. PowerShell

Recommendation: Run Bazel from the command prompt ( cmd.exe ) or from PowerShell.

As of 2020-01-15, do not run 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.

Using Bazel without Bash (MSYS2)

bazel build without Bash

Bazel 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 commands
  • sh_binary or sh_test rule, because these inherently need Bash
  • Starlark rule that uses ctx.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 Bash

Bazel 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:

  • you use —run_under
  • the test rule itself requires Bash (because its executable is a shell script)

bazel run without Bash

Bazel 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:

  • you use —run_under or —script_path
  • the test rule itself requires Bash (because its executable is a shell script)

sh_binary and sh_* rules, and ctx.actions.run_shell() without Bash

You 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).

Читайте также:  Restart network and linux

In the future, there may be an option to use Windows Subsystem for Linux (WSL) to build these rules, but currently it is not a priority for the Bazel-on-Windows subteam.

Setting environment variables

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.

Build on Windows

Build C++ with MSVC

To build C++ targets with MSVC, you need:

(Optional) The BAZEL_VC and BAZEL_VC_FULL_VERSION environment variable.

Bazel automatically detects the Visual C++ compiler on your system. To tell Bazel to use a specific VC installation, you can set the following environment variables:

For Visual Studio 2017 and 2019, set one of BAZEL_VC . Additionally you may also set BAZEL_VC_FULL_VERSION .

    BAZEL_VC the Visual C++ Build Tools installation directory

For Visual Studio 2015 or older, set BAZEL_VC . ( BAZEL_VC_FULL_VERSION is not supported.)

    BAZEL_VC the Visual C++ Build Tools installation directory

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.

If everything is set up, you can build a C++ target now!

Try building a target from one of our sample projects:

By default, the built binaries target x64 architecture. To specify a different target architecture, set the —cpu build option for your target architecture:

  • x64 (default): —cpu=x64_windows or no option
  • x86: —cpu=x64_x86_windows
  • ARM: —cpu=x64_arm_windows
  • ARM64: —cpu=x64_arm64_windows

For example, to build targets for ARM architecture, run:

To build and use Dynamically Linked Libraries (DLL files), see this example.

Build C++ with Clang

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 you use clang-cl.exe as compiler, you 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

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):

Then you can enable the Clang toolchain by either of the following two ways:

  • Specify the following build flags:
  • Register the platform and toolchain in your WORKSPACE file:

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.

Build Java

To build Java targets, you need:

On Windows, Bazel builds two output files for java_binary rules:

  • a .jar file
  • a .exe file that can set up the environment for the JVM and run the binary

Try building a target from one of our sample projects:

Build Python

To build Python targets, you need:

On Windows, Bazel builds two output files for py_binary rules:

  • a self-extracting zip file
  • an executable file that can launch the Python interpreter with the self-extracting zip file as the argument

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:

If you are interested in details about how Bazel builds Python targets on Windows, check out this design doc.

Installing Bazel on Windows

Installing Bazel

Step 1: Check your system

Recommended: 64 bit Windows 10, version 1703 (Creators Update) or newer

To check your Windows version:

  • Click the Start button.
  • Type winver in the search box and press Enter.
  • You should see the About Windows box with your Windows version information.

64 bit Windows 7 or newer

64 bit Windows Server 2008 R2 or newer

Step 2: Install the prerequisites

Step 3: Download Bazel

Alternatively you can:

Step 4: Set up your environment

To make Bazel easily accessible from command prompts or PowerShell by default, you can rename the Bazel binary to bazel.exe and add it to your default paths.

Читайте также:  Активация windows 10 пароль 1111

You can also change your system PATH environment variable to make it permanent. Check out how to set environment variables.

Step 5: Done

You have successfully installed Bazel. To check the installation is correct, try to run:

Next, you can check out more tips and guidance here:

Installing compilers and language runtimes

Depending on which languages you want to build, you will need:

MSYS2 is a software distro and building platform for Windows. It contains Bash and common Unix tools (like grep , tar , git ).

You will need MSYS2 to build, test, or run targets that depend on Bash. Typically these are genrule , sh_binary , sh_test , but there may be more (e.g. Starlark rules). Bazel shows an error if a build target needs Bash but Bazel could not locate it.

Common MSYS2 packages

You will likely need these to build and run targets that depend on Bash. MSYS2 does not install these tools by default, so you need to install them manually. Projects that depend on Bash tools in PATH need this step (for example TensorFlow).

Open the MSYS2 terminal and run this command:

Optional: If you want to use Bazel from CMD or Powershell and still be able to use Bash tools, make sure to add /usr/bin to your PATH environment variable.

You will need this to build C++ code on Windows.

Visual Studio 2015 (or newer) with Visual C++ and Windows 10 SDK

Visual C++ Build Tools 2015 (or newer) and Windows 10 SDK

You will need this to build Java code on Windows.

Also supported: Java 8, 9, and 10

You will need this to build Python code on Windows.

Also supported: Python 2.7 or newer for Windows x86-64

Troubleshooting

Bazel does not find Bash or bash.exe

Possible reasons:

you installed MSYS2 not under the default install path

you installed MSYS2 i686 instead of MSYS2 x86_64

you installed MSYS instead of MSYS2

Solution:

Ensure you installed MSYS2 x86_64.

If that doesn’t help:

Go to Start Menu > Settings.

Find the setting “Edit environment variables for your account”

Look at the list on the top (“User variables for ”), and click the “New…” button below it.

For “Variable name”, enter BAZEL_SH

Click “Browse File…”

Navigate to the MSYS2 directory, then to usr\bin below it.

For example, this might be C:\msys64\usr\bin on your system.

Select the bash.exe or bash file and click OK

The “Variable value” field now has the path to bash.exe . Click OK to close the window.

If you open a new cmd.exe or PowerShell terminal and run Bazel now, it will find Bash.

Bazel does not find Visual Studio or Visual C++

Possible reasons:

you installed multiple versions of Visual Studio

you installed and removed various versions of Visual Studio

you installed various versions of the Windows SDK

you installed Visual Studio not under the default install path

Solution:

Go to Start Menu > Settings.

Find the setting “Edit environment variables for your account”

Look at the list on the top (“User variables for ”), and click the “New…” button below it.

For “Variable name”, enter BAZEL_VC

Click “Browse Directory…”

Navigate to the VC directory of Visual Studio.

For example, this might be C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC on your system.

Select the VC folder and click OK

The “Variable value” field now has the path to VC . Click OK to close the window.

If you open a new cmd.exe or PowerShell terminal and run Bazel now, it will find Visual C++.

Other ways to install Bazel

Using Chocolatey

Install the Chocolatey package manager

Install the Bazel package:

This command will install the latest available version of Bazel and its dependencies, such as the MSYS2 shell. This will not install Visual C++ though.

See Chocolatey installation and package maintenance guide for more information about the Chocolatey package.

Using Scoop

Install the Scoop package manager using the following PowerShell command:

Install the Bazel package:

See Scoop installation and package maintenance guide for more information about the Scoop package.

Installing Bazel on Windows

Installing

Step 1: Check your system

Recommended: 64 bit Windows 10, version 1703 (Creators Update) or newer, enable “Developer Mode”.

64 bit Windows 7 or newer

64 bit Windows Server 2008 R2 or newer

Older Windows 10 versions, disabled “Developer Mode” (enabling the mode just lets you use the —enable_runfiles Bazel flag)

Step 2: Install the prerequisites

Step 3: Download Bazel

Recommended: rename this binary to bazel.exe and move it to a directory on the PATH .

Alternatively you can:

Читайте также:  Windows 10 asus chipset drivers

Step 4 (optional): Configure output directories

You can skip this step. Bazel can work without configuring the output directories, and will use its default values.

By default, Bazel writes to two directories:

The “output user root”, configurable with the —output_user_root flag.

This is where Bazel extracts from itself its embedded tools, its own runtime, and where it writes some log files and some caches.

This is also the default location for the “output base”.

The “output base”, configurable with the —output_base flag.

This is where Bazel writes all output files. By default, this is a subdirectory of the “output user root”.

By default, Bazel also writes in the workspace directory:

The “convenience symlinks”, configurable with the —symlink_prefix flag.

These are the “bazel-bin”, “bazel-testlogs”, and similar directories that Bazel creates in your workspace. These are not really directories but “junctions”: they just point to other directories in your filesystem (under the “output root”).

You can tell Bazel not to create these junctions with —symlink_prefix=/ .

Step 5 (optional): Install compilers and language runtimes

You can skip this step. Bazel can work without these programs, but you may need them.

We recommend installing:

MSYS2 is a software distro and building platform for Windows. It contains Bash and common Unix tools (like grep , tar , git ).

You will need MSYS2 to build, test, or run targets that depend on Bash. Typically these are genrule , sh_binary , sh_test , but there may be more (e.g. Starlark rules). Bazel shows an error if a build target needs Bash but Bazel could not locate it.

Common MSYS2 packages

You will likely need these to build and run targets that depend on Bash. MSYS2 does not install these tools by default, so you need to install them manually.

Open the MSYS2 terminal and run this command:

Make sure you install the C++ build tools with the Windows 10 SDK.

You will need this to build C++ code on Windows.

Visual Studio 2015 (or newer) with Visual C++ and Windows 10 SDK

Visual C++ Build Tools 2015 (or newer) and Windows 10 SDK

Java SE Development Kit 10 (JDK) for Windows x64

You will need this to build Java code on Windows.

Also supported: Java 8 and 9

You will need this to build Python code on Windows.

Also supported: Python 3 or newer for Windows x86-64

Step 6: Done

You have successfully installed Bazel.

Troubleshooting: see troubleshooting below.

Troubleshooting

Bazel does not find Bash or bash.exe

Possible reasons:

you installed MSYS2 not under the default install path

you installed MSYS2 i686 instead of MSYS2 x86_64

you installed MSYS instead of MSYS2

Solution:

Ensure you installed MSYS2 x86_64.

If that doesn’t help:

Go to Start Menu > Settings.

Find the setting “Edit environment variables for your account”

Look at the list on the top (“User variables for ”), and click the “New…” button below it.

For “Variable name”, enter BAZEL_SH

Click “Browse File…”

Navigate to the MSYS2 directory, then to usr\bin below it.

For example, this might be C:\msys64\usr\bin on your system.

Select the bash.exe or bash file and click OK

The “Variable value” field now has the path to bash.exe . Click OK to close the window.

If you open a new cmd.exe or PowerShell terminal and run Bazel now, it will find Bash.

Bazel does not find Visual Studio or Visual C++

Possible reasons:

you installed multiple versions of Visual Studio

you installed and removed various versions of Visual Studio

you installed various versions of the Windows SDK

you installed Visual Studio not under the default install path

Solution:

Go to Start Menu > Settings.

Find the setting “Edit environment variables for your account”

Look at the list on the top (“User variables for ”), and click the “New…” button below it.

For “Variable name”, enter BAZEL_VC

Click “Browse Directory…”

Navigate to the VC directory of Visual Studio.

For example, this might be C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC on your system.

Select the VC folder and click OK

The “Variable value” field now has the path to VC . Click OK to close the window.

If you open a new cmd.exe or PowerShell terminal and run Bazel now, it will find Visual C++.

Other ways to install Bazel

Using Chocolatey

Install the Chocolatey package manager

Install the Bazel package:

This command will install the latest available version of Bazel and its dependencies, such as the MSYS2 shell. This will not install Visual C++ though.

See Chocolatey installation and package maintenance guide for more information about the Chocolatey package.

Using Scoop

Install the Scoop package manager using the following PowerShell command:

Install the Bazel package:

See Scoop installation and package maintenance guide for more information about the Scoop package.

Оцените статью