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] 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