bazel - 如何将规则限制为 cpp 工具链的子集?
问题描述
我有这样的规则
do_action = rule (
implementation = _impl,
attrs = {
...
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
fragments = ["cpp"],
toolchains = [
"@bazel_tools//tools/cpp:toolchain_type",
],
)
我为自定义 cpu 定义了自定义 cc_toolchain:
toolchain(
name = "cc-toolchain-%{toolchain_name}",
toolchain = ":cc-compiler-%{toolchain_name}",
# can be run on this platform
target_compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
cc_toolchain_suite(
name = "toolchain",
toolchains = {
"%{cpu}": ":cc-compiler-%{toolchain_name}",
},
)
我--crostool_top
会在需要时选择此工具链。
我只想在 --crostool_top 与我的自定义工具链之一匹配时才允许调用我的自定义规则。这个怎么做?
解决方案
添加一个新constraint_setting
的constraint_value
,只有你的工具链是target_compatible_with
,然后让所有使用你的规则的目标target_compatible_with
。
在 BUILD 文件中是这样的:
constraint_setting(name = "is_my_toolchain")
constraint_value(
name = "yes_my_toolchain",
constraint_setting = ":is_my_toolchain",
)
然后添加yes_my_toolchain
到target_compatible_with
您的所有工具链上。
在每次使用规则时强制使用它的最简单方法是使用宏。将实际规则重命名为_do_action
(因此它是私有的,不能直接从任何BUILD
文件加载)并添加:
def do_action(target_compatible_with = [], **kwargs):
_do_action(
target_compatible_with = target_compatible_with + [
"//your_package:yes_my_toolchain",
],
**kwargs
)
推荐阅读
- nearprotocol - 与接近原生 rust/assemblyscript 相比,使用接近 evm 可靠度合约的权衡是什么?
- html - 从本地 netbeans 中删除文件而不将其从服务器中删除
- angular - 如果我在父子中使用相同的表单控件,UI 不会更新
- elasticsearch - 在 ElasticSearch 中上传同义词文件
- node.js - Docker 找不到模块 /bin/bash
- ssis - SSIS 2012 包在 win 10 中运行正常,但在 win 7/win server 2012 中运行不正常
- kubernetes - 我可以使用 ArgoCD CLI 获取入口实时清单信息吗?
- forms - ExtJS 7.2.0 - 表单提交在 Chrome 中不起作用
- sparql - 如何使用 SPARQL 从 wikidata 获取世界奇观列表?
- azure - 为 Azure 中的特定 MySQL 服务器创建专用终结点