nixpkgs/pkgs/os-specific/linux/minimal-bootstrap/binutils/static.nix
Aliaksandr ffe5913f56
minimal-bootstrap: trim binutils-static output
Drop unused installed libraries and headers, avoid building unused gprof/libctf components, and replace duplicate target-prefixed tools with symlinks while keeping the conventional paths available.

Size impact against upstream/staging:

- binutils-static closure: 42.482 MiB -> 21.093 MiB (-21.389 MiB)

Assisted-by: codex with gpt-5.5-high
2026-06-08 00:09:36 +03:00

127 lines
3 KiB
Nix

{
lib,
buildPlatform,
hostPlatform,
fetchurl,
bash,
gcc,
musl,
binutils,
gnumake,
gnupatch,
gnused,
gnugrep,
gawk,
diffutils,
findutils,
gnutar,
xz,
}:
let
inherit (import ./common.nix { inherit lib; }) meta;
pname = "binutils-static";
version = "2.46.0";
src = fetchurl {
url = "mirror://gnu/binutils/binutils-${version}.tar.xz";
hash = "sha256-11qU9Nc+ekCG91E+Z+Q56Pzcu3Jv/mP0ZhdE5iVrLPI=";
};
patches = [
# Make binutils output deterministic by default.
./deterministic.patch
];
configureFlags = [
"CC=musl-gcc"
"LDFLAGS=--static"
"--prefix=${placeholder "out"}"
"--build=${buildPlatform.config}"
"--host=${hostPlatform.config}"
"--disable-dependency-tracking"
"--disable-nls"
"--with-sysroot=/"
"--enable-deterministic-archives"
# depends on bison
"--disable-gprofng"
# unused downstream
"--disable-gprof"
# Turn on --enable-new-dtags by default to make the linker set
# RUNPATH instead of RPATH on binaries. This is important because
# RUNPATH can be overridden using LD_LIBRARY_PATH at runtime.
"--enable-new-dtags"
# By default binutils searches $libdir for libraries. This brings in
# libbfd and libopcodes into a default visibility. Drop default lib
# path to force users to declare their use of these libraries.
"--with-lib-path=:"
"--disable-gold"
# unused in the bootstrap path and removed from the output
"--disable-libctf"
"--disable-plugins"
];
in
bash.runCommand "${pname}-${version}"
{
inherit pname version meta;
nativeBuildInputs = [
gcc
musl
binutils
gnumake
gnupatch
gnused
gnugrep
gawk
diffutils
findutils
gnutar
xz
];
passthru.tests.get-version =
result:
bash.runCommand "${pname}-get-version-${version}" { } ''
${result}/bin/ld --version
${result}/${hostPlatform.config}/bin/ld --version
mkdir $out
'';
}
''
# Unpack
tar xf ${src}
cd binutils-${version}
# Patch
${lib.concatMapStringsSep "\n" (f: "patch -Np1 -i ${f}") patches}
# Configure
bash ./configure ${lib.concatStringsSep " " configureFlags}
# Build
make -j $NIX_BUILD_CORES
# Install
# strip to remove build dependency store path references
make -j $NIX_BUILD_CORES install-strip
# gprof/addr2line/elfedit + man pages are unused downstream.
rm -f $out/bin/gprof $out/bin/addr2line $out/bin/elfedit
rm -rf $out/include $out/lib $out/share
# The target-prefixed tools duplicate the unprefixed tools byte-for-byte.
# Keep the conventional target-prefixed paths, but make them symlinks.
targetBin=$out/${hostPlatform.config}/bin
if [ -d "$targetBin" ]; then
for tool in ar as ld ld.bfd nm objcopy objdump ranlib readelf strip; do
if [ -e "$targetBin/$tool" ] && cmp -s "$out/bin/$tool" "$targetBin/$tool"; then
rm "$targetBin/$tool"
ln -s ../../bin/$tool "$targetBin/$tool"
fi
done
fi
''