Skip to content

Commit 0f5da4b

Browse files
committed
Refactor Bazel build with custom proto rules and build all test targets
1 parent f97f23e commit 0f5da4b

18 files changed

Lines changed: 787 additions & 235 deletions

.bazelrc

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
#
16+
# Bazel doesn't need more than 200MB of memory for local build based on memory profiling:
17+
# https://docs.bazel.build/versions/master/skylark/performance.html#memory-profiling
18+
# The default JVM max heapsize is 1/4 of physical memory up to 32GB which could be large
19+
# enough to consume all memory constrained by cgroup in large host.
20+
# Limiting JVM heapsize here to let it do GC more when approaching the limit to
21+
# leave room for compiler/linker.
22+
# The number 3G is chosen heuristically to both support large VM and small VM with RBE.
23+
# Startup options cannot be selected via config.
24+
startup --host_jvm_args=-Xmx3g
25+
startup --host_jvm_args="-DBAZEL_TRACK_SOURCE_DIRECTORIES=1"
26+
1727
# Default build options. These are applied first and unconditionally.
18-
#
1928
common --registry=https://bcr.bazel.build
2029
common --registry=https://baidu.github.io/babylon/registry
30+
common --registry=https://raw.githubusercontent.com/apache/brpc/master/registry
2131

2232
build --cxxopt="-std=c++17"
2333
build --copt="-fno-omit-frame-pointer"
@@ -33,12 +43,39 @@ build --features=per_object_debug_info
3343
# We already have absl in the build, define absl=1 to tell googletest to use absl for backtrace.
3444
build --define absl=1
3545

36-
# For brpc.
37-
test --define=BRPC_BUILD_FOR_UNITTEST=true
46+
# For UT.
47+
build:test --define BRPC_BUILD_FOR_UNITTEST=true
48+
# Hide libunwind's `_Unwind_*` symbols so they don't preempt libgcc_s at
49+
# runtime. Without this, pthread_exit / C++ exception unwinding crashes
50+
# when libunwind.so appears earlier in the dynamic link chain.
51+
# See registry/modules/1.8.3/overlay/BUILD.bazel for details.
52+
build:test --define with_bthread_tracer=false
53+
build:test --define libunwind_hide_unwind_symbols=true
54+
55+
test --config=test
3856
test --test_output=streamed
57+
test --verbose_failures
3958

4059
# Pass PATH, CC, CXX and LLVM_CONFIG variables from the environment.
4160
build --action_env=CC
4261
build --action_env=CXX
4362
build --action_env=LLVM_CONFIG
4463
build --action_env=PATH
64+
65+
# Basic ASAN
66+
build:asan --define=with_asan=true
67+
build:asan --copt -fsanitize=address
68+
build:asan --linkopt -fsanitize=address
69+
# ASAN needs -O1 to get reasonable performance.
70+
build:asan --copt -O1
71+
build:asan --copt -fno-optimize-sibling-calls
72+
73+
# macOS ASAN
74+
build:macos-asan --config=asan
75+
# Workaround, see https://github.com/bazelbuild/bazel/issues/6932
76+
build:macos-asan --copt -Wno-macro-redefined
77+
build:macos-asan --copt -D_FORTIFY_SOURCE=0
78+
# Dynamic link cause issues like: `dyld: malformed mach-o: load commands size (59272) > 32768`
79+
build:macos-asan --dynamic_mode=off
80+
81+
test:asan --test_env=ASAN_OPTIONS=detect_leaks=0:detect_stack_use_after_return=1

.github/workflows/ci-linux.yml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ jobs:
103103
protobuf-install-dir: /protobuf-3.21.12
104104
config-brpc-options: --cc=gcc --cxx=g++ --werror
105105

106-
gcc-compile-with-bazel:
106+
gcc-compile-and-test-with-bazel:
107107
runs-on: ubuntu-22.04
108108
steps:
109109
- uses: actions/checkout@v2
110-
- run: bazel build --verbose_failures -- //... -//example/...
110+
- run: bazel build --verbose_failures -- //... -//example/... -//test/...
111+
- run: bazel test //test/...
111112

112113
gcc-compile-with-boringssl:
113114
runs-on: ubuntu-22.04
@@ -129,6 +130,7 @@ jobs:
129130
--define with_asan=true \
130131
--define with_bthread_tracer=true \
131132
--define BRPC_WITH_NO_PTHREAD_MUTEX_HOOK=true \
133+
--define with_babylon_counter=true \
132134
-- //... -//example/...
133135
134136
clang-compile-with-make-protobuf:
@@ -161,11 +163,12 @@ jobs:
161163
protobuf-install-dir: /protobuf-3.21.12
162164
config-brpc-options: --cc=clang --cxx=clang++ --werror
163165

164-
clang-compile-with-bazel:
166+
clang-compile-and-test-with-bazel:
165167
runs-on: ubuntu-22.04
166168
steps:
167169
- uses: actions/checkout@v2
168-
- run: bazel build --verbose_failures --action_env=CC=clang -- //... -//example/...
170+
- run: bazel build --verbose_failures --action_env=CC=clang -- //... -//example/... -//test/...
171+
- run: bazel test --action_env=CC=clang //test/...
169172

170173
clang-compile-with-boringssl:
171174
runs-on: ubuntu-22.04
@@ -188,6 +191,7 @@ jobs:
188191
--define with_asan=true \
189192
--define with_bthread_tracer=true \
190193
--define BRPC_WITH_NO_PTHREAD_MUTEX_HOOK=true \
194+
--define with_babylon_counter=true \
191195
-- //... -//example/...
192196
193197
clang-unittest:
@@ -226,11 +230,29 @@ jobs:
226230
cd test
227231
sh ./run_tests.sh
228232
229-
bazel-bvar-unittest:
233+
234+
clang-bazel-unittest-bvar-babylon:
230235
runs-on: ubuntu-22.04
231236
steps:
232237
- uses: actions/checkout@v2
233-
- run: bazel test --verbose_failures //test:bvar_test
234238
- run: bazel test --verbose_failures --define with_babylon_counter=true //test:bvar_test
235-
- run: bazel test --verbose_failures --action_env=CC=clang //test:bvar_test
236239
- run: bazel test --verbose_failures --action_env=CC=clang --define with_babylon_counter=true //test:bvar_test
240+
241+
clang-bazel-unittest-new-pb:
242+
runs-on: ubuntu-22.04
243+
env:
244+
TEST_PROTOBUF_VERSION: "35.0"
245+
# protobuf >= 34.x uses new ProtoInfo fields (option_deps,
246+
# extension_declarations) introduced in Bazel 8.x. The repo's
247+
# .bazelversion (7.2.1) is too old. bazelisk honors USE_BAZEL_VERSION.
248+
USE_BAZEL_VERSION: "8.3.1"
249+
steps:
250+
- uses: actions/checkout@v2
251+
- name: Override protobuf version for testing
252+
run: |
253+
sed -i -E "s/(bazel_dep\(name = ['\"]protobuf['\"], version = ['\"])[^'\"]+/\1${TEST_PROTOBUF_VERSION}/" MODULE.bazel
254+
echo "After override:"
255+
grep -E "bazel_dep\(name = ['\"]protobuf['\"]" MODULE.bazel
256+
grep -qE "bazel_dep\(name = ['\"]protobuf['\"], version = ['\"]${TEST_PROTOBUF_VERSION}['\"]" MODULE.bazel \
257+
|| { echo "ERROR: failed to override protobuf version in MODULE.bazel to ${TEST_PROTOBUF_VERSION}"; exit 1; }
258+
- run: bazel test --action_env=CC=clang //test:brpc_unittests

BUILD.bazel

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,55 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
load("@rules_proto//proto:defs.bzl", "proto_library")
17-
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library", "objc_library")
16+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "objc_library")
17+
load("//bazel/tools:brpc_proto_library.bzl", "brpc_proto_library")
1818

1919
licenses(["notice"]) # Apache v2
2020

2121
exports_files(["LICENSE"])
2222

2323
COPTS = [
2424
"-fno-omit-frame-pointer",
25-
"-DBTHREAD_USE_FAST_PTHREAD_MUTEX",
26-
"-D__const__=__unused__",
27-
"-D_GNU_SOURCE",
28-
"-DUSE_SYMBOLIZE",
29-
"-DNO_TCMALLOC",
30-
"-D__STDC_FORMAT_MACROS",
31-
"-D__STDC_LIMIT_MACROS",
32-
"-D__STDC_CONSTANT_MACROS",
3325
] + select({
34-
"//bazel/config:brpc_with_glog": ["-DBRPC_WITH_GLOG=1"],
35-
"//conditions:default": ["-DBRPC_WITH_GLOG=0"],
36-
}) + select({
37-
"//bazel/config:brpc_with_mesalink": ["-DUSE_MESALINK"],
38-
"//conditions:default": [""],
39-
}) + select({
40-
"//bazel/config:brpc_with_thrift": ["-DENABLE_THRIFT_FRAMED_PROTOCOL=1"],
41-
"//conditions:default": [""],
42-
}) + select({
43-
"//bazel/config:brpc_with_thrift_legacy_version": [],
44-
"//conditions:default": ["-DTHRIFT_STDCXX=std"],
45-
}) + select({
46-
"//bazel/config:brpc_with_rdma": ["-DBRPC_WITH_RDMA=1"],
47-
"//conditions:default": [""],
48-
}) + select({
49-
"//bazel/config:brpc_with_debug_bthread_sche_safety": ["-DBRPC_DEBUG_BTHREAD_SCHE_SAFETY=1"],
50-
"//conditions:default": ["-DBRPC_DEBUG_BTHREAD_SCHE_SAFETY=0"],
51-
}) + select({
52-
"//bazel/config:brpc_with_debug_lock": ["-DBRPC_DEBUG_LOCK=1"],
53-
"//conditions:default": ["-DBRPC_DEBUG_LOCK=0"],
54-
}) + select({
5526
"//bazel/config:brpc_with_asan": ["-fsanitize=address"],
56-
"//conditions:default": [""],
57-
}) + select({
58-
"//bazel/config:brpc_with_no_pthread_mutex_hook": ["-DNO_PTHREAD_MUTEX_HOOK"],
59-
"//conditions:default": [""],
27+
"//conditions:default": [],
6028
})
6129

30+
DEFINES = [
31+
"BTHREAD_USE_FAST_PTHREAD_MUTEX",
32+
"__const__=__unused__",
33+
"_GNU_SOURCE",
34+
"USE_SYMBOLIZE",
35+
"NO_TCMALLOC",
36+
"__STDC_FORMAT_MACROS",
37+
"__STDC_LIMIT_MACROS",
38+
"__STDC_CONSTANT_MACROS",
39+
] + select({
40+
"//bazel/config:brpc_with_glog": ["BRPC_WITH_GLOG=1"],
41+
"//conditions:default": ["BRPC_WITH_GLOG=0"],
42+
}) + select({
43+
"//bazel/config:brpc_with_mesalink": ["USE_MESALINK"],
44+
"//conditions:default": [],
45+
}) + select({
46+
"//bazel/config:brpc_with_thrift": ["ENABLE_THRIFT_FRAMED_PROTOCOL=1"],
47+
"//conditions:default": [],
48+
}) + select({
49+
"//bazel/config:brpc_with_thrift_legacy_version": [],
50+
"//conditions:default": ["THRIFT_STDCXX=std"],
51+
}) + select({
52+
"//bazel/config:brpc_with_rdma": ["BRPC_WITH_RDMA=1"],
53+
"//conditions:default": [],
54+
}) + select({
55+
"//bazel/config:brpc_with_debug_bthread_sche_safety": ["BRPC_DEBUG_BTHREAD_SCHE_SAFETY=1"],
56+
"//conditions:default": ["BRPC_DEBUG_BTHREAD_SCHE_SAFETY=0"],
57+
}) + select({
58+
"//bazel/config:brpc_with_debug_lock": ["BRPC_DEBUG_LOCK=1"],
59+
"//conditions:default": ["BRPC_DEBUG_LOCK=0"],
60+
}) + select({
61+
"//bazel/config:brpc_with_no_pthread_mutex_hook": ["NO_PTHREAD_MUTEX_HOOK"],
62+
"//conditions:default": [],
63+
})
64+
6265
LINKOPTS = [
6366
"-pthread",
6467
"-ldl",
@@ -93,7 +96,7 @@ LINKOPTS = [
9396
"//conditions:default": [],
9497
}) + select({
9598
"//bazel/config:brpc_with_asan": ["-fsanitize=address"],
96-
"//conditions:default": [""],
99+
"//conditions:default": [],
97100
})
98101

99102
genrule(
@@ -331,13 +334,13 @@ cc_library(
331334
"src/butil/third_party/dmg_fp/dtoa.cc",
332335
":config_h",
333336
],
334-
copts = COPTS + select({
337+
defines = DEFINES + select({
335338
"//bazel/config:brpc_build_for_unittest": [
336-
"-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
337-
"-DUNIT_TEST",
339+
"UNIT_TEST",
338340
],
339341
"//conditions:default": [],
340342
}),
343+
copts = COPTS,
341344
includes = [
342345
"src/",
343346
],
@@ -354,8 +357,14 @@ cc_library(
354357
"@bazel_tools//src/conditions:darwin": [":macos_lib"],
355358
"//conditions:default": [],
356359
}) + select({
357-
"//bazel/config:brpc_with_boringssl": ["@boringssl//:ssl", "@boringssl//:crypto"],
358-
"//conditions:default": ["@openssl//:ssl", "@openssl//:crypto"],
360+
"//bazel/config:brpc_with_boringssl": [
361+
"@boringssl//:ssl",
362+
"@boringssl//:crypto"
363+
],
364+
"//conditions:default": [
365+
"@openssl//:ssl",
366+
"@openssl//:crypto"
367+
],
359368
}),
360369
)
361370

@@ -381,14 +390,13 @@ cc_library(
381390
defines = [] + select({
382391
"//bazel/config:with_babylon_counter": ["WITH_BABYLON_COUNTER=1"],
383392
"//conditions:default": [],
384-
}),
385-
copts = COPTS + select({
393+
}) + select({
386394
"//bazel/config:brpc_build_for_unittest": [
387-
"-DBVAR_NOT_LINK_DEFAULT_VARIABLES",
388-
"-DUNIT_TEST",
395+
"BVAR_NOT_LINK_DEFAULT_VARIABLES",
389396
],
390397
"//conditions:default": [],
391398
}),
399+
copts = COPTS,
392400
includes = [
393401
"src/",
394402
],
@@ -475,7 +483,6 @@ cc_library(
475483
deps = [
476484
":brpc_idl_options_cc_proto",
477485
":butil",
478-
"@com_google_protobuf//:protoc_lib",
479486
],
480487
)
481488

@@ -487,20 +494,11 @@ filegroup(
487494
visibility = ["//visibility:public"],
488495
)
489496

490-
proto_library(
491-
name = "brpc_idl_options_proto",
492-
srcs = [":brpc_idl_options_proto_srcs"],
493-
strip_import_prefix = "src",
494-
visibility = ["//visibility:public"],
495-
deps = [
496-
"@com_google_protobuf//:descriptor_proto",
497-
],
498-
)
499-
500-
cc_proto_library(
497+
brpc_proto_library(
501498
name = "brpc_idl_options_cc_proto",
499+
srcs = [":brpc_idl_options_proto_srcs"],
500+
include = "src",
502501
visibility = ["//visibility:public"],
503-
deps = [":brpc_idl_options_proto"],
504502
)
505503

506504
filegroup(
@@ -512,21 +510,12 @@ filegroup(
512510
visibility = ["//visibility:public"],
513511
)
514512

515-
proto_library(
516-
name = "brpc_internal_proto",
517-
srcs = [":brpc_internal_proto_srcs"],
518-
strip_import_prefix = "src",
519-
visibility = ["//visibility:public"],
520-
deps = [
521-
":brpc_idl_options_proto",
522-
"@com_google_protobuf//:descriptor_proto",
523-
],
524-
)
525-
526-
cc_proto_library(
513+
brpc_proto_library(
527514
name = "brpc_internal_cc_proto",
515+
srcs = [":brpc_internal_proto_srcs"],
516+
include = "src",
517+
deps = [":brpc_idl_options_cc_proto"],
528518
visibility = ["//visibility:public"],
529-
deps = [":brpc_internal_proto"],
530519
)
531520

532521
cc_library(
@@ -587,5 +576,6 @@ cc_binary(
587576
deps = [
588577
":brpc",
589578
":brpc_idl_options_cc_proto",
579+
"@com_google_protobuf//:protoc_lib",
590580
],
591581
)

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ if(Protobuf_VERSION GREATER 4.21)
206206
absl::hash
207207
absl::layout
208208
absl::log_initialize
209+
absl::log_globals
209210
absl::log_severity
210211
absl::memory
211212
absl::node_hash_map
212213
absl::node_hash_set
213-
absl::optional
214+
absl::random_distributions
215+
absl::random_random
214216
absl::span
215217
absl::status
216218
absl::statusor

0 commit comments

Comments
 (0)