1{ lib
2, buildPlatform
3, hostPlatform
4, fetchurl
5, bash
6, coreutils
7, gnumake
8, gnupatch
9, gnused
10, gnugrep
11, gawk
12, diffutils
13, gnutar
14, xz
15, tinycc
16}:
17
18let
19 # Based on https://github.com/ZilchOS/bootstrap-from-tcc/blob/2e0c68c36b3437386f786d619bc9a16177f2e149/using-nix/2a1-static-binutils.nix
20 pname = "binutils";
21 version = "2.41";
22
23 src = fetchurl {
24 url = "mirror://gnu/binutils/binutils-${version}.tar.xz";
25 hash = "sha256-rppXieI0WeWWBuZxRyPy0//DHAMXQZHvDQFb3wYAdFA=";
26 };
27
28 patches = [
29 # Make binutils output deterministic by default.
30 ./deterministic.patch
31 ];
32
33 configureFlags = [
34 "--prefix=${placeholder "out"}"
35 "--build=${buildPlatform.config}"
36 "--host=${hostPlatform.config}"
37 "--with-sysroot=/"
38 "--enable-deterministic-archives"
39 # depends on bison
40 "--disable-gprofng"
41
42 # Turn on --enable-new-dtags by default to make the linker set
43 # RUNPATH instead of RPATH on binaries. This is important because
44 # RUNPATH can be overridden using LD_LIBRARY_PATH at runtime.
45 "--enable-new-dtags"
46
47 # By default binutils searches $libdir for libraries. This brings in
48 # libbfd and libopcodes into a default visibility. Drop default lib
49 # path to force users to declare their use of these libraries.
50 "--with-lib-path=:"
51 ];
52in
53bash.runCommand "${pname}-${version}" {
54 inherit pname version;
55
56 nativeBuildInputs = [
57 tinycc.compiler
58 gnumake
59 gnupatch
60 gnused
61 gnugrep
62 gawk
63 diffutils
64 gnutar
65 xz
66 ];
67
68 passthru.tests.get-version = result:
69 bash.runCommand "${pname}-get-version-${version}" {} ''
70 ${result}/bin/ld --version
71 mkdir $out
72 '';
73
74 meta = with lib; {
75 description = "Tools for manipulating binaries (linker, assembler, etc.)";
76 homepage = "https://www.gnu.org/software/binutils";
77 license = licenses.gpl3Plus;
78 maintainers = teams.minimal-bootstrap.members;
79 platforms = platforms.unix;
80 };
81} ''
82 # Unpack
83 cp ${src} binutils.tar.xz
84 unxz binutils.tar.xz
85 tar xf binutils.tar
86 rm binutils.tar
87 cd binutils-${version}
88
89 # Patch
90 ${lib.concatMapStringsSep "\n" (f: "patch -Np1 -i ${f}") patches}
91 sed -i 's|/bin/sh|${bash}/bin/bash|' \
92 missing install-sh mkinstalldirs
93 # see libtool's 74c8993c178a1386ea5e2363a01d919738402f30
94 sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh
95 # alias makeinfo to true
96 mkdir aliases
97 ln -s ${coreutils}/bin/true aliases/makeinfo
98 export PATH="$(pwd)/aliases/:$PATH"
99
100 # Configure
101 export CC="tcc -B ${tinycc.libs}/lib"
102 export AR="tcc -ar"
103 export lt_cv_sys_max_cmd_len=32768
104 export CFLAGS="-D__LITTLE_ENDIAN__=1"
105 bash ./configure ${lib.concatStringsSep " " configureFlags}
106
107 # Build
108 make -j $NIX_BUILD_CORES all-libiberty all-gas all-bfd all-libctf all-zlib all-gprof
109 make all-ld # race condition on ld/.deps/ldwrite.Po, serialize
110 make -j $NIX_BUILD_CORES
111
112 # Install
113 make -j $NIX_BUILD_CORES install
114''