From bd0dd33de11c7905028f6521a54a8e99e8de4d94 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Mon, 21 Apr 2025 18:51:52 -0400 Subject: [PATCH 1/2] cc-wrapper: always include libc++ in the search path The libc++ headers are expected to be found in the sysroot when `clang` is invoked for C++ code. Always making them available improves compatibility with build systems like Bazel and SwiftPM, which try to compile C++ code with `clang`. --- pkgs/build-support/cc-wrapper/cc-wrapper.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkgs/build-support/cc-wrapper/cc-wrapper.sh b/pkgs/build-support/cc-wrapper/cc-wrapper.sh index eb5b99643dbac..246c1b1f65f6e 100644 --- a/pkgs/build-support/cc-wrapper/cc-wrapper.sh +++ b/pkgs/build-support/cc-wrapper/cc-wrapper.sh @@ -140,6 +140,15 @@ if [ "$NIX_ENFORCE_NO_NATIVE_@suffixSalt@" = 1 ]; then params=(${kept+"${kept[@]}"}) fi +# Some build systems such as Bazel and SwiftPM use `clang` instead of `clang++`, +# which will find the libc++ headers in the sysroot for C++ files. +if [[ "$isCxx" = 0 && "@isClang@" ]]; then +# This duplicates the behavior of a native toolchain, which can find the +# libc++ headers but requires `-lc++` to be specified explicitly when linking. + isCxx=1 + cxxLibrary=0 +fi + if [[ "$isCxx" = 1 ]]; then if [[ "$cxxInclude" = 1 ]]; then # From 76a8ffa6d8d2f93b81d4e9be327a01cab1c893c0 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Mon, 22 Sep 2025 18:53:09 -0400 Subject: [PATCH 2/2] cc-wrapper: use `-cxx-isystem` instead of `-isystem` on Clang This allows Clang to always look for the libc++ headers when compling C++ code (regardless of whether it is invoked with `clang` or `clang++` while not exposing the headers when compiling plain C code. See https://releases.llvm.org/21.1.0/tools/clang/docs/ClangCommandLineReference.html#cmdoption-clang-cxx-isystem-directory --- pkgs/build-support/cc-wrapper/default.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix index 50ff90609a5e4..427161f4742c0 100644 --- a/pkgs/build-support/cc-wrapper/default.nix +++ b/pkgs/build-support/cc-wrapper/default.nix @@ -741,14 +741,14 @@ stdenvNoCC.mkDerivation { # https://github.com/NixOS/nixpkgs/pull/209870#issuecomment-1500550903) + optionalString (libcxx == null && isClang && (useGccForLibs && gccForLibs.langCC or false)) '' for dir in ${gccForLibs}/include/c++/*; do - include -isystem "$dir" >> $out/nix-support/libcxx-cxxflags + include -cxx-isystem "$dir" >> $out/nix-support/libcxx-cxxflags done for dir in ${gccForLibs}/include/c++/*/${targetPlatform.config}; do - include -isystem "$dir" >> $out/nix-support/libcxx-cxxflags + include -cxx-isystem "$dir" >> $out/nix-support/libcxx-cxxflags done '' + optionalString (libcxx.isLLVM or false) '' - include -isystem "${getDev libcxx}/include/c++/v1" >> $out/nix-support/libcxx-cxxflags + include -cxx-isystem "${getDev libcxx}/include/c++/v1" >> $out/nix-support/libcxx-cxxflags echo "-stdlib=libc++" >> $out/nix-support/libcxx-ldflags '' # GCC NG friendly libc++