บทแนะนำนี้ใช้สถานการณ์ตัวอย่างเพื่ออธิบายวิธีกำหนดค่า Toolchain ของ C++ สำหรับโปรเจ็กต์
สิ่งที่คุณจะได้เรียนรู้
ในบทแนะนำนี้ คุณจะได้เรียนรู้วิธีการต่อไปนี้
- ตั้งค่าสภาพแวดล้อมการสร้าง
- ใช้
--toolchain_resolution_debugเพื่อแก้ปัญหาการแก้ปัญหา Toolchain - กำหนดค่า Toolchain ของ C++
- สร้างกฎ Starlark ที่ให้การกำหนดค่าเพิ่มเติมสำหรับ
cc_toolchainเพื่อให้ Bazel สร้างแอปพลิเคชันด้วยclangได้ - สร้างไบนารี C++ โดยเรียกใช้
bazel build //main:hello-worldในเครื่องระบบ Linux - คอมไพล์ข้ามไบนารีสำหรับ Android โดยเรียกใช้
bazel build //main:hello-world --platforms=//:android_x86_64
ก่อนเริ่มต้น
บทแนะนำนี้จะถือว่าคุณใช้ระบบ Linux และสร้างแอปพลิเคชัน C++
รวมถึงติดตั้งเครื่องมือและไลบรารีที่เหมาะสมเรียบร้อยแล้ว บทแนะนำนี้
ใช้ clang version 19 ซึ่งคุณสามารถติดตั้งในระบบได้
ตั้งค่าสภาพแวดล้อมการสร้าง
ตั้งค่าสภาพแวดล้อมการสร้างดังนี้
ดาวน์โหลดและติดตั้ง Bazel 7.0.2 ขึ้นไป หากยังไม่ได้ทำ
เพิ่มไฟล์
MODULE.bazelว่างเปล่าในโฟลเดอร์รากเพิ่มเป้าหมาย
cc_binaryต่อไปนี้ลงในไฟล์main/BUILDcc_binary( name = "hello-world", srcs = ["hello-world.cc"], )เนื่องจาก Bazel ใช้เครื่องมือภายในหลายรายการที่เขียนด้วย C++ ระหว่างการสร้าง เช่น
process-wrapperระบบจึงระบุ Toolchain ของ C++ เริ่มต้นที่มีอยู่แล้วสำหรับแพลตฟอร์มโฮสต์ ซึ่งจะช่วยให้เครื่องมือภายในเหล่านี้สร้างโดยใช้ Toolchain ที่สร้างขึ้นในบทแนะนำนี้ได้ ดังนั้น ระบบจะสร้างเป้าหมายcc_binarytarget ด้วย Toolchain เริ่มต้นด้วยเรียกใช้การสร้างด้วยคำสั่งต่อไปนี้
bazel build //main:hello-worldการสร้างจะสำเร็จโดยไม่มี Toolchain ที่ลงทะเบียนไว้ใน
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หากไม่ได้ระบุ
--platformsBazel จะสร้างเป้าหมายสำหรับ@platforms//hostโดยใช้@bazel_tools+cc_configure_extension+local_config_cc//:cc-compiler-k8
กำหนดค่า Toolchain ของ C++
หากต้องการกำหนดค่า Toolchain ของ 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", ], )จากนั้นเพิ่มทรัพยากร Dependency ที่เหมาะสมและลงทะเบียน Toolchain ด้วย
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_configBazel จึงแสดงข้อผิดพลาดต่อไปนี้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ให้กำหนด filegroup ว่างเปล่าดังนี้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เป็นผู้ให้บริการที่คุณใช้กำหนดค่า Toolchain ของ C++ หากต้องการแก้ไขข้อผิดพลาดนี้ ให้สร้างกฎ Starlark ที่ให้CcToolchainConfigInfoแก่ Bazel โดยสร้างไฟล์toolchain/cc_toolchain_config.bzlที่มีเนื้อหาต่อไปนี้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")และแทนที่ filegroup "linux_x86_64_toolchain_config" ด้วยการประกาศ กฎ
cc_toolchain_configcc_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 ว่าจะใช้เครื่องมือใด โดยคุณต้องใช้ตัวสร้าง
tool_path()จาก@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl# 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 ต้องทราบตำแหน่งที่จะค้นหาส่วนหัวที่รวมไว้ คุณสามารถแก้ปัญหานี้ได้หลายวิธี เช่น ใช้แอตทริบิวต์
includesของcc_binaryแต่ในที่นี้เราจะแก้ปัญหาที่ระดับ Toolchain ด้วยพารามิเตอร์cxx_builtin_include_directoriesของcc_common.create_cc_toolchain_config_infoโปรดทราบว่าหากคุณ ใช้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++ และค้นหาสัญลักษณ์ของไลบรารีไม่ได้ คุณสามารถแก้ปัญหานี้ได้หลายวิธี เช่น ใช้แอตทริบิวต์
linkoptsของcc_binaryในที่นี้เราจะแก้ปัญหาโดย ตรวจสอบว่าเป้าหมายใดก็ตามที่ใช้ Toolchain ไม่ต้องระบุ แฟล็กนี้คัดลอกโค้ดต่อไปนี้ไปยัง
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ใน ชื่อเป้าหมายลงทะเบียน Toolchain สำหรับ Android ใน
MODULE.bazelregister_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 ควรมีการกำหนดค่า Toolchain ของ C++ ที่แตกต่างกัน คุณ
สามารถแก้ไข cc_toolchain_config ที่มีอยู่สำหรับความแตกต่าง หรือ
สร้างกฎแยกกัน (เช่น ผู้ให้บริการ CcToolchainConfigInfo) สำหรับแพลต101}ฟอร์มแยกกัน
ตรวจสอบงานของคุณ
ในบทแนะนำนี้ คุณได้เรียนรู้วิธีกำหนดค่า Toolchain ของ C++ พื้นฐาน แต่ Toolchain มีประสิทธิภาพมากกว่าตัวอย่างนี้
บทสรุปสำคัญมีดังนี้
- คุณต้องระบุแฟล็ก
platformsที่ตรงกันในบรรทัดคำสั่งเพื่อให้ Bazel แก้ปัญหา Toolchain สำหรับค่าข้อจำกัดเดียวกันใน แพลตฟอร์ม เอกสารประกอบมีข้อมูลเพิ่มเติมเกี่ยวกับแฟล็กการกำหนดค่าเฉพาะภาษา - คุณต้องแจ้งให้ Toolchain ทราบตำแหน่งที่เครื่องมืออยู่ ในบทแนะนำนี้
มีเวอร์ชันที่ง่ายขึ้นซึ่งคุณเข้าถึงเครื่องมือจากระบบได้ หาก
สนใจแนวทางที่ครอบคลุมมากขึ้น คุณสามารถอ่านข้อมูลเกี่ยวกับ
ทรัพยากร Dependency ภายนอกได้ เครื่องมืออาจมาจากโมดูลอื่น และคุณจะต้องทำให้ไฟล์ของเครื่องมือพร้อมใช้งานสำหรับ
cc_toolchainโดยมีทรัพยากร Dependency ของเป้าหมายในแอตทริบิวต์ เช่นcompiler_filesนอกจากนี้ คุณจะต้องเปลี่ยนtool_pathsด้วย - คุณสามารถสร้างฟีเจอร์เพื่อปรับแต่งแฟล็กที่จะส่งไปยัง การดำเนินการต่างๆ ไม่ว่าจะเป็นการลิงก์หรือการดำเนินการประเภทอื่นๆ
อ่านเพิ่มเติม
ดูรายละเอียดเพิ่มเติมได้ที่การกำหนดค่า Toolchain ของ C++