From 8d2ba0feaecb6a00905186a3881e498f4af640bd Mon Sep 17 00:00:00 2001 From: Ilan Joselevich Date: Tue, 3 Sep 2024 20:26:24 +0300 Subject: [PATCH] buildRustCrate: Fix rust-overlay usage I broke the usage of buildRustCrate with a toolchain from rust-overlay when I added support for wasm32-unknown-unknown, this change adds additional conditionals to restore the usage. The toolchain can now be overriden either through ``` buildRustCrate { rust = toolchain; cargo = toolchain } ``` or ``` buildRustCrate.override { rustc = toolchain; cargo = toolchain; } ``` They should now be consistent with each other. --- .../rust/build-rust-crate/build-crate.nix | 2 +- .../rust/build-rust-crate/default.nix | 31 ++++++++----------- pkgs/top-level/all-packages.nix | 15 +++++++-- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pkgs/build-support/rust/build-rust-crate/build-crate.nix b/pkgs/build-support/rust/build-rust-crate/build-crate.nix index 3af13fe70a7d..4bf1b188cd1c 100644 --- a/pkgs/build-support/rust/build-rust-crate/build-crate.nix +++ b/pkgs/build-support/rust/build-rust-crate/build-crate.nix @@ -27,7 +27,7 @@ # since rustc 1.42 the "proc_macro" crate is part of the default crate prelude # https://github.com/rust-lang/cargo/commit/4d64eb99a4#diff-7f98585dbf9d30aa100c8318e2c77e79R1021-R1022 ++ lib.optional (lib.elem "proc-macro" crateType) "--extern proc_macro" - ++ lib.optional (stdenv.hostPlatform.linker == "lld") # Needed when building for targets that use lld. e.g. 'wasm32-unknown-unknown' + ++ lib.optional (stdenv.hostPlatform.linker == "lld" && rustc ? llvmPackages.lld) # Needed when building for targets that use lld. e.g. 'wasm32-unknown-unknown' "-C linker=${rustc.llvmPackages.lld}/bin/lld" ++ lib.optional (stdenv.hasCC && stdenv.hostPlatform.linker != "lld") "-C linker=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc" diff --git a/pkgs/build-support/rust/build-rust-crate/default.nix b/pkgs/build-support/rust/build-rust-crate/default.nix index 841e8e86cbfb..313e7601f617 100644 --- a/pkgs/build-support/rust/build-rust-crate/default.nix +++ b/pkgs/build-support/rust/build-rust-crate/default.nix @@ -16,16 +16,6 @@ }: let - # Returns a true if the builder's rustc was built with support for the target. - targetAlreadyIncluded = lib.elem stdenv.hostPlatform.rust.rustcTarget - (lib.splitString "," (lib.removePrefix "--target=" ( - lib.elemAt (lib.filter (f: lib.hasPrefix "--target=" f) pkgsBuildBuild.rustc.unwrapped.configureFlags) 0) - )); - - # If the build's rustc was built with support for the target then reuse it. (Avoids uneeded compilation for targets like `wasm32-unknown-unknown`) - rustc' = if targetAlreadyIncluded then pkgsBuildBuild.rustc else rustc; - cargo' = if targetAlreadyIncluded then pkgsBuildBuild.cargo else cargo; - # Create rustc arguments to link against the given list of dependencies # and renames. # @@ -85,11 +75,6 @@ let inherit lib stdenv echo_colored noisily mkRustcDepArgs mkRustcFeatureArgs; }; - buildCrate = import ./build-crate.nix { - inherit lib stdenv mkRustcDepArgs mkRustcFeatureArgs needUnstableCLI; - rustc = rustc'; - }; - installCrate = import ./install-crate.nix { inherit stdenv; }; in @@ -103,7 +88,11 @@ crate_: lib.makeOverridable # The rust compiler to use. # # Default: pkgs.rustc - { rust + { rust ? rustc + # The cargo package to use for getting some metadata. + # + # Default: pkgs.cargo + , cargo ? cargo # Whether to build a release version (`true`) or a debug # version (`false`). Debug versions are faster to build # but might be much slower at runtime. @@ -262,6 +251,11 @@ crate_: lib.makeOverridable # https://github.com/kolloch/crate2nix/blame/5b19c1b14e1b0e5522c3e44e300d0b332dc939e7/crate2nix/templates/build.nix.tera#L89 crateBin = lib.filter (bin: !(bin ? name && bin.name == ",")) (crate.crateBin or [ ]); hasCrateBin = crate ? crateBin; + + buildCrate = import ./build-crate.nix { + inherit lib stdenv mkRustcDepArgs mkRustcFeatureArgs needUnstableCLI; + rustc = rust; + }; in stdenv.mkDerivation (rec { @@ -285,7 +279,7 @@ crate_: lib.makeOverridable name = "rust_${crate.crateName}-${crate.version}${lib.optionalString buildTests_ "-test"}"; version = crate.version; depsBuildBuild = [ pkgsBuildBuild.stdenv.cc ]; - nativeBuildInputs = [ rustc' cargo' jq ] + nativeBuildInputs = [ rust cargo jq ] ++ lib.optionals stdenv.hasCC [ stdenv.cc ] ++ lib.optionals stdenv.buildPlatform.isDarwin [ libiconv ] ++ (crate.nativeBuildInputs or [ ]) ++ nativeBuildInputs_; @@ -392,7 +386,8 @@ crate_: lib.makeOverridable ) ) { - rust = rustc'; + rust = crate_.rust or rustc; + cargo = crate_.cargo or cargo; release = crate_.release or true; verbose = crate_.verbose or true; extraRustcOpts = [ ]; diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 327ad752c650..9e8a9e6d2bec 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -15941,9 +15941,18 @@ with pkgs; makeRustPlatform = callPackage ../development/compilers/rust/make-rust-platform.nix { }; - buildRustCrate = callPackage ../build-support/rust/build-rust-crate ({ } // lib.optionalAttrs (stdenv.hostPlatform.libc == null) { - stdenv = stdenvNoCC; # Some build targets without libc will fail to evaluate with a normal stdenv. - }); + buildRustCrate = + let + # Returns a true if the builder's rustc was built with support for the target. + targetAlreadyIncluded = lib.elem stdenv.hostPlatform.rust.rustcTarget + (lib.splitString "," (lib.removePrefix "--target=" ( + lib.elemAt (lib.filter (f: lib.hasPrefix "--target=" f) pkgsBuildBuild.rustc.unwrapped.configureFlags) 0 + ))); + in + callPackage ../build-support/rust/build-rust-crate ({ } // lib.optionalAttrs (stdenv.hostPlatform.libc == null) { + stdenv = stdenvNoCC; # Some build targets without libc will fail to evaluate with a normal stdenv. + } // lib.optionalAttrs targetAlreadyIncluded { inherit (pkgsBuildBuild) rustc cargo; } # Optimization. + ); buildRustCrateHelpers = callPackage ../build-support/rust/build-rust-crate/helpers.nix { }; cargo2junit = callPackage ../development/tools/rust/cargo2junit { };