build - 如何将参数从命令行传递到 bazel 文件
问题描述
我在 .bzl 文件示例中声明了一个变量:VERSION_NUMBER = "00"
. 当我构建不同版本的项目时,我想从命令行覆盖这个变量。示例:bazel build target--sunbversion_number= "99"
我想更改此变量,因为它在某些函数中被调用以创建输出路径的名称。示例:对于版本“00”,输出文件将是:name_00.extension,对于版本“99”,输出文件将是:name_99.extension 这是我的示例:在我声明的 .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 文件中,但是有一些选项,具体取决于您想要做什么。
一种方法是使用“stamping”设置版本,然后将版本化文件放入具有已知名称的 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.txt
也srcs = [":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
有一种方法 only --//:version
,但它需要另一个 Starlark 规则versioned_files
,比如
生成一个文件,其中包含
data
属性中的版本,程序在运行时读取,或者一个 Starlark 规则,它生成一个包含版本的 C++ 文件,然后将其放入 a 的源代码中
cc_library
,程序的其余部分可以在编译时依赖和使用。
这些方法可能需要重构您的程序。
推荐阅读
- python - 从 VBA 到 Python 再回到 Excel
- c++ - cmake:安装外部提供的包含和库
- java - 如何使用类型创建在测试侦听器中定义的全局变量?
- c++ - 断点陷阱是否总是意味着程序是从调试器运行的?
- python - Comparing values on x-axis & referring to values on row before in Dataframes
- javascript - 更改选定选项时处理选择列表以触发事件
- angular - 如何从角度 6 中的“响应标头”获取 JWT 令牌
- amazon-web-services - VPC 中的数据库实例被不同 VPC 中的 EC2 实例访问
- reactjs - 使用 Jest Uncovered 行测试 React 组件
- r - 如何按组(ID)复制最后一行?