计数器

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

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

上下文对象本质上在调用实现函数的整个过程中都存在。在相关函数之外访问这些对象没有用处。如需了解详情,请参阅“规则”页面。

成员

操作

actions ctx.actions

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

aspect_ids

list ctx.aspect_ids

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

attr

struct ctx.attr

用于访问 属性 值的结构体。这些值由用户提供(如果未提供,则使用默认值)。结构体的属性及其值的类型与提供给 rule functionattrs dict 的键和值相对应。请参阅使用示例

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。)检查是否应根据 --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] 进行访问。

executable

struct ctx.executable

一个 struct,其中包含在标记为 executable=True标签类型属性中定义的可执行文件。结构体字段与属性名称相对应。结构体中的每个值都是 FileNone。如果规则中未指定可选属性,则相应的结构体值为 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)

已废弃。请改用 ctx.var 访问变量。
返回展开对“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

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 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.runctx.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={})

创建 runfiles 对象。

参数

参数 说明
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。

字典;或 SymlinkEntrys 的 SymlinkEntry; 默认值为 {}
SymlinkEntry depset 或要添加到 runfiles 的符号链接的映射。符号链接始终添加到主工作区的 runfiles 目录下(例如 <runfiles_root>/_main/<symlink_path>而不是 与当前目标的仓库对应的目录。请参阅规则指南中的 Runfiles 符号链接
字典;或 SymlinkEntrys 的 SymlinkEntry; 默认值为 {}
SymlinkEntry depset 或要添加到 runfiles 的符号链接的映射。请参阅规则指南中的 Runfiles 符号链接

split_attr

struct ctx.split_attr

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

super

unknown ctx.super()

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

target_platform_has_constraint

bool ctx.target_platform_has_constraint(constraintValue)

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

参数

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

toolchains

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 文件中定义的工作区名称。