0

为了解决bug并设置特定于平台的编译器选项,我想将cc_binary(and cc_library) 包装到自定义规则中。我不想使用宏,因为我必须像这样构造我的代码:

deps = select(
        {
            "@platforms//os:linux": deps + ["my-extra-dep"],
            "//conditions:default": deps,
        }
// repeat the same for copts and defines

相反,我想像这样构建我的代码:

// checks @platforms//os:linux
if is_linux():
  deps = deps + ["my-extra-dep"]
  defines = defines + ["custom-define1", ...]

后者仅在可以查询平台属性的规则中才有可能。在宏中,我会卡住select每个单独的属性。

我的问题是我的自定义规则的样板 - 应该是什么attrs,以便我可以透明地转发到本机cc_binary/cc_library规则?我是否错过了一种简单的方法来做到这一点,即不会将cc_*规则的整个接口规范翻译成starlark?

4

1 回答 1

2

不,没有通用的方法可以在规则实施级别重用不同的规则。但是,有一些方法可以使select树更简洁。例如:

def on_linux(d):
   return select({"@platforms//os:linux": d, "//conditions:default": []})

def macro(deps, defines):
   ...
   deps += on_linux(["my-extra-dep"])
   defines += on_linux(["custom-define", "custom-define2"])
于 2021-12-20T19:04:06.153 回答