บทแนะนำนี้ใช้สถานการณ์ตัวอย่างในการอธิบายวิธีกำหนดค่าเครื่องมือเชน C++ สำหรับโปรเจ็กต์
สิ่งที่คุณจะได้เรียนรู้
ในบทแนะนำนี้ คุณจะได้เรียนรู้วิธีต่อไปนี้
- ตั้งค่าสภาพแวดล้อมของบิลด์
- ใช้
--toolchain_resolution_debug
เพื่อแก้ไขข้อบกพร่องของการแปลงเครื่องมือเชน - กำหนดค่าห่วงโซ่เครื่องมือ 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 16
ซึ่งติดตั้งในระบบได้
ตั้งค่าสภาพแวดล้อมของบิลด์
ตั้งค่าสภาพแวดล้อมของบิลด์ดังนี้
หากยังไม่ได้ดำเนินการ ให้ดาวน์โหลดและติดตั้ง Bazel 7.0.2 ขึ้นไป
เพิ่มไฟล์
WORKSPACE
ที่ว่างเปล่าในโฟลเดอร์รูทเพิ่มเป้าหมาย
cc_binary
ต่อไปนี้ในไฟล์main/BUILD
load("@rules_cc//cc:defs.bzl", "cc_binary") cc_binary( name = "hello-world", srcs = ["hello-world.cc"], )
เนื่องจาก Bazel ใช้เครื่องมือภายในจำนวนมากที่เขียนด้วย C++ ระหว่างการสร้าง เช่น
process-wrapper
เชนเครื่องมือ C++ เริ่มต้นที่มีอยู่แล้วจึงมีการระบุสำหรับแพลตฟอร์มโฮสต์ วิธีนี้จะช่วยให้เครื่องมือภายในเหล่านี้สร้างโดยใช้เครื่องมือ ชุดนั้นของเครื่องมือที่สร้างในบทแนะนำนี้ ดังนั้น เป้าหมายcc_binary
จะสร้างขึ้นด้วย Toolchain เริ่มต้นเช่นกันเรียกใช้บิลด์ด้วยคำสั่งต่อไปนี้
bazel build //main:hello-world
บิลด์จะสําเร็จโดยไม่ต้องลงทะเบียนเชนเครื่องมือใน
WORKSPACE
หากต้องการดูสิ่งที่อยู่เบื้องหลัง ให้เรียกใช้คำสั่งต่อไปนี้
bazel build //main:hello-world --toolchain_resolution_debug='@bazel_tools//tools/cpp:toolchain_type' INFO: ToolchainResolution: Target platform @@local_config_platform//:host: Selected execution platform @@local_config_platform//:host, type @@bazel_tools//tools/cpp:toolchain_type -> toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-k8
หากไม่ได้ระบุ
--platforms
Bazel จะสร้างเป้าหมายสำหรับ@local_config_platform//: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", ], )
จากนั้นลงทะเบียน Toolchain กับ
WORKSPACE
ด้วยregister_toolchains( "//toolchain:cc_toolchain_for_linux_x86_64" )
ขั้นตอนนี้จะกำหนด
cc_toolchain
และเชื่อมโยงกับเป้าหมายtoolchain
สำหรับการกำหนดค่าโฮสต์เรียกใช้บิลด์อีกครั้ง เนื่องจากแพ็กเกจ
toolchain
ยังไม่ได้กำหนดเป้าหมายlinux_x86_64_toolchain_config
เบเซลจึงแสดงข้อผิดพลาดต่อไปนี้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 ที่ระบุ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")
และแทนที่กลุ่มไฟล์ "linux_x86_64_toolchain_config" ด้วยการประกาศกฎ
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 ว่าควรใช้เครื่องมือใด คุณจำเป็นต้องใช้ตัวสร้าง 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", 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
เป็นเส้นทางที่ถูกต้องสำหรับระบบของคุณเรียกใช้บิลด์อีกครั้ง 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-16/lib/clang/16/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'
เนื่องจาก Linker ไม่มีไลบรารีมาตรฐาน C++ และไม่พบสัญลักษณ์ ซึ่งทําได้หลายวิธี เช่น การใช้แอตทริบิวต์
linkopts
ของcc_binary
ซึ่งแก้ไขได้โดยการตรวจสอบว่าเป้าหมายใดๆ ที่ใช้ Toolchain ไม่จำเป็นต้องระบุ Flag นี้คัดลอกรหัสต่อไปนี้ไปยัง
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", 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-9/lib/clang/9.0.1/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], )
เมื่อเรียกใช้
bazel build //main:hello-world
ระบบจะสร้างไบนารีสำหรับโฮสต์ได้สำเร็จใน
toolchain/BUILD
ให้คัดลอกเป้าหมายcc_toolchain_config
,cc_toolchain
และtoolchain
แล้วแทนที่linux_x86_64
ด้วยandroid_x86_64
ในชื่อเป้าหมายลงทะเบียน Toolchain สำหรับ Android ใน
WORKSPACE
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++ พื้นฐาน แต่ Toolchain มีประสิทธิภาพมากกว่าตัวอย่างง่ายๆ นี้
สิ่งสำคัญที่ได้จากการเรียนรู้มีดังนี้
- คุณต้องระบุแฟล็ก
platforms
ที่ตรงกันในบรรทัดคำสั่งเพื่อให้ Bazel แปลงค่าเป็น Toolchain สำหรับค่าข้อจำกัดเดียวกันบนแพลตฟอร์ม เอกสารประกอบนี้มีข้อมูลเพิ่มเติมเกี่ยวกับแฟล็กการกำหนดค่าเฉพาะภาษา - คุณจะต้องแจ้งให้ Toolchain ทราบว่าเครื่องมือมีอยู่ที่ใด ในบทแนะนำนี้จะมีเวอร์ชันที่เรียบง่ายให้คุณเข้าถึงเครื่องมือจากระบบ หากสนใจแนวทางที่สามารถดำเนินการได้ด้วยตัวเอง โปรดอ่านเกี่ยวกับพื้นที่ทำงาน เครื่องมือของคุณอาจมาจากพื้นที่ทำงานอื่นและคุณจะต้องทำให้ไฟล์ใช้ได้ใน
cc_toolchain
ด้วยทรัพยากร Dependency เป้าหมายในแอตทริบิวต์ เช่นcompiler_files
คุณก็ต้องเปลี่ยนtool_paths
เช่นกัน - คุณสามารถสร้างฟีเจอร์เพื่อปรับแต่งว่าควรส่งแฟล็กใดไปยังการดำเนินการต่างๆ ไม่ว่าจะเป็นการลิงก์หรือการดำเนินการประเภทอื่นๆ
อ่านเพิ่มเติม
ดูรายละเอียดเพิ่มเติมได้ที่การกำหนดค่าเชนเครื่องมือ C++