summaryrefslogtreecommitdiff
path: root/pkgs/os-specific/linux/minimal-bootstrap/binutils/default.nix
blob: 4ba20f13192e9313c06755327666f51546a26698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
  lib,
  buildPlatform,
  hostPlatform,
  fetchurl,
  bash,
  coreutils,
  gnumake,
  gnupatch,
  gnused,
  gnugrep,
  gawk,
  diffutils,
  gnutar,
  xz,
  tinycc,
}:

let
  # Based on https://github.com/ZilchOS/bootstrap-from-tcc/blob/2e0c68c36b3437386f786d619bc9a16177f2e149/using-nix/2a1-static-binutils.nix
  inherit (import ./common.nix { inherit lib; }) meta;
  pname = "binutils";
  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
    # Fix __attribute__, to fix mmap-related assertion failures.
    ./fix-tinycc-attribute.patch
  ];

  configureFlags = [
    "--prefix=${placeholder "out"}"
    "--build=${buildPlatform.config}"
    "--host=${hostPlatform.config}"
    "--with-sysroot=/"
    "--disable-dependency-tracking"
    "--disable-nls"
    "--enable-deterministic-archives"
    # depends on bison
    "--disable-gprofng"

    # 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=:"
  ];
in
bash.runCommand "${pname}-${version}"
  {
    inherit pname version meta;

    nativeBuildInputs = [
      tinycc.compiler
      gnumake
      gnupatch
      gnused
      gnugrep
      gawk
      diffutils
      gnutar
      xz
    ];

    passthru.tests.get-version =
      result:
      bash.runCommand "${pname}-get-version-${version}" { } ''
        ${result}/bin/ld --version
        mkdir $out
      '';
  }
  ''
    # Unpack
    cp ${src} binutils.tar.xz
    unxz binutils.tar.xz
    tar xf binutils.tar
    rm binutils.tar
    cd binutils-${version}

    # Patch
    ${lib.concatMapStringsSep "\n" (f: "patch -Np1 -i ${f}") patches}
    sed -i 's|/bin/sh|${bash}/bin/bash|' \
      missing install-sh mkinstalldirs
    # see libtool's 74c8993c178a1386ea5e2363a01d919738402f30
    sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh
    # alias makeinfo to true
    mkdir aliases
    ln -s ${coreutils}/bin/true aliases/makeinfo
    export PATH="$(pwd)/aliases/:$PATH"

    # Configure
    export CC="tcc -B ${tinycc.libs}/lib"
    export AR="tcc -ar"
    export lt_cv_sys_max_cmd_len=32768

    export CFLAGS="-D__LITTLE_ENDIAN__=1"
    bash ./configure ${lib.concatStringsSep " " configureFlags}

    # Build
    make -j $NIX_BUILD_CORES all-libiberty all-gas all-bfd all-libctf all-zlib all-gprof
    make all-ld # race condition on ld/.deps/ldwrite.Po, serialize
    make -j $NIX_BUILD_CORES

    # Install
    make -j $NIX_BUILD_CORES install

    # gprof/addr2line/elfedit + man pages are unused downstream.
    rm -f $out/bin/gprof $out/bin/addr2line $out/bin/elfedit
    rm -rf $out/share/info $out/share/man
  ''