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