3
0
Fork 0
forked from mirrors/nixpkgs

buildRustCrate: add support for renaming crates

Before this change, buildRustCrate always called rustc with

--extern libName=[...]libName[...]

However, Cargo permits using a different name under which a dependency
is known to a crate. For example, rand 0.7.0 uses:

[dependencies]
getrandom_package = { version = "0.1.1", package = "getrandom", optional = true }

Which introduces the getrandom dependency such that it is known as
getrandom_package to the rand crate. In this case, the correct extern
flag is of the form

--extern getrandom_package=[...]getrandom[...]

which is currently not supported. In order to support such cases, this
change introduces a crateRenames argument to buildRustCrate. This
argument is an attribute set of dependencies that should be renamed. In
this case, crateRenames would be:

{
  "getrandom" = "getrandom_package";
}

The extern options are then built such that if the libName occurs as
an attribute in this set, it value will be used as the local
name. Otherwise libName will be used as before.
This commit is contained in:
Daniël de Kok 2019-09-08 08:18:09 +02:00
parent 57b66eb3f2
commit 85c6d72011
4 changed files with 39 additions and 18 deletions

View file

@ -1,13 +1,13 @@
{ lib, stdenv, echo_build_heading, noisily, makeDeps }: { lib, stdenv, echo_build_heading, noisily, makeDeps }:
{ crateName, { crateName,
dependencies, dependencies,
crateFeatures, libName, release, libPath, crateFeatures, crateRenames, libName, release, libPath,
crateType, metadata, crateBin, hasCrateBin, crateType, metadata, crateBin, hasCrateBin,
extraRustcOpts, verbose, colors }: extraRustcOpts, verbose, colors }:
let let
deps = makeDeps dependencies; deps = makeDeps dependencies crateRenames;
rustcOpts = rustcOpts =
lib.lists.foldl' (opts: opt: opts + " " + opt) lib.lists.foldl' (opts: opt: opts + " " + opt)
(if release then "-C opt-level=3" else "-C debuginfo=2") (if release then "-C opt-level=3" else "-C debuginfo=2")

View file

@ -9,6 +9,7 @@
, crateHomepage , crateHomepage
, crateFeatures , crateFeatures
, crateName , crateName
, crateRenames
, crateVersion , crateVersion
, extraLinkFlags , extraLinkFlags
, extraRustcOpts , extraRustcOpts
@ -24,7 +25,7 @@ let version_ = lib.splitString "-" crateVersion;
rustcOpts = lib.lists.foldl' (opts: opt: opts + " " + opt) rustcOpts = lib.lists.foldl' (opts: opt: opts + " " + opt)
(if release then "-C opt-level=3" else "-C debuginfo=2") (if release then "-C opt-level=3" else "-C debuginfo=2")
(["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts); (["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts);
buildDeps = makeDeps buildDependencies; buildDeps = makeDeps buildDependencies crateRenames;
authors = lib.concatStringsSep ":" crateAuthors; authors = lib.concatStringsSep ":" crateAuthors;
optLevel = if release then 3 else 0; optLevel = if release then 3 else 0;
completeDepsDir = lib.concatStringsSep " " completeDeps; completeDepsDir = lib.concatStringsSep " " completeDeps;

View file

@ -13,13 +13,18 @@ let
then "macos" then "macos"
else stdenv.hostPlatform.parsed.kernel.name; else stdenv.hostPlatform.parsed.kernel.name;
makeDeps = dependencies: makeDeps = dependencies: crateRenames:
(lib.concatMapStringsSep " " (dep: (lib.concatMapStringsSep " " (dep:
let extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; in let
(if lib.lists.any (x: x == "lib") dep.crateType then extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName;
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib" name = if builtins.hasAttr dep.crateName crateRenames then
lib.strings.replaceStrings ["-"] ["_"] crateRenames.${dep.crateName}
else
extern;
in (if lib.lists.any (x: x == "lib") dep.crateType then
" --extern ${name}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib"
else else
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}") " --extern ${name}=${dep.out}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
) dependencies); ) dependencies);
echo_build_heading = colors: '' echo_build_heading = colors: ''
@ -60,7 +65,7 @@ let
in in
crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides, crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides,
dependencies, buildDependencies, dependencies, buildDependencies, crateRenames,
extraRustcOpts, extraRustcOpts,
preUnpack, postUnpack, prePatch, patches, postPatch, preUnpack, postUnpack, prePatch, patches, postPatch,
preConfigure, postConfigure, preBuild, postBuild, preInstall, postInstall }: preConfigure, postConfigure, preBuild, postBuild, preInstall, postInstall }:
@ -70,7 +75,7 @@ let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverr
buildDependencies_ = buildDependencies; buildDependencies_ = buildDependencies;
processedAttrs = [ processedAttrs = [
"src" "buildInputs" "crateBin" "crateLib" "libName" "libPath" "src" "buildInputs" "crateBin" "crateLib" "libName" "libPath"
"buildDependencies" "dependencies" "features" "buildDependencies" "dependencies" "features" "crateRenames"
"crateName" "version" "build" "authors" "colors" "edition" "crateName" "version" "build" "authors" "colors" "edition"
]; ];
extraDerivationAttrs = lib.filterAttrs (n: v: ! lib.elem n processedAttrs) crate; extraDerivationAttrs = lib.filterAttrs (n: v: ! lib.elem n processedAttrs) crate;
@ -143,13 +148,13 @@ stdenv.mkDerivation (rec {
configurePhase = configureCrate { configurePhase = configureCrate {
inherit crateName buildDependencies completeDeps completeBuildDeps crateDescription inherit crateName buildDependencies completeDeps completeBuildDeps crateDescription
crateFeatures libName build workspace_member release libPath crateVersion crateFeatures crateRenames libName build workspace_member release libPath crateVersion
extraLinkFlags extraRustcOpts extraLinkFlags extraRustcOpts
crateAuthors crateHomepage verbose colors target_os; crateAuthors crateHomepage verbose colors target_os;
}; };
buildPhase = buildCrate { buildPhase = buildCrate {
inherit crateName dependencies inherit crateName dependencies
crateFeatures libName release libPath crateType crateFeatures crateRenames libName release libPath crateType
metadata crateBin hasCrateBin verbose colors metadata crateBin hasCrateBin verbose colors
extraRustcOpts; extraRustcOpts;
}; };
@ -177,4 +182,5 @@ stdenv.mkDerivation (rec {
postInstall = crate_.postInstall or ""; postInstall = crate_.postInstall or "";
dependencies = crate_.dependencies or []; dependencies = crate_.dependencies or [];
buildDependencies = crate_.buildDependencies or []; buildDependencies = crate_.buildDependencies or [];
crateRenames = crate_.crateRenames or {};
} }

View file

@ -22,6 +22,13 @@ let
} }
''; '';
mkBinExtern = name: extern: mkFile name ''
extern crate ${extern};
fn main() {
assert_eq!(${extern}::test(), 23);
}
'';
mkLib = name: mkFile name "pub fn test() -> i32 { return 23; }"; mkLib = name: mkFile name "pub fn test() -> i32 { return 23; }";
mkTest = crateArgs: let mkTest = crateArgs: let
@ -34,12 +41,7 @@ let
libTestBinary = if !isLib then null else mkCrate { libTestBinary = if !isLib then null else mkCrate {
crateName = "run-test-${crateName}"; crateName = "run-test-${crateName}";
dependencies = [ crate ]; dependencies = [ crate ];
src = mkFile "src/main.rs" '' src = mkBinExtern "src/main.rs" libName;
extern crate ${libName};
fn main() {
assert_eq!(${libName}::test(), 23);
}
'';
}; };
in runCommand "run-buildRustCrate-${crateName}-test" { in runCommand "run-buildRustCrate-${crateName}-test" {
@ -71,6 +73,18 @@ let
}; };
crateBinNoPath3 = { crateBin = [{ name = "my-binary5"; }]; src = mkBin "src/bin/main.rs"; }; crateBinNoPath3 = { crateBin = [{ name = "my-binary5"; }]; src = mkBin "src/bin/main.rs"; };
crateBinNoPath4 = { crateBin = [{ name = "my-binary6"; }]; src = mkBin "src/main.rs";}; crateBinNoPath4 = { crateBin = [{ name = "my-binary6"; }]; src = mkBin "src/main.rs";};
crateBinRename1 = {
crateBin = [{ name = "my-binary-rename1"; }];
src = mkBinExtern "src/main.rs" "foo_renamed";
dependencies = [ (mkCrate { crateName = "foo"; src = mkLib "src/lib.rs"; }) ];
crateRenames = { "foo" = "foo_renamed"; };
};
crateBinRename2 = {
crateBin = [{ name = "my-binary-rename2"; }];
src = mkBinExtern "src/main.rs" "foo_renamed";
dependencies = [ (mkCrate { crateName = "foo"; libName = "foolib"; src = mkLib "src/lib.rs"; }) ];
crateRenames = { "foo" = "foo_renamed"; };
};
}; };
brotliCrates = (callPackage ./brotli-crates.nix {}); brotliCrates = (callPackage ./brotli-crates.nix {});
in lib.mapAttrs (key: value: mkTest (value // lib.optionalAttrs (!value?crateName) { crateName = key; })) cases // { in lib.mapAttrs (key: value: mkTest (value // lib.optionalAttrs (!value?crateName) { crateName = key; })) cases // {