您可以使用custom rule 轻松完成编译。您需要使用cc_common API 来执行cc_library 所做的正常操作,但返回DefaultInfo.files provider 中的所有文件。
对于链接,我只使用cc_binary。如果需要,您还可以使用自定义规则执行类似的操作来拆分各个部分。
类似这样的:
load("@rules_cc//cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
load("@rules_cc//cc:toolchain_utils.bzl", "find_cpp_toolchain")
def _my_c_compile_impl(ctx):
cc_toolchain = find_cpp_toolchain(ctx)
source_file = ctx.file.src
output_file = ctx.actions.declare_file(ctx.label.name + ".o")
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = DISABLED_FEATURES + ctx.disabled_features,
)
_, outputs = cc_common.compile(
actions = ctx.actions,
cc_toolchain = cc_toolchain,
srcs = [source_file],
name = ctx.label.name,
)
return [
DefaultInfo(files = depset(direct = outputs.objects)),
]
my_c_compile = rule(
implementation = _my_c_compile_impl,
attrs = {
"src": attr.label(mandatory = True, allow_single_file = True),
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
incompatible_use_toolchain_transition = True,
fragments = ["cpp"],
)
rules_cc my_c_archive example 是一个很好的起点,如果您想自定义超出cc_common.compile 允许的范围。
如果你想让它成为一个通用的调试工具,你可以将类似的逻辑打包成aspect,以将其应用于任意规则。
为了快速调试,--save_temps 是另一种技术。