image.png

什么是Bazel

Bazel是一个类似于Make的编译工具,是Google为其内部软件开发的特点量身定制的工具,如今Google使用它来构建内部大多数的软件。(怪不得看起来很像Android.bp语法 O(∩_∩)O)

Google认为直接用Makefile构建软件速度太慢,结果不可靠,所以构建了一个新的工具叫做Bazel,Bazel的规则层级更高。

image.png

开始使用

Bazel的编译是基于工作区(workspace)的概念。

workspace

workspace存放了所有源代码和Bazel编译输出文件的目录,也就是整个项目的根目录。

workspace需要包含的必要文件:

  • WORKSPACE文件,用于指定当前文件夹就是一个Bazel的工作区。所以WORKSPACE文件总是存在于项目的根目录下。

  • BUILD文件,用于告诉Bazel怎么构建项目的不同部分。(如果工作区中的一个目录包含BUILD文件,那么它就是一个package)

要指定一个目录为Bazel的工作区,就只要在该目录下创建一个空的WORKSPACE文件即可。

但是在百度Apollo源码我们只能看到一个WORKSPACE.in文件:
.
├── apollo_docker.sh
├── apollo.doxygen
├── apollo.sh
├── BUILD
├── CONTRIBUTING.md
├── CPPLINT.cfg
├── cyber
├── docker
├── docs
├── LICENSE
├── MANIFESTO.md
├── modules
├── README.md
├── readthedocs.yml
├── RELEASE.md
├── scripts
├── third_party
├── tools
├── ubuntu_18.04_env
└── WORKSPACE.in

根据官网的描述:

一个工作区是在文件系统包含的源文件要构建的软件,以及符号链接到包含生成输出目录的目录。每个工作空间目录都有一个名为的文本文件WORKSPACE,该文件可以为空,或者可以包含 对构建输出所需的外部依赖项的引用。

包含名为的文件 WORKSPACE的目录被视为工作空间的根。因此,Bazel会忽略工作空间中的任何目录树,这些工作树植根于包含WORKSPACE文件的子目录(因为它们形成另一个工作空间)。

Bazel还支持将WORKSPACE.bazelfile作为文件的别名WORKSPACE。如果两个文件都存在,WORKSPACE.bazel将具有优先权。

可以看出bazel并不支持名为WORKSPACE.in的文件.

继续搜索源码,在文件apollo.sh中:

image.png

这里sed的作用:

sed 可依照脚本的指令来处理、编辑文本文件。
Sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。

文件中大部分描述的就是编译过程中所需要的外部依赖.

具体语法可以参考官网.

BUILD文件

BUILD文件中包含了多个不同类型的bazel指令。

其中最重要的是编译规则(build rule),它告诉bazel怎么编译目标输出,是一个执行文件还是一个库。

BUILD文件中每一个编译规则被称为target,指向了一堆源文件和相关的依赖,一个target也可以指向其他target。
例子:

cc_binary(name = "hello-world",srcs = ["hello-world.c"],
)

其中的cc_binary,name,srcs都是相关的target.这些语法和Android.bp的语法很像,就不在多说.

看一下Apollo源码中的写法:(以location为例)

load("//tools:cpplint.bzl", "cpplint")package(default_visibility = ["//visibility:public"])filegroup(name = "localization_testdata",srcs = glob(["testdata/*"]),
)cpplint()

这里声明加载tools:cpplint.bzl文件的cpplint函数:

def cpplint(data=None, extra_srcs=None):"""For every rule in the BUILD file so far, adds a test rule that runscpplint over the C++ sources listed in that rule.  Thus, BUILD file authorsshould call this function at the *end* of every C++-related BUILD file.By default, only the CPPLINT.cfg from the project root and the currentdirectory are used.  Additional configs can be passed in as data labels.Sources that are not discoverable through the "sources so far" heuristic canbe passed in as extra_srcs=[]."""# Iterate over all rules.for rule in native.existing_rules().values():# Extract the list of C++ source code labels and convert to filenames.candidate_labels = (_extract_labels(rule.get("srcs", ())) +_extract_labels(rule.get("hdrs", ())))source_labels = [label for label in candidate_labelsif _is_source_label(label)]source_filenames = ["$(location %s)" % x for x in source_labels]# Run the cpplint checker as a unit test.if len(source_filenames) > 0:_add_linter_rules(source_labels, source_filenames, rule["name"], data)# Lint all of the extra_srcs separately in a single rule.if extra_srcs:source_labels = extra_srcssource_filenames = ["$(location %s)" % x for x in source_labels]_add_linter_rules(source_labels, source_filenames,"extra_srcs_cpplint", data)

Bazel编译看来也不是那么简单的.....

我们看一下根目录的BUILD文件:

package(default_visibility = ["//visibility:public"],
)exports_files(["CPPLINT.cfg",
])

其中导入了一个cfg文件:

# Stop searching for additional config files.
set noparent# Disable a warning about C++ features that were not in the original
# C++11 specification (and so might not be well-supported).
filter=-build/c++11# Disable header_guard warning
# Consider using #pragma once instead
filter=-build/header_guard

cfg文件中设置了一些编译器的使用.

我们再看一下Bazel自己的配置文件.bazelrc

# load bazelrc from the legacy location
# as recommended in https://github.com/bazelbuild/bazel/issues/6319
import %workspace%/tools/bazel.rc

导入tools/bazel.rc文件,这个文件的内容如下:

# bazelrc file
# https://docs.bazel.build/versions/master/user-manual.html# bazel >= 0.18 looks for %workspace%/.bazelrc (which redirects here)
# Older bazel versions look for %workspace%/tools/bazel.rc (this file)
# See https://github.com/bazelbuild/bazel/issues/6319# +------------------------------------------------------------+
# | Startup Options                                            |
# +------------------------------------------------------------+
startup --batch_cpu_schedulingstartup --host_jvm_args=-XX:-UseParallelGCstartup --output_user_root=/apollo/.cache/bazel
# +------------------------------------------------------------+
# | Test Configurations                                        |
# +------------------------------------------------------------+# By default prints output only from failed tests.
test --test_output=errors# Work around the sandbox issue.
test --spawn_strategy=standalone# Specify protobuf cc toolchain
test --proto_toolchain_for_cc="@com_google_protobuf//:cc_toolchain"# +------------------------------------------------------------+
# | CPP Lint Tests & Unit Tests                                |
# +------------------------------------------------------------+
# By default, cpplint tests are run as part of `bazel test` alongside all of
# the other compilation and test targets.  This is a convenience shortcut to
# only do the cpplint testing and nothing else.
# Do bazel test --config=cpplint <target> to enable this configuration.
# To enable the lint test, the BUILD *must* load the cpplint.bzl by having
# 'load("//tools:cpplint.bzl", "cpplint")' at the beginning and 'cpplint()'
# at the end.
test:cpplint --test_tag_filters=cpplint
test:cpplint --build_tests_only# Regular unit tests.
test:unit_test --test_tag_filters=-cpplint# Coverage tests
test:coverage --test_tag_filters=-cpplint
test:coverage --copt=--coverage
test:coverage --cxxopt=--coverage
test:coverage --cxxopt=-fprofile-arcs
test:coverage --cxxopt=-ftest-coverage
test:coverage --linkopt=-coverage
test:coverage --linkopt=-lgcov
test:coverage --linkopt=-lgcc
test:coverage --linkopt=-lc# +------------------------------------------------------------+
# | Build Configurations                                       |
# +------------------------------------------------------------+
# Do not show warnings from external dependencies.
build --output_filter="^//"build --show_timestamps# Work around the sandbox issue.
build --spawn_strategy=standalone# Specify protobuf cc toolchain
build --proto_toolchain_for_cc="@com_google_protobuf//:cc_toolchain"# build with profiling
build:cpu_prof --linkopt=-lprofilerbuild --copt="-Werror=sign-compare"
build --copt="-Werror=return-type"
build --copt="-Werror=reorder"
build --copt="-Werror=unused-variable"
build --copt="-Werror=unused-but-set-variable"
build --copt="-Werror=switch"# Strict check on type conversion.
# absl/strings/str_cat.h breaks the rule.
# build --per_file_copt=^modules/.*\.cc,-modules/tools/visualizer/.*\.cc,^cyber/.*\.cc@-Werror=conversion# Enable C++14
build --cxxopt="-std=c++1y"
# Enable colorful output of GCC
# build --cxxopt="-fdiagnostics-color=always"# +------------------------------------------------------------+
# | Python Configurations                                      |
# +------------------------------------------------------------+
run --python_path=/usr/bin/python3

也是配置各种编译环境,写的还是蛮复杂的.

参考

官网Bazel简介:编译一个C++工程


http://www.taodudu.cc/news/show-3204914.html

相关文章:

  • 人脸识别算法评价指标——TAR,FAR,FRR,ERR
  • 百度人脸 检测 识别 python3.5 APIV3版本
  • react-native 接百度地图API(显示周边poi)
  • Android 百度地图SDK 实现获取周边位置POI
  • Android集成百度地图SDK
  • android 百度周边雷达,android 百度地图SDK 3.7.0 周边雷达-Radar 周边搜索-PoiSearch
  • Android之百度地图集成
  • 近期好用的资源搜索(阿里云盘、百度云盘)
  • 强烈推荐,7个资源搜索网站,从此告别资源付费
  • 5分钟实现一个百度网盘资源搜索引擎,不用写代码...
  • 3个超实用的资源搜索网站,有了它们,再也没有你找不到的资源!
  • 黑科技丨资源搜索神器
  • 资源搜索,推荐3个比网盘搜索质量度高的平台和技巧!
  • 7个常用资源搜索网站推荐
  • 资源搜索引擎
  • 三分钟就能快速注册好域名的方法
  • 无80和443端口下申请域名SSL证书(适用于 acme.sh 和 certbot)
  • 怎么申请域名和空间
  • 【群晖】命令行 acme.sh 自动申请域名证书
  • 从零开始建网站(一)-- 申请域名
  • 如何申请域名并绑定ip
  • 基于 acme.sh 自动申请域名证书(群晖 Docker)
  • 申请域名绑定IP开通80端口记录
  • 如何申请域名和购买服务器?
  • 纸鸢|如何申请域名并完成备案和解析详细教程步骤
  • 把五个字留给女人
  • 由于不可抗力,这是我今年最后一篇博文了
  • 谢谢你,勾引我老公
  • 因为我们正试图把许多的期待留给春
  • 我是否仍然爱着你

百度Apollo源码学习之Bazel编译介绍相关推荐

  1. 百度apollo源码学习(二)apollo中的工厂模式

    文章目录 一.什么是工厂 二. 简单工厂模式 UML类图 简单工厂模式结构 代码 一.定义抽象产品类AbstractProduct 二.定义具体产品类 三.定义工厂类和工厂方法 应用 扩展 一.扩展具 ...

  2. 百度Apollo源码学习之定位系统介绍

    image.png 什么是GNSS GNSS的全称是全球导航卫星系统(Global Navigation Satellite System). 它是泛指所有的卫星导航系统,包括全球的.区域的和增强的, ...

  3. yara 源码学习(二) 规则编译部分

    yara规则的详细信息请参考: https://yara.readthedocs.io/en/stable/writingrules.html 根据官方文档,yara规则长这个样子: [1]:yara ...

  4. UEFI源码学习01-ARM AARCH64编译、ArmPlatformPriPeiCore(SEC)

    文章目录 1. AARCH64编译环境搭建 2. ArmPlatformPriPeiCore 2.1 QEMU_EFI.fd包含了什么 2.2 QEMU virt aarch64相关 2.3 从第一条 ...

  5. yara 源码学习(一) 综述

    准备工作: yara源码  V1.7.1   https://github.com/VirusTotal/yara/releases/tag/v1.7.1 csdn:https://download. ...

  6. VUE源码学习第一篇--前言

    一.目的 前端技术的发展,现在以vue,react,angular为代表的MVVM模式以成为主流,这三个框架大有三分天下之势.react和angular有facebook与谷歌背书,而vue是以一己之 ...

  7. Apollo 5.5 源码学习笔记(五) | transform模块 | Apollo中的坐标系统详解

    本系列博客旨在记录自己在学习百度无人驾驶开源框架Apollo的心得和体会,欢迎大家阅读和点赞,并提出宝贵意见,大家相互学习,如需转载,请注明出处,谢谢! 文章目录 1.前言 2.车辆传感器布局 3.传 ...

  8. Apollo源码剖析学习笔记2

    Apollo 源码剖析学习笔记2 Talker-ListenerNode 目录中包含了 Node 对象.Reader 对象和 Writer 对象.Node 对象主要对应 Ros 中的 Node 节点, ...

  9. 博通Broadcom SDK源码学习与开发1——SDK源码探究与Cable Modem 系统编译

    声明:原创作品,严禁用于商业目的. 本系列文章将全面剖析以Bcm33xxx芯片开发Cablemodem产品的SDK源码为例,从编译系统到各个功能模块进行分析与探讨. 文章目录 0.写在前篇 1. 博通 ...

最新文章

  1. 循环出按钮点击按钮显示按钮上面文字 vue el-button_前端学习计划之VUE学习(一)...
  2. Python 进阶 — 面向对象编程
  3. zoj3381 Osaisen Choudai!
  4. 保存delphi中的library path
  5. UpdateProgress 控件--用户中断
  6. SAP云平台Neo环境支持nodejs module吗
  7. linux 复制分区文件,dd复制分区后目标分区的大小变成原分区了
  8. Python time localtime()方法
  9. 阿里安全归零实验室招聘各路大牛!offer好说!
  10. java程序员_java程序员这个职业赚钱吗,看一线程序员怎么说
  11. 终端服务器超出最大允许连接数
  12. Netty自带的心跳机制——IdleStateHandler
  13. 内连接与外连接的区别
  14. 破解windows7系统密码
  15. 抓取lol全英雄图(不含皮肤)
  16. 串口调试助手(CM野人版)4.0有严重Bug,已经跟作者反应,等候更新
  17. sublime和vscode 格式化Json ——两步走
  18. react为什么要废弃三个生命周期?
  19. 2004年11月12日
  20. 汽车潮流新能向,“大魔王”实力出道

热门文章

  1. Linux安装Git详细步骤
  2. 网易视频云分享:如何搭建MP4媒体服务器
  3. Excel第12享:countif()函数之精确统计
  4. NetCore版本 考勤门禁解决方案,支持中控系列最新BioFace/XFace,海康DS-K1T671人脸识别+测温
  5. 人体十大最佳黄金养生时间。
  6. VS如何完美运行VC++程序代码
  7. 自定义View 实现左右拖动脉象图
  8. ADG-12A-02-D2-1-52不带位置反馈比例换向阀放大器
  9. 表空间配额和UNLIMITED TABLESPACE权限
  10. 矩阵键盘简易计算机设计报告,矩阵键盘设计实验报告.doc