https://bazel.build/extending/rules?hl=zh-cn#attributes
Attributes
An attribute is a rule argument. Attributes can provide specific values to a target’s implementation, or they can refer to other targets, creating a graph of dependencies.
- Rule-specific attributes, such as srcs or deps, are defined by passing a map from attribute names to schemas (created using the attr module) to the attrs parameter of rule.
- Common attributes, such as name and visibility, are implicitly added to all rules.
- Additional attributes are implicitly added to executable and test rules specifically. Attributes which are implicitly added to a rule can’t be included in the dictionary passed to attrs.
属性是规则参数。属性可以为目标的 implementation 提供特定值,也可以引用其他目标,从而创建依赖关系图。
- 特定于规则的属性(例如 srcs 或 deps)是通过将属性名称到架构的映射(使用 attr 模块创建)传递给 rule 的 attrs 参数来定义的。
- name 和 visibility 等常用属性会隐式添加到所有规则中。
- 其他属性会具体地隐式添加到可执行和测试规则中。隐式添加到规则的属性无法包含在传递给 attrs 的字典中。
Dependency attributes
Rules that process source code usually define the following attributes to handle various types of dependencies:
- srcs specifies source files processed by a target’s actions. Often, the attribute schema specifies which file extensions are expected for the sort of source file the rule processes. Rules for languages with header files generally specify a separate hdrs attribute for headers processed by a target and its consumers.
- deps specifies code dependencies for a target. The attribute schema should specify which providers those dependencies must provide. (For example, cc_library provides CcInfo.)
- data specifies files to be made available at runtime to any executable which depends on a target. That should allow arbitrary files to be specified.
依赖项属性
处理源代码的规则通常会定义以下属性来处理各种类型的依赖项:
- srcs 用于指定目标的操作处理的源文件。通常,属性架构会指定规则处理的源文件类型所需的文件扩展名。针对具有头文件的语言的规则通常会为目标及其使用方处理的标头指定单独的 hdrs 属性。
- deps 用于指定目标的代码依赖项。属性架构应指定这些依赖项必须提供的 provider。(例如,cc_library 提供 CcInfo。)
- data 指定在运行时可供依赖于目标的任何可执行文件使用的文件。这应允许指定任意文件。
example_library = rule(implementation = _example_library_impl,attrs = {"srcs": attr.label_list(allow_files = [".example"]),"hdrs": attr.label_list(allow_files = [".header"]),"deps": attr.label_list(providers = [ExampleInfo]),"data": attr.label_list(allow_files = True),...},
)
These are examples of dependency attributes. Any attribute that specifies an input label (those defined with attr.label_list, attr.label, or attr.label_keyed_string_dict) specifies dependencies of a certain type between a target and the targets whose labels (or the corresponding Label objects) are listed in that attribute when the target is defined. The repository, and possibly the path, for these labels is resolved relative to the defined target.
这些是依赖项属性
的示例。任何指定输入标签的属性(使用 attr.label_list、attr.label 或 attr.label_keyed_string_dict 定义的属性)都指定了目标与在其标签(或相应的 Label 对象)中列出目标(或相应 Label 对象)之间的特定类型的依赖关系。这些标签的代码库(也可能是路径)将相对于定义的目标进行解析。
example_library(name = "my_target",deps = [":other_target"],
)example_library(name = "other_target",...
)
In this example, other_target is a dependency of my_target, and therefore other_target is analyzed first. It is an error if there is a cycle in the dependency graph of targets.
在此示例中,other_target 是 my_target 的依赖项,因此首先分析 other_target。如果目标的依赖关系图中存在循环,则会出错。
Private attributes and implicit dependencies
A dependency attribute with a default value creates an implicit dependency. It is implicit because it’s a part of the target graph that the user doesn’t specify it in a BUILD file. Implicit dependencies are useful for hard-coding a relationship between a rule and a tool (a build-time dependency, such as a compiler), since most of the time a user is not interested in specifying what tool the rule uses. Inside the rule’s implementation function, this is treated the same as other dependencies.
If you want to provide an implicit dependency without allowing the user to override that value, you can make the attribute private by giving it a name that begins with an underscore (_). Private attributes must have default values. It generally only makes sense to use private attributes for implicit dependencies.
私有属性和隐式依赖项
具有默认值的依赖项属性会创建隐式依赖项。由于它是目标图的一部分,因此用户并未在 BUILD 文件中指定它,因此它是隐式的。隐式依赖项适用于对规则与工具(构建时依赖项,例如编译器)之间的关系进行硬编码,因为大多数情况下,用户都不关心指定规则使用的工具。在规则的实现函数内,系统会将其视为与其他依赖项相同。
如果您想提供隐式依赖项,而又不允许用户替换该值,则可以为属性指定一个以下划线 (_) 开头的名称,使其具有私有属性。私有属性必须具有默认值。通常,只有将私有属性用于隐式依赖项才有意义。
example_library = rule(implementation = _example_library_impl,attrs = {..."_compiler": attr.label(default = Label("//tools:example_compiler"),allow_single_file = True,executable = True,cfg = "exec",),},
)
在此示例中,example_library 类型的每个目标都具有对编译器 //tools:example_compiler 的隐式依赖项。这样,example_library 的实现函数就可以生成调用编译器的操作,即使用户没有将其标签作为输入传递也是如此。由于 _compiler 是私有属性,因此在此规则类型的所有目标中,ctx.attr._compiler 将始终指向 //tools:example_compiler。或者,您也可以将该属性命名为 compiler(不带下划线),并保留默认值。这样一来,用户可以在必要时替换其他编译器,但不知道编译器的标签。
隐式依赖项通常用于与规则实现位于同一代码库中的工具。如果该工具来自执行平台或其他代码库,则该规则应从工具链中获取该工具。