From ca9be0511b0661be5255dd1fe0b05c5eaf3e56d8 Mon Sep 17 00:00:00 2001 From: sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> Date: Fri, 16 Apr 2021 13:55:41 +0200 Subject: [PATCH 1/3] binutils-unwrapped: force targetPrefix if cross compiling The binutils build system checks by itself if it is building a cross toolchain or not and prepends or omits a targetPrefix accordingly. This means that we can always pass target via configureTargets. However the binutils build system and our bintools wrapper disagree over whether we are building a cross toolchain or not sometimes since cross compilation can be relatively subtle in nixpkgs. For example every use of crossOverlays will make nixpkgs build a cross toolchain even though localSystem == crossSystem. The cross infrastructure is also used to build native binaries with a different stdenv (musl instead of glibc, clang instead of gcc). In all of these cases stdenv.hostPlatform.config == stdenv.targetPlatform.config, causing binutils to not prepend a target prefix. At the same time stdenv.hostPlatform != stdenv.targetPlatform causing the bintools wrapper to expect a target prefix, thus building an incomplete set of bintools. This is why currently pkgsCross.gnu64 and pkgsCross.musl64 aren't working. The solution is quite simple however: If we detect that we are building a cross toolchain in the binutils-unwrapped expression, we force the targetPrefix with --programprefix and fulfill the expectations of the bintools wrapper at the same time. Tested (on x86_64-linux): * pkgsCross.musl64.hello * pkgsCross.aarch64-multiplatform.hello * pkgs.hello Still not working is pkgsCross.gnu64, since x86_64-unknown-linux-gnu-stage-final-gcc gets confused about targets now, so bootstrapping the stdenv fails. Since this wasn't working previously anyways, it's proably fine to fix this separately. --- .../development/tools/misc/binutils/default.nix | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pkgs/development/tools/misc/binutils/default.nix b/pkgs/development/tools/misc/binutils/default.nix index 995f3f7430c..8042655a859 100644 --- a/pkgs/development/tools/misc/binutils/default.nix +++ b/pkgs/development/tools/misc/binutils/default.nix @@ -107,8 +107,7 @@ stdenv.mkDerivation { hardeningDisable = [ "format" "pie" ]; - # TODO(@Ericson2314): Always pass "--target" and always targetPrefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (stdenv.targetPlatform != stdenv.hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; configureFlags = (if enableShared then [ "--enable-shared" "--disable-static" ] @@ -126,7 +125,19 @@ stdenv.mkDerivation { # RUNPATH instead of RPATH on binaries. This is important because # RUNPATH can be overriden using LD_LIBRARY_PATH at runtime. "--enable-new-dtags" - ] ++ lib.optionals gold [ "--enable-gold" "--enable-plugins" ]; + + # force target prefix. Some versions of binutils will make it empty + # if `--host` and `--target` are too close, even if Nixpkgs thinks + # the platforms are different (e.g. because not all the info makes + # the `config`). Other versions of binutils will always prefix if + # `--target` is passed, even if `--host` and `--target` are the same. + # The easiest thing for us to do is not leave it to chance, and force + # the program prefix to be what we want it to be. + "--program-prefix=${targetPrefix}" + ] ++ lib.optionals gold [ + "--enable-gold" + "--enable-plugins" + ]; doCheck = false; # fails From acdc783418538f23cdfb671bc8fd4b1c076979bd Mon Sep 17 00:00:00 2001 From: sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> Date: Fri, 16 Apr 2021 17:39:04 +0200 Subject: [PATCH 2/3] gcc*: always force --program-prefix and pass --target Don't rely on gcc detecting from the passed platforms which prefix to use, but always specify the prefix nixpkgs expects (or doesn't). This allows us to work around problems where the configure script would add prefix where nixpkgs doesn't expect one (if `--target` was specified, but the same as `--host`) or doesn't add one if nixpkgs expects one (if `--target` and `--host` are the same, but we are actually cross compiling, but the relevant parts of the platform are not encoded into the platform config. See also ca9be0511b0661be5255dd1fe0b05c5eaf3e56d8. --- pkgs/development/compilers/gcc/10/default.nix | 3 +-- pkgs/development/compilers/gcc/4.8/default.nix | 3 +-- pkgs/development/compilers/gcc/4.9/default.nix | 3 +-- pkgs/development/compilers/gcc/6/default.nix | 3 +-- pkgs/development/compilers/gcc/7/default.nix | 3 +-- pkgs/development/compilers/gcc/8/default.nix | 3 +-- pkgs/development/compilers/gcc/9/default.nix | 3 +-- .../compilers/gcc/common/configure-flags.nix | 15 +++++++++++++++ 8 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pkgs/development/compilers/gcc/10/default.nix b/pkgs/development/compilers/gcc/10/default.nix index 1502b09cca6..c265fff2215 100644 --- a/pkgs/development/compilers/gcc/10/default.nix +++ b/pkgs/development/compilers/gcc/10/default.nix @@ -186,8 +186,7 @@ stdenv.mkDerivation ({ dontDisableStatic = true; - # TODO(@Ericson2314): Always pass "--target" and always prefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (targetPlatform != hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; configureFlags = import ../common/configure-flags.nix { inherit diff --git a/pkgs/development/compilers/gcc/4.8/default.nix b/pkgs/development/compilers/gcc/4.8/default.nix index 6a2121a8298..957c85cfde5 100644 --- a/pkgs/development/compilers/gcc/4.8/default.nix +++ b/pkgs/development/compilers/gcc/4.8/default.nix @@ -196,8 +196,7 @@ stdenv.mkDerivation ({ dontDisableStatic = true; - # TODO(@Ericson2314): Always pass "--target" and always prefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (targetPlatform != hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; configureFlags = import ../common/configure-flags.nix { inherit diff --git a/pkgs/development/compilers/gcc/4.9/default.nix b/pkgs/development/compilers/gcc/4.9/default.nix index 709288559d1..5d30f80edb2 100644 --- a/pkgs/development/compilers/gcc/4.9/default.nix +++ b/pkgs/development/compilers/gcc/4.9/default.nix @@ -209,8 +209,7 @@ stdenv.mkDerivation ({ dontDisableStatic = true; - # TODO(@Ericson2314): Always pass "--target" and always prefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (targetPlatform != hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; configureFlags = import ../common/configure-flags.nix { inherit diff --git a/pkgs/development/compilers/gcc/6/default.nix b/pkgs/development/compilers/gcc/6/default.nix index 93c9dde61fc..0ac54e093b4 100644 --- a/pkgs/development/compilers/gcc/6/default.nix +++ b/pkgs/development/compilers/gcc/6/default.nix @@ -232,8 +232,7 @@ stdenv.mkDerivation ({ dontDisableStatic = true; - # TODO(@Ericson2314): Always pass "--target" and always prefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (targetPlatform != hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; configureFlags = import ../common/configure-flags.nix { inherit diff --git a/pkgs/development/compilers/gcc/7/default.nix b/pkgs/development/compilers/gcc/7/default.nix index d9b4c639b5a..233d022514e 100644 --- a/pkgs/development/compilers/gcc/7/default.nix +++ b/pkgs/development/compilers/gcc/7/default.nix @@ -197,8 +197,7 @@ stdenv.mkDerivation ({ dontDisableStatic = true; - # TODO(@Ericson2314): Always pass "--target" and always prefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (targetPlatform != hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; configureFlags = import ../common/configure-flags.nix { inherit diff --git a/pkgs/development/compilers/gcc/8/default.nix b/pkgs/development/compilers/gcc/8/default.nix index 6ecf462d54d..4e57e43437f 100644 --- a/pkgs/development/compilers/gcc/8/default.nix +++ b/pkgs/development/compilers/gcc/8/default.nix @@ -182,8 +182,7 @@ stdenv.mkDerivation ({ dontDisableStatic = true; - # TODO(@Ericson2314): Always pass "--target" and always prefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (targetPlatform != hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; configureFlags = import ../common/configure-flags.nix { inherit diff --git a/pkgs/development/compilers/gcc/9/default.nix b/pkgs/development/compilers/gcc/9/default.nix index 7f35f5c7bb9..92810ecffaf 100644 --- a/pkgs/development/compilers/gcc/9/default.nix +++ b/pkgs/development/compilers/gcc/9/default.nix @@ -199,8 +199,7 @@ stdenv.mkDerivation ({ dontDisableStatic = true; - # TODO(@Ericson2314): Always pass "--target" and always prefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (targetPlatform != hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; configureFlags = import ../common/configure-flags.nix { inherit diff --git a/pkgs/development/compilers/gcc/common/configure-flags.nix b/pkgs/development/compilers/gcc/common/configure-flags.nix index fc4fbb34c50..4cf06cd4107 100644 --- a/pkgs/development/compilers/gcc/common/configure-flags.nix +++ b/pkgs/development/compilers/gcc/common/configure-flags.nix @@ -44,6 +44,9 @@ let crossMingw = targetPlatform != hostPlatform && targetPlatform.libc == "msvcrt"; crossDarwin = targetPlatform != hostPlatform && targetPlatform.libc == "libSystem"; + targetPrefix = lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform) + "${stdenv.targetPlatform.config}-"; + crossConfigureFlags = # Ensure that -print-prog-name is able to find the correct programs. [ @@ -112,6 +115,18 @@ let # Basic configuration ++ [ + # Force target prefix. The behavior if `--target` and `--host` + # are specified is inconsistent: Sometimes specifying `--target` + # always causes a prefix to be generated, sometimes it's only + # added if the `--host` and `--target` differ. This means that + # sometimes there may be a prefix even though nixpkgs doesn't + # expect one and sometimes there may be none even though nixpkgs + # expects one (since not all information is serialized into the + # config attribute). The easiest way out of these problems is to + # always set the program prefix, so gcc will conform to our + # expectations. + "--program-prefix=${targetPrefix}" + (lib.enableFeature enableLTO "lto") "--disable-libstdcxx-pch" "--without-included-gettext" From e75c5353bb337b8e8bacf02c7b369b1e897771b9 Mon Sep 17 00:00:00 2001 From: sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> Date: Fri, 16 Apr 2021 18:00:56 +0200 Subject: [PATCH 3/3] gdb: always set --program-prefix and pass --target This makes the targetPrefix always conform to nixpkgs' expectations instead of relying on the autotools build system to figure it out correctly (which is also inconsistent across versions). See also ca9be0511b0661be5255dd1fe0b05c5eaf3e56d8. --- pkgs/development/tools/misc/gdb/default.nix | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkgs/development/tools/misc/gdb/default.nix b/pkgs/development/tools/misc/gdb/default.nix index 6f923778975..66d4e8bc057 100644 --- a/pkgs/development/tools/misc/gdb/default.nix +++ b/pkgs/development/tools/misc/gdb/default.nix @@ -61,8 +61,7 @@ stdenv.mkDerivation rec { NIX_CFLAGS_COMPILE = "-Wno-format-nonliteral"; - # TODO(@Ericson2314): Always pass "--target" and always prefix. - configurePlatforms = [ "build" "host" ] ++ lib.optional (stdenv.targetPlatform != stdenv.hostPlatform) "target"; + configurePlatforms = [ "build" "host" "target" ]; # GDB have to be built out of tree. preConfigure = '' @@ -72,6 +71,13 @@ stdenv.mkDerivation rec { configureScript = "../configure"; configureFlags = with lib; [ + # Set the program prefix to the current targetPrefix. + # This ensures that the prefix always conforms to + # nixpkgs' expectations instead of relying on the build + # system which only receives `config` which is merely a + # subset of the platform description. + "--program-prefix=${targetPrefix}" + "--enable-targets=all" "--enable-64-bit-bfd" "--disable-install-libbfd" "--disable-shared" "--enable-static"