Skip to content

Add Bazel build support - #37

Open
BYVoid wants to merge 1 commit into
IlyaGrebnov:masterfrom
BYVoid:bazel-build
Open

Add Bazel build support#37
BYVoid wants to merge 1 commit into
IlyaGrebnov:masterfrom
BYVoid:bazel-build

Conversation

@BYVoid

@BYVoid BYVoid commented Aug 27, 2026

Copy link
Copy Markdown

For context: I maintain the libsais entry in the Bazel Central Registry, where versions 2.10.4 and 2.10.4.bcr.1 are already published. The registry currently carries the build files below as an overlay on top of your release tarball, and this PR offers them upstream so that they live next to the sources they build, and so the registry entry can eventually just point at your repository.

This adds an optional Bazel build alongside the existing CMake one, so that libsais can be consumed directly by Bazel projects. Nothing in src/ or include/ is touched, and CMake users are unaffected.

What is added

  • MODULE.bazel — declares the module as libsais version 2.10.4 (would need bumping together with VERSION and CMakeLists.txt on a release).
  • BUILD.bazel — one cc_library per source file: libsais, libsais16, libsais64 and libsais16x64, each exporting only its own header. The 64-bit targets depend on their 32-bit counterparts, since they delegate to them for inputs that fit into int32_t. This is finer-grained than the single CMake libsais target, so a consumer that only needs, say, libsais64 does not link the 16-bit code.
  • test/*.c — a small smoke test per variant, checking the suffix array of "banana". The repository has no test suite today, so these are only meant to make the build verifiable (bazel test //...); happy to drop them if you would rather not have a test/ directory.
  • .gitignore — ignores the bazel-* convenience symlinks and MODULE.bazel.lock.
  • README.md — a short usage section next to the CPM one.

OpenMP

The openmp build flag mirrors the LIBSAIS_USE_OPENMP CMake option and is off by default in the same way:

bazel build <your target> --@libsais//:openmp

It adds -fopenmp (/openmp on MSVC and clang-cl) to the compile and link commands, and defines LIBSAIS_OPENMP. As in CMakeLists.txt, where that definition is PUBLIC, the Bazel defines attribute propagates it to dependents, which matters because the *_omp declarations in the public headers are guarded by it. When the flag is set, the tests additionally run the *_omp entry points with two threads.

LIBSAIS_BUILD_SHARED_LIB has no counterpart here: Bazel decides between static and shared linking at the consuming binary, so a per-library option is not needed.

Testing

bazel test //... passes locally on macOS (Apple clang, Bazel 9.2.0). These exact files are also exercised by the registry's CI, which built and tested them with Bazel 7.x, 8.x, 9.x and rolling on Debian 11, Ubuntu 22.04, macOS and Windows, and additionally with OpenMP enabled on the three platforms whose toolchain supports it — Apple clang ships without OpenMP support. All 28 jobs passed.

I am happy to adjust the layout, drop the tests or the README change, or close this if a second build system is not something you want to maintain.

fmeum pushed a commit to bazelbuild/bazel-central-registry that referenced this pull request Aug 27, 2026
Adds an OpenMP option to the libsais overlay, which 2.10.4 was missing.

In 2.10.4 the overlay had no counterpart to the `LIBSAIS_USE_OPENMP`
option of the upstream CMake build, so enabling OpenMP required
consumers to pass global `--copt`/`--linkopt` flags, which apply to
every dependency in the build. This version adds the flag to the overlay
instead:

```sh
bazel build <your target> --@libsais//:openmp
```

It adds `-fopenmp` (`/openmp` on MSVC and clang-cl) to the compile and
link commands and defines `LIBSAIS_OPENMP`. Upstream declares that
definition as `PUBLIC` in CMake, so the overlay uses the propagating
`defines` attribute: the `*_omp` declarations in the public headers are
guarded by it, and dependents need to see them. It is off by default, as
upstream is.

The tests now also run the `*_omp` entry point with two threads when the
flag is set, and a `verify_targets_openmp` task builds and tests
everything with `--@libsais//:openmp`. That task skips macOS, since
Apple clang ships without OpenMP support. libsais only uses `barrier`,
`master`, `parallel num_threads` and `parallel for schedule(static)`,
all of which are within the OpenMP 2.0 subset that MSVC implements, so
Windows is covered.

Apart from that, the archive and the four targets are unchanged from
2.10.4. The new `bazel_skylib` dependency is used for the `bool_flag`
and the `config_setting_group`s that combine the flag with the compiler.

Verified with `bazel test @libsais//...` against a local registry, and
with `bazel cquery --output=build` with and without the flag, to confirm
the flags and the define are applied only when it is set. The OpenMP
configuration itself cannot be built on this machine, so the
`verify_targets_openmp` task is the first real check of it; the tests'
OpenMP branch was at least compiled here with `-DLIBSAIS_OPENMP` against
the upstream headers.

The same build files are proposed upstream in IlyaGrebnov/libsais#37; if
that lands, the overlay can be dropped in a future version.

@bazel-io skip_check unstable_url
@BYVoid
BYVoid marked this pull request as ready for review August 27, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant