From fb7811bf03398ced5cada7476e089a417db5c962 Mon Sep 17 00:00:00 2001 From: Yureka Date: Sun, 17 Oct 2021 12:28:08 +0200 Subject: [PATCH 1/7] rustc: use autoPatchelfHook for bootstrap binaries This is both simpler and works in more cases, e.g. for the bootstrap binaries linked against musl libc. --- pkgs/development/compilers/rust/binary.nix | 34 +++++----------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/pkgs/development/compilers/rust/binary.nix b/pkgs/development/compilers/rust/binary.nix index 1145f4da8f66..6c3751cbb575 100644 --- a/pkgs/development/compilers/rust/binary.nix +++ b/pkgs/development/compilers/rust/binary.nix @@ -1,4 +1,5 @@ { lib, stdenv, makeWrapper, bash, curl, darwin, zlib +, autoPatchelfHook, gcc , version , src , platform @@ -31,7 +32,9 @@ rec { license = [ licenses.mit licenses.asl20 ]; }; + nativeBuildInputs = lib.optional (!stdenv.isDarwin) autoPatchelfHook; buildInputs = [ bash ] + ++ lib.optionals (!stdenv.isDarwin) [ gcc.cc.lib zlib ] ++ lib.optional stdenv.isDarwin Security; postPatch = '' @@ -42,24 +45,6 @@ rec { ./install.sh --prefix=$out \ --components=${installComponents} - ${optionalString (stdenv.isLinux && bootstrapping) ('' - patchelf \ - --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \ - "$out/bin/rustc" - '' + optionalString (lib.versionAtLeast version "1.46") - # rustc bootstrap needs libz starting from 1.46 - '' - ln -s ${zlib}/lib/libz.so.1 $out/lib/libz.so.1 - ln -s ${zlib}/lib/libz.so $out/lib/libz.so - '' + '' - patchelf \ - --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \ - "$out/bin/rustdoc" - patchelf \ - --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \ - "$out/bin/cargo" - '')} - # Do NOT, I repeat, DO NOT use `wrapProgram` on $out/bin/rustc # (or similar) here. It causes strange effects where rustc loads # the wrong libraries in a bootstrap-build causing failures that @@ -83,8 +68,11 @@ rec { license = [ licenses.mit licenses.asl20 ]; }; - nativeBuildInputs = [ makeWrapper ]; - buildInputs = [ bash ] ++ lib.optional stdenv.isDarwin Security; + nativeBuildInputs = [ makeWrapper ] + ++ lib.optional (!stdenv.isDarwin) autoPatchelfHook; + buildInputs = [ bash ] + ++ lib.optional (!stdenv.isDarwin) gcc.cc.lib + ++ lib.optional stdenv.isDarwin Security; postPatch = '' patchShebangs . @@ -95,12 +83,6 @@ rec { ./install.sh --prefix=$out \ --components=cargo - ${optionalString (stdenv.isLinux && bootstrapping) '' - patchelf \ - --set-interpreter $(cat $NIX_CC/nix-support/dynamic-linker) \ - "$out/bin/cargo" - ''} - wrapProgram "$out/bin/cargo" \ --suffix PATH : "${rustc}/bin" ''; From 0a9c3640d319a284095bbd53214c92d7a99c1c2a Mon Sep 17 00:00:00 2001 From: Yureka Date: Sun, 17 Oct 2021 12:44:49 +0200 Subject: [PATCH 2/7] rustc: set crt-static flag The crt-static option selects if the C runtime is linked dynamically or statically into the resulting binaries. There is a default value of this setting for each platform, but it is not always what we want. For example, musl targets are assumed to always have the C runtime linked statically, but we support both. In practise, this fixes an error in the pkgsMusl.rustc build: > cannot produce dylib for `rustc_driver v0.0.0 (/build/rustc-1.63.0-src/compiler/rustc_driver)` as the target `x86_64-unknown-linux-musl` does not support these crate types --- pkgs/development/compilers/rust/rustc.nix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkgs/development/compilers/rust/rustc.nix b/pkgs/development/compilers/rust/rustc.nix index a996f987bed6..615468f230e3 100644 --- a/pkgs/development/compilers/rust/rustc.nix +++ b/pkgs/development/compilers/rust/rustc.nix @@ -87,6 +87,10 @@ in stdenv.mkDerivation rec { "${setBuild}.cxx=${cxxForBuild}" "${setHost}.cxx=${cxxForHost}" "${setTarget}.cxx=${cxxForTarget}" + + "${setBuild}.crt-static=${lib.boolToString stdenv.buildPlatform.isStatic}" + "${setHost}.crt-static=${lib.boolToString stdenv.hostPlatform.isStatic}" + "${setTarget}.crt-static=${lib.boolToString stdenv.targetPlatform.isStatic}" ] ++ optionals (!withBundledLLVM) [ "--enable-llvm-link-shared" "${setBuild}.llvm-config=${llvmSharedForBuild.dev}/bin/llvm-config" From 202524ddf7e3a6931fd245ca0cbe959cd19769e3 Mon Sep 17 00:00:00 2001 From: Yureka Date: Sat, 13 Aug 2022 14:32:16 +0200 Subject: [PATCH 3/7] rustc: remove broken flag for musl It builds! --- pkgs/development/compilers/rust/rustc.nix | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkgs/development/compilers/rust/rustc.nix b/pkgs/development/compilers/rust/rustc.nix index 615468f230e3..cfed71f0e80b 100644 --- a/pkgs/development/compilers/rust/rustc.nix +++ b/pkgs/development/compilers/rust/rustc.nix @@ -192,8 +192,5 @@ in stdenv.mkDerivation rec { maintainers = with maintainers; [ madjar cstrahan globin havvy ]; license = [ licenses.mit licenses.asl20 ]; platforms = platforms.linux ++ platforms.darwin; - # rustc can't generate binaries for dynamically linked Musl. - # https://github.com/NixOS/nixpkgs/issues/179242 - broken = stdenv.targetPlatform.isMusl && !stdenv.targetPlatform.isStatic; }; } From acf9dd62386cda3dbcaf8603ce57f8caac7effee Mon Sep 17 00:00:00 2001 From: Yureka Date: Sat, 13 Aug 2022 14:32:26 +0200 Subject: [PATCH 4/7] cargo: add broken flag for musl A build script crashes: > cannot produce dylib for `rustc_driver v0.0.0 (/build/rustc-1.63.0-src/compiler/rustc_driver)` as the target `x86_64-unknown-linux-musl` does not support these crate types --- pkgs/development/compilers/rust/cargo.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/development/compilers/rust/cargo.nix b/pkgs/development/compilers/rust/cargo.nix index b50f36f0d9b6..2d8361eb2a4e 100644 --- a/pkgs/development/compilers/rust/cargo.nix +++ b/pkgs/development/compilers/rust/cargo.nix @@ -72,5 +72,7 @@ rustPlatform.buildRustPackage { maintainers = with maintainers; [ retrry ]; license = [ licenses.mit licenses.asl20 ]; platforms = platforms.unix; + # weird segfault in a build script + broken = stdenv.targetPlatform.isMusl && !stdenv.targetPlatform.isStatic; }; } From 66ac47bdf6aa5d46678ae82802fe06f7376a22cc Mon Sep 17 00:00:00 2001 From: Yureka Date: Sat, 13 Aug 2022 14:43:18 +0200 Subject: [PATCH 5/7] cargoSetupHook: remove unneeded rustflags for aarch64+static cross The linked issue was resolved upstream and the `-lgcc` is no longer required since https://github.com/rust-lang/compiler-builtins/pull/377 was merged. --- pkgs/build-support/rust/hooks/default.nix | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkgs/build-support/rust/hooks/default.nix b/pkgs/build-support/rust/hooks/default.nix index d33692457077..430f758e9ab8 100644 --- a/pkgs/build-support/rust/hooks/default.nix +++ b/pkgs/build-support/rust/hooks/default.nix @@ -75,10 +75,6 @@ in { ${lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) '' [target."${shortTarget}"] "linker" = "${ccForHost}" - ${# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633 - lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) '' - "rustflags" = [ "-C", "target-feature=+crt-static", "-C", "link-arg=-lgcc" ] - ''} ''} ''; }; From 51c62063e366d14e4353c85f22adb085330b9684 Mon Sep 17 00:00:00 2001 From: Yureka Date: Sat, 13 Aug 2022 14:43:50 +0200 Subject: [PATCH 6/7] cargoSetupHook: set crt-static Tell rust if we want our binaries linked statically or dynamically. Otherwise the compiler will always produce statically linked binaries for musl targets, as this is the default. --- pkgs/build-support/rust/hooks/default.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/build-support/rust/hooks/default.nix b/pkgs/build-support/rust/hooks/default.nix index 430f758e9ab8..652b074c3020 100644 --- a/pkgs/build-support/rust/hooks/default.nix +++ b/pkgs/build-support/rust/hooks/default.nix @@ -76,6 +76,7 @@ in { [target."${shortTarget}"] "linker" = "${ccForHost}" ''} + "rustflags" = [ "-C", "target-feature=${if stdenv.hostPlatform.isStatic then "+" else "-"}crt-static" ] ''; }; } ./cargo-setup-hook.sh) {}; From a67c5f82c0558147c9310a45d2dd450095cc2a83 Mon Sep 17 00:00:00 2001 From: Yureka Date: Sat, 13 Aug 2022 14:44:45 +0200 Subject: [PATCH 7/7] python3.pkgs.pyelftools: skip tests on non-glibc targets pyelftools ships a readelf binary for its test suite that is linked against glibc. Replacing this readelf binary with our stdenv version doesn't work. --- pkgs/development/python-modules/pyelftools/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/development/python-modules/pyelftools/default.nix b/pkgs/development/python-modules/pyelftools/default.nix index cec999bf999a..1f0bcc15b3ea 100644 --- a/pkgs/development/python-modules/pyelftools/default.nix +++ b/pkgs/development/python-modules/pyelftools/default.nix @@ -20,7 +20,7 @@ buildPythonPackage rec { hash = "sha256-+T5C0ah2oj5E8fWaQbuzYRVgD5bSiUbaArrlxNLojvw="; }; - doCheck = stdenv.hostPlatform.system == "x86_64-linux"; + doCheck = stdenv.hostPlatform.system == "x86_64-linux" && stdenv.hostPlatform.isGnu; checkPhase = '' patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" test/external_tools/readelf