Skip to content

Commit

Permalink
gcc: fix build with new SDK pattern (#346949)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilazy authored Oct 10, 2024
2 parents 66d78e0 + e500744 commit 72b50cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 42 deletions.
52 changes: 13 additions & 39 deletions pkgs/development/compilers/gcc/common/builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 // {
Expand All @@ -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
Expand Down Expand Up @@ -74,24 +58,14 @@ 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
declare -a prefixExtraLDFlags=()
prefixExtraLDFlags=("-L$libc_libdir")
nixDontSetRpathVar=NIX_DONT_SET_RPATH''${post}
if test "''${!nixDontSetRpathVar-}" != "1"; then
prefixExtraLDFlags+=("-rpath" "$libc_libdir")
fi
extraLDFlags=("''${prefixExtraLDFlags[@]}" "''${extraLDFlags[@]}")
for i in "''${extraLDFlags[@]}"; do
declare -g EXTRA_LDFLAGS''${post}+=" -Wl,$i"
done
done
Expand Down
11 changes: 8 additions & 3 deletions pkgs/development/compilers/gcc/common/configure-flags.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
, threadsCross
, version

, binutils, gmp, mpfr, libmpc, isl
, apple-sdk, binutils, gmp, mpfr, libmpc, isl

, enableLTO
, enableMultilib
Expand Down Expand Up @@ -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
Expand All @@ -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}/"
]
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/compilers/gcc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
, nukeReferences
, callPackage
, majorMinorVersion
, apple-sdk
, cctools
, darwin
}:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 72b50cc

Please sign in to comment.