一、依赖
- 安装msys2。由于构建bazel本身,以及大量bazel构建脚本包含genrule需要bash,因此目前windows环境下使用bazel,强依赖bash
https://github.com/msys2/msys2-installer/releases/download/2024-11-16/msys2-x86_64-20241116.exe - 安装vs studio
- 安装java。构建bazel本身以及bazel运行需要java
- 构建bazel.
export http_proxy="http://192.168.2.1:1081" export https_proxy="http://192.168.2.1:1081" export MSYS_NO_PATHCONV=1 export MSYS2_ARG_CONV_EXCL="*" export BAZEL_VC="C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC" export BAZEL_SH="$(cygpath -m $(realpath $(which bash)))" export PATH="/c/Python311:/c/Program Files/bazel::$PATH" export JAVA_HOME="C:/Program Files/Java/jdk-21" env EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" ./compile.sh
二、配置
修改系统环境变量
BAZEL_SH=E:/msys64/usr/bin/bash.exe
JAVA_HOME=C:\Program Files\Java\jdk-21
CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
Path=C:\Strawberry\perl\bin:C:\Python311\Scripts:C:\Python311:C:\Program Files\CMake\bin:C:\Program Files\bazel:C:\ProgramData\chocolatey\lib\gperf\tools:C:\Strawberry\perl\site\bin:C:\Strawberry\perl\bin:%JAVA_HOME%\bin
编译命令
$env:http_proxy = "http://192.168.2.1:1081"
$env:https_proxy = "http://192.168.2.1:1081"
$env:Path += ";C:/Python311;C:/ProgramData/chocolatey/bin"
$env:BAZEL_SH="E:/msys64/usr/bin/bash.exe"
$env:BAZEL_VC="C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC"
$env:PYTHON_LIB_PATH="C:/Python311/Lib"
$env:PYTHON_DIRECTORY="C:/Python311/Scripts"
$env:INCLUDE="C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.41.34120\include;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.41.34120\atlmfc\include;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\VS\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\VS\UnitTest\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\cppwinrt;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\Include\um"
$env:LIB += ";C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\VS\UnitTest\lib;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64"
bazel --output_user_root=D:\bazel_cache build //...
三、注意点
-
从github下载的bazel自带了jdk,bazel没法访问使用自签名证书网站,因此需要自己构建bazel,然后使用命令信任自签名证书
keytool -importcert -keystore "C:\Program Files\Java\jdk-21\lib\security\cacerts" -file "D:\share\ubuntu\cert\certs\ca.root.cert.pem" -alias gitlab.ca -storepass changeit
-
对于国内网络环境,编译时,可以在powersell设置代理环境变量
$env:http_proxy = "http://192.168.2.1:1081" $env:https_proxy = "http://192.168.2.1:1081"
-
windows对路径中单文件名长度有限制,基本所有人都会遇到这个问题,比如编译中遇到找不到某个文件,明明该文件就是存在的,即使修改系统配置打开Enable Win32 long paths,然后会有路径长度问题。需要指定bazel --output_user_root=D:\bazel_cache build //...,或者在.bazelrc中加入,需要注意的是--output_base选项是针对单个项目的,因此不同项目不可以使用同一个路径
startup --windows_enable_symlinks startup --output_base=D:/bazel_cache
ERROR: C:/users/tiger/_bazel_tiger/rtzwbyqu/external/com_google_protobuf/src/google/protobuf/json/BUILD.bazel:66:11: Compiling src/google/protobuf/json/internal/zero_copy_buffered_stream.cc [for tool] failed: (Exit 2): cl.exe failed: error executing CppCompile command (from target @@com_google_protobuf//src/google/protobuf/json:zero_copy_buffered_stream) C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.41.34120\bin\HostX64\x64\cl.exe ... (remaining 1 argument skipped) external/com_google_protobuf/src/google/protobuf/json/internal/zero_copy_buffered_stream.cc(8): fatal error C1083: Cannot open include file: 'google/protobuf/json/internal/zero_copy_buffered_stream.h': No such file or directory Use --verbose_failures to see the command lines of failed build steps.
四、其它
pacman -Syu
pacman -S gperf
dumpbin /SYMBOLS bazel-out/x64_windows-opt/bin/external/curl/curl.lib | findstr "lzma_code"
dumpbin /DEPENDENTS bazel-bin/my_project/my_binary.exe
cl @bazel-out/x64_windows-opt/bin/src/client/grpc_client/_objs/grpc_repo_client_test/grpc_repo_client_test.obj.params
$env:http_proxy = "http://192.168.2.1:1081"
$env:https_proxy = "http://192.168.2.1:1081"
$env:Path += ";C:/Python311;C:/ProgramData/chocolatey/bin"
$env:BAZEL_SH="E:/msys64/usr/bin/bash.exe"
$env:BAZEL_VC="C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC"
$env:PYTHON_LIB_PATH="C:/Python311/Lib"
$env:PYTHON_DIRECTORY="C:/Python311/Scripts"
$env:INCLUDE="C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.41.34120\include;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.41.34120\atlmfc\include;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\VS\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\VS\UnitTest\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\cppwinrt;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\Include\um"
$env:LIB += ";C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\VS\UnitTest\lib;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64"