Is there an easy way to tell bazel to use a specific compiler for building packages?
Related to
opened 04:21AM - 31 Dec 25 UTC
3.skill: sprintable
0.kind: build failure
5.scope: tracking
With https://github.com/NixOS/nixpkgs/pull/471587 merged, GCC 15 becomes the def… ault C/C++ compiler on Linux. This brings some breaking changes that may cause some packages to fail to compile:
1. GCC 15 updates the default C version from `gnu17` to `gnu23`, which brings some syntax breakages. `bool`, `true`, `false`, `nullptr`, `thread_local`, etc., are currently reserved keywords, so they can no longer be defined as variable names in the code. The parameters of function declaration must be explicit, and can no longer be empty to indicate that any parameters can be accepted.
2. Reduced the number of headers in C++ that implicitly include other standard library headers, which would result in the need to add a `<cstdint>` header in some old code.
3. Some new compilation warnings have been added, such as `-Wrestrict`, which generally will not error out, but some projects may turn on -Werror by default.
See https://gcc.gnu.org/gcc-15/porting_to.html for more detailed explanation.
We've tried to fix most of the packages that affect more in staging-next, but some leaf packages may still fail. When you try to fix these packages, you can try the following methods:
1. Check the upstream repository to see if there is already a fix. If there is, you can pull the corresponding commit with `fetchpatch`, or update to the latest version if a new version is available.
2. Search for the package in https://repology.org/projects/, and check how other distributions such as arch, debian, gentoo, and alpine patch these packages.
3. It's better if you can fix it yourself, but don't forget to submit patches upstream if possible.
4. If none of these methods are applicable, you can fix by setting `env.NIX_CFLAGS_COMPILE = "-std=gnu17"` (some projects may require gnu11 or even older) and adding. Note that in mixed C/C++ projects, `NIX_CFLAGS_COMPILE` may be incorrectly passed to clang's c++ frontend, causing it to fail on macOS (TODO: ask for proper solution). If C++ code lacks headers, you can use `sed` to add headers temporarily, for example, `sed -e '1i #include <cstdint>' -i db/blob/blob_file_meta.h`.
5. For packages that are already unmaintained upstream for a long time, one possible option is to drop them directly instead of fixing them. You can also tell by repology whether other distributions still package them.
we have bazel packages that are failing with gcc15, but that should work with its previous version.
For example, I was investigating Build failure: protoc-gen-js · Issue #475586 · NixOS/nixpkgs · GitHub .
It seemed to me that a candidate solution would be to pin the compiler to gcc14, but I have a hard time understanding this build system, and even less the nixpkgs abstraction built on top of it…
A solution from protoc-gen-js: fix build by pinning to gcc14 by gepbird · Pull Request #476867 · NixOS/nixpkgs · GitHub by @gepbird ; If there is an standard environment bundling the needed compiler, one can override the stdenv of buildBazelPackage with it:
let
buildBazelPackage' =
buildBazelPackage.override {
stdenv = gcc14Stdenv;
}
in
buildBazelPackage' rec { ... }