- 下载和安装:
bazel的官网下有各种版本 https://github.com/bazelbuild/bazel/releases
官网:
https://github.com/bazelbuild/bazel
- bazel编译一个最简单的项目示例
目录:
tree -L 2
├── BUILD
├── WORKSPACE
├── build.sh
├── main.cpp
└── subdirectory
├── BUILD
├── file.h
├── file1.cpp
└── file2.cpp
build文件
package(default_visibility = ["http://visibility:public"])
cc_library(
name = "my_project_lib",
srcs = [
"main.cpp",
],
deps = [
"http://subdirectory:subdirectory_lib",
],
)
cc_binary(
name = "my_project",
deps = [
":my_project_lib",
],
)
main.cpp
#include <iostream>
#include "subdirectory/file.h"
int main() {
std::cout << "Hello, World!" << std::endl;
printMessage1();
printMessage2();
return 0;
}
子目录 subdirectory/BUILD
package(default_visibility = ["http://visibility:public"])
cc_library(
name = "subdirectory_lib",
srcs = [
"file1.cpp",
"file2.cpp",
],
hdrs = [
"file.h",
],
)
subdirectory/file1.cpp
#include <iostream>
void printMessage1() {
std::cout << "This is file1.cpp" << std::endl;
}
subdirectory/file2.cpp
#include <iostream>
void printMessage2() {
std::cout << "This is file2.cpp" << std::endl;
}
subdirectory/file.h
#ifndef FILE_H
#define FILE_H
void printMessage1();
void printMessage2();
#endif // FILE_H
build.sh
# build
bazel build :my_project
- 查看编译依赖:
bazel query 可以打印出依赖图,对于复杂项目,这个可以命令可以快速分析出依赖
# query
bazel query 'deps(//:my_project)' -k --output=build > deps.log
deps.log
#~test/bazel_repo/BUILD:13:10
cc_binary(
name = "my_project",
deps = ["http://:my_project_lib"],
)
# Rule my_project instantiated at (most recent call last):
# ~test/bazel_repo/BUILD:13:10 in <toplevel>
#~test/bazel_repo/BUILD:3:11
cc_library(
name = "my_project_lib",
deps = ["http://subdirectory:subdirectory_lib"],
srcs = ["http://:main.cpp"],
)
# Rule my_project_lib instantiated at (most recent call last):
# ~test/bazel_repo/BUILD:3:11 in <toplevel>
#~test/bazel_repo/subdirectory/BUILD:3:11
cc_library(
name = "subdirectory_lib",
srcs = ["http://subdirectory:file1.cpp", "http://subdirectory:file2.cpp"],
hdrs = ["http://subdirectory:file.h"],
)
- 编译debug版本
# debug
bazel build :my_project -c dbg
- 编译指定依赖库:
package(default_visibility = ["http://visibility:public"])
load("http://bazel:cpplint.bzl", "cpplint")
load("http://bazel:build.bzl", "cc_test")
cc_library(
name = "service",
srcs = glob(["*.cpp"]),
hdrs = glob(["*.h"]),
deps = [
"http://other/framework:framework",
"@other_proto//:other_proto",
"@brpc",
"@glog",
],
)
- 编译指定依赖公共库
WORKSPACE文件中声明了brpc的依赖。在WORKSPACE文件中添加以下内容:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "brpc",
urls = ["https://github.com/apache/incubator-brpc/archive/refs/tags/0.9.7.zip"],
strip_prefix = "incubator-brpc-0.9.7",
)
load("@brpc//:workspace.bzl", "brpc_dependencies")
brpc_dependencies()
在你的BUILD文件中,将brpc添加为my_project_lib的依赖项。修改cc_library规则如下:
cc_library(
name = "my_project_lib",
srcs = [
"main.cpp",
],
hdrs = [
"http://subdirectory:file.h",
],
deps = [
"http://subdirectory:subdirectory_lib",
"@brpc//:brpc",
],
)
- 编译协议:
proto_library(
name = "xxxx_proto_pb",
srcs = glob([
"xxxx_proto_pb/*.proto",
]),
)
cc_proto_library(
name = "xxx_proto",
deps = [":xxxx_proto_pb"],
)
- 项目构建:
#!/bin/bash
set -ex
function Build() {
main_target="http://xxxx:xxxx"
other_targets="http://test/yyy:yyyy"
bazel build ${build_args} ${main_target}
bazel build ${build_args} ${other_targets}
}
function Move() {
cp ./start.sh ./output/
cp -r ./conf ./output/
cp ./bazel-bin/xxxx ./output/
}
# main
rm -rf ./output && mkdir ./output
build_args="--loading_phase_threads=1 --copt="-g" --copt="-O2""
build_args=${build_args}" --remote_http_cache=xxxxx"
Build
Move