Skip to content

Commit

Permalink
gcc: do not allow version skew when cross-building gcc
Browse files Browse the repository at this point in the history
When `gcc` is cross-built (`build` != `target` && `host` == `target`)
`gcc` assumes that it has a compatible cross-compiler in the environment
that can build target libraries. Version of a cross-compiler has to
match the compiler being cross-built as libraries frequently use fresh
compiler features, like `-std=c++26` or target-specific types like
`_Bfloat16`.

Version mismatch causes build failures like:

    #351905

Similar problems (but on a smaller scale) happen when a `gcc`
cross-compiler is built (`build` == `host` && `host` != `target`) built
by a mismatching version of a native compiler (`build` == `host` &&
`host` == `target`). That was worked around by forcing `gcc9Stdenv` for
older compiler versions.

Let's fix both problems by requiring the same compiler version for
cross-case.

Closes: #351905
  • Loading branch information
trofi committed Nov 1, 2024
1 parent 2315372 commit 028da88
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pkgs/development/compilers/gcc/all.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{ lib
, stdenv
, gccStdenv
, gcc9Stdenv
, pkgs
, callPackage
, isl_0_17, isl_0_20
, libcCross
Expand All @@ -15,6 +14,7 @@ let
versions = import ./versions.nix;
gccForMajorMinorVersion = majorMinorVersion:
let
majorVersion = lib.versions.major majorMinorVersion;
atLeast = lib.versionAtLeast majorMinorVersion;
attrName = "gcc${lib.replaceStrings ["."] [""] majorMinorVersion}";
pkg = lowPrio (wrapCC (callPackage ./default.nix ({
Expand All @@ -27,9 +27,25 @@ let
isl = if stdenv.hostPlatform.isDarwin then null
else if atLeast "9" then isl_0_20
else /* atLeast "7" */ isl_0_17;
} // lib.optionalAttrs (!(atLeast "9")) {
# gcc 10 is too strict to cross compile gcc <= 8
stdenv = if (stdenv.targetPlatform != stdenv.buildPlatform) && stdenv.cc.isGNU then gcc9Stdenv else stdenv;
# do not allow version skew when cross-building gcc
#
# When `gcc` is cross-built (`build` != `target` && `host` == `target`)
# `gcc` assumes that it has a compatible cross-compiler in the environment
# that can build target libraries. Version of a cross-compiler has to
# match the compiler being cross-built as libraries frequently use fresh
# compiler features, like `-std=c++26` or target-specific types like
# `_Bfloat16`.
# Version mismatch causes build failures like:
# https://github.com/NixOS/nixpkgs/issues/351905
#
# Similar problems (but on a smaller scale) happen when a `gcc`
# cross-compiler is built (`build` == `host` && `host` != `target`) built
# by a mismatching version of a native compiler (`build` == `host` &&
# `host` == `target`).
#
# Let's fix both problems by requiring the same compiler version for
# cross-case.
stdenv = if (stdenv.targetPlatform != stdenv.buildPlatform) && stdenv.cc.isGNU then pkgs."gcc${majorVersion}Stdenv" else stdenv;
})));
in
lib.nameValuePair attrName pkg;
Expand Down

0 comments on commit 028da88

Please sign in to comment.