From 92186a49bf72a74e507549669cde90977c977efe Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Mon, 14 Aug 2023 02:02:31 -0700 Subject: [PATCH] gcc: factor out forceLibgccToBuildCrtStuff --- .../compilers/gcc/common/builder.nix | 6 +++ .../gcc/common/libgcc-buildstuff.nix | 37 +++++++++++++++++++ .../compilers/gcc/common/libgcc.nix | 14 +++---- .../compilers/gcc/common/pre-configure.nix | 36 +----------------- 4 files changed, 51 insertions(+), 42 deletions(-) create mode 100644 pkgs/development/compilers/gcc/common/libgcc-buildstuff.nix diff --git a/pkgs/development/compilers/gcc/common/builder.nix b/pkgs/development/compilers/gcc/common/builder.nix index cd8d4572a158..6df4e32ddb76 100644 --- a/pkgs/development/compilers/gcc/common/builder.nix +++ b/pkgs/development/compilers/gcc/common/builder.nix @@ -3,7 +3,13 @@ , enableMultilib }: +let + forceLibgccToBuildCrtStuff = + import ./libgcc-buildstuff.nix { inherit lib stdenv; }; +in + originalAttrs: (stdenv.mkDerivation (finalAttrs: originalAttrs // { + passthru = (originalAttrs.passthru or {}) // { inherit forceLibgccToBuildCrtStuff; }; preUnpack = '' oldOpts="$(shopt -po nounset)" || true set -euo pipefail diff --git a/pkgs/development/compilers/gcc/common/libgcc-buildstuff.nix b/pkgs/development/compilers/gcc/common/libgcc-buildstuff.nix new file mode 100644 index 000000000000..e7dc570a560c --- /dev/null +++ b/pkgs/development/compilers/gcc/common/libgcc-buildstuff.nix @@ -0,0 +1,37 @@ +{ lib +, stdenv +}: + +# Trick to build a gcc that is capable of emitting shared libraries *without* having the +# targetPlatform libc available beforehand. Taken from: +# https://web.archive.org/web/20170222224855/http://frank.harvard.edu/~coldwell/toolchain/ +# https://web.archive.org/web/20170224235700/http://frank.harvard.edu/~coldwell/toolchain/t-linux.diff +let + # crt{i,n}.o are the first and last (respectively) object file + # linked when producing an executable. Traditionally these + # files are delivered as part of the C library, but on GNU + # systems they are in fact built by GCC. Since libgcc needs to + # build before glibc, we can't wait for them to be copied by + # glibc. At this early pre-glibc stage these files sometimes + # have different names. + crtstuff-ofiles = + if stdenv.targetPlatform.isPower + then "ecrti.o ecrtn.o ncrti.o ncrtn.o" + else "crti.o crtn.o"; + + # Normally, `SHLIB_LC` is set to `-lc`, which means that + # `libgcc_s.so` cannot be built until `libc.so` is available. + # The assignment below clobbers this variable, removing the + # `-lc`. + # + # On PowerPC we add `-mnewlib`, which means "libc has not been + # built yet". This causes libgcc's Makefile to use the + # gcc-built `{e,n}crt{n,i}.o` instead of failing to find the + # versions which have been repackaged in libc as `crt{n,i}.o` + # + SHLIB_LC = lib.optionalString stdenv.targetPlatform.isPower "-mnewlib"; + +in '' + echo 'libgcc.a: ${crtstuff-ofiles}' >> libgcc/Makefile.in + echo 'SHLIB_LC=${SHLIB_LC}' >> libgcc/Makefile.in + '' diff --git a/pkgs/development/compilers/gcc/common/libgcc.nix b/pkgs/development/compilers/gcc/common/libgcc.nix index b14d111e361f..4ab6eb2b3b44 100644 --- a/pkgs/development/compilers/gcc/common/libgcc.nix +++ b/pkgs/development/compilers/gcc/common/libgcc.nix @@ -44,14 +44,14 @@ lib.optional (lib.versionAtLeast version "11.0") !langJit && !stdenv.hostPlatform.isDarwin && enableShared - ; + ; - # For some reason libgcc_s.so has major-version "2" on m68k but - # "1" everywhere else. Might be worth changing this to "*". - libgcc_s-version-major = - if targetPlatform.isM68k - then "2" - else "1"; + # For some reason libgcc_s.so has major-version "2" on m68k but + # "1" everywhere else. Might be worth changing this to "*". + libgcc_s-version-major = + if targetPlatform.isM68k + then "2" + else "1"; in (pkg: pkg.overrideAttrs (previousAttrs: lib.optionalAttrs ((!langC) || langJit || enableLibGccOutput) { diff --git a/pkgs/development/compilers/gcc/common/pre-configure.nix b/pkgs/development/compilers/gcc/common/pre-configure.nix index 933a132ce4d1..5cb2f186fd1d 100644 --- a/pkgs/development/compilers/gcc/common/pre-configure.nix +++ b/pkgs/development/compilers/gcc/common/pre-configure.nix @@ -112,39 +112,5 @@ in lib.optionalString (hostPlatform.isSunOS && hostPlatform.is64bit) '' export inhibit_libc=true '' -# Trick to build a gcc that is capable of emitting shared libraries *without* having the -# targetPlatform libc available beforehand. Taken from: -# https://web.archive.org/web/20170222224855/http://frank.harvard.edu/~coldwell/toolchain/ -# https://web.archive.org/web/20170224235700/http://frank.harvard.edu/~coldwell/toolchain/t-linux.diff + lib.optionalString (targetPlatform != hostPlatform && withoutTargetLibc && enableShared) - (let - - # crt{i,n}.o are the first and last (respectively) object file - # linked when producing an executable. Traditionally these - # files are delivered as part of the C library, but on GNU - # systems they are in fact built by GCC. Since libgcc needs to - # build before glibc, we can't wait for them to be copied by - # glibc. At this early pre-glibc stage these files sometimes - # have different names. - crtstuff-ofiles = - if targetPlatform.isPower - then "ecrti.o ecrtn.o ncrti.o ncrtn.o" - else "crti.o crtn.o"; - - # Normally, `SHLIB_LC` is set to `-lc`, which means that - # `libgcc_s.so` cannot be built until `libc.so` is available. - # The assignment below clobbers this variable, removing the - # `-lc`. - # - # On PowerPC we add `-mnewlib`, which means "libc has not been - # built yet". This causes libgcc's Makefile to use the - # gcc-built `{e,n}crt{n,i}.o` instead of failing to find the - # versions which have been repackaged in libc as `crt{n,i}.o` - # - SHLIB_LC = lib.optionalString targetPlatform.isPower "-mnewlib"; - - in '' - echo 'libgcc.a: ${crtstuff-ofiles}' >> libgcc/Makefile.in - echo 'SHLIB_LC=${SHLIB_LC}' >> libgcc/Makefile.in - '') - + (import ./libgcc-buildstuff.nix { inherit lib stdenv; })