Skip to content

Commit c8601dc

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

22 files changed

Lines changed: 885 additions & 265 deletions

.bazelrc

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,29 @@
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

32+
build --verbose_failures
33+
# Keep SHT_SYMTAB in built binaries so google::Symbolize can resolve
34+
# in-binary functions (e.g. TestBody() in test binaries) by name
35+
# instead of falling back to "<unknown>". Bazel's default
36+
# `--strip=sometimes` strips debug/symbol sections in fastbuild mode,
37+
# which is what `bazel test` uses unless `-c dbg` is given.
38+
build --strip=never
2239
build --cxxopt="-std=c++17"
2340
build --copt="-fno-omit-frame-pointer"
2441
# Use gnu17 for asm keyword.
@@ -33,12 +50,38 @@ build --features=per_object_debug_info
3350
# We already have absl in the build, define absl=1 to tell googletest to use absl for backtrace.
3451
build --define absl=1
3552

36-
# For brpc.
37-
test --define=BRPC_BUILD_FOR_UNITTEST=true
53+
# For UT.
54+
build:test --define BRPC_BUILD_FOR_UNITTEST=true
55+
# Hide libunwind's `_Unwind_*` symbols so they don't preempt libgcc_s at
56+
# runtime. Without this, pthread_exit / C++ exception unwinding crashes
57+
# when libunwind.so appears earlier in the dynamic link chain.
58+
# See registry/modules/1.8.3/overlay/BUILD.bazel for details.
59+
build:test --define with_bthread_tracer=false
60+
build:test --define libunwind_hide_unwind_symbols=true
61+
62+
test --config=test
3863
test --test_output=streamed
3964

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

.github/workflows/ci-linux.yml

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,39 @@ 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 -- //:brpc
111+
- run: bazel test //test/...
111112

112113
gcc-compile-with-boringssl:
113114
runs-on: ubuntu-22.04
114115
steps:
115116
- uses: actions/checkout@v2
116-
- run: bazel build --verbose_failures --define with_mesalink=false --define with_glog=true --define with_thrift=true --define BRPC_WITH_BORINGSSL=true -- //... -//example/...
117+
- run: |
118+
bazel build --define with_mesalink=false \
119+
--define with_glog=true \
120+
--define with_thrift=true \
121+
--define BRPC_WITH_BORINGSSL=true \
122+
-- //:brpc
117123
118124
gcc-compile-with-bazel-all-options:
119125
runs-on: ubuntu-22.04
120126
steps:
121127
- uses: actions/checkout@v2
122128
- run: |
123-
bazel build --verbose_failures \
124-
--define with_mesalink=false \
129+
bazel build --define with_mesalink=false \
125130
--define with_glog=true \
126131
--define with_thrift=true \
127132
--define with_debug_bthread_sche_safety=true \
128133
--define with_debug_lock=true \
129134
--define with_asan=true \
130135
--define with_bthread_tracer=true \
131136
--define BRPC_WITH_NO_PTHREAD_MUTEX_HOOK=true \
132-
-- //... -//example/...
137+
--define with_babylon_counter=true \
138+
-- //:brpc
133139
134140
clang-compile-with-make-protobuf:
135141
runs-on: ubuntu-22.04
@@ -161,25 +167,34 @@ jobs:
161167
protobuf-install-dir: /protobuf-3.21.12
162168
config-brpc-options: --cc=clang --cxx=clang++ --werror
163169

164-
clang-compile-with-bazel:
170+
clang-compile-and-test-with-bazel:
165171
runs-on: ubuntu-22.04
166172
steps:
167173
- uses: actions/checkout@v2
168-
- run: bazel build --verbose_failures --action_env=CC=clang -- //... -//example/...
174+
- run: bazel build --action_env=CC=clang -- //:brpc
175+
- run: |
176+
bazel test --test_output=streamed \
177+
--action_env=CC=clang \
178+
//test/...
169179
170180
clang-compile-with-boringssl:
171181
runs-on: ubuntu-22.04
172182
steps:
173183
- uses: actions/checkout@v2
174-
- run: bazel build --verbose_failures --action_env=CC=clang --define with_mesalink=false --define with_glog=true --define with_thrift=true --define BRPC_WITH_BORINGSSL=true -- //... -//example/...
184+
- run: |
185+
bazel build --action_env=CC=clang \
186+
--define with_mesalink=false \
187+
--define with_glog=true \
188+
--define with_thrift=true \
189+
--define BRPC_WITH_BORINGSSL=true \
190+
-- //:brpc
175191
176192
clang-compile-with-bazel-all-options:
177193
runs-on: ubuntu-22.04
178194
steps:
179195
- uses: actions/checkout@v2
180196
- run: |
181-
bazel build --verbose_failures \
182-
--action_env=CC=clang \
197+
bazel build --action_env=CC=clang \
183198
--define with_mesalink=false \
184199
--define with_glog=true \
185200
--define with_thrift=true \
@@ -188,7 +203,8 @@ jobs:
188203
--define with_asan=true \
189204
--define with_bthread_tracer=true \
190205
--define BRPC_WITH_NO_PTHREAD_MUTEX_HOOK=true \
191-
-- //... -//example/...
206+
--define with_babylon_counter=true \
207+
-- //:brpc
192208
193209
clang-unittest:
194210
runs-on: ubuntu-22.04
@@ -226,11 +242,31 @@ jobs:
226242
cd test
227243
sh ./run_tests.sh
228244
229-
bazel-bvar-unittest:
245+
246+
clang-bazel-unittest-bvar-babylon:
230247
runs-on: ubuntu-22.04
231248
steps:
232249
- uses: actions/checkout@v2
233-
- run: bazel test --verbose_failures //test:bvar_test
234-
- 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
236-
- run: bazel test --verbose_failures --action_env=CC=clang --define with_babylon_counter=true //test:bvar_test
250+
- run: |
251+
bazel test --action_env=CC=clang \
252+
--define with_babylon_counter=true \
253+
//test:bvar_unittests
254+
255+
clang-bazel-unittest-new-pb:
256+
runs-on: ubuntu-22.04
257+
env:
258+
TEST_PROTOBUF_VERSION: "34.1"
259+
# protobuf >= 34.x uses new ProtoInfo fields (option_deps,
260+
# extension_declarations) introduced in Bazel 8.x. The repo's
261+
# .bazelversion (7.2.1) is too old. bazelisk honors USE_BAZEL_VERSION.
262+
USE_BAZEL_VERSION: "8.3.1"
263+
steps:
264+
- uses: actions/checkout@v2
265+
- name: Override protobuf version for testing
266+
run: |
267+
sed -i -E "s/(bazel_dep\(name = ['\"]protobuf['\"], version = ['\"])[^'\"]+/\1${TEST_PROTOBUF_VERSION}/" MODULE.bazel
268+
echo "After override:"
269+
grep -E "bazel_dep\(name = ['\"]protobuf['\"]" MODULE.bazel
270+
grep -qE "bazel_dep\(name = ['\"]protobuf['\"], version = ['\"]${TEST_PROTOBUF_VERSION}['\"]" MODULE.bazel \
271+
|| { echo "ERROR: failed to override protobuf version in MODULE.bazel to ${TEST_PROTOBUF_VERSION}"; exit 1; }
272+
- run: bazel test --action_env=CC=clang //test:brpc_unittests

0 commit comments

Comments
 (0)