-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bazel fails to build in clang stdenv #216047
Comments
Addendum: the above nix-info is with 22.11, but same behavior also with $ nix-shell -p nix-info --run "nix-info -m"
- system: `"x86_64-linux"`
- host os: `Linux 5.15.93, NixOS, 23.05 (Stoat), 23.05pre453471.e5530aba13c`
- multi-user?: `yes`
- sandbox: `yes`
- version: `nix-env (Nix) 2.13.2`
- channels(root): `"nixos"`
- nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos` |
I see that the example does not set up any toolchain explicitly, so it's going to use Bazel's builtin autodetecting cc toolchain. In rules_nixpkgs we found that a clang based cc-toolchain requires additional compiler and linker flags to work with Bazel. You can find that configuration here. |
You might be interested in the toolchain we are using in the rules_ll flake which wraps Bazel in a Clang/LLVM toolchain. |
After a bit of stall on this issue here, I dug into the underlying reason and possible fix suggestions. TL;DRbazel uses the canonical name of the compiler to compile c++, In NixOS,
What bazel uses as compilerThe problem seems to be that bazel by default uses the generic compiler driver for c++, so depending on the stdenv, this would be Here, using
... the wrapper script emits that it uses plain
... and here, using
Now we see that bazel chooses plain
Can these compilers deal with C++ ?So, in both cases, bazel chose the common name of the plain compiler This does indeed work with Let's compile a super-simple c++ program including a c++ header with both With
This works! ... with
This emits an error:
The error indicates that it behaves like a c-compiler and not as a c++ compiler that knows how where to find c++ headers. It does work, if we use
Conclusion, possible solutionsSo all we need is bazel invoking There see two possible simple solutions
I think I would prefer the first solution (fix |
One possibility to get a nix-backed C++ toolchain for Bazel that uses the default
This setup generates a cc toolchain config that uses tools from nixpkgs (like this). This exact approach is probably not what you'd want in nixpkgs, but a similar approach might be possible. I think a nice solution could be a tool that does the same kind of toolchain autogeneration as |
Dupe of #150655? Sorry should have noted this when you CC'd me initially, must have missed the notification. |
Yes, looks like this and the other issue describe the same problem @uri-canva |
Describe the bug
Compilation using
bazel
in aclang13Stdenv
environment fails.Something similar shows up in Darwin, which as clang as default environment, but this is to demonstrate with a self-contained example that this is a general problem with an
isClang
environment.Steps To Reproduce
I've created a self-contained example repository with a small project that uses C and C++ files
https://github.com/hzeller/nix-bazel-with-clang
This example provides a
shell.nix
that enables clang13StdenvExpected behavior
This should compile and run the small main program in
bazel-bin/main
, outputting... this happens instead
... but what actually happens is that the compilation fails:
Additional context
Symptoms of this issue have been described in other bugs, but often in the Darwin context #150655
Also seen in unsuccessful pull request attempting to add Darwin support #214797
People have sometimes applied various workarounds successfully, but this of course need to work 'out of the box'.
Darwin is not the main culprit though, it is just surfacing the issue because there, the stdenv is based on clang.
The problem is more the way bazel and the compiler environment is wired up, setting flags and possibly providing compiler-wrappers needed, so I hope with the self-contained example in the git repo mentioned above it is possible for the nix-
bazel
maintainers to get to the root of the problem.Notify maintainers
CC @NixOS/bazel @aherrmann @ylecornec
Also FYI @uri-canva
Metadata
Please run
nix-shell -p nix-info --run "nix-info -m"
and paste the result.The text was updated successfully, but these errors were encountered: