最近在使用 bazel ,尝试使用交叉编译,在这做个记录

配置的地方有几个:

  1. .bazelrc 文件配置,用于激活 config 配置
  2. 创建 toolchain 目录,并创建BUILD文件
  3. 创建对应的 .bzl 文件

.bazelrc

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

toolchain/BUILD

load(":darwin_x86_64_toolchain_config.bzl", "darwin_x86_64_toolchain_config")

package(default_visibility = ["//visibility:public"])

#filegroup(name = "clang_suite")

cc_toolchain_suite(
    name = "clang_suite",
    toolchains = {
        "darwin_x86_64": ":darwin_x86_64_toolchain",
    },
)
filegroup(name = "empty")

cc_toolchain(
    name = "darwin_x86_64_toolchain",
    toolchain_identifier = "darwin_x86_64-toolchain",
    toolchain_config = ":darwin_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,
)
darwin_x86_64_toolchain_config(name = "darwin_x86_64_toolchain_config")

toolchain/darwin_x86_64_toolchain_config.bzl

# toolchain/darwin_x86_64_toolchain_config.bzl:
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
   "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
   "feature",
   "flag_group",
   "flag_set",
   "tool_path",
)

all_link_actions = [
   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 = "/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",
        ),
    ]

    features = [
       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,
          cxx_builtin_include_directories = [
            "/Library/Developer/CommandLineTools/usr/include/",
            "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/",
            "/Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/include/",
            "/usr/include",
          ],
          toolchain_identifier = "local",
          host_system_name = "local",
          target_system_name = "local",
          target_cpu = "darwin_x86_64",
          target_libc = "unknown",
          compiler = "clang",
          abi_version = "unknown",
          abi_libc_version = "unknown",
          tool_paths = tool_paths,
     )

darwin_x86_64_toolchain_config = rule(
    implementation = _impl,
    attrs = {},
    provides = [CcToolchainConfigInfo],
)

编译的时候带上参数 --config

bazel build --config=darwin_x86_64_config //...