如何在 bazel 中设置动态加载的库依赖项?

How can you set up dynamically loaded library dependencies in bazel?

假设我有一个单元测试使用 dlopen 从提供的共享库中加载和调用代码

int main(int argc, char* argv[]) {
  const char* library = argv[1];
  // calls dlopen with library and does some testing
}

给定声明的库

cc_library(
    name = "a",
    srcs = ["a.cpp"],
    visibility = ["//visibility:public"],
)

有什么方法可以使用 cc_test 将单元测试设置为 运行 并将库 a 的位置作为参数传入? (最好也是以独立于平台的方式,这样我就不必担心库使用什么后缀)

您可以编写访问运行文件的自定义 skylark 规则(相关讨论 here)。

一起破解一些东西:

构建:

load("//:foo.bzl", "runfiles")

cc_binary(
    name = "liba.so",
    srcs = [ "a.cc" ],
    linkshared = 1,
)

runfiles(
    name = "runfiles",
    deps = [ ":liba.so" ],
)

foo.bzl:

def _impl(ctx):
  for dep in ctx.attr.deps:
    print(dep.files_to_run.executable.path)

runfiles = rule(
  implementation = _impl,
  attrs = {
    "deps": attr.label_list(allow_files=True),
  }
)