0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

build-rust-crate: nixpkgs-fmt

This commit is contained in:
happysalada 2021-09-03 12:14:46 +09:00 committed by Raphael Megzari
parent 0e8d59e3cb
commit 0585c981f1

View file

@ -4,8 +4,16 @@
# This can be useful for deploying packages with NixOps, and to share # This can be useful for deploying packages with NixOps, and to share
# binary dependencies between projects. # binary dependencies between projects.
{ lib, stdenv, defaultCrateOverrides, fetchCrate, pkgsBuildBuild, rustc, rust { lib
, cargo, jq }: , stdenv
, defaultCrateOverrides
, fetchCrate
, pkgsBuildBuild
, rustc
, rust
, cargo
, jq
}:
let let
# Create rustc arguments to link against the given list of dependencies # Create rustc arguments to link against the given list of dependencies
@ -13,18 +21,21 @@ let
# #
# See docs for crateRenames below. # See docs for crateRenames below.
mkRustcDepArgs = dependencies: crateRenames: mkRustcDepArgs = dependencies: crateRenames:
lib.concatMapStringsSep " " (dep: lib.concatMapStringsSep " "
(dep:
let let
normalizeName = lib.replaceStrings [ "-" ] [ "_" ]; normalizeName = lib.replaceStrings [ "-" ] [ "_" ];
extern = normalizeName dep.libName; extern = normalizeName dep.libName;
# Find a choice that matches in name and optionally version. # Find a choice that matches in name and optionally version.
findMatchOrUseExtern = choices: findMatchOrUseExtern = choices:
lib.findFirst (choice: lib.findFirst
(choice:
(!(choice ? version) (!(choice ? version)
|| choice.version == dep.version or "")) || choice.version == dep.version or ""))
{ rename = extern; } { rename = extern; }
choices; choices;
name = if lib.hasAttr dep.crateName crateRenames then name =
if lib.hasAttr dep.crateName crateRenames then
let choices = crateRenames.${dep.crateName}; let choices = crateRenames.${dep.crateName};
in in
normalizeName ( normalizeName (
@ -34,11 +45,13 @@ let
) )
else else
extern; 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" " --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}.rlib"
else else
" --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}") " --extern ${name}=${dep.lib}/lib/lib${extern}-${dep.metadata}${stdenv.hostPlatform.extensions.sharedLibrary}")
) dependencies; )
dependencies;
# Create feature arguments for rustc. # Create feature arguments for rustc.
mkRustcFeatureArgs = lib.concatMapStringsSep " " (f: ''--cfg feature=\"${f}\"''); mkRustcFeatureArgs = lib.concatMapStringsSep " " (f: ''--cfg feature=\"${f}\"'');
@ -65,7 +78,8 @@ in
* Any unrecognized parameters will be passed as to * Any unrecognized parameters will be passed as to
* the underlying stdenv.mkDerivation. * the underlying stdenv.mkDerivation.
*/ */
crate_: lib.makeOverridable ( crate_: lib.makeOverridable
(
# The rust compiler to use. # The rust compiler to use.
# #
# Default: pkgs.rustc # Default: pkgs.rustc
@ -185,13 +199,28 @@ in
, postInstall , postInstall
}: }:
let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverrides crate_); let
crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: { }) crateOverrides crate_);
dependencies_ = dependencies; dependencies_ = dependencies;
buildDependencies_ = buildDependencies; buildDependencies_ = buildDependencies;
processedAttrs = [ processedAttrs = [
"src" "nativeBuildInputs" "buildInputs" "crateBin" "crateLib" "libName" "libPath" "src"
"buildDependencies" "dependencies" "features" "crateRenames" "nativeBuildInputs"
"crateName" "version" "build" "authors" "colors" "edition" "buildInputs"
"crateBin"
"crateLib"
"libName"
"libPath"
"buildDependencies"
"dependencies"
"features"
"crateRenames"
"crateName"
"version"
"build"
"authors"
"colors"
"edition"
"buildTests" "buildTests"
]; ];
extraDerivationAttrs = builtins.removeAttrs crate processedAttrs; extraDerivationAttrs = builtins.removeAttrs crate processedAttrs;
@ -250,12 +279,14 @@ stdenv.mkDerivation (rec {
# Seed the symbol hashes with something unique every time. # Seed the symbol hashes with something unique every time.
# https://doc.rust-lang.org/1.0.0/rustc/metadata/loader/index.html#frobbing-symbols # 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); depsMetadata = lib.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
hashedMetadata = builtins.hashString "sha256" hashedMetadata = builtins.hashString "sha256"
(crateName + "-" + crateVersion + "___" + toString (mkRustcFeatureArgs crateFeatures) + (crateName + "-" + crateVersion + "___" + toString (mkRustcFeatureArgs crateFeatures) +
"___" + depsMetadata + "___" + rustAttrs.toRustTarget stdenv.hostPlatform); "___" + depsMetadata + "___" + rustAttrs.toRustTarget stdenv.hostPlatform);
in lib.substring 0 10 hashedMetadata; in
lib.substring 0 10 hashedMetadata;
build = crate.build or ""; build = crate.build or "";
# Either set to a concrete sub path to the crate root # 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" ]; outputDev = if buildTests then [ "out" ] else [ "lib" ];
} // extraDerivationAttrs } // extraDerivationAttrs
)) { )
)
{
rust = rustc; rust = rustc;
release = crate_.release or true; release = crate_.release or true;
verbose = crate_.verbose or true; verbose = crate_.verbose or true;