From 97c400a0e8f9703f398d48218229bd5955c345f5 Mon Sep 17 00:00:00 2001 From: Julien Moutinho Date: Tue, 4 Jun 2024 16:35:49 +0200 Subject: [PATCH] cc-wrapper: fix -Bprefix to not confuse lib/libc++.so and bin/c++ Before this commit, `pkgs/build-support/cc-wrapper/add-flags.sh` was using `-B@out@/bin` instead of `-B@bintools@/bin` to force `cc` to use `ld-wrapper.sh` when calling `ld`. That was confusing `cc` when asked to print the location of a library precisely named `c++` because `-B` prefixes are also used by `cc` to find libraries, see https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#index-B Indeed, instead of having `cc --print-file-name c++` failing to found a `c++` library and just returning the given `c++` string to let a linker resolve it thereafter, it was finding that `@out@/bin/c++` executable, mistaking it for a library and returning its absolute path, forcing the linker to load an executable as a library. Before this commit: ```console $ nix run -f . stdenv.cc -- --print-file-name c++ /nix/store/9bv7dcvmfcjnmg5mnqwqlq2wxfn8d7yi-gcc-wrapper-13.2.0/bin/c++ ``` After this commit: ```console $ nix run -f . stdenv.cc -- --print-file-name c++ c++ ``` Fixes https://gitlab.haskell.org/ghc/ghc/-/issues/23138#note_567034 where this behavior was breaking GHC on Darwin. [Confirmed by @414owen](https://github.com/NixOS/nixpkgs/pull/317224#issuecomment-2171276177): > This fixed all our haskell builds on Arm64 darwin, which were trying > to link in clang++... --- pkgs/build-support/cc-wrapper/add-flags.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/build-support/cc-wrapper/add-flags.sh b/pkgs/build-support/cc-wrapper/add-flags.sh index 3f2b0ccf0a6b..2c3dc8884023 100644 --- a/pkgs/build-support/cc-wrapper/add-flags.sh +++ b/pkgs/build-support/cc-wrapper/add-flags.sh @@ -29,8 +29,8 @@ done # Arocc does not support "-B" if [[ -z "@isArocc@" ]]; then - # `-B@out@/bin' forces cc to use ld-wrapper.sh when calling ld. - NIX_CFLAGS_COMPILE_@suffixSalt@="-B@out@/bin/ $NIX_CFLAGS_COMPILE_@suffixSalt@" + # `-B@bintools@/bin' forces cc to use ld-wrapper.sh when calling ld. + NIX_CFLAGS_COMPILE_@suffixSalt@="-B@bintools@/bin/ $NIX_CFLAGS_COMPILE_@suffixSalt@" fi # Export and assign separately in order that a failing $(..) will fail