From 59990161d01ad6fa7d18a8650c25b04f1bfd39ef Mon Sep 17 00:00:00 2001 From: karlmoriaty <3070687583@qq.com> Date: Fri, 13 Nov 2026 13:57:08 +0800 Subject: [PATCH] Add Bazel build files Add a Bazel build for the C library, maintained like the existing CMake and Meson builds under build/. MODULE.bazel stays at the repository root because Bazel requires it there; the library target lives in lib/BUILD.bazel next to the existing BUCK and Makefile. A workflow builds //lib:zstd on every change so the build description does not bit-rot. --- .github/workflows/bazel.yml | 33 ++++++++++++++++++++++++++ .gitignore | 2 ++ MODULE.bazel | 5 ++++ build/bazel/README.md | 46 +++++++++++++++++++++++++++++++++++++ lib/BUILD.bazel | 31 +++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 .github/workflows/bazel.yml create mode 100644 MODULE.bazel create mode 100644 build/bazel/README.md create mode 100644 lib/BUILD.bazel diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml new file mode 100644 index 00000000000..09d095dbcd8 --- /dev/null +++ b/.github/workflows/bazel.yml @@ -0,0 +1,33 @@ +name: bazel + +permissions: + contents: read + +on: + pull_request: + branches: + - dev + push: + branches: + - dev + +concurrency: + group: bazel-ref + cancel-in-progress: true + +jobs: + build: + name: bazel build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Bazel + uses: bazel-contrib/setup-bazel@0.14.0 + with: + bazelisk-cache: true + disk-cache: enabled + repository-cache: true + + - name: Build libzstd + run: bazel build //lib:zstd diff --git a/.gitignore b/.gitignore index 6868bc7245d..d63e066575a 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,8 @@ projects/ bin/ .buckd/ buck-out/ +bazel-* +MODULE.bazel.lock build-* *.gcda cmakebuild/ diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 00000000000..3ad8064f891 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,5 @@ +module( + name = "zstd", + version = "1.6.0", +) +bazel_dep(name = "platforms", version = "0.0.11") \ No newline at end of file diff --git a/build/bazel/README.md b/build/bazel/README.md new file mode 100644 index 00000000000..39b90a858d1 --- /dev/null +++ b/build/bazel/README.md @@ -0,0 +1,46 @@ +# Bazel build + +This directory documents the Bazel build for zstd. Like the CMake and Meson +builds under `build/`, it is community maintained. + +## Building from the repository root + +```sh +bazel build //lib:zstd +``` + +This produces a static library (`libzstd.a` on Linux/macOS, `zstd.lib` on +Windows). + +## Layout + +- `MODULE.bazel` must stay at the repository root, because Bazel treats the + root as the workspace and pre-1.0 modules do not accept it anywhere else. + It is the only file Bazel requires at the root. +- `lib/BUILD.bazel` defines the `zstd` target next to the existing + `lib/BUCK`, `Makefile` and `libzstd.mk`. +- `.github/workflows/bazel.yml` builds `//lib:zstd` on every change to keep + the build description from bit-rotting. + +## Configuration + +`ZSTD_LEGACY_SUPPORT` is disabled and multithreading is off by default, +matching the static library built by `make lib`. The MSVC C11 flag is applied +through `select()` in `lib/BUILD.bazel`, so no `.bazelrc` is needed. + +## Using zstd from another Bazel workspace + +With Bzlmod, depend on the module and use the `zstd` target: + +```python +bazel_dep(name = "zstd", version = "1.6.0") +``` + +When zstd is vendored with `local_path_override` or `archive_override`, the +same `//lib:zstd` target is available. + +## Adding languages or features + +The target currently builds the C library only. The command line tools in +`programs/` are not wired into Bazel yet; a follow-up can add them as a +`cc_binary` if there is demand. \ No newline at end of file diff --git a/lib/BUILD.bazel b/lib/BUILD.bazel new file mode 100644 index 00000000000..46ea44e0dc8 --- /dev/null +++ b/lib/BUILD.bazel @@ -0,0 +1,31 @@ +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) + +cc_library( + name = "zstd", + srcs = glob([ + "common/*.c", + "compress/*.c", + "decompress/*.c", + "deprecated/*.c", + "dictBuilder/*.c", + ]), + hdrs = glob([ + "*.h", + "common/*.h", + "compress/*.h", + "decompress/*.h", + "deprecated/*.h", + "dictBuilder/*.h", + ]), + copts = select({ + "@platforms//os:windows": ["/std:c11"], + "//conditions:default": [], + }), + includes = ["."], + local_defines = [ + "XXH_NAMESPACE=ZSTD_", + "ZSTD_LEGACY_SUPPORT=0", + ], +) \ No newline at end of file