【Bazel入门与精通】 rules之属性

2024-06-11 05:20
文章标签 bazel rules 入门 精通 属性

本文主要是介绍【Bazel入门与精通】 rules之属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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(不带下划线),并保留默认值。这样一来,用户可以在必要时替换其他编译器,但不知道编译器的标签。

隐式依赖项通常用于与规则实现位于同一代码库中的工具。如果该工具来自执行平台或其他代码库,则该规则应从工具链中获取该工具。

这篇关于【Bazel入门与精通】 rules之属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1050298

相关文章

C++必修:模版的入门到实践

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:C++学习 贝蒂的主页:Betty’s blog 1. 泛型编程 首先让我们来思考一个问题,如何实现一个交换函数? void swap(int& x, int& y){int tmp = x;x = y;y = tmp;} 相信大家很快就能写出上面这段代码,但是如果要求这个交换函数支持字符型

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

ps基础入门

1.基础      1.1新建文件      1.2创建指定形状      1.4移动工具          1.41移动画布中的任意元素          1.42移动画布          1.43修改画布大小          1.44修改图像大小      1.5框选工具      1.6矩形工具      1.7图层          1.71图层颜色修改          1

C++入门01

1、.h和.cpp 源文件 (.cpp)源文件是C++程序的实际实现代码文件,其中包含了具体的函数和类的定义、实现以及其他相关的代码。主要特点如下:实现代码: 源文件中包含了函数、类的具体实现代码,用于实现程序的功能。编译单元: 源文件通常是一个编译单元,即单独编译的基本单位。每个源文件都会经过编译器的处理,生成对应的目标文件。包含头文件: 源文件可以通过#include指令引入头文件,以使

LVGL快速入门笔记

目录 一、基础知识 1. 基础对象(lv_obj) 2. 基础对象的大小(size) 3. 基础对象的位置(position) 3.1 直接设置方式 3.2 参照父对象对齐 3.3 获取位置 4. 基础对象的盒子模型(border-box) 5. 基础对象的样式(styles) 5.1 样式的状态和部分 5.1.1 对象可以处于以下状态States的组合: 5.1.2 对象

C语言入门系列:探秘二级指针与多级指针的奇妙世界

文章目录 一,指针的回忆杀1,指针的概念2,指针的声明和赋值3,指针的使用3.1 直接给指针变量赋值3.2 通过*运算符读写指针指向的内存3.2.1 读3.2.2 写 二,二级指针详解1,定义2,示例说明3,二级指针与一级指针、普通变量的关系3.1,与一级指针的关系3.2,与普通变量的关系,示例说明 4,二级指针的常见用途5,二级指针扩展到多级指针 小结 C语言的学习之旅中,二级

打造坚固的SSH防护网:端口敲门入门指南

欢迎来到我的博客,代码的世界里,每一行都是一个故事 🎏:你只管努力,剩下的交给时间 🏠 :小破站 打造坚固的SSH防护网:端口敲门入门指南 前言什么是端口敲门端口敲门的优点1. 增强安全性2. 动态防火墙规则3. 隐匿服务4. 改善日志管理5. 灵活性和兼容性6. 低资源消耗7. 防御暴力破解和扫描8. 便于合法用户访问9. 适用于不同类型的服务 端口敲

好书推荐《深度学习入门 基于Python的理论与实现》

如果你对Python有一定的了解,想对深度学习的基本概念和工作原理有一个透彻的理解,想利用Python编写出简单的深度学习程序,那么这本书绝对是最佳的入门教程,理由如下:     (1)撰写者是一名日本普通的AI工作者,主要记录了他在深度学习中的笔记,这本书站在学习者的角度考虑,秉承“解剖”深度学习的底层技术,不使用任何现有的深度学习框架、尽可能仅使用基本的数学知识和Python库。从零创建一个

手把手教你入门vue+springboot开发(五)--docker部署

文章目录 前言一、前端打包二、后端打包三、docker运行总结 前言 前面我们重点介绍了vue+springboot前后端分离开发的过程,本篇我们结合docker容器来研究一下打包部署过程。 一、前端打包 在VSCode的命令行中输入npm run build可以打包前端代码,出现下图提示表示打包完成。 打包成功后会在前端工程目录生成dist目录,如下图所示: 把

CALayer相关的属性

iOS开发UI篇—CAlayer层的属性 一、position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设置CALayer在父层中的位置 以父层的左上角为原点(0, 0)   @property CGPoint anchorPoint; 称为“定位点”、“锚点”