计数器

传递给规则或方面实现函数的上下文对象。它提供分析当前目标所需的访问权限和方法。

具体来说,它允许实现函数访问当前目标的标签、属性、配置及其依赖项的提供程序。它包含用于声明输出文件和生成这些文件的操作的方法。

上下文对象本质上在实现函数的调用期间存在。在关联函数之外访问这些对象没有意义。如需了解详情,请参阅“规则”页面

成员

操作

actions ctx.actions

包含用于声明输出文件和生成这些文件的操作的方法。

aspect_ids

list ctx.aspect_ids

应用于目标的所有方面的 ID 列表。仅在方面实现函数中可用。

attr

struct ctx.attr

用于访问属性值的结构。这些值由用户提供(如果未提供,则使用默认值)。结构体的属性及其值的类型对应于提供给 rule 函数attrs 字典的键和值。查看使用示例

bin_dir

root ctx.bin_dir

与 bin 目录对应的根目录。

build_file_path

string ctx.build_file_path

已弃用:请改用 ctx.label.package + '/BUILD'。相应规则的 BUILD 文件路径(相对于源代码根目录)。

build_setting_value

unknown ctx.build_setting_value

当前目标所表示的 build 设置的值。如果此上下文不是用于设置 build_setting 属性的规则实例,则读取此属性会出错。

配置

configuration ctx.configuration

当前目标的 build 配置。如需了解详情,请参阅 Starlark 配置类型配置文档

coverage_instrumented

bool ctx.coverage_instrumented(target=None)

返回在为此规则或(如果提供了 target)为相应目标指定的规则执行编译操作时,是否应生成代码覆盖率插桩。(如果提供的是非规则或 Starlark 规则目标,则返回 False。)检查当前规则(如果未提供 Target)的来源或 Target 的来源是否应根据 --instrumentation_filter 和 --instrument_test_targets 配置设置进行插桩。这与配置中的 coverage_enabled 不同,后者用于记录是否为整个运行启用覆盖率数据收集,但不会记录是否应检测特定目标。

参数

参数 说明
target 目标;或 None; 默认值为 None
指定规则的目标。如果未提供,则默认为当前规则。

created_actions

StarlarkValue ctx.created_actions()

对于将 _skylark_testable 设置为 True 的规则,此方法会返回一个 Actions 提供程序,表示当前规则迄今为止创建的所有操作。对于所有其他规则,返回 None。请注意,创建后续操作时,提供程序不会更新,因此如果您想检查这些操作,必须再次调用此函数。

此函数旨在帮助为规则实现辅助函数编写测试,这些辅助函数可能会接收 ctx 对象并在其上创建操作。

disabled_features

list ctx.disabled_features

用户针对相应规则明确停用的一组功能。

exec_groups

ExecGroupCollection ctx.exec_groups

适用于相应规则的执行组的集合,按名称编制索引。使用 ctx.exec_groups[name_of_group] 访问。

可执行文件

struct ctx.executable

包含在标记为 executable=True标签类型属性中定义的可执行文件的 struct。结构体字段与属性名称相对应。结构体中的每个值可以是 FileNone。如果规则中未指定可选属性,则相应的结构体值为 None。如果标签类型未标记为 executable=True,则不会生成相应的结构字段。查看使用示例

expand_location

string ctx.expand_location(input, targets=[])

通过将 $(location //x) 替换为目标 //x 的输出文件的路径,展开给定字符串中的所有 $(location ...) 模板。扩展仅适用于指向相应规则的直接依赖项或在可选实参 targets 中明确列出的标签。如果引用的目标具有多个输出,

$(location ...) 将导致错误。在这种情况下,请使用 $(locations ...),因为它会生成以空格分隔的输出路径列表。它也可以安全地用于单个输出文件。

此函数可让用户在 BUILD 文件中指定命令(例如针对 genrule)。在其他情况下,最好直接操作标签。

参数

参数 说明
input string; required
要展开的字符串。
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)

已弃用。请改用 ctx.var 访问变量。
展开对“创建变量”的所有引用后返回一个字符串。变量必须采用以下格式:$(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

The set of features that are explicitly enabled by the user for this rule. See example of use.

file

struct ctx.file

A 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]
In other words, use file to access the (singular) default output of a dependency. See example of use.

files

struct ctx.files

A 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]
In other words, use files to access the default outputs of a dependency. See example of use.

fragments

fragments ctx.fragments

Allows access to configuration fragments in target configuration.

genfiles_dir

root ctx.genfiles_dir

The root corresponding to genfiles directory.

info_file

File ctx.info_file

The file that is used to hold the non-volatile workspace status for the current build request. See documentation for --workspace_status_command for more information.

label

Label ctx.label

The label of the target currently being analyzed.

outputs

structure ctx.outputs

A pseudo-struct containing all the predeclared output files, represented by 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 outputs dict, then for every entry in the dict, there is a field whose name is the key and whose value is the corresponding File.
  • For every attribute of type attr.output that 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 corresponding File; otherwise the field value is None.
  • For every attribute of type attr.output_list that the rule declares, there is a field whose name is the attribute's name. The field value is a list of File objects 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 executable or test, 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 the executable arg of DefaultInfo.

package_relative_label

Label ctx.package_relative_label(input)

Converts the input string into a Label object, in the context of the package of the target currently being analyzed. If the input is already a 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()Label() 使用的是调用它的 .bzl 文件的软件包的上下文,而不是当前正在分析的目标的软件包。此函数的行为与 native.package_relative_label() 相同,但不能在规则或方面实现函数中使用。

参数

参数 说明
input string;或 Label; 必需
输入标签字符串或 Label 对象。如果传递的是 Label 对象,则按原样返回。

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 string; 默认值为 ''
用于解析的命令。
attribute 字符串;或 None; 默认值为 None
要针对哪个关联属性发出错误,或 None。
expand_locations bool; 默认值为 False
是否展开 $(location) 变量?如需了解详情,请参阅 ctx.expand_location()
make_variables dict;或 None; 默认值为 None
要展开的变量,或 None。
tools 目标序列;默认值为 []
工具列表(目标列表)。
label_dict dict; 默认值为 {}
已解析标签的字典和相应的文件列表(标签:文件列表的字典)。
execution_requirements dict;默认值为 {}
用于安排操作以解决此命令的信息。如需了解实用键,请参阅标记

resolve_tools

tuple ctx.resolve_tools(*, tools=[])

返回一个元组 (inputs, empty list),其中包含运行工具所需的已解析输入的 depset,适合作为 ctx.actions.runctx.actions.run_shell 方法的同名实参进行传递。

ctx.resolve_command 相比,此方法不需要在机器上安装 Bash,因此适用于在 Windows 上构建的规则。为了保持向后兼容性,空列表会作为元组的一部分返回。

参数

参数 说明
tools 目标序列;默认值为 []
工具列表(目标列表)。

规则

rule_attributes ctx.rule

应用方面所针对的规则的规则属性描述符。仅在方面实现函数中可用。

runfiles

runfiles ctx.runfiles(files=[], transitive_files=None, collect_data=False, collect_default=False, symlinks={}, root_symlinks={})

创建 runfiles 对象。

参数

参数 说明
files 文件序列;默认值为 []
要添加到 runfiles 中的文件列表。
transitive_files Filedepset;或 None; 默认值为 None
要添加到 runfiles 中的文件(传递)集。depset 应使用 default 顺序(顾名思义,这是默认顺序)。
collect_data bool; 默认值为 False
不建议使用此参数。请参阅runfiles 指南

是否从 srcs、data 和 deps 属性中的依赖项收集数据运行文件。

collect_default bool; 默认值为 False
不建议使用此参数。请参阅runfiles 指南

是否从 srcs、data 和 deps 属性中的依赖项收集默认的 runfiles。

dict;或 SymlinkEntrydepset;默认值为 {}
可以是 SymlinkEntry depset,也可以是要添加到 runfiles 中的符号链接映射。符号链接始终添加到主工作区的 runfiles 目录下(例如 <runfiles_root>/_main/<symlink_path>而非与当前目标的代码库对应的目录)。请参阅规则指南中的 Runfiles 符号链接
dict;或 SymlinkEntrydepset;默认值为 {}
要添加到 runfiles 中的符号链接的 SymlinkEntry depset 或映射。请参阅规则指南中的 Runfiles 符号链接

split_attr

struct ctx.split_attr

用于访问具有拆分配置的属性的值的结构。如果属性是标签列表,则 split_attr 的值是一个字典,其中包含拆分的键(作为字符串)以及相应分支中 ConfiguredTarget 的列表。如果属性是标签,则 split_attr 的值是一个字典,其中包含拆分的键(以字符串形式)和单个 ConfiguredTarget。具有拆分配置的属性仍会显示在 attr 结构体中,但其值将是合并了所有拆分分支的单个列表。

super

unknown ctx.super()

实验性:调用父级的实现函数并返回其提供程序

target_platform_has_constraint

bool ctx.target_platform_has_constraint(constraintValue)

如果给定的限制值是当前目标平台的一部分,则返回 true。

参数

参数 说明
constraintValue ConstraintValueInfo; 必需
用于检查目标平台的限制条件值。

工具链

ToolchainContext ctx.toolchains

相应规则的默认执行组的工具链。

var

dict ctx.var

配置变量的字典(字符串到字符串)。

version_file

File ctx.version_file

用于保存当前 build 请求的临时工作区状态的文件。如需了解详情,请参阅有关 --workspace_status_command 的文档。

workspace_name

string ctx.workspace_name

工作区的名称,实际上是主代码库的执行根名称和 runfiles 前缀。如果 --enable_bzlmod 为开启状态,则为固定字符串 _main。否则,这是在 WORKSPACE 文件中定义的工作区名称。