首页 > 解决方案 > 基于带有bazel的工具链选择()的最佳方法是什么?

问题描述

文档(https://docs.bazel.build/configurable-attributes.html)提供了以下示例,遗憾的是它不起作用:

cc_library(
    name = "my_lib",
    deps = select(
        {
            "//tools/cc_target_os:android": [":android_deps"],
            "//tools/cc_target_os:windows": [":windows_deps"],
        },
        no_match_error = "Please build with an Android or Windows toolchain",
    ),
)

可悲的是,诸如“@platforms//os:macos”和“@platforms//os:windows”之类的匹配器只能检测到 HOST 平台,而不是 TARGET 平台。当在不同的架构上交叉编译时,这会中断。

我想出了一个有效的“android”匹配器:

config_setting(
    name = "android",
    values = {"crosstool_top": "//external:android/crosstool"},
)

但无法找到匹配 windows、macos 或 linux TARGET 工具链的方法。

谢谢!

标签: bazel

解决方案


我认为您正在寻找平台:httpstarget_compatible_with : //docs.bazel.build/versions/master/platforms.html和cc_library.

我建议你想要类似的东西:

cc_library(
    name = "my_lib_android",
    deps = [":android_deps"],
    target_compatible_with = [
        "@platforms//os:android",
    ],
)

cc_library(
    name = "my_lib_windows",
    deps = [":windows_deps"],
    target_compatible_with = [
        "@platforms//os:windows",
    ],
)


然后,my_lib 的用户将不得不依赖 my_lib_android 或 my_lib_windows(视情况而定)。


推荐阅读