如何在不配置新工具链的情况下告诉 Bazel 使用哪个编译器
How to tell Bazel which compiler to use without configuring a new toolchain
我想强制 Bazel 使用新的编译器来构建我的项目。
据我了解,我可以配置一个新的工具链或使用一些命令行选项。
我尝试了以下选项:
-CC=clang bazel build //main:hello-world
-export CC=C:\cygwin64\bin\clang++
- bazel build //main:hello-world --client_env=CC=clang
我认为这些不起作用,因为如果我用
编译同一个 hello-world.cc 文件
clang hello-world.cc
我收到错误:
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
但有
CC=clang bazel build //main:hello-world
or
bazel build //main:hello-world --client_env=CC=clang
构建成功。
知道为什么结果不同吗?如何解决?
我在哪里可以查看 Bazel 使用的是哪个编译器?
编辑:
此处输出:
CC=C:\cygwin64\bin\clang bazel build -s //main:hello-world
Starting local Bazel server and connecting to it...
Loading:
Loading: 0 packages loaded
Analyzing: target //main:hello-world (1 packages loaded, 0 targets configured)
Analyzing: target //main:hello-world (11 packages loaded, 18 targets configured)
INFO: Analyzed target //main:hello-world (14 packages loaded, 59 targets configured).
INFO: Found 1 target...
[0 / 6] [Prepa] BazelWorkspaceStatusAction stable-status.txt ... (4 actions, 0 running)
SUBCOMMAND: # //main:hello-world [action 'Compiling main/hello-world.cc', configuration: 1f7c3bee164375155568e7845819f422152903a0240344d14b0c6474746ef185, execution platform: @local_config_platform//:host]
cd C:/cygwin64/home/user/_bazel_user/5r4lncy4/execroot/__main__
SET INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\ATLMFC\include;C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\include;C:\Program Files (x86)\Windows Kits\NETFXSDK.8\include\um;C:\Program Files (x86)\Windows Kits\include.0.18362.0\ucrt;C:\Program Files (x86)\Windows Kits\include.0.18362.0\shared;C:\Program Files (x86)\Windows Kits\include.0.18362.0\um;C:\Program Files (x86)\Windows Kits\include.0.18362.0\winrt;C:\Program Files (x86)\Windows Kits\include.0.18362.0\cppwinrt
SET PATH=C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\FSharp\;C:\Program Files (x86)\Windows Kits\bin.0.18362.0\x64;C:\Program Files (x86)\Windows Kits\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\Tools\;;C:\WINDOWS\system32;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\VC\Linux\bin\ConnectionManagerExe
SET PWD=/proc/self/cwd
SET RUNFILES_MANIFEST_ONLY=1
SET TEMP=C:\cygwin64\tmp
SET TMP=C:\cygwin64\tmp
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/HostX64/x64/cl.exe /nologo /DCOMPILER_MSVC /DNOMINMAX /D_WIN32_WINNT=0x0601 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /bigobj /Zm500 /EHsc /wd4351 /wd4291 /wd4250 /wd4996 /I. /Ibazel-out/x64_windows-fastbuild/bin /Iexternal/bazel_tools /Ibazel-out/x64_windows-fastbuild/bin/external/bazel_tools /showIncludes /MD /Od /Z7 /wd4117 -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" /Fobazel-out/x64_windows-fastbuild/bin/main/_objs/hello-world/hello-world.obj /c main/hello-world.cc
SUBCOMMAND: # //main:hello-world [action 'Linking main/hello-world.exe', configuration: 1f7c3bee164375155568e7845819f422152903a0240344d14b0c6474746ef185, execution platform: @local_config_platform//:host]
cd C:/cygwin64/home/user/_bazel_user/5r4lncy4/execroot/__main__
SET LIB=C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\ATLMFC\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\lib\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK.8\lib\um\x64;C:\Program Files (x86)\Windows Kits\lib.0.18362.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\lib.0.18362.0\um\x64;
SET PATH=C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\FSharp\;C:\Program Files (x86)\Windows Kits\bin.0.18362.0\x64;C:\Program Files (x86)\Windows Kits\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\Tools\;;C:\WINDOWS\system32;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\VC\Linux\bin\ConnectionManagerExe
SET PWD=/proc/self/cwd
SET RUNFILES_MANIFEST_ONLY=1
SET TEMP=C:\cygwin64\tmp
SET TMP=C:\cygwin64\tmp
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/HostX64/x64/link.exe @bazel-out/x64_windows-fastbuild/bin/main/hello-world.exe-2.params
Target //main:hello-world up-to-date:
bazel-bin/main/hello-world.exe
INFO: Elapsed time: 4.561s, Critical Path: 0.67s
INFO: 2 processes: 2 local.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
知道为什么会这样吗?我正在使用 windows 10。提前谢谢你
当没有定义您自己的工具链时,bazel 会尝试为您执行自动配置,这确实会在定义时考虑到 CC
变量的值。为什么您的裸编译尝试失败从发布的错误消息部分并不完全清楚,但是如果有疑问,您可以将 -s
添加到您的 bazel 命令行以查看调用的命令并查看调用了哪个编译器:
$ CC=/usr/bin/clang bazel build -s //:hello
INFO: Analyzed target //:hello (1 packages loaded, 15 targets configured).
INFO: Found 1 target...
SUBCOMMAND: # //:greet [action 'Compiling greet.cpp', configuration: dd44f3279b21e91676f65783c46b7b7816026ef7e696e80c251418ec64ffdf5e]
(cd /tmp/_bzl/execroot/__main__ && \
exec env - \
PATH=/bin:/usr/bin:/usr/local/bin \
PWD=/proc/self/cwd \
/usr/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -I/usr/lib64/qt/include -MD -MF bazel-out/k8-fastbuild/bin/_objs/greet/greet.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/_objs/greet/greet.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -Ibazel-out/k8-fastbuild/bin/_virtual_includes/greet -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c greet.cpp -o bazel-out/k8-fastbuild/bin/_objs/greet/greet.pic.o)
SUBCOMMAND: # //:hello [action 'Compiling hello.cpp', configuration: dd44f3279b21e91676f65783c46b7b7816026ef7e696e80c251418ec64ffdf5e]
(cd /tmp/_bzl/execroot/__main__ && \
exec env - \
PATH=/bin:/usr/bin:/usr/local/bin \
PWD=/proc/self/cwd \
/usr/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -I/usr/lib64/qt/include -MD -MF bazel-out/k8-fastbuild/bin/_objs/hello/hello.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/_objs/hello/hello.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -Ibazel-out/k8-fastbuild/bin/_virtual_includes/greet -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c hello.cpp -o bazel-out/k8-fastbuild/bin/_objs/hello/hello.pic.o)
SUBCOMMAND: # //:hello [action 'Linking hello', configuration: dd44f3279b21e91676f65783c46b7b7816026ef7e696e80c251418ec64ffdf5e]
(cd /tmp/_bzl/execroot/__main__ && \
exec env - \
PATH=/bin:/usr/bin:/usr/local/bin \
PWD=/proc/self/cwd \
/usr/bin/clang @bazel-out/k8-fastbuild/bin/hello-2.params)
Target //:hello up-to-date:
bazel-bin/hello
INFO: Elapsed time: 1.606s, Critical Path: 0.44s
INFO: 3 processes: 3 linux-sandbox.
INFO: Build completed successfully, 5 total actions
Windows 有点特殊/不同的情况:我已经开始查看相应的 rules_cc
, but from there actually followed back to the docs,目前推荐的方法是添加平台定义(例如,在您的工作区根目录 BUILD
):
platform(
name = "x64_windows-clang-cl",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
"@bazel_tools//tools/cpp:clang-cl",
],
)
并在您的 WORKSPACE
文件中注册额外的工具链:
register_execution_platforms(
":x64_windows-clang-cl"
)
register_toolchains(
"@local_config_cc//:cc-toolchain-x64_windows-clang-cl",
)
然后(现在)使用带边框的 --incompatible_enable_cc_toolchain_resolution
标志。
至少目前,或者你应该能够通过 运行 bazel 告诉 bazel 在 Windows 上使用 LLVM:--compiler=clang-cl
.
您可能需要将 BAZEL_LLVM
(这次)设置为安装 LLVM 的目录(或尝试依靠自动解析)。
也就是说,看起来您无法在 Windows 上使用 cygwin LLVM 编译器,同时仍然依赖自动工具链配置。我怀疑你真的需要为此定义你自己的工具链(不确定在 Windows 的 bazel 和基于 cygwin 的编译器之间跨越边界时你会遇到多大的问题,每个编译器对使用什么路径名都有不同的想法)。
我想强制 Bazel 使用新的编译器来构建我的项目。 据我了解,我可以配置一个新的工具链或使用一些命令行选项。 我尝试了以下选项:
-CC=clang bazel build //main:hello-world
-export CC=C:\cygwin64\bin\clang++
- bazel build //main:hello-world --client_env=CC=clang
我认为这些不起作用,因为如果我用
编译同一个 hello-world.cc 文件clang hello-world.cc
我收到错误:
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
但有
CC=clang bazel build //main:hello-world
or
bazel build //main:hello-world --client_env=CC=clang
构建成功。
知道为什么结果不同吗?如何解决? 我在哪里可以查看 Bazel 使用的是哪个编译器?
编辑:
此处输出:
CC=C:\cygwin64\bin\clang bazel build -s //main:hello-world
Starting local Bazel server and connecting to it...
Loading:
Loading: 0 packages loaded
Analyzing: target //main:hello-world (1 packages loaded, 0 targets configured)
Analyzing: target //main:hello-world (11 packages loaded, 18 targets configured)
INFO: Analyzed target //main:hello-world (14 packages loaded, 59 targets configured).
INFO: Found 1 target...
[0 / 6] [Prepa] BazelWorkspaceStatusAction stable-status.txt ... (4 actions, 0 running)
SUBCOMMAND: # //main:hello-world [action 'Compiling main/hello-world.cc', configuration: 1f7c3bee164375155568e7845819f422152903a0240344d14b0c6474746ef185, execution platform: @local_config_platform//:host]
cd C:/cygwin64/home/user/_bazel_user/5r4lncy4/execroot/__main__
SET INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\ATLMFC\include;C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\include;C:\Program Files (x86)\Windows Kits\NETFXSDK.8\include\um;C:\Program Files (x86)\Windows Kits\include.0.18362.0\ucrt;C:\Program Files (x86)\Windows Kits\include.0.18362.0\shared;C:\Program Files (x86)\Windows Kits\include.0.18362.0\um;C:\Program Files (x86)\Windows Kits\include.0.18362.0\winrt;C:\Program Files (x86)\Windows Kits\include.0.18362.0\cppwinrt
SET PATH=C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\FSharp\;C:\Program Files (x86)\Windows Kits\bin.0.18362.0\x64;C:\Program Files (x86)\Windows Kits\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\Tools\;;C:\WINDOWS\system32;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\VC\Linux\bin\ConnectionManagerExe
SET PWD=/proc/self/cwd
SET RUNFILES_MANIFEST_ONLY=1
SET TEMP=C:\cygwin64\tmp
SET TMP=C:\cygwin64\tmp
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/HostX64/x64/cl.exe /nologo /DCOMPILER_MSVC /DNOMINMAX /D_WIN32_WINNT=0x0601 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /bigobj /Zm500 /EHsc /wd4351 /wd4291 /wd4250 /wd4996 /I. /Ibazel-out/x64_windows-fastbuild/bin /Iexternal/bazel_tools /Ibazel-out/x64_windows-fastbuild/bin/external/bazel_tools /showIncludes /MD /Od /Z7 /wd4117 -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" /Fobazel-out/x64_windows-fastbuild/bin/main/_objs/hello-world/hello-world.obj /c main/hello-world.cc
SUBCOMMAND: # //main:hello-world [action 'Linking main/hello-world.exe', configuration: 1f7c3bee164375155568e7845819f422152903a0240344d14b0c6474746ef185, execution platform: @local_config_platform//:host]
cd C:/cygwin64/home/user/_bazel_user/5r4lncy4/execroot/__main__
SET LIB=C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\ATLMFC\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\lib\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK.8\lib\um\x64;C:\Program Files (x86)\Windows Kits\lib.0.18362.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\lib.0.18362.0\um\x64;
SET PATH=C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.25.28610\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\FSharp\;C:\Program Files (x86)\Windows Kits\bin.0.18362.0\x64;C:\Program Files (x86)\Windows Kits\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\Tools\;;C:\WINDOWS\system32;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\VC\Linux\bin\ConnectionManagerExe
SET PWD=/proc/self/cwd
SET RUNFILES_MANIFEST_ONLY=1
SET TEMP=C:\cygwin64\tmp
SET TMP=C:\cygwin64\tmp
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/HostX64/x64/link.exe @bazel-out/x64_windows-fastbuild/bin/main/hello-world.exe-2.params
Target //main:hello-world up-to-date:
bazel-bin/main/hello-world.exe
INFO: Elapsed time: 4.561s, Critical Path: 0.67s
INFO: 2 processes: 2 local.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
知道为什么会这样吗?我正在使用 windows 10。提前谢谢你
当没有定义您自己的工具链时,bazel 会尝试为您执行自动配置,这确实会在定义时考虑到 CC
变量的值。为什么您的裸编译尝试失败从发布的错误消息部分并不完全清楚,但是如果有疑问,您可以将 -s
添加到您的 bazel 命令行以查看调用的命令并查看调用了哪个编译器:
$ CC=/usr/bin/clang bazel build -s //:hello
INFO: Analyzed target //:hello (1 packages loaded, 15 targets configured).
INFO: Found 1 target...
SUBCOMMAND: # //:greet [action 'Compiling greet.cpp', configuration: dd44f3279b21e91676f65783c46b7b7816026ef7e696e80c251418ec64ffdf5e]
(cd /tmp/_bzl/execroot/__main__ && \
exec env - \
PATH=/bin:/usr/bin:/usr/local/bin \
PWD=/proc/self/cwd \
/usr/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -I/usr/lib64/qt/include -MD -MF bazel-out/k8-fastbuild/bin/_objs/greet/greet.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/_objs/greet/greet.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -Ibazel-out/k8-fastbuild/bin/_virtual_includes/greet -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c greet.cpp -o bazel-out/k8-fastbuild/bin/_objs/greet/greet.pic.o)
SUBCOMMAND: # //:hello [action 'Compiling hello.cpp', configuration: dd44f3279b21e91676f65783c46b7b7816026ef7e696e80c251418ec64ffdf5e]
(cd /tmp/_bzl/execroot/__main__ && \
exec env - \
PATH=/bin:/usr/bin:/usr/local/bin \
PWD=/proc/self/cwd \
/usr/bin/clang -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -fcolor-diagnostics -fno-omit-frame-pointer '-std=c++0x' -I/usr/lib64/qt/include -MD -MF bazel-out/k8-fastbuild/bin/_objs/hello/hello.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/_objs/hello/hello.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -Ibazel-out/k8-fastbuild/bin/_virtual_includes/greet -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c hello.cpp -o bazel-out/k8-fastbuild/bin/_objs/hello/hello.pic.o)
SUBCOMMAND: # //:hello [action 'Linking hello', configuration: dd44f3279b21e91676f65783c46b7b7816026ef7e696e80c251418ec64ffdf5e]
(cd /tmp/_bzl/execroot/__main__ && \
exec env - \
PATH=/bin:/usr/bin:/usr/local/bin \
PWD=/proc/self/cwd \
/usr/bin/clang @bazel-out/k8-fastbuild/bin/hello-2.params)
Target //:hello up-to-date:
bazel-bin/hello
INFO: Elapsed time: 1.606s, Critical Path: 0.44s
INFO: 3 processes: 3 linux-sandbox.
INFO: Build completed successfully, 5 total actions
Windows 有点特殊/不同的情况:我已经开始查看相应的 rules_cc
, but from there actually followed back to the docs,目前推荐的方法是添加平台定义(例如,在您的工作区根目录 BUILD
):
platform(
name = "x64_windows-clang-cl",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
"@bazel_tools//tools/cpp:clang-cl",
],
)
并在您的 WORKSPACE
文件中注册额外的工具链:
register_execution_platforms(
":x64_windows-clang-cl"
)
register_toolchains(
"@local_config_cc//:cc-toolchain-x64_windows-clang-cl",
)
然后(现在)使用带边框的 --incompatible_enable_cc_toolchain_resolution
标志。
至少目前,或者你应该能够通过 运行 bazel 告诉 bazel 在 Windows 上使用 LLVM:--compiler=clang-cl
.
您可能需要将 BAZEL_LLVM
(这次)设置为安装 LLVM 的目录(或尝试依靠自动解析)。
也就是说,看起来您无法在 Windows 上使用 cygwin LLVM 编译器,同时仍然依赖自动工具链配置。我怀疑你真的需要为此定义你自己的工具链(不确定在 Windows 的 bazel 和基于 cygwin 的编译器之间跨越边界时你会遇到多大的问题,每个编译器对使用什么路径名都有不同的想法)。