summary refs log tree commit diff
path: root/pkgs/stdenv/linux/default.nix
blob: a2a971589470b228d1887d468b3b36507d7f9e7e (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# This file constructs the standard build environment for the
# Linux/i686 platform.  It's completely pure; that is, it relies on no
# external (non-Nix) tools, such as /usr/bin/gcc, and it contains a C
# compiler and linker that do not search in default locations,
# ensuring purity of components produced by it.

# The function defaults are for easy testing.
{ system ? builtins.currentSystem
, allPackages ? import ../../top-level/all-packages.nix
, platform ? null, config ? {}, lib }:

rec {

  bootstrapFiles =
    if system == "i686-linux" then import ./bootstrap/i686.nix
    else if system == "x86_64-linux" then import ./bootstrap/x86_64.nix
    else if system == "armv5tel-linux" then import ./bootstrap/armv5tel.nix
    else if system == "armv6l-linux" then import ./bootstrap/armv6l.nix
    else if system == "armv7l-linux" then import ./bootstrap/armv6l.nix
    else if system == "mips64el-linux" then import ./bootstrap/loongson2f.nix
    else abort "unsupported platform for the pure Linux stdenv";


  commonPreHook =
    ''
      export NIX_ENFORCE_PURITY=1
      ${if system == "x86_64-linux" then "NIX_LIB64_IN_SELF_RPATH=1" else ""}
      ${if system == "mips64el-linux" then "NIX_LIB32_IN_SELF_RPATH=1" else ""}
    '';


  # The bootstrap process proceeds in several steps.


  # 1) Create a standard environment by downloading pre-built binaries
  # of coreutils, GCC, etc.


  # Download and unpack the bootstrap tools (coreutils, GCC, Glibc, ...).
  bootstrapTools = derivation {
    name = "bootstrap-tools";

    builder = bootstrapFiles.sh;

    args =
      if system == "armv5tel-linux" || system == "armv6l-linux"
        || system == "armv7l-linux"
      then [ ./scripts/unpack-bootstrap-tools-arm.sh ]
      else [ ./scripts/unpack-bootstrap-tools.sh ];

    # FIXME: get rid of curl.
    inherit (bootstrapFiles) bzip2 mkdir curl cpio;

    tarball = import <nix/fetchurl.nix> {
      inherit (bootstrapFiles.bootstrapTools) url sha256;
    };

    inherit system;

    # Needed by the GCC wrapper.
    langC = true;
    langCC = true;
  };


  # This function builds the various standard environments used during
  # the bootstrap.
  stdenvBootFun =
    { gcc, extraAttrs ? {}, overrides ? (pkgs: {}), extraBuildInputs ? [], fetchurl }:

    import ../generic {
      inherit system config extraBuildInputs;
      name = "stdenv-linux-boot";
      preHook =
        ''
          # Don't patch #!/interpreter because it leads to retained
          # dependencies on the bootstrapTools in the final stdenv.
          dontPatchShebangs=1
          ${commonPreHook}
        '';
      shell = "${bootstrapTools}/bin/sh";
      initialPath = [ bootstrapTools ];
      fetchurlBoot = fetchurl;
      inherit gcc;
      # Having the proper 'platform' in all the stdenvs allows getting proper
      # linuxHeaders for example.
      extraAttrs = extraAttrs // { inherit platform; };
      overrides = pkgs: (overrides pkgs) // {
        inherit fetchurl;
      };
    };

  # Build a dummy stdenv with no GCC or working fetchurl.  This is
  # because we need a stdenv to build the GCC wrapper and fetchurl.
  stdenvLinuxBoot0 = stdenvBootFun {
    gcc = "/no-such-path";
    fetchurl = null;
  };


  fetchurl = import ../../build-support/fetchurl {
    stdenv = stdenvLinuxBoot0;
    curl = bootstrapTools;
  };


  # The Glibc include directory cannot have the same prefix as the GCC
  # include directory, since GCC gets confused otherwise (it will
  # search the Glibc headers before the GCC headers).  So create a
  # dummy Glibc.
  bootstrapGlibc = stdenvLinuxBoot0.mkDerivation {
    name = "bootstrap-glibc";
    buildCommand = ''
      mkdir -p $out
      ln -s ${bootstrapTools}/lib $out/lib
      ln -s ${bootstrapTools}/include-glibc $out/include
    '';
  };


  # A helper function to call gcc-wrapper.
  wrapGCC =
    { gcc ? bootstrapTools, libc, binutils, coreutils, shell ? "", name ? "bootstrap-gcc-wrapper" }:

    lib.makeOverridable (import ../../build-support/gcc-wrapper) {
      nativeTools = false;
      nativeLibc = false;
      inherit gcc binutils coreutils libc shell name;
      stdenv = stdenvLinuxBoot0;
    };


  # Create the first "real" standard environment.  This one consists
  # of bootstrap tools only, and a minimal Glibc to keep the GCC
  # configure script happy.
  stdenvLinuxBoot1 = stdenvBootFun {
    gcc = wrapGCC {
      libc = bootstrapGlibc;
      binutils = bootstrapTools;
      coreutils = bootstrapTools;
    };
    inherit fetchurl;
  };


  # 2) These are the packages that we can build with the first
  #    stdenv.  We only need binutils, because recent Glibcs
  #    require recent Binutils, and those in bootstrap-tools may
  #    be too old.
  stdenvLinuxBoot1Pkgs = allPackages {
    inherit system platform;
    bootStdenv = stdenvLinuxBoot1;
  };

  binutils1 = stdenvLinuxBoot1Pkgs.binutils.override { gold = false; };


  # 3) 2nd stdenv that we will use to build only Glibc.
  stdenvLinuxBoot2 = stdenvBootFun {
    gcc = wrapGCC {
      libc = bootstrapGlibc;
      binutils = binutils1;
      coreutils = bootstrapTools;
    };
    overrides = pkgs: {
      inherit (stdenvLinuxBoot1Pkgs) perl;
    };
    inherit fetchurl;
  };


  # 4) These are the packages that we can build with the 2nd
  #    stdenv.
  stdenvLinuxBoot2Pkgs = allPackages {
    inherit system platform;
    bootStdenv = stdenvLinuxBoot2;
  };


  # 5) Build Glibc with the bootstrap tools.  The result is the full,
  #    dynamically linked, final Glibc.
  stdenvLinuxGlibc = stdenvLinuxBoot2Pkgs.glibc;


  # 6) Construct a third stdenv identical to the 2nd, except that this
  #    one uses the Glibc built in step 3.  It still uses the recent
  #    binutils and the rest of the bootstrap tools, including GCC.
  stdenvLinuxBoot3 = stdenvBootFun {
    gcc = wrapGCC {
      binutils = binutils1;
      coreutils = bootstrapTools;
      libc = stdenvLinuxGlibc;
    };
    overrides = pkgs: {
      glibc = stdenvLinuxGlibc;
      inherit (stdenvLinuxBoot1Pkgs) perl;
      # Link GCC statically against GMP etc.  This makes sense because
      # these builds of the libraries are only used by GCC, so it
      # reduces the size of the stdenv closure.
      gmp = pkgs.gmp.override { stdenv = pkgs.makeStaticLibraries pkgs.stdenv; };
      mpfr = pkgs.mpfr.override { stdenv = pkgs.makeStaticLibraries pkgs.stdenv; };
      mpc = pkgs.mpc.override { stdenv = pkgs.makeStaticLibraries pkgs.stdenv; };
      isl = pkgs.isl.override { stdenv = pkgs.makeStaticLibraries pkgs.stdenv; };
      cloog = pkgs.cloog.override { stdenv = pkgs.makeStaticLibraries pkgs.stdenv; };
      ppl = pkgs.ppl.override { stdenv = pkgs.makeStaticLibraries pkgs.stdenv; };
    };
    extraAttrs = {
      glibc = stdenvLinuxGlibc; # Required by gcc47 build
    };
    extraBuildInputs = [ stdenvLinuxBoot2Pkgs.patchelf stdenvLinuxBoot1Pkgs.paxctl ];
    inherit fetchurl;
  };


  # 7) The packages that can be built using the third stdenv.
  stdenvLinuxBoot3Pkgs = allPackages {
    inherit system platform;
    bootStdenv = stdenvLinuxBoot3;
  };


  # 8) Construct a fourth stdenv identical to the second, except that
  #    this one uses the new GCC from step 7.  The other tools
  #    (e.g. coreutils) are still from the bootstrap tools.
  stdenvLinuxBoot4 = stdenvBootFun {
    gcc = wrapGCC rec {
      binutils = binutils1;
      coreutils = bootstrapTools;
      libc = stdenvLinuxGlibc;
      gcc = stdenvLinuxBoot3Pkgs.gcc.gcc;
      name = "";
    };
    extraBuildInputs = [ stdenvLinuxBoot2Pkgs.patchelf stdenvLinuxBoot3Pkgs.xz.bin ];
    overrides = pkgs: {
      inherit (stdenvLinuxBoot1Pkgs) perl;
      inherit (stdenvLinuxBoot3Pkgs) gettext gnum4 gmp;
    };
    inherit fetchurl;
  };


  # 9) The packages that can be built using the fourth stdenv.
  stdenvLinuxBoot4Pkgs = allPackages {
    inherit system platform;
    bootStdenv = stdenvLinuxBoot4;
  };


  # 10) Construct the final stdenv.  It uses the Glibc and GCC, and
  #     adds in a new binutils that doesn't depend on bootstrap-tools,
  #     as well as dynamically linked versions of all other tools.
  #
  #     When updating stdenvLinux, make sure that the result has no
  #     dependency (`nix-store -qR') on bootstrapTools or the
  #     first binutils built.
  stdenvLinux = import ../generic rec {
    inherit system config;

    preHook =
      ''
        # Make "strip" produce deterministic output, by setting
        # timestamps etc. to a fixed value.
        commonStripFlags="--enable-deterministic-archives"
        ${commonPreHook}
      '';

    initialPath =
      ((import ../common-path.nix) {pkgs = stdenvLinuxBoot4Pkgs;});

    extraBuildInputs =
      [ stdenvLinuxBoot4Pkgs.patchelf stdenvLinuxBoot4Pkgs.paxctl ];

    gcc = wrapGCC rec {
      inherit (stdenvLinuxBoot4Pkgs) binutils coreutils;
      libc = stdenvLinuxGlibc;
      gcc = stdenvLinuxBoot4.gcc.gcc;
      shell = stdenvLinuxBoot4Pkgs.bash + "/bin/bash";
      name = "";
    };

    shell = stdenvLinuxBoot4Pkgs.bash + "/bin/bash";

    fetchurlBoot = fetchurl;

    extraAttrs = {
      glibc = stdenvLinuxGlibc;
      inherit platform bootstrapTools;
      shellPackage = stdenvLinuxBoot4Pkgs.bash;
    };

    overrides = pkgs: {
      inherit gcc;
      inherit (stdenvLinuxBoot3Pkgs) glibc;
      inherit (stdenvLinuxBoot4Pkgs) binutils;
      inherit (stdenvLinuxBoot4Pkgs)
        gzip bzip2 xz bash coreutils diffutils findutils gawk
        gnumake gnused gnutar gnugrep gnupatch patchelf
        attr acl pcre paxctl;
    };
  };

}