build-rust-crate: nixpkgs-fmt

launchpad/nixpkgs/master
happysalada 3 years ago committed by Raphael Megzari
parent 0e8d59e3cb
commit 0585c981f1
  1. 63
      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 [ "-" ] [ "_" ];
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}\"'');
@ -65,7 +78,8 @@ in
* 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;
@ -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
@ -298,7 +329,9 @@ stdenv.mkDerivation (rec {
outputDev = if buildTests then [ "out" ] else [ "lib" ];
} // extraDerivationAttrs
)) {
)
)
{
rust = rustc;
release = crate_.release or true;
verbose = crate_.verbose or true;

Loading…
Cancel
Save