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

วันที่ รายงานปัญหา ดูแหล่งที่มา ตอนกลางคืน · 7.3 · 7.2 · 7.1 · 7.0 · 6.5

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

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

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

  • ตั้งค่าสภาพแวดล้อมของบิลด์
  • กำหนดค่าเครื่องมือเชน C++
  • สร้างกฎ Starlark ที่ให้องค์ประกอบเพิ่มเติม การกำหนดค่าสำหรับ cc_toolchain เพื่อให้ Bazel สร้างแอปพลิเคชันได้ กับ clang
  • ยืนยันผลลัพธ์ที่คาดหวังโดยการเรียกใช้ bazel build --config=clang_config //main:hello-world บนเครื่อง Linux
  • สร้างแอปพลิเคชัน C++

ก่อนเริ่มต้น

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

บทแนะนำนี้จะถือว่าคุณกำลังใช้ Linux และได้สร้าง แอปพลิเคชัน C++ และติดตั้งเครื่องมือและไลบรารีที่เหมาะสม บทแนะนำนี้ใช้ clang version 9.0.1 ซึ่งคุณสามารถติดตั้งในระบบของคุณ

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

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

  2. ดาวน์โหลด ตัวอย่างโปรเจ็กต์ C++ จาก GitHub แล้วนำไปวางไว้ในไดเรกทอรีที่ว่างเปล่าในเครื่องของคุณ

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

    cc_binary(
        name = "hello-world",
        srcs = ["hello-world.cc"],
    )
    
  4. สร้างไฟล์ .bazelrc ที่รูทของไดเรกทอรีพื้นที่ทำงานด้วยค่า เนื้อหาต่อไปนี้เพื่อให้ใช้แฟล็ก --config ได้

    # Use our custom-configured c++ toolchain.
    
    build:clang_config --crosstool_top=//toolchain:clang_suite
    
    # Use --cpu as a differentiator.
    
    build:clang_config --cpu=k8
    
    # Use the default Bazel C++ toolchain to build the tools used during the
    # build.
    
    build:clang_config --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
    

สำหรับรายการ build:{config_name} --flag=value ค่าสถานะบรรทัดคำสั่ง --config={config_name} เชื่อมโยงกับแฟล็กนั้นๆ โปรดดู เอกสารประกอบเกี่ยวกับแฟล็กที่ใช้ crosstool_top cpu และ host_crosstool_top

เมื่อคุณสร้างเป้าหมาย กับ bazel build --config=clang_config //main:hello-world Bazel จะใช้ Toolchain ที่กำหนดเองจาก cc_toolchain_suite //toolchain:clang_suite ชุดโปรแกรมอาจแสดงรายการที่แตกต่างกัน toolchain สำหรับ CPU แบบต่างๆ และนั่นเป็นเหตุผลว่าทำไมความแตกต่างด้วย Flag --cpu=k8

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

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

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

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

    bazel build --config=clang_config //main:hello-world
    

    เนื่องจากคุณระบุ --crosstool_top=//toolchain:clang_suite ใน .bazelrc Bazel แสดงข้อผิดพลาดต่อไปนี้

    No such package `toolchain`: BUILD file not found on package path.
    

    ในไดเรกทอรีพื้นที่ทำงาน ให้สร้างไดเรกทอรี toolchain สำหรับแพ็กเกจ และไฟล์ BUILD ที่ว่างเปล่าภายในไดเรกทอรี toolchain

  2. เรียกใช้บิลด์อีกครั้ง เนื่องจากแพ็กเกจ toolchain ยังไม่ได้กำหนด เป้าหมาย clang_suite Bazel แสดงข้อผิดพลาดต่อไปนี้

    No such target '//toolchain:clang_suite': target 'clang_suite' not declared
    in package 'toolchain' defined by .../toolchain/BUILD
    

    ในไฟล์ toolchain/BUILD ให้กำหนดกลุ่มไฟล์ที่ว่างเปล่าดังนี้

    package(default_visibility = ["//visibility:public"])
    
    filegroup(name = "clang_suite")
    
  3. เรียกใช้บิลด์อีกครั้ง Bazel แสดงข้อผิดพลาดต่อไปนี้

    '//toolchain:clang_suite' does not have mandatory providers: 'ToolchainInfo'
    

    Bazel พบว่าแฟล็ก --crosstool_top ชี้ไปยังกฎที่ ไม่ได้ระบุ ToolchainInfo ที่จำเป็น ดังนั้นคุณต้องชี้ --crosstool_top ไปยังกฎที่ระบุ ToolchainInfo - ซึ่งก็คือกฎcc_toolchain_suite ใน toolchain/BUILD ให้แทนที่กลุ่มไฟล์ที่ว่างเปล่าด้วยข้อมูลต่อไปนี้

    cc_toolchain_suite(
        name = "clang_suite",
        toolchains = {
            "k8": ":k8_toolchain",
        },
    )
    

    แอตทริบิวต์ toolchains จะแมป --cpu โดยอัตโนมัติ (และ --compiler เมื่อระบุ) เป็น cc_toolchain คุณยังไม่ กำหนดเป้าหมาย cc_toolchain แล้ว Bazel จะบ่น เร็วๆ นี้

  4. เรียกใช้บิลด์อีกครั้ง Bazel แสดงข้อผิดพลาดต่อไปนี้

    Rule '//toolchain:k8_toolchain' does not exist
    

    ตอนนี้คุณต้องกำหนดเป้าหมาย cc_toolchain สำหรับทุกค่าในฟิลด์ cc_toolchain_suite.toolchains เพิ่มข้อมูลต่อไปนี้ลงใน toolchain/BUILD ไฟล์:

    filegroup(name = "empty")
    
    cc_toolchain(
        name = "k8_toolchain",
        toolchain_identifier = "k8-toolchain",
        toolchain_config = ":k8_toolchain_config",
        all_files = ":empty",
        compiler_files = ":empty",
        dwp_files = ":empty",
        linker_files = ":empty",
        objcopy_files = ":empty",
        strip_files = ":empty",
        supports_param_files = 0,
    )
    
  5. เรียกใช้บิลด์อีกครั้ง Bazel แสดงข้อผิดพลาดต่อไปนี้

    Rule '//toolchain:k8_toolchain_config' does not exist
    

    จากนั้นเพิ่ม ":k8_toolchain_config" กำหนดเป้าหมายเป็นไฟล์ toolchain/BUILD:

    filegroup(name = "k8_toolchain_config")
    
  6. เรียกใช้บิลด์อีกครั้ง Bazel แสดงข้อผิดพลาดต่อไปนี้

    '//toolchain:k8_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")
    

    และแทนที่ "k8_toolchain_config" ที่มีการประกาศ กฎ cc_toolchain_config ข้อ:

    cc_toolchain_config(name = "k8_toolchain_config")
    
  7. เรียกใช้บิลด์อีกครั้ง 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 เป็นเส้นทางที่ถูกต้อง สำหรับระบบของคุณ

  8. เรียกใช้บิลด์อีกครั้ง Bazel แสดงข้อผิดพลาดต่อไปนี้

     ..../BUILD:3:1: undeclared inclusion(s) in rule '//main:hello-world':
     this rule is missing dependency declarations for the following files included by 'main/hello-world.cc':
     '/usr/include/c++/9/ctime'
     '/usr/include/x86_64-linux-gnu/c++/9/bits/c++config.h'
     '/usr/include/x86_64-linux-gnu/c++/9/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-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,
     )
    
  9. เรียกใช้คำสั่งบิลด์อีกครั้ง คุณจะเห็นข้อผิดพลาดอย่างเช่น

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

    คัดลอกรหัสต่อไปนี้ไปยัง 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",
          "flag_group",
          "flag_set",
          "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],
      )
    
  10. หากคุณเรียกใช้ bazel build --config=clang_config //main:hello-world สร้างได้ในที่สุด

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

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

สรุปประเด็นสำคัญมีดังนี้ - คุณต้องระบุแฟล็ก --crosstool_top ในบรรทัดคำสั่งซึ่งควร ชี้ไปที่ cc_toolchain_suite - คุณสามารถสร้างทางลัดสำหรับการกำหนดค่าหนึ่งๆ ได้โดยใช้ .bazelrc ไฟล์ - cc_toolchain_suite อาจแสดง cc_toolchains สำหรับ CPU ที่แตกต่างกันและ คอมไพเลอร์ คุณใช้แฟล็กบรรทัดคำสั่ง เช่น --cpu เพื่อแยกความแตกต่างได้ - คุณต้องแจ้งให้ Toolchain ทราบว่าเครื่องมืออยู่ ณ ที่ใด ในบทแนะนำนี้ มีเวอร์ชันแบบง่ายที่คุณเข้าถึงเครื่องมือจากระบบ ถ้า คุณสนใจในแนวทางที่พัฒนาได้ด้วยตนเองมากยิ่งขึ้น คุณสามารถอ่านเกี่ยวกับ พื้นที่ทำงานที่นี่ เครื่องมือของคุณอาจมาจาก พื้นที่ทำงานที่แตกต่างกัน และคุณจะต้องทำให้ไฟล์ใช้งานได้ ไปยัง cc_toolchain พร้อมทรัพยากร Dependency เป้าหมายในแอตทริบิวต์ เช่น compiler_files คุณจะต้องเปลี่ยน tool_paths ด้วย - คุณสามารถสร้างฟีเจอร์เพื่อกำหนดค่าแฟล็กที่ควรส่งไปยัง การกระทำต่างๆ ไม่ว่าจะเป็นการลิงก์ หรือการกระทำประเภทอื่นๆ

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

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