目录结构:
BUILD
load(":getdeps.bzl", "getdeps")
getdeps(
name = "testA",
srcs = ["a.txt"],
deps = [":testB"]
)
getdeps(
name = "testB",
srcs = ["b.txt"],
deps = [":testC"]
)
getdeps(
name = "testC",
srcs = ["c.txt"],
)
如果只是获取当前target的资源, 以及依赖了哪些 就是下面的代码:
getdeps.bzl
def _getdeps(ctx):
srcs = ctx.attr.srcs
deps = ctx.attr.deps
print("srcs:",srcs)
print("deps:",deps)
getdeps = rule(
implementation = _getdeps,
attrs = {
"srcs":attr.label_list(allow_files = True),
"deps":attr.label_list(),
}
)
调用 bazel build //getDeps:testB
能看到每个target的srcs 和deps , 都是BUILD里面写的一样
下面让我们来获取所有的srcs 或者deps
代码
FooFiles = provider(fields = ["getDeps", "getSrcs"])
获取目标及其传递依赖项的源文件。
#参数:
#src:源文件的列表
#Deps:直接依赖的目标列表
#返回:
#传递源的集合
def get_transitive_srcs(srcs,deps):
return depset(
srcs,
transitive = [dep[FooFiles].getSrcs for dep in deps],
)
def get_transitive_srcs2(srcs,deps):
return depset(
deps,
transitive = [dep[FooFiles].getDeps for dep in deps],
)
def _getdeps(ctx):
print("---------%s---------"%(ctx.attr.name))
srcs = ctx.attr.srcs
deps = ctx.attr.deps
trans_srcs = get_transitive_srcs(srcs,deps)
trans_deps = get_transitive_srcs2(srcs,deps)
print("trans_srcs:",trans_srcs)
print("trans_deps:",trans_deps)
print("---------%s---------\n\n\n"%(ctx.attr.name))
return [
FooFiles(getSrcs = trans_srcs,getDeps = trans_deps),
]
getdeps = rule(
implementation = _getdeps,
attrs = {
"srcs":attr.label_list(allow_files = True),
"deps":attr.label_list(),
}
)