From ac31fb750578c31122d71ba739421fc8ce1d34d1 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Thu, 29 Aug 2024 21:50:47 -0400 Subject: [PATCH 1/3] gcc: remove staging-next workaround --- .../compilers/gcc/common/builder.nix | 51 +++++-------------- 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/pkgs/development/compilers/gcc/common/builder.nix b/pkgs/development/compilers/gcc/common/builder.nix index 0e89cbd615ce..e7e3891780ce 100644 --- a/pkgs/development/compilers/gcc/common/builder.nix +++ b/pkgs/development/compilers/gcc/common/builder.nix @@ -7,13 +7,6 @@ let forceLibgccToBuildCrtStuff = import ./libgcc-buildstuff.nix { inherit lib stdenv; }; - - # todo(@reckenrode) Remove in staging. This is ugly, but it avoid unwanted rebuilds on Darwin and Linux. - enableDarwinFixesForStagingNext = - version: - stdenv.buildPlatform.isDarwin - && stdenv.buildPlatform.isx86_64 - && lib.versionOlder version "10"; in originalAttrs: (stdenv.mkDerivation (finalAttrs: originalAttrs // { @@ -27,20 +20,11 @@ originalAttrs: (stdenv.mkDerivation (finalAttrs: originalAttrs // { if test "$staticCompiler" = "1"; then EXTRA_LDFLAGS="-static" - ${ - if enableDarwinFixesForStagingNext finalAttrs.version then - '' - elif test "''${NIX_DONT_SET_RPATH-}" != "1"; then - EXTRA_LDFLAGS="-Wl,-rpath,''${!outputLib}/lib" - else - EXTRA_LDFLAGS="" - '' - else - '' - else - EXTRA_LDFLAGS="-Wl,-rpath,''${!outputLib}/lib" - '' - }fi + elif test "''${NIX_DONT_SET_RPATH-}" != "1"; then + EXTRA_LDFLAGS="-Wl,-rpath,''${!outputLib}/lib" + else + EXTRA_LDFLAGS="" + fi # GCC interprets empty paths as ".", which we don't want. if test -z "''${CPATH-}"; then unset CPATH; fi @@ -74,24 +58,13 @@ originalAttrs: (stdenv.mkDerivation (finalAttrs: originalAttrs // { extraLDFlags=("-L/usr/lib64" "-L/usr/lib") libc_libdir="/usr/lib" fi - ${ - if enableDarwinFixesForStagingNext finalAttrs.version then - '' - extraLDFlags=("-L$libc_libdir") - nixDontSetRpathVar=NIX_DONT_SET_RPATH''${post} - if test "''${!nixDontSetRpathVar-}" != "1"; then - extraLDFlags+=("-rpath" "$libc_libdir") - fi - extraLDFlags+=("''${extraLDFlags[@]}") - '' - else - '' - extraLDFlags=("-L$libc_libdir" "-rpath" "$libc_libdir" - "''${extraLDFlags[@]}") - '' -# The strange indentation with the next line is to ensure the string renders the same when the condition is false, -# which is necessary to prevent unwanted rebuilds in staging-next. -} for i in "''${extraLDFlags[@]}"; do + extraLDFlags=("-L$libc_libdir") + nixDontSetRpathVar=NIX_DONT_SET_RPATH''${post} + if test "''${!nixDontSetRpathVar-}" != "1"; then + extraLDFlags+=("-rpath" "$libc_libdir") + fi + extraLDFlags+=("''${extraLDFlags[@]}") + for i in "''${extraLDFlags[@]}"; do declare -g EXTRA_LDFLAGS''${post}+=" -Wl,$i" done done From a77d0eb81915997170a1e4b4c7d4c7ac4fcd5320 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Thu, 10 Oct 2024 16:59:52 -0400 Subject: [PATCH 2/3] gcc: fix `extraLDFlags` logic after undoing staging-next workaroud The original logic was prepending to the array, but this one prepends the array to itself, which breaks the x86_64-linux stdenv bootstrap. The correct thing to do is build up the arguments in a temporary array and prepend it like the original code was doing. --- pkgs/development/compilers/gcc/common/builder.nix | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkgs/development/compilers/gcc/common/builder.nix b/pkgs/development/compilers/gcc/common/builder.nix index e7e3891780ce..874a43a75f82 100644 --- a/pkgs/development/compilers/gcc/common/builder.nix +++ b/pkgs/development/compilers/gcc/common/builder.nix @@ -58,12 +58,13 @@ originalAttrs: (stdenv.mkDerivation (finalAttrs: originalAttrs // { extraLDFlags=("-L/usr/lib64" "-L/usr/lib") libc_libdir="/usr/lib" fi - extraLDFlags=("-L$libc_libdir") + declare -a prefixExtraLDFlags=() + prefixExtraLDFlags=("-L$libc_libdir") nixDontSetRpathVar=NIX_DONT_SET_RPATH''${post} if test "''${!nixDontSetRpathVar-}" != "1"; then - extraLDFlags+=("-rpath" "$libc_libdir") + prefixExtraLDFlags+=("-rpath" "$libc_libdir") fi - extraLDFlags+=("''${extraLDFlags[@]}") + extraLDFlags=("''${prefixExtraLDFlags[@]}" "''${extraLDFlags[@]}") for i in "''${extraLDFlags[@]}"; do declare -g EXTRA_LDFLAGS''${post}+=" -Wl,$i" done From e50074418d6a12c5053ab5120fb920f5fc5e5958 Mon Sep 17 00:00:00 2001 From: Randy Eckenrode Date: Thu, 29 Aug 2024 21:50:47 -0400 Subject: [PATCH 3/3] gcc: set up sysroot for the Darwin SDK The GCC bootstrap builds each stage with an unwrapped GCC, which needs to correctly handle looking for libraries and headers in sysroot. Setting the native header folder to `/usr/include` will actually cause GCC to search `/usr/include`, which is the SDKROOT in nixpkgs. --- .../compilers/gcc/common/configure-flags.nix | 11 ++++++++--- pkgs/development/compilers/gcc/default.nix | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkgs/development/compilers/gcc/common/configure-flags.nix b/pkgs/development/compilers/gcc/common/configure-flags.nix index 104851930213..25d4f1f53bae 100644 --- a/pkgs/development/compilers/gcc/common/configure-flags.nix +++ b/pkgs/development/compilers/gcc/common/configure-flags.nix @@ -5,7 +5,7 @@ , threadsCross , version -, binutils, gmp, mpfr, libmpc, isl +, apple-sdk, binutils, gmp, mpfr, libmpc, isl , enableLTO , enableMultilib @@ -112,7 +112,11 @@ let ] ++ lib.optionals (!withoutTargetLibc) [ (if libcCross == null - then "--with-native-system-header-dir=${lib.getDev stdenv.cc.libc}/include" + then ( + # GCC will search for the headers relative to SDKROOT on Darwin, so it will find them in the store. + if targetPlatform.isDarwin then "--with-native-system-header-dir=/usr/include" + else "--with-native-system-header-dir=${lib.getDev stdenv.cc.libc}/include" + ) else "--with-native-system-header-dir=${lib.getDev libcCross}${libcCross.incdir or "/include"}") # gcc builds for cross-compilers (build != host) or cross-built # gcc (host != target) always apply the offset prefix to disentangle @@ -132,7 +136,8 @@ let # # We pick "/" path to effectively avoid sysroot offset and make it work # as a native case. - "--with-build-sysroot=/" + # Darwin requires using the SDK as the sysroot for `SDKROOT` to work correctly. + "--with-build-sysroot=${if targetPlatform.isDarwin then apple-sdk.sdkroot else "/"}" # Same with the stdlibc++ headers embedded in the gcc output "--with-gxx-include-dir=${placeholder "out"}/include/c++/${version}/" ] diff --git a/pkgs/development/compilers/gcc/default.nix b/pkgs/development/compilers/gcc/default.nix index f7e2e06bb79a..d5d59adabb74 100644 --- a/pkgs/development/compilers/gcc/default.nix +++ b/pkgs/development/compilers/gcc/default.nix @@ -34,6 +34,7 @@ , nukeReferences , callPackage , majorMinorVersion +, apple-sdk , cctools , darwin }: @@ -105,6 +106,7 @@ let ; # inherit generated with 'nix eval --json --impure --expr "with import ./. {}; lib.attrNames (lib.functionArgs gcc${majorVersion}.cc.override)" | jq '.[]' --raw-output' inherit + apple-sdk binutils buildPackages cargo