Skip to content

Commit c710610

Browse files
committed
Add Bazel build support
1 parent 8e2fb19 commit c710610

File tree

13 files changed

+269
-26
lines changed

13 files changed

+269
-26
lines changed

.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build --copt='-std=c++17' --linkopt='-L/usr/local/lib'
2+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: '[Kafka API] Bazel Build'
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
env:
10+
KAFKA_SRC_LINK: https://archive.apache.org/dist/kafka/2.8.1/kafka_2.13-2.8.1.tgz
11+
CPU_CORE_NUM: 2
12+
LIBRDKAFKA_VERSION: 1.7.0
13+
14+
jobs:
15+
kafka-api-bazel-build:
16+
runs-on: ${{ matrix.os }}
17+
18+
env:
19+
OS_VERSION: ${{ matrix.os }}
20+
TEST_LABELS: ${{ matrix.test-labels }}
21+
22+
strategy:
23+
matrix:
24+
include:
25+
- os: ubuntu-20.04
26+
test-labels: unit,integration
27+
28+
steps:
29+
- uses: actions/checkout@v2
30+
31+
- name: Update the List for Available Packages
32+
if: contains(matrix.os, 'ubuntu')
33+
run: |
34+
sed -e 's/azure.archive.ubuntu.com/us.archive.ubuntu.com/g' -e t -e d /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/nonazure.list
35+
sudo apt-get update
36+
37+
- name: Install Dependencies
38+
run: |
39+
# 1. Install librdkafka
40+
wget -nv https://github.com/edenhill/librdkafka/archive/v${LIBRDKAFKA_VERSION}.tar.gz
41+
tar -xzf v${LIBRDKAFKA_VERSION}.tar.gz
42+
cd librdkafka-${LIBRDKAFKA_VERSION}
43+
./configure
44+
make -j${CPU_CORE_NUM} && sudo make install
45+
cd ../
46+
47+
# 2. Install boost lib
48+
if [[ ${OS_VERSION} == 'ubuntu'* ]]; then
49+
sudo apt install -y libboost-all-dev
50+
elif [[ ${OS_VERSION} == 'macos'* ]]; then
51+
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
52+
brew install boost
53+
fi
54+
55+
- name: Build
56+
run: |
57+
bazel build //...
58+
59+
- name: Test
60+
if: matrix.test-labels
61+
timeout-minutes: 15
62+
run: |
63+
# Setup Kafka cluster for integration test & regression test
64+
if [[ ${TEST_LABELS} == *'integration'* ]]; then
65+
# Install kafka
66+
time wget -nv ${KAFKA_SRC_LINK}
67+
tar -xzf `basename ${KAFKA_SRC_LINK}`
68+
export PATH=`pwd`/`basename ${KAFKA_SRC_LINK} .tgz`/bin:$PATH
69+
70+
# Start Kafka cluster
71+
rm -f test.env
72+
scripts/start-local-kafka-cluster.py --zookeeper-port 52181 --broker-ports 50091 50092 50093 --temp-dir ./tmp &
73+
74+
# Wait server to be ready
75+
for i in {1..60}; do cat test.env 2>/dev/null && break || sleep 1; done
76+
77+
# Set the environment variables: KAFKA_BROKER_LIST, KAFKA_BROKER_PIDS
78+
source test.env
79+
fi
80+
81+
# Run tests
82+
bazel test //... --test_env=KAFKA_BROKER_LIST=$KAFKA_BROKER_LIST --test_tag_filters="${TEST_LABELS}" --test_verbose_timeout_warnings --test_output=all
83+
84+
# Stop Kafka cluster for integration test & regression test
85+
if [[ ${TEST_LABELS} == *'integration'* ]]; then
86+
# Stop kafka cluster
87+
kafka-server-stop.sh
88+
zookeeper-server-stop.sh
89+
fi
90+

.github/workflows/kafka_api_ci_tests.yml

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ on:
44
pull_request:
55
push:
66
branches:
7-
- master
7+
- main
88

99
env:
1010
KAFKA_SRC_LINK: https://archive.apache.org/dist/kafka/2.8.1/kafka_2.13-2.8.1.tgz
1111
CPU_CORE_NUM: 2
1212
LIBRDKAFKA_VERSION: 1.7.0
13-
BUILD_SUB_DIR: build/sub-build
13+
BUILD_SUB_DIR: builds/sub-build
1414

1515
jobs:
1616
kafka-api-tests:
@@ -32,43 +32,43 @@ jobs:
3232
include:
3333
- os: macos-10.15
3434
build-cxx: clang++
35-
test-labels: UT|IT
35+
test-labels: unit|integration
3636

3737
- os: ubuntu-20.04
3838
build-cxx: g++
3939
build-type: Debug
40-
test-labels: UT|IT
40+
test-labels: unit|integration
4141
enable-ut-stubs: true
4242

4343
- os: ubuntu-20.04
4444
build-cxx: g++
4545
build-type: Release
46-
test-labels: RT
46+
test-labels: robustness
4747

4848
- os: ubuntu-20.04
4949
build-cxx: g++
5050
build-type: Release
5151
cxx-standard: 14
52-
test-labels: UT|IT
52+
test-labels: unit|integration
5353

5454
- os: ubuntu-20.04
5555
build-cxx: g++
5656
check-option: asan
57-
test-labels: UT|IT
57+
test-labels: unit|integration
5858

5959
- os: ubuntu-20.04
6060
build-cxx: g++
6161
check-option: tsan
62-
test-labels: UT|IT
62+
test-labels: unit|integration
6363

6464
- os: ubuntu-20.04
6565
build-cxx: g++
6666
check-option: ubsan
67-
test-labels: UT|IT
67+
test-labels: unit|integration
6868

6969
- os: ubuntu-20.04
7070
build-cxx: clang++
71-
test-labels: UT|IT
71+
test-labels: unit|integration
7272
generate-doc: true
7373
with-installation: true
7474

@@ -79,11 +79,11 @@ jobs:
7979

8080
- os: ubuntu-18.04
8181
build-cxx: g++
82-
test-labels: UT|IT
82+
test-labels: unit|integration
8383

8484
- os: ubuntu-18.04
8585
build-cxx: clang++
86-
test-labels: RT
86+
test-labels: robustness
8787

8888
steps:
8989
- uses: actions/checkout@v2
@@ -96,7 +96,7 @@ jobs:
9696
9797
- name: Install Dependencies
9898
run: |
99-
mkdir -p $BUILD_SUB_DIR
99+
mkdir -p ${BUILD_SUB_DIR}
100100
cd ${BUILD_SUB_DIR}
101101
102102
# 1. Install clang/clang-tidy
@@ -110,10 +110,10 @@ jobs:
110110
sudo apt install -y clang-tidy
111111
fi
112112
113-
# 2. Install googletest (v1.10.0)
114-
wget -nv https://github.com/google/googletest/archive/release-1.10.0.tar.gz
115-
tar -xzf release-1.10.0.tar.gz
116-
cd googletest-release-1.10.0
113+
# 2. Install googletest (v1.11.0)
114+
wget -nv https://github.com/google/googletest/archive/refs/tags/release-1.11.0.tar.gz
115+
tar -xzf release-1.11.0.tar.gz
116+
cd googletest-release-1.11.0
117117
env CXX=${BUILD_CXX} cmake ./
118118
make -j${CPU_CORE_NUM} && sudo make install
119119
cd ../
@@ -211,7 +211,7 @@ jobs:
211211
cd ${BUILD_SUB_DIR}
212212
213213
# Setup Kafka cluster for integration test & regression test
214-
if [[ ${TEST_LABELS} == *'IT'* ]] || [[ ${TEST_LABELS} == *'RT'* ]]; then
214+
if [[ ${TEST_LABELS} == *'integration'* ]] || [[ ${TEST_LABELS} == *'robustness'* ]]; then
215215
# Install kafka
216216
time wget -nv ${KAFKA_SRC_LINK}
217217
tar -xzf `basename ${KAFKA_SRC_LINK}`
@@ -229,7 +229,7 @@ jobs:
229229
fi
230230
231231
# Install gtest-parallel
232-
if [[ ${TEST_LABELS} == *'IT'* ]]; then
232+
if [[ ${TEST_LABELS} == *'integration'* ]]; then
233233
wget https://github.com/google/gtest-parallel/archive/refs/heads/master.zip
234234
unzip master.zip
235235
export PATH=`pwd`/gtest-parallel-master:$PATH
@@ -239,7 +239,7 @@ jobs:
239239
ctest -VV -L "${TEST_LABELS}"
240240
241241
# Stop Kafka cluster for integration test & regression test
242-
if [[ ${TEST_LABELS} == *'IT'* ]] || [[ ${TEST_LABELS} == *'RT'* ]]; then
242+
if [[ ${TEST_LABELS} == *'integration'* ]] || [[ ${TEST_LABELS} == *'robustness'* ]]; then
243243
# Stop kafka cluster
244244
kafka-server-stop.sh
245245
zookeeper-server-stop.sh
@@ -249,7 +249,7 @@ jobs:
249249
runs-on: windows-latest
250250

251251
env:
252-
TEST_LABELS: ${{ matrix.test-labels }}
252+
TEST_LABELS: ${{ matrix.test-labels }}
253253

254254
strategy:
255255
matrix:
@@ -278,8 +278,6 @@ jobs:
278278
vcpkg install boost-program-options
279279
280280
cp -v "C:\VCPKG\INSTALLED\x86-windows\lib\boost_program_options-vc140-mt.lib" "C:\VCPKG\INSTALLED\x86-windows\lib\boost_program_options.lib"
281-
cp -v "C:\VCPKG\INSTALLED\x86-windows\bin\boost_program_options-vc142-mt-x32-1_77.dll" "C:\VCPKG\INSTALLED\x86-windows\bin\boost_program_options.dll"
282-
cp -v "C:\VCPKG\INSTALLED\x86-windows\bin\boost_program_options-vc142-mt-x32-1_77.pdb" "C:\VCPKG\INSTALLED\x86-windows\bin\boost_program_options.pdb"
283281
284282
# Install rapidjson
285283
vcpkg install rapidjson

BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cc_library(
2+
name = "modern-cpp-kafka-api",
3+
4+
hdrs = glob(["include/kafka/*.h", "include/kafka/addons/*.h"]),
5+
6+
includes = ["include"],
7+
8+
linkopts = ["-lpthread", "-lrdkafka"],
9+
10+
visibility = ["//visibility:public"],
11+
)
12+

WORKSPACE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
3+
http_archive(
4+
name = "gtest",
5+
urls = ["https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip"],
6+
strip_prefix = "googletest-release-1.11.0",
7+
)
8+
9+
http_archive(
10+
name = "rapidjson",
11+
build_file = "//customrules:rapidjson.BUILD",
12+
urls = ["https://github.com/Tencent/rapidjson/archive/refs/heads/master.zip"],
13+
strip_prefix="rapidjson-master",
14+
)
15+

customrules/BUILD

Whitespace-only changes.

customrules/rapidjson.BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cc_library(
2+
name = "rapidjson",
3+
4+
hdrs = glob(["include/rapidjson/**"]),
5+
6+
includes = ["include"],
7+
8+
visibility = ["//visibility:public"],
9+
)
10+

examples/BUILD

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
cc_binary(
2+
name = "kafka_sync_producer",
3+
4+
srcs = ["kafka_sync_producer.cc"],
5+
6+
deps = ["//:modern-cpp-kafka-api"],
7+
)
8+
9+
cc_binary(
10+
name = "kafka_async_producer_copy_payload",
11+
12+
srcs = ["kafka_async_producer_copy_payload.cc"],
13+
14+
deps = ["//:modern-cpp-kafka-api"],
15+
)
16+
17+
cc_binary(
18+
name = "kafka_async_producer_not_copy_payload",
19+
20+
srcs = ["kafka_async_producer_not_copy_payload.cc"],
21+
22+
deps = ["//:modern-cpp-kafka-api"],
23+
)
24+
25+
cc_binary(
26+
name = "kafka_auto_commit_consumer",
27+
28+
srcs = ["kafka_auto_commit_consumer.cc"],
29+
30+
deps = ["//:modern-cpp-kafka-api"],
31+
)
32+
33+
cc_binary(
34+
name = "kafka_manual_commit_consumer",
35+
36+
srcs = ["kafka_manual_commit_consumer.cc"],
37+
38+
deps = ["//:modern-cpp-kafka-api"],
39+
)
40+

tests/BUILD

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
cc_test(
2+
name = "kafka-unit-test",
3+
4+
srcs = glob(["unit/*.cc", "utils/*.h"]),
5+
6+
deps = ["//:modern-cpp-kafka-api",
7+
"@gtest//:gtest",
8+
"@gtest//:gtest_main",
9+
"@rapidjson//:rapidjson"],
10+
11+
timeout = "short",
12+
13+
tags = ["unit"],
14+
)
15+
16+
17+
cc_test(
18+
name = "kafka-integration-test",
19+
20+
srcs = glob(["integration/*.cc", "utils/*.h"]),
21+
22+
deps = ["//:modern-cpp-kafka-api",
23+
"@gtest//:gtest",
24+
"@gtest//:gtest_main"],
25+
26+
timeout = "long",
27+
shard_count = 8,
28+
29+
tags = ["integration"],
30+
)
31+
32+
cc_test(
33+
name = "kafka-robustness-test",
34+
35+
srcs = glob(["robustness/*.cc", "utils/*.h"]),
36+
37+
deps = ["//:modern-cpp-kafka-api",
38+
"@gtest//:gtest",
39+
"@gtest//:gtest_main"],
40+
41+
timeout = "long",
42+
shard_count = 1,
43+
44+
tags = ["robustness"],
45+
)
46+

tests/integration/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ target_link_libraries("${PROJECT_NAME}" modern-cpp-kafka-api gtest gtest_main)
88

99
add_test(NAME ${PROJECT_NAME}
1010
COMMAND gtest-parallel ./${PROJECT_NAME})
11-
set_tests_properties(${PROJECT_NAME} PROPERTIES LABELS "IT")
11+
set_tests_properties(${PROJECT_NAME} PROPERTIES LABELS "integration")
1212

1313
if (BUILD_OPTION_USE_ASAN OR BUILD_OPTION_USE_TSAN)
1414
target_compile_options(${PROJECT_NAME} PRIVATE "-fno-sanitize=all")

0 commit comments

Comments
 (0)