NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.69k stars 13.83k forks source link

`llvmPackages_17.clang`: `clang` 17 cannot locate C++ std headers #273875

Open Whovian9369 opened 10 months ago

Whovian9369 commented 10 months ago

Describe the bug

When trying to build some software, build is unable to complete due to missing headers.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Use clang_17 and llvmPackages_17.stdenv.mkDerivation on project that requires it.
  2. Run nix build.
  3. Build fails partway through due to missing header.

Expected behavior

clang to find the std headers.

Additional context

I've been trying to build Redumper for my own testing, and have been unable to build it successfully. Here is a log of the build, with NIX_CFLAGS_COMPILE="-v": 061glri5zp3s9rlis00vyj9pcbpcqd2x-redumper-build_297.drv.log

Click to open derivation used for testing: ```nix { lib , llvmPackages_17 , fetchFromGitHub , cmake , ninja , clang_17 , build_type ? "Release" # Typical values include `Debug`, `Release`, `RelWithDebInfo` and `MinSizeRel` # Usually set to "Release" for GitHub Actions use, so that's what it's set to by default here. }: llvmPackages_17.stdenv.mkDerivation rec { pname = "redumper"; version = "build_297"; src = fetchFromGitHub { owner = "superg"; repo = "redumper"; rev = version; hash = "sha256-AOuWkdFT8e/aEvvORcAtLHPFgRUzFsZ86VIddEOyts4="; }; nativeBuildInputs = [ cmake ninja clang_17 # Should alias to `llvmPackages_17.clang` ]; env.NIX_CFLAGS_COMPILE = "-v"; env.gh_run_version = "297"; cmakeFlags = [ "-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON" "-DREDUMPER_CLANG_LINK_OPTIONS=-static" "-DREDUMPER_VERSION_BUILD=$gh_run_version" ]; meta = with lib; { description = "Low level CD dumper utility"; homepage = "https://github.com/superg/redumper"; license = licenses.gpl3Only; maintainers = with maintainers; [ whovian9369 ]; mainProgram = "redumper"; platforms = platforms.all; }; } ```

Notify maintainers

@dtzWill @Ericson2314 @lovek323 @primeos @alyssais @RaitoBezarius @rrbutani @sternenseemann

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.1.64, NixOS, 23.11 (Tapir), 23.11.928.50aa30a13c4a`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.1`
 - channels(root): `"nixos-23.11"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

Add a :+1: reaction to issues you find important.

wegank commented 10 months ago

This doesn't seem to be specific to llvmPackages_17, as replacing it with llvmPackages_16 doesn't help.

hmenke commented 10 months ago

You need llvmPackages_17.libcxxStdenv.mkDerivation, because Redumper hardcodes -stdlib=libc++ inside CMakeLists.txt when it detects that Clang is used as a C++ compiler.

Whovian9369 commented 10 months ago

Thanks, I'll test that for my specific issue. I'll leave this open as I'm personally unsure if it is "enough" to close as user error.

hmenke commented 10 months ago

I just tried it myself and it doesn't help. But I found this related upstream bug: https://github.com/llvm/llvm-project/issues/61006

superg commented 10 months ago

You need llvmPackages_17.libcxxStdenv.mkDerivation, because Redumper hardcodes -stdlib=libc++ inside CMakeLists.txt when it detects that Clang is used as a C++ compiler.

Can you please elaborate on the hardcoding part? Is there a better way to specify a C++ runtime library?

romatthe commented 8 months ago

I ran into this precise problem while also trying to package redumper myself yesterday and running into a brick wall.

The issue linked by @hmenke suggests specifying the includes manually, but I wouldn't know how to do that on NixOS (and with my extremely minimal cmake/gcc/clang knowledge). Did you manage to get this working @Whovian9369? Is there anything @superg can do to help us with this?

Whovian9369 commented 8 months ago

Did you manage to get this working @Whovian9369?

Haven't tried anything more since, no. If you want to talk about this at all and use Discord, please feel free to send me a friend request on Discord, @romatthe !

I-mikan-I commented 7 months ago

This seems to be a nix issue with the wrapper. The same llvm packages end up working on ubuntu.

Whovian9369 commented 3 months ago

I just tested this, and it seems to still be occurring when using: llvmPackages_18.stdenv.mkDerivation, nativeBuildInputs = [ cmake ninja clang_18 ];

Said log: 27naig5iqc1d71hss2ybghd2lxg3sx9k-redumper-git-371.drv.log

Click to open derivation used for testing: ```nix { lib, fetchFromGitHub, cmake, ninja, build_type ? "Release", llvmPackages_18, clang_18 }: llvmPackages_18.stdenv.mkDerivation rec { pname = "redumper-git"; version = "371"; src = fetchFromGitHub { owner = "superg"; repo = "redumper"; rev = "f52fd2e5617e27f281c2ae23f8e8cd14dc49f2a7"; hash = "sha256-n2XPMSEQvSPGATHSW1QrBUTnmTJvTt1N5l1VubtH4R8="; }; nativeBuildInputs = [ cmake ninja clang_18 # Should alias to `llvmPackages_18.clang` ]; cmakeFlags = [ "-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON" "-DREDUMPER_CLANG_LINK_OPTIONS=-static" "-DREDUMPER_VERSION_BUILD=${version}" ]; meta = { description = "Low level CD dumper utility"; homepage = "https://github.com/superg/redumper"; license = lib.licenses.gpl3Only; maintainers = with lib.maintainers; [ ]; mainProgram = "redumper"; platforms = lib.platforms.all; }; } ```
kanashimia commented 3 months ago

Even compiling something simple like a hello world through cmake doesn't work when you use libcxx llvm stdenv, it seems to be a problem with the dependency scanner. You can work around it by manually specifying the headers in the build flags but that is awful.

lf- commented 3 months ago

Possible related issues: https://github.com/NixOS/nixpkgs/issues/277564 https://github.com/NixOS/nixpkgs/issues/191152

cc @qyriad because i know you were complaining about the cc wrapper assuming libstdc++ on non-macOS.

emilazy commented 1 month ago

cc @RossComputerGuy

nixos-discourse commented 1 month ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/clang-cannot-locate-c-std-headers/51095/1

RossComputerGuy commented 1 month ago

I think I figured out the problem. I had to explicitly pass includes for glibc's include dir and a C++ stdlib impl header directory. Using NIX_CFLAGS_COMPILE or other similar arguments was not good enough. This might mean that we will have to wrap clang-scan-deps.

leo60228 commented 2 weeks ago

There's already a wrapped clang-scan-deps in llvmPackages.clang-tools. Adding it to nativeBuildInputs fixes the issue.

hughobrien commented 5 days ago

Seems I'm late to the redumper packaging party.

@leo60228 am I to understand that you are able to build it? As below my clangStdenv still has library path issues (same if I use GNU ld)

Running phase: buildPhase
[1/190] Scanning /build/source/generate_offsets.cc for CXX dependencies
warning: argument unused during compilation: '-fuse-ld=lld'
warning: argument unused during compilation: '-stdlib=libc++'
[3/190] Building CXX object CMakeFiles/generate_offsets.dir/generate_offsets.cc.o
clang++: warning: argument unused during compilation: '-fuse-ld=lld' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-stdlib=libc++' [-Wunused-command-line-argument]
[12/190] Linking CXX executable generate_offsets/Threads.c.oK
FAILED: generate_offsets 
: && /nix/store/4rz4z2bkb68vwbdxcwq0jxh2fyhhiqkh-clang-wrapper-18.1.8/bin/clang++ -fuse-ld=lld -O2 -g -DNDEBUG -stdlib=libc++ -static CMakeFiles/generate_offsets.dir/generate_offsets.cc.o -o generate_offsets   && :
ld.lld: error: unable to find library -lc++
ld.lld: error: unable to find library -lm
ld.lld: error: unable to find library -lc
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
error: builder for '/nix/store/w50715li5j8rdv7yv51gdm92fqyw4swa-redumper-415.drv' failed with exit code 1;
       last 10 log lines:
       > clang++: warning: argument unused during compilation: '-fuse-ld=lld' [-Wunused-command-line-argument]
       > clang++: warning: argument unused during compilation: '-stdlib=libc++' [-Wunused-command-line-argument]
       > [12/190] Linking CXX executable generate_offsets/Threads.c.oK
       > FAILED: generate_offsets
       > : && /nix/store/4rz4z2bkb68vwbdxcwq0jxh2fyhhiqkh-clang-wrapper-18.1.8/bin/clang++ -fuse-ld=lld -O2 -g -DNDEBUG -stdlib=libc++ -static CMakeFiles/generate_offsets.dir/generate_offsets.cc.o -o generate_offsets   && :
       > ld.lld: error: unable to find library -lc++
       > ld.lld: error: unable to find library -lm
       > ld.lld: error: unable to find library -lc
       > clang++: error: linker command failed with exit code 1 (use -v to see invocation)
       > ninja: build stopped: subcommand failed.
       For full logs, run 'nix log /nix/store/w50715li5j8rdv7yv51gdm92fqyw4swa-redumper-415.drv'.
derivation ```nix { lib , fetchFromGitHub , clangStdenv , cmake , ninja , llvmPackages }: clangStdenv.mkDerivation (finalAttrs: { pname = "redumper"; version = "415"; src = fetchFromGitHub { owner = "superg"; repo = "redumper"; rev = "build_${finalAttrs.version}"; hash = "sha256-c5bVCIJMsFrn/Z6ufvm72Ro3d5JkqSeUfM/B1PQBg3g="; }; cmakeBuildType = "RelWithDebInfo"; nativeBuildInputs = [ cmake ninja llvmPackages.clang-tools llvmPackages.lld ]; enableParallelBuilding = false; cmakeFlags = [ "-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON" "-DREDUMPER_CLANG_LINK_OPTIONS=-static" "-DREDUMPER_VERSION_BUILD=${finalAttrs.version}" "-DCMAKE_CXX_FLAGS=-fuse-ld=lld" "-G Ninja" ]; buildPhase = '' cmake --build . -j 1 ''; meta = with lib; { homepage = "https://github.com/superg/redumper"; description = "Low level CD dumper utility"; license = licenses.gpl3Plus; maintainers = with maintainers; [ hughobrien ]; platforms = platforms.linux; }; }) ```