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

บทแนะนำนี้ใช้สถานการณ์ตัวอย่างเพื่ออธิบายวิธีกำหนดค่า 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 ซึ่งคุณสามารถติดตั้งในระบบได้

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

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

  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++ เริ่มต้นที่มีอยู่แล้วสำหรับแพลตฟอร์มโฮสต์ ซึ่งจะช่วยให้เครื่องมือภายในเหล่านี้สร้างโดยใช้ Toolchain ที่สร้างขึ้นในบทแนะนำนี้ได้ ดังนั้น ระบบจะสร้างเป้าหมาย cc_binary target ด้วย 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

กำหนดค่า Toolchain ของ 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 ให้กำหนด filegroup ว่างเปล่าดังนี้

    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 เป็นผู้ให้บริการที่คุณใช้กำหนดค่า 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_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'
    

    สาเหตุของข้อผิดพลาดนี้คือตัวลิงก์ไม่มีไลบรารีมาตรฐาน 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ใน ชื่อเป้าหมาย

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

    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) สำหรับแพลต101}ฟอร์มแยกกัน

ตรวจสอบงานของคุณ

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

บทสรุปสำคัญมีดังนี้

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

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

ดูรายละเอียดเพิ่มเติมได้ที่การกำหนดค่า Toolchain ของ C++