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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/26023.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

B3810 [语言月赛 202307] 扶苏和串

[语言月赛 202307] 扶苏和串 题目背景 众所周知,每个月入门赛的字符串题都是扶苏来枚举 idea 出出来的。 题目描述 给定一个 01 字符串 s s s,你可以任选 s s s 的一个非空子串,把这个子串在 s s s 中翻转一次。 问你能得到字典序最小…

R语言:使用 stringr 包进行字符串处理和正则表达式匹配

在 R 编程中,字符串处理和正则表达式匹配是常见的需求,而 stringr 包提供了强大的工具来简化这些操作。本文将介绍 stringr 包的基础用法,并结合正则表达式的相关内容,帮助你高效处理字符串数据。 字符串基础 创建字符串 在 R …

大模型训练数据白皮书深度解读

摘要 随着人工智能技术的飞速发展,大模型在各个领域的应用日益广泛。《大模型训练数据白皮书》为我们提供了对大模型训练数据重要性的深刻理解,本文将深度解读白皮书的主要内容,探讨大模型训练数据的关键要素、面临的挑战与未来发展趋势。 …

金融量化分析开源工具:TuShare

TuShare:一站式金融数据解决方案,让量化分析触手可及- 精选真开源,释放新价值。 概览 TuShare,是Github社区上一个专为金融量化分析师和数据爱好者设计的开源工具,提供了从数据采集、清洗加工到数据存储的全流程服务。…

缓存更新策略中级总结

背景 看到好些人在写更新缓存数据代码时,先删除缓存,然后再更新数据库,而后续的操作会把数据再装载的缓存中。然而,这个是逻辑是错误的。试想,两个并发操作,一个是更新操作,另一个是查询操作…

构建大语言模型友好型网站

以大语言模型为代表的AI 技术迅速发展,将会影响原有信息网络的方式。其中一个明显的趋势是通过chatGPT 对话代替搜索引擎和浏览器来获取信息。 互联网时代,主要是通过网站(website)提供信息。网站主要为人类阅读的方式构建的。主要…

高通Android开关机动画踩坑简单记录

1、下面报错有可能是selinux的原因 Read-only file system 2、接着push 动画 reboot之后抓取logcat出现 以下报错。看着大概意思像是压缩格式有问题。 3、于是重新压缩一下报错没有再出现 ,压缩格式默认是标准,这里必须要改成存储格式哈 4、修改之后重新…

GitHub工程git merge出现冲突处理方式

GitHub工程git merge出现冲突处理方式 1. 源由2. 冲突解决2.1 触发合并冲突2.2 查看冲突状态2.3 打开冲突文件2.4 解决冲突2.5 标记冲突已解决2.6 继续合并2.7 检查合并结果 3. 示例 1. 源由 在产品研发中,常见的技术动作就是merge,而这个操作通常有两种…

Mysql基础 - 事务

Mysql基础 - 事务 文章目录 Mysql基础 - 事务1 事务简介2 事务操作2.1 控制事务一2.2 控制事务二 3 事务四大特性4 并发事务问题5 事务隔离级别 1 事务简介 事务是一组操作的集合,他是一个不可分割的工作单位,事务会把所有操作作为一个整体一起向系统提…

tcp协议的面向字节流介绍,粘包问题(解决的本质)

目录 面向字节流 引入 介绍 比喻 处理数据 粘包问题 引入 介绍 解决的本质 面向字节流 引入 对于udp来说,它是面向数据报的 一旦要发送数据,因为没有发送缓冲区,且不需要维护连接,直接封装完报头就发出去了依靠报头中的udp长度字段,可以拆分出报文然后交付给上层一个…

解决Vue项目Network: unavailable的问题

在vscode使用 npm run serve 运行 Vue项目时发现一个问题,项目只能通过Local访问而不能通过Network访问,终端显示如下: 碰到这种情况的解决方法:在环境变量的path中添加“C:\Windows\System32\Wbem” 1.找到“环境变量”&#xf…

Apollo9.0 PNC源码学习之Control模块(二)

前面文章:Apollo9.0 PNC源码学习之Control模块(一) 本文将对具体控制器以及原理做一个剖析 1 PID控制器 1.1 PID理论基础 如下图所示,PID各参数(Kp,Ki,Kd)的作用: 任何闭环控制系统的首要任务是要稳、准、快的响…

【JavaEE】Spring Boot MyBatis详解(一)

一.MyBatis的基本概念与相关配置. 1.基本概念 MyBatis是一款优秀的持久层框架,用于简化JDBC的开发。MyBatis本是Apache的一个开源项目iBatis,2010年这个项目由apache迁移到了google code,并且改名为MyBatis. 2013年11月迁移到Github.持久层…

.net core webapi跨域

var builder WebApplication.CreateBuilder(args);// Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();//此处1 …

13 RTP包的使用

RTP RTP包最主要的就是Sequence number。 对于发送者来说,视频的每一个帧都有很多包组成。对于接收端来接收的时候是有一个队列进行接收的。这个队列大小都是通过计算的。有了队列之后就会不断的往队列中插入数据。当队列中有的数据超时一直组不成包的时候&#xf…

STM32 UART串口与RTOS的结合使用

STM32 UART串口与RTOS的结合使用 摘要: 实时操作系统(RTOS)为嵌入式系统提供了多任务处理和实时性能。STM32微控制器结合RTOS,可以有效地管理串口通信任务,提高系统的响应速度和稳定性。本文将探讨STM32 UART串口与RT…

MacOS升级ruby版本

MacOS自带ruby版本是2.x,可以通过“ruby -v”查看版本号 $ ruby -v ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin22]homebrew安装的ruby版本号可以通过“brew info ruby”命令参看 $ brew info ruby > ruby: stable 3.3.2 (bottled)…

深度学习的可微渲染

深度学习的可微渲染 可微渲染(Differentiable Rendering)是深度学习领域的一个重要概念,它将传统的计算机图形学与深度学习结合起来,通过使渲染过程可微分(differentiable),以便于在深度学习模…

SpringMVC01-初始SpringMVC

SpringMVC 回顾MVC 什么是MVC MVC是模型(Model)、视图(View)、控制器(Controller)的简写,是一种软件设计规范。是将业务逻辑、数据、显示分离的方法来组织代码。MVC主要作用是降低了视图与业务逻辑间的双向偶合。MVC不是一种设计模式,MVC是一种架构模…

如何使用Pandas处理数据?

一、技术难点 Pandas是Python中一个强大的数据处理和分析库,它提供了高效、灵活且易于使用的数据结构,主要用于数据清洗、转换、聚合和可视化等任务。然而,在使用Pandas处理数据时,也会遇到一些技术难点。 数据导入与导出&#…