bazel-编译一个源文件生成可执行程序
demo1 使用bazel编译一个源文件生成可执行程序简单示例
demo1目录树
demo1
├── app
│ ├── BUILD
│ └── hello_world.cpp
├── README.md
└── WORKSPACE
workspace
A workspace is a directory on your filesystem that contains the source files for the software you want to build, as well as symbolic links to directories that contain the build outputs. Each workspace directory has a text file named
WORKSPACEwhich may be empty, or may contain references to external dependencies required to build the outputs. See also the Workspace Rules section in the Build Encyclopedia.
workspace可以理解为一个project的根目录所在地,那么如果要指定一个目录为bazel的workspace,只需要在该目录下创建一个workspace文件即可。workspace可以为空,当然,当工程庞大复杂有外部依赖时,可以在workspace中指定外部依赖引用。
本例中,在demo1目录下创建了一个空的workspace文件,那么demo1目录将作为一个workspace。
这里使用一个类比的例子。一般写字楼里会入驻很多公司,workspace就好比一个公司,每个公司都有自己的大门,而这个大门就好比是workspace文件。我们只要看到公司的大门,就知道,哦,这是一个公司。同样,我们看到一个workspace文件就知道本目录是一个workspace空间。
package
The primary unit of code organization in a workspace is the package. A package is a collection of related files and a specification of the dependencies among them.
A package is defined as a directory containing a file named
BUILD, residing beneath the top-level directory in the workspace. A package includes all files in its directory, plus all subdirectories beneath it, except those which themselves contain a BUILD file.
package可以理解为workspace空间中的子模块。在workspace空间下创建一个目录,该目录下再创建一个BUILD文件,那么该目录就可以看作是一个package。该目录下的所有对象都属于该package,除了子目录下又有BUILD文件。
本例中,app就是一个package。
一个package就好比一个公司里不同部门的办公室,每个办公室都有自己的门,而这个门就好比是一个BUILD文件。
target
A package is a container. The elements of a package are called targets. Most targets are one of two principal kinds, filesand rules. Additionally, there is another kind of target, package groups, but they are far less numerous.
Files are further divided into two kinds. Source files are usually written by the efforts of people, and checked in to the repository.Generated files, sometimes called derived files, are not checked in, but are generated by the build tool from source files according to specific rules.
The second kind of target is the rule. A rule specifies the relationship between a set of input and a set of output files, including the necessary steps to derive the outputs from the inputs. The outputs of a rule are always generated files. The inputs to a rule may be source files, but they may be generated files also; consequently, outputs of one rule may be the inputs to another, allowing long chains of rules to be constructed.
package中的target主要有两种:文件和规则。文件也分为两种:源文件和生成文件。规则定义怎样根据输入生成相应的输出。
# label
All targets belong to exactly one package. The name of a target is called its label, and a typical label in canonical form looks like this:
//app:hello_world
Each label has two parts, a package name (app) and a target name (hello_world). Every label uniquely identifies a target. Labels sometimes appear in other forms; when the colon is omitted, the target name is assumed to be the same as the last component of the package name, so these two labels are equivalent:
lable标签属于一个包,一个标签由两部分组成:包名+目标名并由“//”开头,即 //package-name:target-name。这类似与一个绝对路径,保证搜索不会出错。本例中报名就是app,目标名就是hello_world。
rules
A rule specifies the relationship between inputs and outputs, and the steps to build the outputs. Rules can be of one of many different kinds or classes, which produce compiled executables and libraries, test executables and other supported outputs as described in the Build Encyclopedia.
Every rule has a name, specified by the
nameattribute, of type string. The name must be a syntactically valid target name, as specified above. In some cases, the name is somewhat arbitrary, and more interesting are the names of the files generated by the rule; this is true of genrules. In other cases, the name is significant: for*_binaryand*_testrules, for example, the rule name determines the name of the executable produced by the build.Every rule has a set of attributes; the applicable attributes for a given rule, and the significance and semantics of each attribute are a function of the rule's class; see the Build Encyclopedia for the full list of supported rules and their corresponding attributes. Each attribute has a name and a type. Some of the common types an attribute can have are integer, label, list of labels, string, list of strings, output label, list of output labels. Not all attributes need to be specified in every rule. Attributes thus form a dictionary from keys (names) to optional, typed values.
规则指定了生成什么目标,如,可执行程序,库文件等。每一条规则都有名字,而名字通过name属性定义。每条规则包含很多属性,每个属性可看成是key-value,value值有自己支持类型,如 integer, label, list of labels, string, list of strings, output label, list of output labels。
我们前面提到,一个package包含一个BUILD文件,而BUILD文件中要写什么内容呢?就是rules。即需要我们来写构建规则,使bazel构建出我们想要的东东。
构建规则的内容涉及非常多的细节,本系列采用示例的形式,由浅到深,逐渐的带出构建规则。
本例中,BUILD文件内容如下:
cc_binary(
name = "hello_world",
srcs = ["hello_world.cpp"],
)
cc_binary即声明了一个构建规则,用于编译生成一个可执行文件。可执行文件名(目标名)由name属性指定,name属性的值的类型可以看出是string类型。srcs属性指定了源文件,srcs属性的值的类型可以看出是list of strings。
*_binary 规则:指定生成相应语言的可执行程序。cc_binary表示c++可执行程序,jave_binary表示java可执行程序。
*_library 规则:指定生成相应语言的库。
*_test 规则:是一个特殊的bianry规则,通常用于自动化测试。
当我们准备好了workspace,package,编写好了BUILD文件,接下来就可以用bazel来生成我们想要的可执行程序。

先进入到workspace空间,即demo1目录;然后指定编译那个target,即执行bazel build //app:hello_world;其中app是本空间中包路径,hello_world是指定package中的目标路径。最终生成可执行程序hello_world。同时所有的生成的文件都在workspace所在的目录下。
]
生成的hello_world文件放在bazel-bin下,执行bazel-bin/app/hello_world即可运行该程序。
dependency graph
当一个项目庞大后,我想找到各部分之间的依赖关系,我们可以通过依赖图来查看。
]
通过query命令来生成依赖图。从中可以看出,//app:hello_world" 依赖 "//app:hello_world.cpp"
bazel-编译一个源文件生成可执行程序的更多相关文章
- QStringLiteral(源代码里有一个通过构造函数产生的从const char*到QString的隐式转换,QStringLiteral字符串可以放在代码的任何地方,编译期直接生成utf16字符串,速度很快,体积变大)
原作者: Olivier Goffart 点击打开链接http://woboq.com/blog/qstringliteral.html 译者: zzjin 点击打开链接http://www.tuic ...
- 使用ant自动编译、打包生成apk文件
上次使用命令行生成apk文件<Android 命令行编译.打包生成apk文件>,学习命令行生成的目的是为了编写ant打下基础. 一. ant环境 下载ant包,配置环境变量 二.ant编译 ...
- Android 自动编译、打包生成apk文件 2 - 使用原生Ant方式
from://http://blog.csdn.net/androiddevelop/article/details/11100109 相关文章列表: <Android 自动编译.打包生成apk ...
- 分步编译一个C语言文件
一. 以下是C程序一般的编译过程: 从图中看到: 将编写的一个c程序(源代码 )转换成可以在硬件上运行的程序(可执行代码 ),需要进行编译阶段 和链接这两个阶段. 其中, 1. 编译阶段先通过“编 ...
- python如何编译py文件生成pyc、pyo、pyd以及如何和C语言结合使用
python执行py文件的流程 当我们执行一个py文件的时候,直接python xx.py即可,那么这个流程是怎么样的呢.先说明一下,python执行代码实际上是先打开文件然后执行里面的代码,所以文件 ...
- 组件化框架设计之apt编译时期自动生成代码&动态类加载(二)
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 本篇文章将继续从以下两个内容来介绍组件化框架设计: apt编译时 ...
- java如何在eclipse编译时自动生成代码
用eclipse写java代码,自动编译时,如何能够触发一个动作,这个动作是生成本项目的代码,并且编译完成后,自动生成的代码也编译好了, java编辑器中就可以做到对新生成的代码的自动提示? 不生成代 ...
- 用命令行编译java并生成可执行的jar包
用命令行编译java并生成可执行的jar包 1.编写源代码. 编写源文件:CardLayoutDemo.java并保存,例如:I:\myApp\CardLayoutDemo.java.程序结构如下: ...
- 使用 DJ Java Decompiler 将整个jar包反编译成源文件
使用 DJ Java Decompiler 将整个jar包反编译成源文件 所使用的软件是 DJ Java Decompiler 3.9. 下面是一个有用的参考文档,说明如何批量编译 http://ww ...
随机推荐
- js 操作select和option常用代码整理
1.获取选中select的value和text,html代码如下: <select id="mySelect"> <option value="1&qu ...
- go语言基础之defer和匿名函数结合使用
1.匿名函数结合使用 示例1: package main //必须 import "fmt" func main() { a := 10 b := 20 defer func() ...
- django from组件 实现增加 删除 编辑(推荐用法)
实现效果: 代码示例: 代码: models.py from django.db import models class UserInfo(models.Model): username = mode ...
- .NET-DataTable或DataSet转JSON
通过AJAX异步减少网络内容传输,而JSON则可以把传输内容缩减到纯数据:然后利用jQuery内置的AJAX功能直接获得JSON格式的数据:在客户端直接绑定到数据控件里面,从而达到最优. #regio ...
- oracle中database links的使用
1.在pl/sql developer中创建database links 2.使用database links select * from table1@xtbg 注意:@xtbg是database ...
- bundle中vim相关快捷键的使用
http://www.cnblogs.com/respawn/archive/2012/08/21/2649483.html http://blog.163.com/liao_ya/blog/stat ...
- object sender, LinkLabelLinkClickedEventArgs e 参数解释
开始是一个LinkLabel 控件,LinkLabel 绑定了Clicke事件 LinkLabel Delete = new LinkLabel(); Delete.Text = "删除&q ...
- 算法笔记_132:最大流量问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 何为最大流量问题? 给定一个有向图,并为每一个顶点设定编号为0~n,现在求取从顶点0(PS:也可以称为源点)到顶点n(PS:也可以称为汇点)后,顶点 ...
- QtGui.QColorDialog
he QtGui.QColorDialog provides a dialog widget for selecting colour values. #!/usr/bin/python # -*- ...
- ant design pro (十六)advanced 权限管理
一.概述 原文地址:https://pro.ant.design/docs/authority-management-cn 权限控制是中后台系统中常见的需求之一,你可以利用我们提供的权限控制组件,实现 ...