具体而言,它允许实现函数访问当前目标的标签、属性、配置及其依赖项的提供程序。它具有用于声明输出文件和生成这些文件的操作的方法。
上下文对象本质上在调用实现函数的整个过程中都存在。在相关函数之外访问这些对象没有用处。如需了解详情,请参阅“规则”页面。
成员
- 操作
- aspect_ids
- 属性
- bin_dir
- build_file_path
- build_setting_value
- 配置
- coverage_instrumented
- created_actions
- disabled_features
- exec_groups
- 可执行文件
- expand_location
- expand_make_variables
- 功能
- 文件
- 文件
- 片段
- genfiles_dir
- info_file
- 标签
- 输出
- package_relative_label
- resolve_command
- resolve_tools
- 规则
- runfiles
- split_attr
- super
- target_platform_has_constraint
- 工具链
- var
- version_file
- workspace_name
操作
actions ctx.actions
aspect_ids
list ctx.aspect_ids
attr
struct ctx.attr
rule function 的 attrs dict 的键和值相对应。请参阅使用示例。
bin_dir
root ctx.bin_dir
build_file_path
string ctx.build_file_path
ctx.label.package + '/BUILD'。此规则的 BUILD 文件的路径,相对于源根目录。
build_setting_value
unknown ctx.build_setting_value
build_setting 属性的规则实例的上下文,则读取此内容会出错。
配置
configuration ctx.configuration
coverage_instrumented
bool ctx.coverage_instrumented(target=None)
target,则返回是否应为该目标指定的规则生成代码覆盖率插桩。(如果提供了非规则或 Starlark 规则目标,则此函数返回 False。)检查是否应根据 --instrumentation_filter 和 --instrument_test_targets 配置设置,对当前规则的来源(如果未提供目标)或目标的来源进行插桩。这与 coverage_enabled 在 配置 中的不同,后者用于说明是否为整个运行启用了覆盖率数据收集,但不会说明是否应插桩特定目标。
参数
| 参数 | 说明 |
|---|---|
target
|
目标;或 None;
默认值为 None指定规则的目标。如果未提供,则默认为当前规则。 |
created_actions
StarlarkValue ctx.created_actions()
True 的规则,此函数会返回一个 Actions 提供方,表示到目前为止为当前规则创建的所有操作。对于所有其他规则,此函数会返回 None。请注意,当创建后续操作时,提供程序不会更新,因此如果您想检查这些操作,则必须再次调用此函数。此函数旨在帮助为规则实现帮助程序函数编写测试,这些函数可能会接收
ctx 对象并在其上创建操作。
disabled_features
list ctx.disabled_features
exec_groups
ExecGroupCollection ctx.exec_groups
ctx.exec_groups[name_of_group] 进行访问。
executable
struct ctx.executable
struct,其中包含在标记为 executable=True 的 标签类型属性中定义的可执行文件。结构体字段与属性名称相对应。结构体中的每个值都是 File 或 None。如果规则中未指定可选属性,则相应的结构体值为 None。如果标签类型未标记为 executable=True,则不会生成相应的结构体字段。请参阅使用示例。
expand_location
string ctx.expand_location(input, targets=[])
$(location //x) 替换为目标 //x 的输出文件的路径,展开给定字符串中的所有 $(location ...) 模板。展开仅适用于指向此规则的直接依赖项或在可选参数 targets 中明确列出的标签。$(location ...) 如果引用的目标有多个输出,将导致错误。在这种情况下,请使用 $(locations ...),因为它会生成以空格分隔的输出路径列表。它也可以安全地用于单个输出文件。此函数有助于让用户在 BUILD 文件中指定命令(例如
genrule)。在其他情况下,直接操作标签通常更好。
参数
| 参数 | 说明 |
|---|---|
input
|
字符串;
必需 要展开的字符串。 |
targets
|
目标序列;默认值为 []用于查找其他信息的目标的列表。这些目标按如下方式展开: DefaultInfo.files 中包含单个文件的目标会展开为该文件。其他目标会展开为 DefaultInfo.executable 文件(如果已设置且启用了 --incompatible_locations_prefers_executable),否则会展开为 DefaultInfo.files。
|
None。
expand_make_variables
string ctx.expand_make_variables(attribute_name, command, additional_substitutions)
返回展开对“Make 变量”的所有引用后的字符串。变量必须采用以下格式:
$(VAR_NAME)。此外,$$VAR_NAME expands to $VAR_NAME. Examples:
ctx.expand_make_variables("cmd", "$(MY_VAR)", {"MY_VAR": "Hi"}) # == "Hi"
ctx.expand_make_variables("cmd", "$$PWD", {}) # == "$PWD"
Additional variables may come from other places, such as configurations. Note that this function is experimental.
Parameters
| Parameter | Description |
|---|---|
attribute_name
|
string;
required The attribute name. Used for error reporting. |
command
|
string;
required The expression to expand. It can contain references to "Make variables". |
additional_substitutions
|
dict;
required Additional substitutions to make beyond the default make variables. |
features
list ctx.features
file
struct ctx.file
struct containing files defined in label type attributes marked as allow_single_file. The struct fields correspond to the attribute names. The struct value is always a File or None. If an optional attribute is not specified in the rule then the corresponding struct value is None. If a label type is not marked as allow_single_file, no corresponding struct field is generated. It is a shortcut for:list(ctx.attr.<ATTR>.files)[0]
file to access the (singular) default output of a dependency. See example of use.
files
struct ctx.files
struct containing files defined in label or label list type attributes. The struct fields correspond to the attribute names. The struct values are list of Files. It is a shortcut for:[f for t in ctx.attr.<ATTR> for f in t.files]
files to access the default outputs of a dependency. See example of use.
fragments
fragments ctx.fragments
genfiles_dir
root ctx.genfiles_dir
info_file
File ctx.info_file
label
Label ctx.label
outputs
structure ctx.outputs
File objects. See the Rules page for more information and examples.This field does not exist on aspect contexts, since aspects do not have predeclared outputs.
The fields of this object are defined as follows. It is an error if two outputs produce the same field name or have the same label.
- If the rule declares an
outputsdict, then for every entry in the dict, there is a field whose name is the key and whose value is the correspondingFile. - For every attribute of type
attr.outputthat the rule declares, there is a field whose name is the attribute's name. If the target specified a label for that attribute, then the field value is the correspondingFile; otherwise the field value isNone. - For every attribute of type
attr.output_listthat the rule declares, there is a field whose name is the attribute's name. The field value is a list ofFileobjects corresponding to the labels given for that attribute in the target, or an empty list if the attribute was not specified in the target. - (Deprecated) If the rule is marked
executableortest, there is a field named"executable", which is the default executable. It is recommended that instead of using this, you pass another file (either predeclared or not) to theexecutablearg ofDefaultInfo.
package_relative_label
Label ctx.package_relative_label(input)
Label, it is returned unchanged.The result of this function is the same Label value as would be produced by passing the given string to a label-valued attribute of the rule and accessing the corresponding label field.
Usage note: The difference between this function and Label() 的区别在于,Label() 使用调用它的 .bzl 文件的软件包的上下文,而不是当前正在分析的目标的软件包。此函数与
native.package_relative_label() 的行为相同,后者无法在规则或
方面实现函数中使用。
参数
| 参数 | 说明 |
|---|---|
input
|
字符串;或标签;
必需 输入标签字符串或标签对象。如果传递了标签对象,则会原样返回。 |
resolve_command
tuple ctx.resolve_command(*, command='', attribute=None, expand_locations=False, make_variables=None, tools=[], label_dict={}, execution_requirements={})
(inputs, command, empty list),其中包含已解析的输入列表和已解析的命令的 argv 列表,这两个列表都适合作为 ctx.action 方法的同名实参传递。Windows 用户请注意:此方法需要 Bash (MSYS2)。请考虑改用
resolve_tools()(如果符合您的需求)。空列表作为元组的第三个成员返回,以实现向后兼容。
参数
| 参数 | 说明 |
|---|---|
command
|
字符串;
默认值为 ''要解析的命令。 |
attribute
|
字符串;或 None;
默认值为 None要为其发出错误的关联属性的名称,或 None。 |
expand_locations
|
布尔值;
默认值为 False是否展开 $(location) 变量?如需了解详情,请参阅 ctx.expand_location()。 |
make_variables
|
字典;或 None;
默认值为 None要展开的 Make 变量,或 None。 |
tools
|
目标序列;默认值为 []工具列表(目标列表)。 |
label_dict
|
字典;
默认值为 {}已解析的标签和相应文件列表的字典(标签:文件列表的字典)。 |
execution_requirements
|
字典;
默认值为 {}用于安排操作以解析此命令的信息。如需了解有用的键,请参阅标签。 |
resolve_tools
tuple ctx.resolve_tools(*, tools=[])
(inputs, empty list),其中包含运行工具所需的已解析输入的 depset,适合作为 ctx.actions.run 和 ctx.actions.run_shell 方法的同名实参传递。与
ctx.resolve_command 相比,此方法不需要在机器上安装 Bash,因此适用于在 Windows 上构建的规则。空列表作为元组的一部分返回,以实现向后兼容。
参数
| 参数 | 说明 |
|---|---|
tools
|
目标序列;默认值为 []工具列表(目标列表)。 |
rule
rule_attributes ctx.rule
runfiles
runfiles ctx.runfiles(files=[], transitive_files=None, collect_data=False, collect_default=False, symlinks={}, root_symlinks={})
参数
| 参数 | 说明 |
|---|---|
files
|
文件序列;
默认值为 []要添加到 runfiles 的文件列表。 |
transitive_files
|
depset of Files;或 None;
默认值为 None要添加到 runfiles 的(传递)文件集。depset 应使用 default 顺序(顾名思义,这是默认顺序)。
|
collect_data
|
布尔值;
默认值为 False不建议使用此参数。请参阅 runfiles 指南。 是否从 srcs、data 和 deps 属性中的依赖项收集数据 runfiles。 |
collect_default
|
布尔值;
默认值为 False不建议使用此参数。请参阅 runfiles 指南。 是否从 srcs、data 和 deps 属性中的依赖项收集默认 runfiles。 |
symlinks
|
字典;或 SymlinkEntrys 的 SymlinkEntry;
默认值为 {}SymlinkEntry depset 或要添加到 runfiles 的符号链接的映射。符号链接始终添加到主工作区的 runfiles 目录下(例如 <runfiles_root>/_main/<symlink_path>,而不是 与当前目标的仓库对应的目录。请参阅规则指南中的 Runfiles 符号链接。
|
root_symlinks
|
字典;或 SymlinkEntrys 的 SymlinkEntry;
默认值为 {}SymlinkEntry depset 或要添加到 runfiles 的符号链接的映射。请参阅规则指南中的 Runfiles 符号链接。 |
split_attr
struct ctx.split_attr
super
unknown ctx.super()
target_platform_has_constraint
bool ctx.target_platform_has_constraint(constraintValue)
参数
| 参数 | 说明 |
|---|---|
constraintValue
|
ConstraintValueInfo;
必需 用于检查目标平台的限制条件值。 |
toolchains
ToolchainContext ctx.toolchains
var
dict ctx.var
version_file
File ctx.version_file
workspace_name
string ctx.workspace_name
--enable_bzlmod 处于开启状态,则此名称是固定字符串 _main。否则,此名称是 WORKSPACE 文件中定义的工作区名称。