【问题标题】:How to use genrule output as string to expand_template substitutions in Bazel?如何使用 genrule 输出作为字符串来扩展 Bazel 中的模板替换?
【发布时间】:2019-04-26 11:02:19
【问题描述】:

看来genrule只能输出一个Target,而expand_template替换只接受string_dict,请问如何使用genrule输出来expand_template?

gen.bzl

def _expand_impl(ctx):
    ctx.actions.expand_template(
        template = ctx.file._template,
        output = ctx.outputs.source_file,
        substitutions = {
            "{version}": ctx.attr.version,
        }
    )

expand = rule(
    implementation = _expand_impl,
    attrs = {
        "version": attr.string(mandatory = True),
        "_template": attr.label(
            default = Label("//version:local.go.in"),
            allow_single_file = True,
        ),
    },
    outputs = {"source_file": "local.go"},
)

构建

load("@io_bazel_rules_go//go:def.bzl", "go_library")

filegroup(
    name = "templates",
    srcs = ["local.go.in"],
)

genrule(
    name = "inject",
    outs = ["VERSION"],
    local = 1,
    cmd = "git rev-parse HEAD",
)

load(":gen.bzl", "expand")

expand(
    name = "expand",
    version = ":inject",
)

go_library(
    name = "go_default_library",
    srcs = [
        "default.go",
        ":expand", # Keep
    ],
    importpath = "go.megvii-inc.com/brain/data/version",
    visibility = ["//visibility:public"],
)

和local.go.in

package version

func init() {
    V = "{version}"
}

我希望 local.go.in 中的 {version} 可以替换为 git rev-parse HEAD 输出。

【问题讨论】:

    标签: bazel


    【解决方案1】:

    这里的问题是ctx.actions.expand_template()substitutions 参数必须在分析阶段(即运行_expand_impl 时)已知,这发生在运行genrule 的git rev-parse HEAD 命令之前(即,在执行阶段)。

    有几种方法可以做到这一点。最简单的就是在 genrule 中做所有事情:

    genrule(
        name = "gen_local_go",
        srcs = ["local.go.in"],
        outs = ["local.go"],
        local = 1,
        cmd = 'sed "s/{VERSION}/$(git rev-parse HEAD)/" "$<" > "$@"',
    )
    

    这依赖于主机上可用的sed,但任何其他可以输入一个文件、修改文本并将其输出到另一个文件的程序都可以工作。

    另一种选择是使用--workspace_status_command 的组合 这里有更多细节: How to run a shell command at analysis time in bazel? 这种方法的优点是它避免了本地流派。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 2011-10-15
      • 2023-03-31
      相关资源
      最近更新 更多