buildRustCrate: Add support for standard library deps

We are replicating one mechanism behind `-Z build-std`.

There isn't yet crate2nix support for this, but one can (and I do) add
the missing stdlib deps (for this feature to pick up) with overrides.
This commit is contained in:
John Ericson 2022-01-12 08:48:49 +00:00
parent 7d4ed41a2c
commit cc29693a09
2 changed files with 23 additions and 6 deletions

View file

@ -1,4 +1,8 @@
{ lib, stdenv, mkRustcDepArgs, mkRustcFeatureArgs, rust }:
{ lib, stdenv
, mkRustcDepArgs, mkRustcFeatureArgs, needUnstableCLI
, rust
}:
{ crateName,
dependencies,
crateFeatures, crateRenames, libName, release, libPath,
@ -18,6 +22,8 @@
(mkRustcFeatureArgs crateFeatures)
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"--target" (rust.toRustTargetSpec stdenv.hostPlatform)
] ++ lib.optionals (needUnstableCLI dependencies) [
"-Z" "unstable-options"
] ++ extraRustcOpts
# 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

View file

@ -46,17 +46,28 @@ let
)
else
extern;
opts = lib.optionalString (dep.stdlib or false) "noprelude:";
filename =
if lib.any (x: x == "lib" || x == "rlib") dep.crateType
then "${dep.metadata}.rlib"
else "${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}";
in
(if lib.any (x: x == "lib" || x == "rlib") dep.crateType then
" --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}.rlib"
else
" --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
" --extern ${opts}${name}=${dep.lib}/lib/lib${extern}-${filename}"
)
dependencies;
# Create feature arguments for rustc.
mkRustcFeatureArgs = lib.concatMapStringsSep " " (f: ''--cfg feature=\"${f}\"'');
# Whether we need to use unstable command line flags
#
# Currently just needed for standard library dependencies, which have a
# special "noprelude:" modifier. If in later versions of Rust this is
# stabilized we can account for that here, too, so we don't opt into
# instability unnecessarily.
needUnstableCLI = dependencies:
lib.any (dep: dep.stdlib or false) dependencies;
inherit (import ./log.nix { inherit lib; }) noisily echo_colored;
configureCrate = import ./configure-crate.nix {
@ -64,7 +75,7 @@ let
};
buildCrate = import ./build-crate.nix {
inherit lib stdenv mkRustcDepArgs mkRustcFeatureArgs rust;
inherit lib stdenv mkRustcDepArgs mkRustcFeatureArgs needUnstableCLI rust;
};
installCrate = import ./install-crate.nix { inherit stdenv; };