Bazel BUILD rules for popular C/C++ open-source libraries that don't natively support Bazel.
Each library ships with a .bzl workspace rule, a custom .BUILD file, and a smoke test under test/ to verify correctness.
| Library | Version | Type |
|---|---|---|
| LevelDB | v1.20 | Key-value store |
| spdlog | v1.13.0 | Logging (header-only) |
| LZ4 | v1.9.4 | Compression |
| GoogleTest | v1.14.0 | Testing framework |
In your WORKSPACE file:
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "bazel_deps",
branch = "main",
remote = "https://github.com/dddw1216/bazel_deps.git",
)# Pick what you need
load("@bazel_deps//bazel_deps/spdlog:spdlog.bzl", "spdlog_workspace")
spdlog_workspace()
load("@bazel_deps//bazel_deps/leveldb:leveldb.bzl", "leveldb_workspace")
leveldb_workspace()
load("@bazel_deps//bazel_deps/lz4:lz4.bzl", "lz4_workspace")
lz4_workspace()
load("@bazel_deps//bazel_deps/googletest:googletest.bzl", "googletest_workspace")
googletest_workspace()cc_binary(
name = "my_app",
srcs = ["main.cc"],
deps = [
"@spdlog//:spdlog",
"@com_github_google_leveldb//:leveldb",
"@lz4//:lz4",
],
)
cc_test(
name = "my_test",
srcs = ["my_test.cc"],
deps = ["@com_google_googletest//:gtest_main"],
)A Docker container named dev (standard Ubuntu with Bazel) is used for building and testing. The host ~/project directory is mounted inside the container.
# Enter the dev container
docker exec -it -w /Users/mengdandan/project/bazel_deps dev bash
# Build all active targets
bazel build ...
# Build with debug symbols
bash build.sh debug
# Run a specific test
bazel run //test/lz4:lz4_test
# Format BUILD files
bash buildifier.sh
# Clean everything
bash clean.shbazel_deps/
├── WORKSPACE # Dependency registry
├── .bazelrc # Build config (C++17, platform settings)
├── bazel_deps/
│ └── <library>/
│ ├── <library>.bzl # Download & workspace rule
│ ├── <library>.BUILD # Bazel build rules
│ └── BUILD # Package marker
└── test/
└── <library>/
├── BUILD # Test target
└── <test>.cc # Smoke test source
- Create
bazel_deps/<name>/with a.bzlfile (download config) and a.BUILDfile (compile rules) - Add an empty
BUILDfile in the same directory - Load and call the workspace rule in
WORKSPACE - Write a smoke test under
test/<name>/ - Build and verify in the Docker container
Apache License 2.0