Skip to content

Commit 8c8cf88

Browse files
author
donghuixu
committed
bazel support example and unittest
1 parent 6b5fc52 commit 8c8cf88

File tree

7 files changed

+342
-12
lines changed

7 files changed

+342
-12
lines changed

BUILD

+35-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ exports_files(["LICENSE"])
55
load(":bazel/brpc.bzl", "brpc_proto_library")
66

77
config_setting(
8-
name = "glog_mode",
9-
define_values = {"with_glog": "1"}
8+
name = "with_glog",
9+
define_values = {"with_glog": "true"},
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
config_setting(
14+
name = "unittest",
15+
define_values = {"unittest": "true"},
1016
)
1117

1218
COPTS = [
@@ -20,7 +26,7 @@ COPTS = [
2026
"-D__STDC_CONSTANT_MACROS",
2127
"-DGFLAGS_NS=google",
2228
] + select({
23-
":glog_mode": ["-DBRPC_WITH_GLOG=1"],
29+
":with_glog": ["-DBRPC_WITH_GLOG=1"],
2430
"//conditions:default": ["-DBRPC_WITH_GLOG=0"],
2531
})
2632

@@ -47,7 +53,7 @@ genrule(
4753
#undef BRPC_WITH_GLOG
4854
#endif
4955
#define BRPC_WITH_GLOG """ + select({
50-
":glog_mode": "1",
56+
":with_glog": "1",
5157
"//conditions:default": "0",
5258
}) +
5359
"""
@@ -191,12 +197,20 @@ cc_library(
191197
deps = [
192198
"@com_google_protobuf//:protobuf",
193199
"@com_github_gflags_gflags//:gflags",
194-
"@com_github_google_glog//:glog",
195-
],
200+
] + select({
201+
":with_glog": ["@com_github_google_glog//:glog"],
202+
"//conditions:default": [],
203+
}),
196204
includes = [
197205
"src/",
198206
],
199-
copts = COPTS,
207+
copts = COPTS + select({
208+
":unittest": [
209+
"-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
210+
"-DUNIT_TEST",
211+
],
212+
"//conditions:default": [],
213+
}),
200214
linkopts = LINKOPTS,
201215
visibility = ["//visibility:public"],
202216
)
@@ -206,7 +220,13 @@ cc_library(
206220
srcs = glob([
207221
"src/bvar/*.cpp",
208222
"src/bvar/detail/*.cpp",
209-
]),
223+
],
224+
exclude = [
225+
"src/bvar/default_variables.cpp",
226+
]) + select({
227+
":unittest": [],
228+
"//conditions:default": ["src/bvar/default_variables.cpp"],
229+
}),
210230
hdrs = glob([
211231
"src/bvar/*.h",
212232
"src/bvar/utils/*.h",
@@ -218,7 +238,13 @@ cc_library(
218238
deps = [
219239
":butil",
220240
],
221-
copts = COPTS,
241+
copts = COPTS + select({
242+
":unittest": [
243+
"-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
244+
"-DUNIT_TEST",
245+
],
246+
"//conditions:default": [],
247+
}),
222248
linkopts = LINKOPTS,
223249
visibility = ["//visibility:public"],
224250
)

build_in_travis_ci.sh

+16-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@ if [ -z "$CC" ]; then
1010
echo "CC must be set"
1111
exit 1
1212
fi
13+
14+
function runcmd()
15+
{
16+
eval $@
17+
[[ $? != 0 ]] && {
18+
exit 1
19+
}
20+
return 0
21+
}
22+
1323
echo "build combination: PURPOSE=$PURPOSE CXX=$CXX CC=$CC"
1424

1525
if [ "$PURPOSE" = "compile-with-bazel" ]; then
16-
bazel build -j 8 --copt -DHAVE_ZLIB=1 //...
26+
runcmd "bazel build -c opt --copt -DHAVE_ZLIB=1 //..."
27+
runcmd "bazel test -c opt --copt -DHAVE_ZLIB=1 --define=unittest=true //..."
28+
# Build with glog
29+
runcmd "bazel build -c opt --copt -DHAVE_ZLIB=1 --define=with_glog=true //..."
30+
runcmd "bazel test -c opt --copt -DHAVE_ZLIB=1 --define=with_glog=true --define=unittest=true //..."
1731
exit 0
1832
fi
19-
33+
2034
# The default env in travis-ci is Ubuntu.
2135
if ! sh config_brpc.sh --headers=/usr/include --libs=/usr/lib --nodebugsymbols --cxx=$CXX --cc=$CC; then
2236
echo "Fail to configure brpc"

example/BUILD

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
COPTS = [
2+
"-D__STDC_FORMAT_MACROS",
3+
"-DBTHREAD_USE_FAST_PTHREAD_MUTEX",
4+
"-D__const__=",
5+
"-D_GNU_SOURCE",
6+
"-DUSE_SYMBOLIZE",
7+
"-DNO_TCMALLOC",
8+
"-D__STDC_LIMIT_MACROS",
9+
"-D__STDC_CONSTANT_MACROS",
10+
"-fPIC",
11+
"-Wno-unused-parameter",
12+
"-fno-omit-frame-pointer",
13+
"-DGFLAGS_NS=google",
14+
] + select({
15+
"//:with_glog": ["-DBRPC_WITH_GLOG=1"],
16+
"//conditions:default": ["-DBRPC_WITH_GLOG=0"],
17+
})
18+
19+
proto_library(
20+
name = "echo_c++_proto",
21+
srcs = [
22+
"echo_c++/echo.proto",
23+
],
24+
)
25+
26+
cc_proto_library(
27+
name = "cc_echo_c++_proto",
28+
deps = [
29+
":echo_c++_proto",
30+
],
31+
)
32+
33+
cc_binary(
34+
name = "echo_c++_server",
35+
srcs = [
36+
"echo_c++/server.cpp",
37+
],
38+
includes = [
39+
"echo_c++",
40+
],
41+
deps = [
42+
":cc_echo_c++_proto",
43+
"//:brpc",
44+
],
45+
copts = COPTS,
46+
)
47+
48+
cc_binary(
49+
name = "echo_c++_client",
50+
srcs = [
51+
"echo_c++/client.cpp",
52+
],
53+
includes = [
54+
"echo_c++",
55+
],
56+
deps = [
57+
":cc_echo_c++_proto",
58+
"//:brpc",
59+
],
60+
copts = COPTS,
61+
)

0 commit comments

Comments
 (0)