Skip to content

Commit

Permalink
buildRustCrate: add support for renaming crates
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danieldk committed Sep 8, 2019
1 parent 57b66eb commit 85c6d72
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pkgs/build-support/rust/build-rust-crate/build-crate.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{ lib, stdenv, echo_build_heading, noisily, makeDeps }:
{ crateName,
dependencies,
crateFeatures, libName, release, libPath,
crateFeatures, crateRenames, libName, release, libPath,
crateType, metadata, crateBin, hasCrateBin,
extraRustcOpts, verbose, colors }:

let

deps = makeDeps dependencies;
deps = makeDeps dependencies crateRenames;
rustcOpts =
lib.lists.foldl' (opts: opt: opts + " " + opt)
(if release then "-C opt-level=3" else "-C debuginfo=2")
Expand Down
3 changes: 2 additions & 1 deletion pkgs/build-support/rust/build-rust-crate/configure-crate.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
, crateHomepage
, crateFeatures
, crateName
, crateRenames
, crateVersion
, extraLinkFlags
, extraRustcOpts
Expand All @@ -24,7 +25,7 @@ let version_ = lib.splitString "-" crateVersion;
rustcOpts = lib.lists.foldl' (opts: opt: opts + " " + opt)
(if release then "-C opt-level=3" else "-C debuginfo=2")
(["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts);
buildDeps = makeDeps buildDependencies;
buildDeps = makeDeps buildDependencies crateRenames;
authors = lib.concatStringsSep ":" crateAuthors;
optLevel = if release then 3 else 0;
completeDepsDir = lib.concatStringsSep " " completeDeps;
Expand Down
24 changes: 15 additions & 9 deletions pkgs/build-support/rust/build-rust-crate/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ let
then "macos"
else stdenv.hostPlatform.parsed.kernel.name;

makeDeps = dependencies:
makeDeps = dependencies: crateRenames:
(lib.concatMapStringsSep " " (dep:
let extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; in
(if lib.lists.any (x: x == "lib") dep.crateType then
" --extern ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib"
let
extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName;
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
" --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);

echo_build_heading = colors: ''
Expand Down Expand Up @@ -60,7 +65,7 @@ let
in

crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides,
dependencies, buildDependencies,
dependencies, buildDependencies, crateRenames,
extraRustcOpts,
preUnpack, postUnpack, prePatch, patches, postPatch,
preConfigure, postConfigure, preBuild, postBuild, preInstall, postInstall }:
Expand All @@ -70,7 +75,7 @@ let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverr
buildDependencies_ = buildDependencies;
processedAttrs = [
"src" "buildInputs" "crateBin" "crateLib" "libName" "libPath"
"buildDependencies" "dependencies" "features"
"buildDependencies" "dependencies" "features" "crateRenames"
"crateName" "version" "build" "authors" "colors" "edition"
];
extraDerivationAttrs = lib.filterAttrs (n: v: ! lib.elem n processedAttrs) crate;
Expand Down Expand Up @@ -143,13 +148,13 @@ stdenv.mkDerivation (rec {

configurePhase = configureCrate {
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
crateAuthors crateHomepage verbose colors target_os;
};
buildPhase = buildCrate {
inherit crateName dependencies
crateFeatures libName release libPath crateType
crateFeatures crateRenames libName release libPath crateType
metadata crateBin hasCrateBin verbose colors
extraRustcOpts;
};
Expand Down Expand Up @@ -177,4 +182,5 @@ stdenv.mkDerivation (rec {
postInstall = crate_.postInstall or "";
dependencies = crate_.dependencies or [];
buildDependencies = crate_.buildDependencies or [];
crateRenames = crate_.crateRenames or {};
}
26 changes: 20 additions & 6 deletions pkgs/build-support/rust/build-rust-crate/test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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; }";

mkTest = crateArgs: let
Expand All @@ -34,12 +41,7 @@ let
libTestBinary = if !isLib then null else mkCrate {
crateName = "run-test-${crateName}";
dependencies = [ crate ];
src = mkFile "src/main.rs" ''
extern crate ${libName};
fn main() {
assert_eq!(${libName}::test(), 23);
}
'';
src = mkBinExtern "src/main.rs" libName;
};

in runCommand "run-buildRustCrate-${crateName}-test" {
Expand Down Expand Up @@ -71,6 +73,18 @@ let
};
crateBinNoPath3 = { crateBin = [{ name = "my-binary5"; }]; src = mkBin "src/bin/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 {});
in lib.mapAttrs (key: value: mkTest (value // lib.optionalAttrs (!value?crateName) { crateName = key; })) cases // {
Expand Down

0 comments on commit 85c6d72

Please sign in to comment.