如何将参数从命令行传递到 bazel 文件



>我在 .bzl 文件中声明了一个变量,例如:VERSION_NUMBER = "00". 我想在构建不同版本的项目时从命令行覆盖此变量。 示例:Bazel 构建目标--sunbversion_number= "99"我想更改此变量,因为它在某个函数中被调用以创建输出路径的名称。 考试:对于版本"00",输出文件将为:name_00.扩展名 对于版本"99",输出文件将为:name_99.扩展名 这是我的例子: 在.bzl文件中,我声明:
SUBVERSION_NUMBER = "99"以及返回有关SUBVERSION_NUMBER
def get_name(SUBVERSION_NUMBER):
return "test-"+SUBVERSION_NUMBER的文件名称的功能

OUTPUT_NAME = get_name("99")

然后我的 genrule():
genrule(name = "test",
srcs = [srcs]
outs = [OUTPUT_NAME+".tek"],
cmd = "cmd to generate the file" )
当我构建此规则时,我得到输出文件test-99.tek
我想要的是当我运行bazel build test --//version=01或任何其他建议的解决方案时,我想获得输出test-01.tek谢谢

没有办法像这样将值从命令行获取到 bzl 文件中,但有几个选项,具体取决于您要执行的操作。

一种方法是使用"标记"来设置版本,然后将版本控制的文件放在具有已知名称的zip文件中,例如使用genrule。构建标记通常用于将版本号和其他信息嵌入到输出文件本身中,但也可以在此处使用。zip 文件是必需的,因为输出文件名必须在加载时知道(即在分析时处理命令行中的任何配置数据之前)。

像这样:

# out.txt is the original unversioned file
genrule(
name = "gen_out",
outs = ["out.txt"],
cmd = "echo foo > $@",
)
genrule(
name = "gen_versioned_out",
outs = ["out_versioned.zip"],
srcs = [":out.txt"],
tools = ["@bazel_tools//tools/zip:zipper"],
stamp = True,
cmd = """
# bazel-out/stable-status.txt is created when stamp = True
# Value of BUILD_EMBED_LABEL key comes from --embed_label on the command line
version="$$(grep BUILD_EMBED_LABEL bazel-out/stable-status.txt | cut -d ' ' -f 2)"
# Set a reasonable default if --embed_label was not specified
if [ -z "$$version" ]; then version="0"; fi
file="$$(basename $<)"
name="$${file%%.*}"
ext="$${file#*.}"
versioned_file="$${name}_$${version}.$${ext}"
# zipper allows specifying the name of the file in the zip directly, unlike the
# regular zip tool.
# c = create
# $@ = output file from "outs"
# $< = input file from "srcs"
# $$versioned_file=$< = "put this file in to the archive with this name"
$(location @bazel_tools//tools/zip:zipper) c "$@" "$$versioned_file=$<"
""",
)

然后:

$ bazel build out_versioned.zip --embed_label=99
INFO: Analyzed target //:out_versioned.zip (7 packages loaded, 19 targets configured).
INFO: Found 1 target...
Target //:out_versioned.zip up-to-date:
bazel-bin/out_versioned.zip
INFO: Elapsed time: 0.340s, Critical Path: 0.10s
INFO: 3 processes: 1 internal, 2 linux-sandbox.
INFO: Build completed successfully, 3 total actions
$ unzip -l bazel-bin/out_versioned.zip
Archive:  bazel-bin/out_versioned.zip
Length      Date    Time    Name
---------  ---------- -----   ----
4  2010-01-01 00:00   out_99.txt
---------                     -------
4                     1 file

因此,这将生成一个具有已知名称的 zip 文件,其中包含版本控制文件。

另一种方法是使用"用户定义的构建设置": https://docs.bazel.build/versions/master/skylark/config.html#user-defined-build-settings

像这样:

defs.bzl

def _version_file_impl(ctx):
version = ctx.attr._version[VersionProvider].version

name, extension = ctx.file.file.basename.rsplit(".", 1)

versioned_file = ctx.actions.declare_file(
"%s_%s.%s" % (name, version, extension))

copy_args = ctx.actions.args()
copy_args.add_all([ctx.file.file, versioned_file])

ctx.actions.run_shell(
inputs = [ctx.file.file],
outputs = [versioned_file],
command = 'cp "$1" "$2"',
arguments = [copy_args])
return DefaultInfo(files = depset([versioned_file]))
version_file = rule(
implementation = _version_file_impl,
attrs = {
"file": attr.label(mandatory = True, allow_single_file = True),
"_version": attr.label(default = "//:version"),
}
)
VersionProvider = provider(fields = ["version"])
def _version_flag_impl(ctx):
return VersionProvider(version = ctx.build_setting_value)
version_flag = rule(
implementation = _version_flag_impl,
build_setting = config.int(flag = True),
)

BUILD

load(":defs.bzl", "version_flag", "version_file")
version_flag(
name = "version",
build_setting_default = 0,
)
genrule(
name = "gen_out",
outs = ["out.txt"],
cmd = "echo foo > $@",
)
version_file(
name = "versioned_out",
file = ":out.txt",
)

然后:

$ bazel build :versioned_out --//:version=99
INFO: Analyzed target //:versioned_out (5 packages loaded, 10 targets configured).
INFO: Found 1 target...
Target //:versioned_out up-to-date:
bazel-bin/out_99.txt
INFO: Elapsed time: 0.322s, Critical Path: 0.06s
INFO: 3 processes: 1 internal, 2 linux-sandbox.
INFO: Build completed successfully, 3 total actions

因此,这将生成一个名称中包含版本的文件。但是,没有标签来引用版本控制文件本身,因此bazel build :out_99.txtsrcs = [":out_99.txt"]不起作用,您必须通过versioned_out目标。


更新:

下面是一个可以对多个输出进行版本控制的版本:

defs.bzl

def _versioned_files_impl(ctx):
version = ctx.attr._version[VersionProvider].version
versioned_files = []
for f in ctx.attr.src.files.to_list():
name, extension = f.basename.rsplit(".", 1)
versioned_file = ctx.actions.declare_file(
"%s_%s.%s" % (name, version, extension))
versioned_files.append(versioned_file)
copy_args = ctx.actions.args()
copy_args.add_all([f, versioned_file])
ctx.actions.run_shell(
inputs = [f],
outputs = [versioned_file],
command = 'cp "$1" "$2"',
arguments = [copy_args])
return DefaultInfo(files = depset(versioned_files))
versioned_files = rule(
implementation = _versioned_files_impl,
attrs = {
"src": attr.label(mandatory = True),
"_version": attr.label(default = "//:version"),
}
)
VersionProvider = provider(fields = ["version"])
def _version_flag_impl(ctx):
return VersionProvider(version = ctx.build_setting_value)
version_flag = rule(
implementation = _version_flag_impl,
build_setting = config.int(flag = True),
)

BUILD

load(":defs.bzl", "version_flag", "versioned_files")
version_flag(
name = "version",
build_setting_default = 0,
)
genrule(
name = "gen_out",
outs = ["foo.txt", "bar.txt", "baz.txt"],
cmd = """
echo foo > $(location foo.txt)
echo bar > $(location bar.txt)
echo baz > $(location baz.txt)
""",
)
versioned_files(
name = "versioned_files",
src = ":gen_out",
)

用法:

$ bazel build versioned_files --//:version=123
INFO: Analyzed target //:versioned_files (5 packages loaded, 9 targets configured).
INFO: Found 1 target...
Target //:versioned_files up-to-date:
bazel-bin/foo_123.txt
bazel-bin/bar_123.txt
bazel-bin/baz_123.txt
INFO: Elapsed time: 0.491s, Critical Path: 0.06s
INFO: 5 processes: 1 internal, 4 linux-sandbox.
INFO: Build completed successfully, 5 total actions

更新:

将版本放入 cc 目标定义的示例:

BUILD

cc_binary(
name = "main",
srcs = ["main.cc"],
defines = ["VERSION=\"$(VERSION)\""],
)

main.cc

#include <iostream>
#ifndef VERSION
#define VERSION "0.0.0"
#endif
int main() {
std::cout << "version: " << VERSION << std::endl;
return 0;
}

构建并运行:

$ bazel run main --define=VERSION=1.2.3
INFO: Analyzed target //:main (15 packages loaded, 52 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
bazel-bin/main
INFO: Elapsed time: 0.524s, Critical Path: 0.26s
INFO: 6 processes: 4 internal, 2 linux-sandbox.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
version: 1.2.3

结合上述方法,您必须在命令行上指定--//:version=1.2.3--define=VERSION=1.2.3。有一种方法只能--//:version,但它需要另一个像versioned_files这样的Starlark规则,要么

  1. 生成一个文件,其中包含data属性中的版本,程序在运行时读取,或者

  2. 一个 Starlark 规则,它生成一个包含版本的C++文件,然后将其放入cc_library的源代码中,程序的其余部分可以在编译时依赖和使用。

这些方法可能需要重构程序。

最新更新