bazel常用用法

下载安装:wget https://github.com/bazelbuild/bazel/releases/download/0.18.0/bazel-0.18.0-installer-linux-x86_64.sh

常见的库用bazel编译的例子

https://github.com/bazelment/trunk

可以顺序看下来

有心人的学习笔记(中文版)

https://blog.gmem.cc/bazel-study-note

package: A directory within the workspace that contains a BUILD file is a package.

package_group: Package groups are sets of packages whose purpose is to limit accessibility of certain rules.

label: All targets belong to exactly one package. The name of a target is called its label, every label uniquely identifies a target.

label syntax: //path/to/package:target-name

If the target is a rule target, then path/to/package is the path to the directory containing the BUILD file, and target-name is what you named the target in the BUILD file (the name attribute). If the target is a file target, then path/to/package is the path to the root of the package, and target-name is the name of the target file, including its full path.

When referencing targets within the same package, you can skip the package path and just use //:target-name. When referencing targets within the same BUILD file, you can even skip the // workspace root identifier and just use :target-name.

Labels start with "//", but package names never do, thus "my/app" is the package containing "//my/app".

内容
定义
链接
Bazel概念(workspace、package、target)

workspace:

package: A directory within the workspace that contains a BUILD file is a package.

target: Each instance of a build rule in the BUILD file is called a target and points to a specific set of source files and dependencies.

https://docs.bazel.build/versions/0.27.0/build-ref.html
Bazel命令行参数   https://docs.bazel.build/versions/0.27.0/guide.html
Bazel外部依赖  

https://docs.bazel.build/versions/0.27.0/external.html

https://docs.bazel.build/versions/0.27.0/best-practices.html#depending-on-binaries

查看依赖图

bazel query --noimplicit_deps 'deps(//main:hello-world)'   --output graph

sudo apt update && sudo apt install graphviz xdot

xdot <(bazel query --noimplicit_deps 'deps(//main:hello-world)' --output graph)

常见规则

cc_binary

cc_library

cc_proto_library

其他规则

filegroup 给一个target集合取一个方便的名字

config_setting ??? https://docs.bazel.build/versions/master/be/general.html#config_setting

规则里的常见属性

srcs: 指定源文件,也可以加二进制文件

outs: 

hdrs: 指定头文件

copts: 指定编译选项,如 "-I<include-paths>"

linkopts: 指定链接选项,如“-pthread”

visibility: 指定target的可见性,默认是同一个BUILD file里的target是可见的

strip_prefix: 去掉前缀

deps: 依赖

固定套路,加载bazel扩展

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")

事实依赖和声明依赖之间的关系

A target X is actually dependent on target Y if and only if Y must be present, built and up-to-date in order for X to be built correctly. "Built" could mean generated, processed, compiled, linked, archived, compressed, executed, or any of the other kinds of tasks that routinely occur during a build.

A target X has a declared dependency on target Y if and only if there is a dependency edge from X to Y in the package of X.

For correct builds, the graph of actual dependencies A must be a subgraph of the graph of declared dependencies D.

三种常见依赖

srcs: Files consumed directly by the rule or rules that output source files.

deps: Rule pointing to separately-compiled modules providing header files, symbols, libraries, data, etc.

data: 比如单元测试需要用到的数据等

为保证增量build的正确性,最好让bazel掌握输入文件的完整集合,而不是目录。

可以使用glob函数,方便使用,“**”可以强制其递归。

data = glob(["testdata/**"])  # use this instead

目录label只可能使用在data依赖中,其他依赖类型不允许。

修改bazel的cache目录

bazel --output_user_root=/path/to/directory build //foo:bar

bazelrc

https://docs.bazel.build/versions/master/best-practices.html#bazelrc

https://docs.bazel.build/versions/master/guide.html#bazelrc

bazel编译可调试版本

bazel build xxx -c dbg

猜你喜欢

转载自www.cnblogs.com/anhongyu/p/12727707.html