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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM