diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix index abc88910c36d2fc..d97f86327c79ad2 100644 --- a/pkgs/build-support/cc-wrapper/default.nix +++ b/pkgs/build-support/cc-wrapper/default.nix @@ -331,11 +331,18 @@ stdenv.mkDerivation { # compile, because it uses "#include_next " to find the # limits.h file in ../includes-fixed. To remedy the problem, # another -idirafter is necessary to add that directory again. + # + # We use --sysroot=/nix/store/does/not/exist to drop embedded default + # path to glibc headers gcc was built against. Without it -idirafter + # only appends to the list and outdated glibc headers end up being + # used. 'cc-cflags-before' is used to allow user's --sysroot= option + # to override our default. + optionalString (libc != null) ('' touch "$out/nix-support/libc-cflags" touch "$out/nix-support/libc-ldflags" echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags '' + optionalString (!(cc.langD or false)) '' + echo "--sysroot=/nix/store/does/not/exist" >> $out/nix-support/cc-cflags-before echo "-idirafter ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags '' + optionalString (isGNU && (!(cc.langD or false))) '' for dir in "${cc}"/lib/gcc/*/*/include-fixed; do diff --git a/pkgs/stdenv/linux/default.nix b/pkgs/stdenv/linux/default.nix index 870fb04c3883fee..50c1c5130cf1ab7 100644 --- a/pkgs/stdenv/linux/default.nix +++ b/pkgs/stdenv/linux/default.nix @@ -9,14 +9,11 @@ # is used to build all other packages (including the bootstrapFiles). # # Goals of the bootstrap process: -# 1. final stdenv must not reference any of the bootstrap files. -# 2. final stdenv must not contain any of the bootstrap files -# (the only current violation is libgcc_s.so in glibc). +# 1. final stdenv must not reference any of the bootstrap packages. +# 2. final stdenv must not contain any of the bootstrap files. # 3. final stdenv must not contain any of the files directly # generated by the bootstrap code generators (assembler, linker, -# compiler). The only current violations are: libgcc_s.so in glibc, -# the lib{mpfr,mpc,gmp,isl} which are statically linked -# into the final gcc). +# compiler). # # These goals ensure that final packages and final stdenv are built # exclusively using nixpkgs package definitions and don't depend @@ -109,12 +106,13 @@ let ''; - # The bootstrap process proceeds in several steps. - - - # Create a standard environment by downloading pre-built binaries of - # coreutils, GCC, etc. - + # The bootstrap process proceeds in several steps. Our high-level plan + # is the following: + # + # - build binutils with bootstrapTools + # - build gcc with bootstrapTools + # - build glibc with bootstrapTools + gcc from nixpkgs (embed libgcc_s.so.1) + # - build gcc with nixpkgs toolchain # Download and unpack the bootstrap tools (coreutils, GCC, Glibc, ...). bootstrapTools = import (if localSystem.libc == "musl" then ./bootstrap-tools-musl else ./bootstrap-tools) { @@ -284,21 +282,63 @@ in }; }) - # 2nd stdenv that contains our own rebuilt binutils and is used for - # compiling our own Glibc. + # compiling our own gcc. # # resulting stage2 stdenv: - # - coreutils, glibc, gcc: from bootstrapFiles - # - binutils: from nixpkgs, built by bootstrapFiles toolchain + # - coreutils, glibc: from bootstrapFiles + # - binutils, gcc: from nixpkgs, built by bootstrapFiles toolchain (prevStage: stageFun prevStage { name = "bootstrap-stage2"; overrides = self: super: { inherit (prevStage) ccWrapperStdenv - gcc-unwrapped coreutils gnugrep - perl gnum4 bison; + bash bison gnum4 coreutils gnugrep + patchelf perl texinfo xz; + + ${localSystem.libc} = getLibc prevStage; + + gcc-unwrapped = + let makeStaticLibrariesAndMark = pkg: + lib.makeOverridable (pkg.override { stdenv = self.makeStaticLibraries self.stdenv; }) + .overrideAttrs (a: { pname = "${a.pname}-stage2"; }); + in super.gcc-unwrapped.override { + # 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 = makeStaticLibrariesAndMark super.gmp; + mpfr = makeStaticLibrariesAndMark super.mpfr; + libmpc = makeStaticLibrariesAndMark super.libmpc; + isl = makeStaticLibrariesAndMark super.isl_0_20; + # Use a deterministically built compiler + # see https://github.com/NixOS/nixpkgs/issues/108475 for context + reproducibleBuild = true; + profiledCompiler = false; + }; + }; + + # `libtool` comes with obsolete config.sub/config.guess that don't recognize Risc-V. + extraNativeBuildInputs = + lib.optional (localSystem.isRiscV) prevStage.updateAutotoolsGnuConfigScriptsHook; + }) + + # 3rd stdenv that contains our own rebuilt binutils and gcc and is + # used for compiling our own Glibc. + # + # resulting stage3 stdenv: + # - coreutils: from bootstrapFiles + # - glibc, binutils, gcc: from nixpkgs, built by bootstrapFiles toolchain + (prevStage: stageFun prevStage { + name = "bootstrap-stage3"; + + overrides = self: super: { + inherit (prevStage) + ccWrapperStdenv + gcc-unwrapped + bash bison coreutils gnum4 gnugrep + patchelf perl; + dejagnu = super.dejagnu.overrideAttrs (a: { doCheck = false; } ); # We need libidn2 and its dependency libunistring as glibc dependency. @@ -356,21 +396,24 @@ in }) - # Construct a third stdenv identical to the 2nd, except that this + # Construct a fourth stdenv identical to the 3rd, except that this # one uses the rebuilt Glibc from stage2. It still uses the recent # binutils and rest of the bootstrap tools, including GCC. # - # resulting stage3 stdenv: + # resulting stage4 stdenv: # - coreutils, gcc: from bootstrapFiles - # - glibc, binutils: from nixpkgs, built by bootstrapFiles toolchain + # - binutils, gcc: from nixpkgs, built by bootstrapFiles toolchain + # - glibc: from nixpkgs, built by nixpkgs toolchain (prevStage: stageFun prevStage { - name = "bootstrap-stage3"; + name = "bootstrap-stage4"; overrides = self: super: rec { inherit (prevStage) ccWrapperStdenv - binutils coreutils gnugrep - perl patchelf linuxHeaders gnum4 bison libidn2 libunistring; + bison binutils coreutils gnugrep gnum4 + patchelf perl texinfo xz + linuxHeaders libidn2 libunistring; + ${localSystem.libc} = getLibc prevStage; gcc-unwrapped = let makeStaticLibrariesAndMark = pkg: @@ -397,28 +440,27 @@ in }) - # Construct a fourth stdenv that uses the new GCC. But coreutils is + # Construct a fifth stdenv that uses the new GCC. But coreutils is # still from the bootstrap tools. # - # resulting stage4 stdenv: + # resulting stage5 stdenv: # - coreutils: from bootstrapFiles - # - glibc, binutils: from nixpkgs, built by bootstrapFiles toolchain - # - gcc: from nixpkgs, built by bootstrapFiles toolchain. Can assume - # it has almost no code from bootstrapTools as gcc bootstraps - # internally. The only exceptions are crt files from glibc - # built by bootstrapTools used to link executables and libraries, - # and the bootstrapTools-built, statically-linked - # lib{mpfr,mpc,gmp,isl}.a which are linked into the final gcc - # (see commit cfde88976ba4cddd01b1bb28b40afd12ea93a11d). + # - binutils: from nixpkgs, built by bootstrapFiles toolchain + # - glibc, gcc: from nixpkgs, built by nixpkgs toolchain (prevStage: stageFun prevStage { - name = "bootstrap-stage4"; + name = "bootstrap-stage5"; overrides = self: super: { # Zlib has to be inherited and not rebuilt in this stage, # because gcc (since JAR support) already depends on zlib, and # then if we already have a zlib we want to use that for the # other purposes (binutils and top-level pkgs) too. - inherit (prevStage) gettext gnum4 bison perl texinfo zlib linuxHeaders libidn2 libunistring; + inherit (prevStage) + bash gettext gnum4 bison + perl texinfo which + # xz - rebuild to pull in rebuilt version into final stdenv + zlib linuxHeaders libidn2 libunistring; + ${localSystem.libc} = getLibc prevStage; binutils = super.binutils.override { # Don't use stdenv's shell but our own @@ -468,15 +510,7 @@ in # binutils built. # # resulting stage5 (final) stdenv: - # - coreutils, binutils: from nixpkgs, built by nixpkgs toolchain - # - glibc: from nixpkgs, built by bootstrapFiles toolchain - # - gcc: from nixpkgs, built by bootstrapFiles toolchain. Can assume - # it has almost no code from bootstrapTools as gcc bootstraps - # internally. The only exceptions are crt files from glibc - # built by bootstrapTools used to link executables and libraries, - # and the bootstrapTools-built, statically-linked - # lib{mpfr,mpc,gmp,isl}.a which are linked into the final gcc - # (see commit cfde88976ba4cddd01b1bb28b40afd12ea93a11d). + # - coreutils, binutils, glibc, gcc: from nixpkgs, built by nixpkgs toolchain (prevStage: { inherit config overlays; stdenv = import ../generic rec {