如何将 `cc_binary`/`cc_library` 包装到自定义规则中(不是宏!)?

How do I wrap `cc_binary`/`cc_library` into a custom rule (not macro!)?

为了解决 bug,并且为了设置特定于平台的编译器选项,我想将 cc_binary(和 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?

不,没有在规则实施级别重用不同规则的通用方法。但是,有一些方法可以使 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"])