From b81284ec71a1d66aeb11e2ecdcf72fd192d309d8 Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Fri, 19 Jan 2024 20:01:58 -0800 Subject: [PATCH] gcc: link $lib/lib -> $lib/$targetConfig correctly and consistently When native-compiling, gcc will install libraries into: /nix/store/...-$targetConfig-gcc-$version-lib/lib When cross-compiling, gcc will install libraries into: /nix/store/...-$targetConfig-gcc-$version-lib/$targetConfig When cross-compiling, we intended to create a link from $lib/lib to $lib/$targetConfig, so that downstream users can always safely assume that "${lib.getLib stdenv.cc.cc}/lib" is where the gcc libraries are, regardless of whether `stdenv.cc.cc` is a cross compiler or a native compiler. Unfortunately, there were two problems with how we were trying to create these links: 1. The link would be created only when `enableLibGccOutput==true` 2. The link was being created from the incorrect source `$lib/lib/lib` instead of `$lib/lib`. Both of these mistakes are my fault. This commit corrects them by creating the link using `ln -Ts` (which is more predictable) and by creating the link from `gcc/common/builder.nix` rather than from `gcc/common/libgcc.nix`. --- pkgs/development/compilers/gcc/common/builder.nix | 8 ++++++++ pkgs/development/compilers/gcc/common/libgcc.nix | 4 ---- pkgs/development/compilers/gcc/default.nix | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pkgs/development/compilers/gcc/common/builder.nix b/pkgs/development/compilers/gcc/common/builder.nix index 98525b5e237e..25c564633865 100644 --- a/pkgs/development/compilers/gcc/common/builder.nix +++ b/pkgs/development/compilers/gcc/common/builder.nix @@ -1,6 +1,7 @@ { lib , stdenv , enableMultilib +, targetConfig }: let @@ -196,6 +197,13 @@ originalAttrs: (stdenv.mkDerivation (finalAttrs: originalAttrs // { mkdir -p "$out/''${targetConfig}/lib" mkdir -p "''${!outputLib}/''${targetConfig}/lib" '' + + # if cross-compiling, link from $lib/lib to $lib/${targetConfig}. + # since native-compiles have $lib/lib as a directory (not a + # symlink), this ensures that in every case we can assume that + # $lib/lib contains the .so files + lib.optionalString (with stdenv; targetPlatform.config != hostPlatform.config) '' + ln -Ts "''${!outputLib}/''${targetConfig}/lib" $lib/lib + '' + # Make `lib64` symlinks to `lib`. lib.optionalString (!enableMultilib && stdenv.hostPlatform.is64bit && !stdenv.hostPlatform.isMips64n32) '' ln -s lib "$out/''${targetConfig}/lib64" diff --git a/pkgs/development/compilers/gcc/common/libgcc.nix b/pkgs/development/compilers/gcc/common/libgcc.nix index c8342ae90054..a7de840adc8d 100644 --- a/pkgs/development/compilers/gcc/common/libgcc.nix +++ b/pkgs/development/compilers/gcc/common/libgcc.nix @@ -83,10 +83,6 @@ in lib.optionalString (!langC) '' rm -f $out/lib/libgcc_s.so* '' - + lib.optionalString (hostPlatform != targetPlatform) '' - mkdir -p $lib/lib/ - ln -s ${targetPlatformSlash}lib $lib/lib - '' # TODO(amjoseph): remove the `libgcc_s.so` symlinks below and replace them # with a `-L${gccForLibs.libgcc}/lib` in cc-wrapper's diff --git a/pkgs/development/compilers/gcc/default.nix b/pkgs/development/compilers/gcc/default.nix index cc3546bed22c..0144ab4cfff9 100644 --- a/pkgs/development/compilers/gcc/default.nix +++ b/pkgs/development/compilers/gcc/default.nix @@ -103,6 +103,7 @@ let inherit version; disableBootstrap = atLeast11 && !stdenv.hostPlatform.isDarwin && (atLeast12 -> !profiledCompiler); inherit (stdenv) buildPlatform hostPlatform targetPlatform; + targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null; patches = callFile ./patches {}; @@ -124,6 +125,7 @@ let inherit version; buildPlatform hostPlatform targetPlatform + targetConfig patches crossMingw stageNameAddon @@ -329,7 +331,7 @@ lib.pipe ((callFile ./common/builder.nix {}) ({ ++ optional (is7 && targetPlatform.isAarch64) "--enable-fix-cortex-a53-843419" ++ optional (is7 && targetPlatform.isNetBSD) "--disable-libcilkrts"; - targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null; + inherit targetConfig; buildFlags = # we do not yet have Nix-driven profiling