Merge pull request #68296 from danieldk/crateRenames

buildRustCrate: add support for renaming crates
This commit is contained in:
Andreas Rammhold 2019-09-10 10:57:54 +02:00 committed by GitHub
commit a69a6c1117
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 18 deletions

View File

@ -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")

View File

@ -9,6 +9,7 @@
, crateHomepage
, crateFeatures
, crateName
, crateRenames
, crateVersion
, extraLinkFlags
, extraRustcOpts
@ -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;

View File

@ -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 ${extern}=${dep.out}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
extern;
in (if lib.lists.any (x: x == "lib") dep.crateType then
" --extern ${name}=${dep.out}/lib/lib${extern}-${dep.metadata}.rlib"
else
" --extern ${name}=${dep.out}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
) dependencies);
echo_build_heading = colors: ''
@ -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 }:
@ -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;
@ -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;
};
@ -177,4 +182,5 @@ stdenv.mkDerivation (rec {
postInstall = crate_.postInstall or "";
dependencies = crate_.dependencies 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; }";
mkTest = crateArgs: let
@ -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" {
@ -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 // {