build-rust-crate: nixpkgs-fmt

launchpad/nixpkgs/master
happysalada 3 years ago committed by Raphael Megzari
parent 0e8d59e3cb
commit 0585c981f1
  1. 105
      pkgs/build-support/rust/build-rust-crate/default.nix

@ -4,8 +4,16 @@
# This can be useful for deploying packages with NixOps, and to share
# binary dependencies between projects.
{ lib, stdenv, defaultCrateOverrides, fetchCrate, pkgsBuildBuild, rustc, rust
, cargo, jq }:
{ lib
, stdenv
, defaultCrateOverrides
, fetchCrate
, pkgsBuildBuild
, rustc
, rust
, cargo
, jq
}:
let
# Create rustc arguments to link against the given list of dependencies
@ -13,18 +21,21 @@ let
#
# See docs for crateRenames below.
mkRustcDepArgs = dependencies: crateRenames:
lib.concatMapStringsSep " " (dep:
lib.concatMapStringsSep " "
(dep:
let
normalizeName = lib.replaceStrings ["-"] ["_"];
normalizeName = lib.replaceStrings [ "-" ] [ "_" ];
extern = normalizeName dep.libName;
# Find a choice that matches in name and optionally version.
findMatchOrUseExtern = choices:
lib.findFirst (choice:
lib.findFirst
(choice:
(!(choice ? version)
|| choice.version == dep.version or ""))
{ rename = extern; }
choices;
name = if lib.hasAttr dep.crateName crateRenames then
name =
if lib.hasAttr dep.crateName crateRenames then
let choices = crateRenames.${dep.crateName};
in
normalizeName (
@ -34,11 +45,13 @@ let
)
else
extern;
in (if lib.any (x: x == "lib" || x == "rlib") dep.crateType then
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}")
) dependencies;
)
dependencies;
# Create feature arguments for rustc.
mkRustcFeatureArgs = lib.concatMapStringsSep " " (f: ''--cfg feature=\"${f}\"'');
@ -60,12 +73,13 @@ let
rustAttrs = rust;
in
/* The overridable pkgs.buildRustCrate function.
/* The overridable pkgs.buildRustCrate function.
*
* Any unrecognized parameters will be passed as to
* the underlying stdenv.mkDerivation.
*/
crate_: lib.makeOverridable (
crate_: lib.makeOverridable
(
# The rust compiler to use.
#
# Default: pkgs.rustc
@ -185,13 +199,28 @@ in
, postInstall
}:
let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverrides crate_);
let
crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: { }) crateOverrides crate_);
dependencies_ = dependencies;
buildDependencies_ = buildDependencies;
processedAttrs = [
"src" "nativeBuildInputs" "buildInputs" "crateBin" "crateLib" "libName" "libPath"
"buildDependencies" "dependencies" "features" "crateRenames"
"crateName" "version" "build" "authors" "colors" "edition"
"src"
"nativeBuildInputs"
"buildInputs"
"crateBin"
"crateLib"
"libName"
"libPath"
"buildDependencies"
"dependencies"
"features"
"crateRenames"
"crateName"
"version"
"build"
"authors"
"colors"
"edition"
"buildTests"
];
extraDerivationAttrs = builtins.removeAttrs crate processedAttrs;
@ -203,10 +232,10 @@ let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverr
# crate2nix has a hack for the old bash based build script that did split
# entries at `,`. No we have to work around that hack.
# https://github.com/kolloch/crate2nix/blame/5b19c1b14e1b0e5522c3e44e300d0b332dc939e7/crate2nix/templates/build.nix.tera#L89
crateBin = lib.filter (bin: !(bin ? name && bin.name == ",")) (crate.crateBin or []);
crateBin = lib.filter (bin: !(bin ? name && bin.name == ",")) (crate.crateBin or [ ]);
hasCrateBin = crate ? crateBin;
in
stdenv.mkDerivation (rec {
in
stdenv.mkDerivation (rec {
inherit (crate) crateName;
inherit
@ -228,8 +257,8 @@ stdenv.mkDerivation (rec {
name = "rust_${crate.crateName}-${crate.version}${lib.optionalString buildTests_ "-test"}";
version = crate.version;
depsBuildBuild = [ pkgsBuildBuild.stdenv.cc ];
nativeBuildInputs = [ rust stdenv.cc cargo jq ] ++ (crate.nativeBuildInputs or []) ++ nativeBuildInputs_;
buildInputs = (crate.buildInputs or []) ++ buildInputs_;
nativeBuildInputs = [ rust stdenv.cc cargo jq ] ++ (crate.nativeBuildInputs or [ ]) ++ nativeBuildInputs_;
buildInputs = (crate.buildInputs or [ ]) ++ buildInputs_;
dependencies = map lib.getLib dependencies_;
buildDependencies = map lib.getLib buildDependencies_;
@ -250,12 +279,14 @@ stdenv.mkDerivation (rec {
# Seed the symbol hashes with something unique every time.
# https://doc.rust-lang.org/1.0.0/rustc/metadata/loader/index.html#frobbing-symbols
metadata = let
metadata =
let
depsMetadata = lib.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
hashedMetadata = builtins.hashString "sha256"
(crateName + "-" + crateVersion + "___" + toString (mkRustcFeatureArgs crateFeatures) +
"___" + depsMetadata + "___" + rustAttrs.toRustTarget stdenv.hostPlatform);
in lib.substring 0 10 hashedMetadata;
in
lib.substring 0 10 hashedMetadata;
build = crate.build or "";
# Either set to a concrete sub path to the crate root
@ -263,14 +294,14 @@ stdenv.mkDerivation (rec {
workspace_member = crate.workspace_member or ".";
crateVersion = crate.version;
crateDescription = crate.description or "";
crateAuthors = if crate ? authors && lib.isList crate.authors then crate.authors else [];
crateAuthors = if crate ? authors && lib.isList crate.authors then crate.authors else [ ];
crateHomepage = crate.homepage or "";
crateType =
if lib.attrByPath ["procMacro"] false crate then ["proc-macro"] else
if lib.attrByPath ["plugin"] false crate then ["dylib"] else
(crate.type or ["lib"]);
if lib.attrByPath [ "procMacro" ] false crate then [ "proc-macro" ] else
if lib.attrByPath [ "plugin" ] false crate then [ "dylib" ] else
(crate.type or [ "lib" ]);
colors = lib.attrByPath [ "colors" ] "always" crate;
extraLinkFlags = lib.concatStringsSep " " (crate.extraLinkFlags or []);
extraLinkFlags = lib.concatStringsSep " " (crate.extraLinkFlags or [ ]);
edition = crate.edition or null;
extraRustcOpts =
lib.optionals (crate ? extraRustcOpts) crate.extraRustcOpts
@ -297,20 +328,22 @@ stdenv.mkDerivation (rec {
outputs = if buildTests then [ "out" ] else [ "out" "lib" ];
outputDev = if buildTests then [ "out" ] else [ "lib" ];
} // extraDerivationAttrs
)) {
} // extraDerivationAttrs
)
)
{
rust = rustc;
release = crate_.release or true;
verbose = crate_.verbose or true;
extraRustcOpts = [];
features = [];
nativeBuildInputs = [];
buildInputs = [];
extraRustcOpts = [ ];
features = [ ];
nativeBuildInputs = [ ];
buildInputs = [ ];
crateOverrides = defaultCrateOverrides;
preUnpack = crate_.preUnpack or "";
postUnpack = crate_.postUnpack or "";
prePatch = crate_.prePatch or "";
patches = crate_.patches or [];
patches = crate_.patches or [ ];
postPatch = crate_.postPatch or "";
preConfigure = crate_.preConfigure or "";
postConfigure = crate_.postConfigure or "";
@ -318,8 +351,8 @@ stdenv.mkDerivation (rec {
postBuild = crate_.postBuild or "";
preInstall = crate_.preInstall or "";
postInstall = crate_.postInstall or "";
dependencies = crate_.dependencies or [];
buildDependencies = crate_.buildDependencies or [];
crateRenames = crate_.crateRenames or {};
dependencies = crate_.dependencies or [ ];
buildDependencies = crate_.buildDependencies or [ ];
crateRenames = crate_.crateRenames or { };
buildTests = crate_.buildTests or false;
}

Loading…
Cancel
Save