บทแนะนำ Bazel: กำหนดค่าเชนเครื่องมือ C++

รายงานปัญหา ดูแหล่งที่มา Nightly · 8.4 · 8.3 · 8.2 · 8.1 · 8.0 · 7.6

บทแนะนำนี้ใช้สถานการณ์ตัวอย่างเพื่ออธิบายวิธีกำหนดค่าชุดเครื่องมือ C++ สำหรับโปรเจ็กต์

สิ่งที่คุณจะได้เรียนรู้

ในบทแนะนำนี้ คุณจะได้เรียนรู้วิธีทำสิ่งต่อไปนี้

  • ตั้งค่าสภาพแวดล้อมของบิลด์
  • ใช้ --toolchain_resolution_debug เพื่อแก้ไขข้อบกพร่องในการแก้ปัญหา 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 ซึ่งคุณติดตั้งในระบบได้

ตั้งค่าสภาพแวดล้อมของบิลด์

ตั้งค่าสภาพแวดล้อมการสร้างดังนี้

  1. หากยังไม่ได้ดำเนินการ ให้ดาวน์โหลดและติดตั้ง Bazel 7.0.2 ขึ้นไป

  2. เพิ่มไฟล์ MODULE.bazel ที่ว่างเปล่าในโฟลเดอร์รูท

  3. เพิ่มเป้าหมาย cc_binary ต่อไปนี้ลงในไฟล์ main/BUILD

    cc_binary(
        name = "hello-world",
        srcs = ["hello-world.cc"],
    )
    

    เนื่องจาก Bazel ใช้เครื่องมือภายในหลายอย่างที่เขียนด้วย C++ ในระหว่างการบิลด์ เช่น process-wrapper จึงมีการระบุ Toolchain C++ เริ่มต้นที่มีอยู่แล้วสำหรับแพลตฟอร์มโฮสต์ ซึ่งจะช่วยให้เครื่องมือภายในเหล่านี้สร้างได้โดยใช้ เครื่องมือเชนของเครื่องมือที่สร้างขึ้นในบทแนะนำนี้ ดังนั้น cc_binary เป้าหมาย จึงสร้างขึ้นด้วย Toolchain เริ่มต้นเช่นกัน

  4. เรียกใช้บิลด์ด้วยคำสั่งต่อไปนี้

    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
    

    หากไม่ได้ระบุ --platforms Bazel จะสร้างเป้าหมายสำหรับ @platforms//host โดยใช้ @bazel_tools+cc_configure_extension+local_config_cc//:cc-compiler-k8

กำหนดค่าเครื่องมือ C++

หากต้องการกำหนดค่า Toolchain C++ ให้สร้างแอปพลิเคชันซ้ำๆ และแก้ไข ข้อผิดพลาดแต่ละรายการทีละรายการตามที่อธิบายไว้ดังนี้

นอกจากนี้ยังถือว่า clang version 9.0.1 แม้ว่ารายละเอียดจะเปลี่ยนไปเล็กน้อยระหว่าง clang เวอร์ชันต่างๆ

  1. เพิ่ม 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 สำหรับ การกำหนดค่าโฮสต์

  2. เรียกใช้บิลด์อีกครั้ง เนื่องจากแพ็กเกจ 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.
    
  3. ในไฟล์ toolchain/BUILD ให้กำหนดกลุ่มไฟล์ว่างดังนี้

    package(default_visibility = ["//visibility:public"])
    
    filegroup(name = "linux_x86_64_toolchain_config")
    
  4. เรียกใช้บิลด์อีกครั้ง 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กฎ ให้เพิ่มคำสั่ง load ไปยังtoolchain/BUILD ใต้คำสั่ง package โดยตรง

    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")
    
  5. เรียกใช้บิลด์อีกครั้ง 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" ด้วยเหตุผลทางประวัติศาสตร์

  6. เรียกใช้บิลด์อีกครั้ง 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,
    )
    
  7. เรียกใช้คำสั่งบิลด์อีกครั้ง คุณจะเห็นข้อผิดพลาดดังนี้

    /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 ไม่ต้องระบุ แฟล็กนี้

    คัดลอกโค้ดต่อไปนี้ไปยัง 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++"

  8. เมื่อเรียกใช้ bazel build //main:hello-world แล้ว ควรสร้างไบนารี สําหรับโฮสต์ได้สําเร็จ

  9. ใน toolchain/BUILD ให้คัดลอกเป้าหมาย cc_toolchain_config, cc_toolchain และ toolchain แล้วแทนที่ linux_x86_64 ด้วย android_x86_64 ในชื่อเป้าหมาย

    ใน MODULE.bazel ให้ลงทะเบียน Toolchain สำหรับ Android

    register_toolchains(
        "//toolchain:cc_toolchain_for_linux_x86_64",
        "//toolchain:cc_toolchain_for_android_x86_64"
    )
    
  10. เรียกใช้ bazel build //main:hello-world --android_platforms=//toolchain:android_x86_64 เพื่อสร้างไบนารีสำหรับ Android

ในทางปฏิบัติ Linux และ Android ควรมีการกำหนดค่า Toolchain C++ ที่แตกต่างกัน คุณสามารถแก้ไข cc_toolchain_config ที่มีอยู่สำหรับความแตกต่าง หรือสร้างกฎแยกต่างหาก (เช่น ผู้ให้บริการ CcToolchainConfigInfo) สำหรับแพลตฟอร์มแยกต่างหาก

ตรวจสอบงาน

ในบทแนะนำนี้ คุณได้เรียนรู้วิธีกำหนดค่า Toolchain C++ พื้นฐาน แต่ Toolchain มีประสิทธิภาพมากกว่าตัวอย่างนี้

ประเด็นสำคัญมีดังนี้

  • คุณต้องระบุแฟล็ก platforms ที่ตรงกันในบรรทัดคำสั่งเพื่อให้ Bazel แก้ไข Toolchain สำหรับค่าข้อจำกัดเดียวกันใน แพลตฟอร์ม เอกสารประกอบมีข้อมูลเพิ่มเติมเกี่ยวกับค่าสถานะการกำหนดค่าเฉพาะภาษา
  • คุณต้องแจ้งให้ Toolchain ทราบว่าเครื่องมืออยู่ที่ใด ในบทแนะนำนี้ มีเวอร์ชันที่ง่ายขึ้นซึ่งคุณจะเข้าถึงเครื่องมือจากระบบได้ หากสนใจแนวทางที่ครอบคลุมมากขึ้น โปรดอ่านเกี่ยวกับการอ้างอิงภายนอก เครื่องมือของคุณอาจมาจากโมดูลอื่น และคุณจะต้องทำให้ไฟล์ของเครื่องมือพร้อมใช้งานกับ cc_toolchain โดยมีทรัพยากร Dependency เป้าหมายในแอตทริบิวต์ เช่น compiler_files tool_paths ก็จะต้องมีการเปลี่ยนแปลงด้วยเช่นกัน
  • คุณสามารถสร้างฟีเจอร์เพื่อปรับแต่งว่าควรส่งแฟล็กใดไปยัง การดำเนินการต่างๆ ไม่ว่าจะเป็นการลิงก์หรือการดำเนินการประเภทอื่นๆ

อ่านเพิ่มเติม

โปรดดูรายละเอียดเพิ่มเติมที่หัวข้อการกำหนดค่า ชุดเครื่องมือ C++