Bazel 概念与对象
用 Draw.io 打开 bazel-overview.drawio.svg
用 Draw.io 打开 bazel-concept.drawio.svg
用 Draw.io 打开 bazel-external-dep.drawio.svg
CPP use cases
Adding include paths
https://bazel.build/tutorials/cpp-use-cases#add-include-paths
Sometimes you cannot (or do not want to) root include paths at the workspace root. Existing libraries might already have an include directory that doesn’t match its path in your workspace. For example, suppose you have the following directory structure:
└── my-project
├── legacy
│ └── some_lib
│ ├── BUILD
│ ├── include
│ │ └── some_lib.h
│ └── some_lib.cc
└── WORKSPACE
Bazel will expect some_lib.h
to be included as legacy/some_lib/include/some_lib.h
, but suppose some_lib.cc
includes "some_lib.h"
. To make that include path valid, legacy/some_lib/BUILD
will need to specify that the some_lib/include
directory is an include directory:
|
|
This is especially useful for external dependencies, as their header files must otherwise be included with a /
prefix.
Include external libraries
https://bazel.build/tutorials/cpp-use-cases#include-external-libraries
Suppose you are using Google Test {: .external}. You can use one of the repository functions in the WORKSPACE
file to download Google Test and make it available in your repository:
|
|
Note: If the destination already contains a BUILD
file, you can leave out the build_file
attribute.
Then create gtest.BUILD
, a BUILD
file used to compile Google Test.
|
|
Writing and running C++ tests
For example, you could create a test ./test/hello-test.cc
, such as:
|
|
Then create ./test/BUILD
file for your tests:
|
|
To make hello-greet
visible to hello-test
, you must add "//test:__pkg__",
to the visibility
attribute in ./main/BUILD
.
Now you can use bazel test
to run the test.
bazel test test:hello-test
This produces the following output:
INFO: Found 1 test target...
Target //test:hello-test up-to-date:
bazel-bin/test/hello-test
INFO: Elapsed time: 4.497s, Critical Path: 2.53s
//test:hello-test PASSED in 0.3s
Executed 1 out of 1 tests: 1 test passes.
External Dependency
Directory layout
After being fetched, the repo can be found in the subdirectory external
in the output base, under its canonical name.
You can run the following command to see the contents of the repo with the canonical name canonical_name
:
|
|
e.g:
|
|
Inspect
Query
dependency graph
|
|
Envoy repo as example:
|
|
What packages exist beneath(下面) //source/
$ bazel query '//source/...' --output package | head
Loading: 0 packages loaded
source/common/access_log
source/common/api
source/common/buffer
source/common/common
source/common/config
source/common/conn_pool
source/common/crypto
source/common/event
source/common/filesystem
source/common/filter
What rules are defined in the //source/exe
package?
$ bazel query 'kind(rule, //source/exe:*)' --output label_kind | head
Loading: 0 packages loaded
alias rule //source/exe:envoy
cc_binary rule //source/exe:envoy-static
cc_library rule //source/exe:envoy_common_lib
cc_library rule //source/exe:envoy_common_lib_with_external_headers
cc_library rule //source/exe:envoy_common_with_core_extensions_lib
cc_library rule //source/exe:envoy_common_with_core_extensions_lib_with_external_headers
cc_library rule //source/exe:envoy_main_common_lib
cc_library rule //source/exe:envoy_main_common_lib_with_external_headers
cc_library rule //source/exe:envoy_main_common_with_core_extensions_lib
cc_library rule //source/exe:envoy_main_common_with_core_extensions_lib_with_external_headers
What files are generated by rules in the foo
package?
$ bazel query 'kind("generated file", //source/exe:*)'
//source/exe:envoy-static.dwp
//source/exe:envoy-static.stripped
Loading: 0 packages loaded