本教學課程會使用範例情境,說明如何為專案設定 C++ 工具鍊。
課程內容
在本教學課程中,您將瞭解如何:
- 設定建構環境
- 使用
--toolchain_resolution_debug
偵錯工具鍊解析度 - 設定 C++ 工具鍊
- 建立 Starlark 規則,為
cc_toolchain
提供額外設定,讓 Bazel 可以使用clang
建構應用程式 - 在 Linux 電腦上執行
bazel build //main:hello-world
,建構 C++ 二進位檔 - 執行
bazel build //main:hello-world --platforms=//:android_x86_64
,跨平台編譯 Android 適用的二進位檔
事前準備
本教學課程假設您使用 Linux,且已成功建構 C++ 應用程式,並安裝適當的工具和程式庫。本教學課程使用 clang version 19
,您可以在系統上安裝這個工具。
設定建構環境
請依下列步驟設定建構環境:
如果尚未完成,請下載並安裝 Bazel 7.0.2 以上版本。
在根資料夾中新增空白的
MODULE.bazel
檔案。在
main/BUILD
檔案中新增下列cc_binary
目標:cc_binary( name = "hello-world", srcs = ["hello-world.cc"], )
由於 Bazel 在建構期間會使用許多以 C++ 撰寫的內部工具 (例如
process-wrapper
),因此系統會為主機平台指定預先存在的預設 C++ 工具鍊。這樣一來,這些內部工具就能使用本教學課程中建立的工具鍊進行建構。因此,cc_binary
目標也會使用預設工具鍊建構。執行下列指令來建構:
bazel build //main:hello-world
建構成功,但
MODULE.bazel
中未註冊任何工具鍊。如要進一步瞭解幕後運作方式,請執行下列指令:
bazel build //main:hello-world --toolchain_resolution_debug='@bazel_tools//tools/cpp:toolchain_type' INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host, type @@bazel_tools//tools/cpp:toolchain_type -> toolchain @@bazel_tools+cc_configure_extension+local_config_cc//:cc-compiler-k8
如果未指定
--platforms
,Bazel 會使用@platforms//host
建構@bazel_tools+cc_configure_extension+local_config_cc//:cc-compiler-k8
的目標。
設定 C++ 工具鍊
如要設定 C++ 工具鍊,請重複建構應用程式,並按照下列說明逐一排除每個錯誤。
此外,它也假設 clang version 9.0.1
,但詳細資料在不同版本的 Clang 之間只會略有變動。
新增「
toolchain/BUILD
」filegroup(name = "empty") cc_toolchain( name = "linux_x86_64_toolchain", toolchain_identifier = "linux_x86_64-toolchain", toolchain_config = ":linux_x86_64_toolchain_config", all_files = ":empty", compiler_files = ":empty", dwp_files = ":empty", linker_files = ":empty", objcopy_files = ":empty", strip_files = ":empty", supports_param_files = 0, ) toolchain( name = "cc_toolchain_for_linux_x86_64", toolchain = ":linux_x86_64_toolchain", toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", exec_compatible_with = [ "@platforms//cpu:x86_64", "@platforms//os:linux", ], target_compatible_with = [ "@platforms//cpu:x86_64", "@platforms//os:linux", ], )
然後新增適當的依附元件,並使用
MODULE.bazel
註冊工具鍊,bazel_dep(name = "platforms", version = "0.0.10") register_toolchains( "//toolchain:cc_toolchain_for_linux_x86_64" )
這個步驟會定義
cc_toolchain
,並將其繫結至主機設定的toolchain
目標。再次執行建構作業。由於
toolchain
套件尚未定義linux_x86_64_toolchain_config
目標,因此 Bazel 會擲回下列錯誤:ERROR: toolchain/BUILD:4:13: in toolchain_config attribute of cc_toolchain rule //toolchain:linux_x86_64_toolchain: rule '//toolchain:linux_x86_64_toolchain_config' does not exist.
在
toolchain/BUILD
檔案中,定義空白的檔案群組,如下所示:package(default_visibility = ["//visibility:public"]) filegroup(name = "linux_x86_64_toolchain_config")
再次執行建構作業。Bazel 會擲回下列錯誤:
'//toolchain:linux_x86_64_toolchain_config' does not have mandatory providers: 'CcToolchainConfigInfo'.
CcToolchainConfigInfo
是用來設定 C++ 工具鍊的供應商。如要修正這項錯誤,請建立 Starlark 規則,透過建立包含下列內容的toolchain/cc_toolchain_config.bzl
檔案,向 Bazel 提供CcToolchainConfigInfo
:def _impl(ctx): return cc_common.create_cc_toolchain_config_info( ctx = ctx, toolchain_identifier = "k8-toolchain", host_system_name = "local", target_system_name = "local", target_cpu = "k8", target_libc = "unknown", compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", ) cc_toolchain_config = rule( implementation = _impl, attrs = {}, provides = [CcToolchainConfigInfo], )
cc_common.create_cc_toolchain_config_info()
建立所需的提供者CcToolchainConfigInfo
。如要使用cc_toolchain_config
規則,請在套件陳述式正下方,將載入陳述式新增至toolchain/BUILD
:load(":cc_toolchain_config.bzl", "cc_toolchain_config")
並將「linux_x86_64_toolchain_config」filegroup 替換為
cc_toolchain_config
規則的宣告:cc_toolchain_config(name = "linux_x86_64_toolchain_config")
再次執行建構作業。Bazel 會擲回下列錯誤:
.../BUILD:1:1: C++ compilation of rule '//:hello-world' failed (Exit 1) src/main/tools/linux-sandbox-pid1.cc:421: "execvp(toolchain/DUMMY_GCC_TOOL, 0x11f20e0)": No such file or directory Target //:hello-world failed to build`
此時,Bazel 已有足夠的資訊可嘗試建構程式碼,但仍不知道要使用哪些工具完成必要的建構動作。您將修改 Starlark 規則實作,告知 Bazel 要使用哪些工具。為此,您需要
@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl
中的tool_path()
建構函式:# toolchain/cc_toolchain_config.bzl: # NEW load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "tool_path") def _impl(ctx): tool_paths = [ # NEW tool_path( name = "gcc", # Compiler is referenced by the name "gcc" for historic reasons. path = "/usr/bin/clang", ), tool_path( name = "ld", path = "/usr/bin/ld", ), tool_path( name = "ar", path = "/usr/bin/ar", ), tool_path( name = "cpp", path = "/bin/false", ), tool_path( name = "gcov", path = "/bin/false", ), tool_path( name = "nm", path = "/bin/false", ), tool_path( name = "objdump", path = "/bin/false", ), tool_path( name = "strip", path = "/bin/false", ), ] return cc_common.create_cc_toolchain_config_info( ctx = ctx, toolchain_identifier = "local", host_system_name = "local", target_system_name = "local", target_cpu = "k8", target_libc = "unknown", compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", tool_paths = tool_paths, # NEW )
確認
/usr/bin/clang
和/usr/bin/ld
是系統的正確路徑。請注意,出於歷史原因,編譯器是以「gcc」名稱參照。再次執行建構作業。Bazel 會擲回下列錯誤:
ERROR: main/BUILD:3:10: Compiling main/hello-world.cc failed: absolute path inclusion(s) found in rule '//main:hello-world': the source file 'main/hello-world.cc' includes the following non-builtin files with absolute paths (if these are builtin files, make sure these paths are in your toolchain): '/usr/include/c++/13/ctime' '/usr/include/x86_64-linux-gnu/c++/13/bits/c++config.h' '/usr/include/x86_64-linux-gnu/c++/13/bits/os_defines.h' ...
Bazel 必須知道要在何處搜尋內含的標頭。解決這個問題的方法有很多種,例如使用
cc_binary
的includes
屬性,但這裡是在工具鍊層級解決,使用cc_common.create_cc_toolchain_config_info
的cxx_builtin_include_directories
參數。請注意,如果您使用其他版本的clang
,包含路徑會有所不同。這些路徑也可能因發行版本而異。修改
toolchain/cc_toolchain_config.bzl
中的傳回值,如下所示:return cc_common.create_cc_toolchain_config_info( ctx = ctx, cxx_builtin_include_directories = [ # NEW "/usr/lib/llvm-19/lib/clang/19/include", "/usr/include", ], toolchain_identifier = "local", host_system_name = "local", target_system_name = "local", target_cpu = "k8", target_libc = "unknown", compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", tool_paths = tool_paths, )
再次執行建構指令,您會看到類似下列的錯誤:
/usr/bin/ld: bazel-out/k8-fastbuild/bin/main/_objs/hello-world/hello-world.o: in function `print_localtime()': hello-world.cc:(.text+0x68): undefined reference to `std::cout'
這是因為連結器缺少 C++ 標準程式庫,因此找不到符號。解決方法有很多種,例如使用
cc_binary
的linkopts
屬性。這裡的解決方法是確保使用工具鍊的任何目標都不必指定這個標記。將下列程式碼複製到
toolchain/cc_toolchain_config.bzl
:# NEW load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") # NEW load( "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", # NEW "flag_group", # NEW "flag_set", # NEW "tool_path", ) all_link_actions = [ # NEW ACTION_NAMES.cpp_link_executable, ACTION_NAMES.cpp_link_dynamic_library, ACTION_NAMES.cpp_link_nodeps_dynamic_library, ] def _impl(ctx): tool_paths = [ tool_path( name = "gcc", # Compiler is referenced by the name "gcc" for historic reasons. path = "/usr/bin/clang", ), tool_path( name = "ld", path = "/usr/bin/ld", ), tool_path( name = "ar", path = "/bin/false", ), tool_path( name = "cpp", path = "/bin/false", ), tool_path( name = "gcov", path = "/bin/false", ), tool_path( name = "nm", path = "/bin/false", ), tool_path( name = "objdump", path = "/bin/false", ), tool_path( name = "strip", path = "/bin/false", ), ] features = [ # NEW feature( name = "default_linker_flags", enabled = True, flag_sets = [ flag_set( actions = all_link_actions, flag_groups = ([ flag_group( flags = [ "-lstdc++", ], ), ]), ), ], ), ] return cc_common.create_cc_toolchain_config_info( ctx = ctx, features = features, # NEW cxx_builtin_include_directories = [ "/usr/lib/llvm-19/lib/clang/19/include", "/usr/include", ], toolchain_identifier = "local", host_system_name = "local", target_system_name = "local", target_cpu = "k8", target_libc = "unknown", compiler = "clang", abi_version = "unknown", abi_libc_version = "unknown", tool_paths = tool_paths, ) cc_toolchain_config = rule( implementation = _impl, attrs = {}, provides = [CcToolchainConfigInfo], )
請注意,這段程式碼使用 GNU C++ 程式庫 libstdc++。如要使用 LLVM C++ 程式庫,請使用「-lc++」而非「-lstdc++」。
執行
bazel build //main:hello-world
後,應該就能成功為主機建構二進位檔。在
toolchain/BUILD
中,複製cc_toolchain_config
、cc_toolchain
和toolchain
目標,並將目標名稱中的linux_x86_64
替換為android_x86_64
。在
MODULE.bazel
中,註冊 Android 的工具鍊register_toolchains( "//toolchain:cc_toolchain_for_linux_x86_64", "//toolchain:cc_toolchain_for_android_x86_64" )
執行
bazel build //main:hello-world --android_platforms=//toolchain:android_x86_64
,為 Android 建構二進位檔。
實際上,Linux 和 Android 應有不同的 C++ 工具鍊設定。您可以修改現有的 cc_toolchain_config
來因應差異,也可以為不同平台建立個別規則 (即 CcToolchainConfigInfo
提供者)。
查看作業
在本教學課程中,您已瞭解如何設定基本的 C++ 工具鍊,但工具鍊的功能比這個範例更強大。
重點如下:
- 您需要在指令列中指定相符的
platforms
旗標,Bazel 才能在平台上解析出相同限制值適用的工具鍊。如要進一步瞭解語言專屬設定旗標,請參閱說明文件。 - 您必須讓工具鍊知道工具所在位置。本教學課程提供簡化版,您可從系統存取工具。如要瞭解更獨立的方法,請參閱外部依附元件。您的工具可能來自不同模組,您必須透過目標依附元件 (例如
compiler_files
) 將工具檔案提供給cc_toolchain
。tool_paths
也需要變更。 - 您可以建立功能,自訂應傳遞至不同動作的標記,無論是連結或其他類型的動作。
延伸閱讀
詳情請參閱 C++ 工具鍊設定。