From 3697ddeabe2c51a868a2b1d1f2626f7778d673b4 Mon Sep 17 00:00:00 2001 From: Artturin Date: Wed, 15 Feb 2023 00:12:41 +0200 Subject: [PATCH 1/3] bintools-wrapper: wrap all 'ld.*' allows using wrapBintoolsWith with all linkers ``` $ nix build ".#binutils" $ ls ./result/bin/ld* ./result/bin/ld* ./result/bin/ld.bfd* ./result/bin/ld.gold* $ nix build "nixpkgs#binutils" $ ls ./result/bin/ld* ./result/bin/ld* ./result/bin/ld.bfd* ./result/bin/ld.gold* ``` --- pkgs/build-support/bintools-wrapper/default.nix | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkgs/build-support/bintools-wrapper/default.nix b/pkgs/build-support/bintools-wrapper/default.nix index 45ae3eba9b44..05b5a3732580 100644 --- a/pkgs/build-support/bintools-wrapper/default.nix +++ b/pkgs/build-support/bintools-wrapper/default.nix @@ -192,10 +192,9 @@ stdenv.mkDerivation { unset ldInner '') + '' - for variant in ld.gold ld.bfd ld.lld; do - local underlying=$ldPath/${targetPrefix}$variant - [[ -e "$underlying" ]] || continue - wrap ${targetPrefix}$variant ${./ld-wrapper.sh} $underlying + for variant in $ldPath/${targetPrefix}ld.*; do + basename=$(basename "$variant") + wrap $basename ${./ld-wrapper.sh} $variant done ''; From b5abc3d0907c7c5d70479c60ff7f1cf262ead4c8 Mon Sep 17 00:00:00 2001 From: Artturin Date: Fri, 17 Feb 2023 04:56:48 +0200 Subject: [PATCH 2/3] bintools-wrapper: dont wrap ld if it doesn't exist not all linkers have a ld binary in bin also note the '${ld:-}' which allows users to set the ld path with a env var > '${foo:-val}' $foo, or val if unset (or null) --- pkgs/build-support/bintools-wrapper/default.nix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkgs/build-support/bintools-wrapper/default.nix b/pkgs/build-support/bintools-wrapper/default.nix index 05b5a3732580..15d81e852f83 100644 --- a/pkgs/build-support/bintools-wrapper/default.nix +++ b/pkgs/build-support/bintools-wrapper/default.nix @@ -184,7 +184,9 @@ stdenv.mkDerivation { done '' + (if !useMacosReexportHack then '' - wrap ${targetPrefix}ld ${./ld-wrapper.sh} ''${ld:-$ldPath/${targetPrefix}ld} + if [ -e ''${ld:-$ldPath/${targetPrefix}ld} ]; then + wrap ${targetPrefix}ld ${./ld-wrapper.sh} ''${ld:-$ldPath/${targetPrefix}ld} + fi '' else '' ldInner="${targetPrefix}ld-reexport-delegate" wrap "$ldInner" ${./macos-sierra-reexport-hack.bash} ''${ld:-$ldPath/${targetPrefix}ld} From 299a7bd35e552fe6e444096658d7e9ebafb038af Mon Sep 17 00:00:00 2001 From: Artturin Date: Fri, 17 Feb 2023 05:18:19 +0200 Subject: [PATCH 3/3] stdenvAdapters: add useMoldLinker --- pkgs/stdenv/adapters.nix | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkgs/stdenv/adapters.nix b/pkgs/stdenv/adapters.nix index 42d19a0fad4b..2ea9a359f07b 100644 --- a/pkgs/stdenv/adapters.nix +++ b/pkgs/stdenv/adapters.nix @@ -189,6 +189,28 @@ rec { }); }); + useMoldLinker = stdenv: let + bintools = stdenv.cc.bintools.override { + extraBuildCommands = '' + wrap ld.mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold + wrap ld ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold + ''; + }; + in stdenv.override (old: { + cc = stdenv.cc.override { + inherit bintools; + }; + allowedRequisites = + lib.mapNullable (rs: rs ++ [ bintools pkgs.mold (lib.getLib pkgs.mimalloc) (lib.getLib pkgs.openssl) ]) (stdenv.allowedRequisites or null); + # gcc >12.1.0 supports '-fuse-ld=mold' + # the wrap ld above in bintools supports gcc <12.1.0 and shouldn't harm >12.1.0 + # https://github.com/rui314/mold#how-to-use + } // lib.optionalAttrs (stdenv.cc.isClang || (stdenv.cc.isGNU && lib.versionAtLeast stdenv.cc.version "12")) { + mkDerivationFromStdenv = extendMkDerivationArgs old (args: { + NIX_CFLAGS_LINK = toString (args.NIX_CFLAGS_LINK or "") + " -fuse-ld=mold"; + }); + }); + /* Modify a stdenv so that it builds binaries optimized specifically for the machine they are built on.