Skip to content

Commit

Permalink
Clean up cross bootstrapping
Browse files Browse the repository at this point in the history
For a long time, we've had `crossLibcStdenv`, `*Cross` libc attributes,
and `*bsdCross` pre-libc package sets. This was always bad because
having "cross" things is "not declarative": the naming doesn't reflect
what packages *need* but rather how we *provide* something. This is
ugly, and creates needless friction between cross and native building.

Now, almost all of these `*Cross` attributes are gone: just these are
kept:

- Glibc's and Musl's are kept, because those packages are widely used
  and I didn't want to risk changing the native builds of those at this
  time.

- generic `libcCross`, `theadsCross`, and friends, because these relate
  to the convolulted GCC bootstrap which still needs to be redone.

The BSD and obscure Linux or freestnanding libcs have conversely all
been made to use a new `stdenvNoLibc`, which is like the old
`crossLibcStdenv` except:

1. It usable for native and cross alike

2. It named according to what it *is* ("a standard environment without
   libc but with a C compiler"), rather than some non-compositional
   jargon ("the stdenv used for building libc when cross compiling",
   yuck).

I should have done this change long ago, but I was stymied because of
"infinite recursions". The problem was that in too many cases we are
overriding `stdenv` to *remove* things we don't need, and this risks
cyles since those more minimal stdenvs are used to build things in the
more maximal stdenvs.

The solution is to pass `stage.nix` `stdenvNoCC`, so we can override to
*build up* rather than *tear down*. For now, the full `stdenv` is also
passed, so I don't need to change the native bootstraps, but I can see
this changing as we make things more uniform and clean those up.

(adapted from commit 51f1eca)
(adapted from commit 1743662)
  • Loading branch information
Ericson2314 committed Sep 6, 2024
1 parent 1fc1202 commit 5f134ec
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 101 deletions.
8 changes: 4 additions & 4 deletions pkgs/build-support/bintools-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
, stdenvNoCC
, runtimeShell
, bintools ? null, libc ? null, coreutils ? null, gnugrep ? null
, netbsd ? null, netbsdCross ? null
, netbsd ? null
, sharedLibraryLoader ?
if libc == null then
null
else if stdenvNoCC.targetPlatform.isNetBSD then
if !(targetPackages ? netbsdCross) then
if !(targetPackages ? netbsd) then
netbsd.ld_elf_so
else if libc != targetPackages.netbsdCross.headers then
targetPackages.netbsdCross.ld_elf_so
else if libc != targetPackages.netbsd.headers then
targetPackages.netbsd.ld_elf_so
else
null
else
Expand Down
3 changes: 2 additions & 1 deletion pkgs/by-name/uc/uclibc-ng/package.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ lib
, stdenv
, stdenvNoLibc
, buildPackages
, fetchurl
, gitUpdater
Expand All @@ -9,6 +9,7 @@
}:

let
stdenv = stdenvNoLibc;
isCross = (stdenv.buildPlatform != stdenv.hostPlatform);
configParser = ''
function parseconfig {
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/misc/or1k/newlib.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ stdenv, fetchFromGitHub, crossLibcStdenv, buildPackages }:
{ stdenv, fetchFromGitHub, stdenvNoLibc, buildPackages }:

crossLibcStdenv.mkDerivation {
stdenvNoLibc.mkDerivation {
name = "newlib";
src = fetchFromGitHub {
owner = "openrisc";
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/misc/vc4/newlib.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ stdenv, texinfo, flex, bison, fetchFromGitHub, crossLibcStdenv, buildPackages }:
{ stdenv, texinfo, flex, bison, fetchFromGitHub, stdenvNoLibc, buildPackages }:

crossLibcStdenv.mkDerivation {
stdenvNoLibc.mkDerivation {
name = "newlib";
src = fetchFromGitHub {
owner = "itszor";
Expand Down
4 changes: 2 additions & 2 deletions pkgs/os-specific/bsd/freebsd/pkgs/mkDerivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
lib,
stdenv,
stdenvNoCC,
stdenvNoLibs,
stdenvNoLibc,
overrideCC,
buildPackages,
stdenvNoLibcxx ? overrideCC stdenv buildPackages.llvmPackages.clangNoLibcxx,
Expand All @@ -28,7 +28,7 @@ lib.makeOverridable (
if attrs.noCC or false then
stdenvNoCC
else if attrs.noLibc or false then
stdenvNoLibs
stdenvNoLibc
else if attrs.noLibcxx or false then
stdenvNoLibcxx
else
Expand Down
4 changes: 2 additions & 2 deletions pkgs/os-specific/bsd/netbsd/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
lib,
crossLibcStdenv,
stdenvNoLibc,
stdenvNoCC,
makeScopeWithSplicing',
generateSplicesForMkScope,
Expand Down Expand Up @@ -37,7 +37,7 @@ makeScopeWithSplicing' {

compatIfNeeded = lib.optional (!stdenvNoCC.hostPlatform.isNetBSD) self.compat;

stdenvLibcMinimal = crossLibcStdenv.override (old: {
stdenvLibcMinimal = stdenvNoLibc.override (old: {
cc = old.cc.override {
libc = self.libcMinimal;
noLibc = false;
Expand Down
4 changes: 2 additions & 2 deletions pkgs/os-specific/bsd/netbsd/pkgs/mkDerivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
lib,
stdenv,
stdenvNoCC,
crossLibcStdenv,
stdenvNoLibc,
stdenvLibcMinimal,
runCommand,
rsync,
Expand All @@ -28,7 +28,7 @@ lib.makeOverridable (
if attrs.noCC or false then
stdenvNoCC
else if attrs.noLibc or false then
crossLibcStdenv
stdenvNoLibc
else if attrs.libcMinimal or false then
stdenvLibcMinimal
else
Expand Down
4 changes: 2 additions & 2 deletions pkgs/os-specific/bsd/openbsd/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
lib,
crossLibcStdenv,
stdenvNoLibc,
makeScopeWithSplicing',
generateSplicesForMkScope,
buildPackages,
Expand All @@ -22,7 +22,7 @@ makeScopeWithSplicing' {
// {
version = "7.5";

stdenvLibcMinimal = crossLibcStdenv.override (old: {
stdenvLibcMinimal = stdenvNoLibc.override (old: {
cc = old.cc.override {
libc = self.libcMinimal;
noLibc = false;
Expand Down
4 changes: 2 additions & 2 deletions pkgs/os-specific/bsd/openbsd/pkgs/libcMinimal/package.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
lib,
crossLibcStdenv,
stdenvNoLibc,
mkDerivation,
fetchpatch,
bsdSetupHook,
Expand Down Expand Up @@ -65,7 +65,7 @@ mkDerivation {
# Suppress lld >= 16 undefined version errors
# https://github.com/freebsd/freebsd-src/commit/2ba84b4bcdd6012e8cfbf8a0d060a4438623a638
env.NIX_LDFLAGS = lib.optionalString (
crossLibcStdenv.hostPlatform.linker == "lld"
stdenvNoLibc.hostPlatform.linker == "lld"
) "--undefined-version";

makeFlags = [
Expand Down
4 changes: 2 additions & 2 deletions pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
lib,
stdenv,
stdenvNoCC,
crossLibcStdenv,
stdenvNoLibc,
stdenvLibcMinimal,
runCommand,
rsync,
Expand All @@ -22,7 +22,7 @@ lib.makeOverridable (
if attrs.noCC or false then
stdenvNoCC
else if attrs.noLibc or false then
crossLibcStdenv
stdenvNoLibc
else if attrs.libcMinimal or false then
stdenvLibcMinimal
else
Expand Down
6 changes: 3 additions & 3 deletions pkgs/os-specific/windows/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ lib, stdenv, buildPackages
, newScope, overrideCC, crossLibcStdenv, libcCross
, newScope, overrideCC, stdenvNoLibc, libcCross
}:

lib.makeScope newScope (self: with self; {
Expand All @@ -14,11 +14,11 @@ lib.makeScope newScope (self: with self; {
mingw_runtime = mingwrt;

mingw_w64 = callPackage ./mingw-w64 {
stdenv = crossLibcStdenv;
stdenv = stdenvNoLibc;
};

# FIXME untested with llvmPackages_16 was using llvmPackages_8
crossThreadsStdenv = overrideCC crossLibcStdenv
crossThreadsStdenv = overrideCC stdenvNoLibc
(if stdenv.hostPlatform.useLLVM or false
then buildPackages.llvmPackages.clangNoLibcxx
else buildPackages.gccWithoutTargetLibc.override (old: {
Expand Down
56 changes: 33 additions & 23 deletions pkgs/stdenv/cross/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,44 @@ in lib.init bootStages ++ [
if crossSystem.isStatic
then buildPackages.stdenvAdapters.makeStatic
else lib.id;
stdenvNoCC = adaptStdenv (buildPackages.stdenv.override (old: rec {
buildPlatform = localSystem;
hostPlatform = crossSystem;
targetPlatform = crossSystem;

# Prior overrides are surely not valid as packages built with this run on
# a different platform, and so are disabled.
overrides = _: _: {};
extraBuildInputs = [ ]; # Old ones run on wrong platform
allowedRequisites = null;

cc = null;
hasCC = false;

extraNativeBuildInputs = old.extraNativeBuildInputs
++ lib.optionals
(hostPlatform.isLinux && !buildPlatform.isLinux)
[ buildPackages.patchelf ]
++ lib.optional
(let f = p: !p.isx86 || builtins.elem p.libc [ "musl" "wasilibc" "relibc" ] || p.isiOS || p.isGenode;
in f hostPlatform && !(f buildPlatform) )
buildPackages.updateAutotoolsGnuConfigScriptsHook
;
}));
in {
inherit config;
overlays = overlays ++ crossOverlays;
selfBuild = false;
inherit stdenvNoCC;
stdenv = let
baseStdenv = adaptStdenv (buildPackages.stdenv.override (old: rec {
buildPlatform = localSystem;
hostPlatform = crossSystem;
targetPlatform = crossSystem;

# Prior overrides are surely not valid as packages built with this run on
# a different platform, and so are disabled.
overrides = _: _: {};
extraBuildInputs = [ ] # Old ones run on wrong platform
++ lib.optionals hostPlatform.isDarwin [ buildPackages.targetPackages.darwin.apple_sdk.frameworks.CoreFoundation ]
;
allowedRequisites = null;
inherit (stdenvNoCC) hostPlatform targetPlatform;
baseStdenv = stdenvNoCC.override {
# Old ones run on wrong platform
extraBuildInputs = lib.optionals hostPlatform.isDarwin [
buildPackages.targetPackages.darwin.apple_sdk.frameworks.CoreFoundation
];

hasCC = !targetPlatform.isGhcjs;
hasCC = !stdenvNoCC.targetPlatform.isGhcjs;

cc = if crossSystem.useiOSPrebuilt or false
then buildPackages.darwin.iosSdkPkgs.clang
Expand All @@ -81,16 +100,7 @@ in lib.init bootStages ++ [
then buildPackages.arocc
else buildPackages.gcc;

extraNativeBuildInputs = old.extraNativeBuildInputs
++ lib.optionals
(hostPlatform.isLinux && !buildPlatform.isLinux)
[ buildPackages.patchelf ]
++ lib.optional
(let f = p: !p.isx86 || builtins.elem p.libc [ "musl" "wasilibc" "relibc" ] || p.isiOS || p.isGenode;
in f hostPlatform && !(f buildPlatform) )
buildPackages.updateAutotoolsGnuConfigScriptsHook
;
}));
};
in if config ? replaceCrossStdenv then config.replaceCrossStdenv { inherit buildPackages baseStdenv; } else baseStdenv;
})

Expand Down
2 changes: 2 additions & 0 deletions pkgs/stdenv/darwin/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ assert bootstrapTools.passthru.isFromBootstrapFiles or false; # sanity check

# libc++, and libc++abi do not need CoreFoundation. Avoid propagating the CF from prior
# stages to the final stdenv via rpath by dropping it from `extraBuildInputs`.
stdenvNoCC = super.stdenvNoCC.override { extraBuildInputs = [ ]; };
stdenvNoCF = self.stdenv.override { extraBuildInputs = [ ]; };

libcxxBootstrapStdenv = self.overrideCC stdenvNoCF (
Expand Down Expand Up @@ -1543,6 +1544,7 @@ assert bootstrapTools.passthru.isFromBootstrapFiles or false; # sanity check

# Don’t link anything in this stage against CF to prevent propagating CF from prior stages to
# the final stdenv, which happens because of the rpath hook.
stdenvNoCC = super.stdenvNoCC.override { extraBuildInputs = [ ]; };
stdenv =
let
stdenvNoCF = super.stdenv.override { extraBuildInputs = [ ]; };
Expand Down
13 changes: 10 additions & 3 deletions pkgs/top-level/aliases.nix
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mapAliases ({
auditBlasHook = throw "'auditBlasHook' has been removed since it never worked"; # Added 2024-04-02
authy = throw "'authy' has been removed since it reached end of life"; # Added 2024-04-19
avldrums-lv2 = x42-avldrums; # Added 2020-03-29
avrlibcCross = avrlibc; # Added 2024-09-06
awesome-4-0 = awesome; # Added 2022-05-05
aws-env = throw "aws-env has been removed as the upstream project was unmaintained"; # Added 2024-06-11
aws-google-auth = throw "aws-google-auth has been removed as the upstream project was unmaintained"; # Added 2024-07-31
Expand Down Expand Up @@ -251,6 +252,7 @@ mapAliases ({
cosmic-tasks = tasks; # Added 2024-07-04
cpp-ipfs-api = cpp-ipfs-http-client; # Project has been renamed. Added 2022-05-15
crispyDoom = crispy-doom; # Added 2023-05-01
crossLibcStdenv = stdenvNoLibc; # Added 2024-09-06
cryptowatch-desktop = throw "Cryptowatch Desktop was sunset on September 30th 2023 and has been removed from nixpkgs"; # Added 2023-12-22
clash = throw "'clash' has been removed, upstream gone. Consider using 'mihomo' instead."; # added 2023-11-10
clasp = clingo; # added 2022-12-22
Expand Down Expand Up @@ -979,6 +981,7 @@ mapAliases ({
mpd_clientlib = libmpdclient; # Added 2021-02-11
mpdevil = plattenalbum; # Added 2024-05-22
mpg321 = throw "'mpg321' has been removed due to it being unmaintained by upstream. Consider using mpg123 instead."; # Added 2024-05-10
msp430NewlibCross = msp430Newlib; # Added 2024-09-06
mumble_git = throw "'mumble_git' has been renamed to/replaced by 'pkgs.mumble'"; # Converted to throw 2023-09-10
murmur_git = throw "'murmur_git' has been renamed to/replaced by 'pkgs.murmur'"; # Converted to throw 2023-09-10
mutt-with-sidebar = mutt; # Added 2022-09-17
Expand Down Expand Up @@ -1010,6 +1013,8 @@ mapAliases ({
nagiosPluginsOfficial = monitoring-plugins;
neochat = libsForQt5.kdeGear.neochat; # added 2022-05-10
neoload = throw "'neoload' has been removed as it is broken and unmaintained"; # Added 2024-03-02
newlibCross = newlib; # Added 2024-09-06
newlib-nanoCross = newlib-nano; # Added 2024-09-06
nitrokey-udev-rules = libnitrokey; # Added 2023-03-25
nix-direnv-flakes = nix-direnv;
nix-repl = throw (
Expand Down Expand Up @@ -1487,6 +1492,8 @@ mapAliases ({
uberwriter = apostrophe; # Added 2020-04-23
ubootBeagleboneBlack = ubootAmx335xEVM; # Added 2020-01-21
ubuntu_font_family = ubuntu-classic; # Added 2024-02-19
uclibc = uclibc-ng; # Added 2022-06-16
uclibcCross = uclibc-ng; # Added 2022-06-16
ue4 = throw "ue4 has been removed, because the package was broken for years"; # Added 2023-11-22
uefi-firmware-parser = throw "The uefi-firmware-parser package was dropped since it was unmaintained."; # Added 2024-06-21
uhd3_5 = throw "uhd3_5 has been removed, because it was no longer needed"; # Added 2023-10-07
Expand Down Expand Up @@ -1636,9 +1643,9 @@ mapAliases ({
inherit (stdenv.hostPlatform) system; # Added 2021-10-22
inherit (stdenv) buildPlatform hostPlatform targetPlatform; # Added 2023-01-09

freebsdCross = freebsd; # Added 2024-06-18
netbsdCross = netbsd; # Added 2024-06-18
openbsdCross = openbsd; # Added 2024-06-18
freebsdCross = freebsd; # Added 2024-09-06
netbsdCross = netbsd; # Added 2024-09-06
openbsdCross = openbsd; # Added 2024-09-06

# LLVM packages for (integration) testing that should not be used inside Nixpkgs:
llvmPackages_latest = llvmPackages_18;
Expand Down
Loading

0 comments on commit 5f134ec

Please sign in to comment.