buildRustCrate: builtins -> lib where possible

We can just use `lib` instead of `builtins` in all cases but the
`hashString` case. Also changed a few lines to make use of some optional
helpers from lib.
This commit is contained in:
Andreas Rammhold 2019-12-11 21:53:42 +01:00
parent 1eda713fa4
commit 0aac0e8d2c
No known key found for this signature in database
GPG Key ID: E432E410B5E48C86
4 changed files with 30 additions and 32 deletions

View File

@ -9,7 +9,7 @@
deps = makeDeps dependencies crateRenames; deps = makeDeps dependencies crateRenames;
rustcOpts = rustcOpts =
lib.lists.foldl' (opts: opt: opts + " " + opt) lib.foldl' (opts: opt: opts + " " + opt)
(if release then "-C opt-level=3" else "-C debuginfo=2") (if release then "-C opt-level=3" else "-C debuginfo=2")
(["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts); (["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts);
rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}"; rustcMeta = "-C metadata=${metadata} -C extra-filename=-${metadata}";

View File

@ -20,9 +20,9 @@
, verbose , verbose
, workspace_member }: , workspace_member }:
let version_ = lib.splitString "-" crateVersion; let version_ = lib.splitString "-" crateVersion;
versionPre = if lib.tail version_ == [] then "" else builtins.elemAt version_ 1; versionPre = if lib.tail version_ == [] then "" else lib.elemAt version_ 1;
version = lib.splitVersion (lib.head version_); version = lib.splitVersion (lib.head version_);
rustcOpts = lib.lists.foldl' (opts: opt: opts + " " + opt) rustcOpts = lib.foldl' (opts: opt: opts + " " + opt)
(if release then "-C opt-level=3" else "-C debuginfo=2") (if release then "-C opt-level=3" else "-C debuginfo=2")
(["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts); (["-C codegen-units=$NIX_BUILD_CORES"] ++ extraRustcOpts);
buildDeps = makeDeps buildDependencies crateRenames; buildDeps = makeDeps buildDependencies crateRenames;
@ -90,9 +90,9 @@ in ''
export HOST="${stdenv.hostPlatform.config}" export HOST="${stdenv.hostPlatform.config}"
export PROFILE=${if release then "release" else "debug"} export PROFILE=${if release then "release" else "debug"}
export OUT_DIR=$(pwd)/target/build/${crateName}.out export OUT_DIR=$(pwd)/target/build/${crateName}.out
export CARGO_PKG_VERSION_MAJOR=${builtins.elemAt version 0} export CARGO_PKG_VERSION_MAJOR=${lib.elemAt version 0}
export CARGO_PKG_VERSION_MINOR=${builtins.elemAt version 1} export CARGO_PKG_VERSION_MINOR=${lib.elemAt version 1}
export CARGO_PKG_VERSION_PATCH=${builtins.elemAt version 2} export CARGO_PKG_VERSION_PATCH=${lib.elemAt version 2}
export CARGO_PKG_VERSION_PRE="${versionPre}" export CARGO_PKG_VERSION_PRE="${versionPre}"
export CARGO_PKG_HOMEPAGE="${crateHomepage}" export CARGO_PKG_HOMEPAGE="${crateHomepage}"
export NUM_JOBS=1 export NUM_JOBS=1

View File

@ -16,12 +16,12 @@ let
makeDeps = dependencies: crateRenames: makeDeps = dependencies: crateRenames:
(lib.concatMapStringsSep " " (dep: (lib.concatMapStringsSep " " (dep:
let let
extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; extern = lib.replaceStrings ["-"] ["_"] dep.libName;
name = if builtins.hasAttr dep.crateName crateRenames then name = if lib.hasAttr dep.crateName crateRenames then
lib.strings.replaceStrings ["-"] ["_"] crateRenames.${dep.crateName} lib.strings.replaceStrings ["-"] ["_"] crateRenames.${dep.crateName}
else else
extern; extern;
in (if lib.lists.any (x: x == "lib") dep.crateType then in (if lib.any (x: x == "lib") 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}")
@ -61,8 +61,7 @@ let
configureCrate = import ./configure-crate.nix { inherit lib stdenv echo_build_heading noisily makeDeps; }; configureCrate = import ./configure-crate.nix { inherit lib stdenv echo_build_heading noisily makeDeps; };
buildCrate = import ./build-crate.nix { inherit lib stdenv echo_build_heading noisily makeDeps rust; }; buildCrate = import ./build-crate.nix { inherit lib stdenv echo_build_heading noisily makeDeps rust; };
installCrate = import ./install-crate.nix; installCrate = import ./install-crate.nix;
in
in
crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides, crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides,
dependencies, buildDependencies, crateRenames, dependencies, buildDependencies, crateRenames,
@ -95,34 +94,33 @@ stdenv.mkDerivation (rec {
depsBuildBuild = [ rust stdenv.cc ]; depsBuildBuild = [ rust stdenv.cc ];
buildInputs = (crate.buildInputs or []) ++ buildInputs_; buildInputs = (crate.buildInputs or []) ++ buildInputs_;
dependencies = dependencies =
builtins.map map
(dep: lib.getLib (dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })) (dep: lib.getLib (dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; }))
dependencies_; dependencies_;
buildDependencies = buildDependencies =
builtins.map map
(dep: lib.getLib (dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; })) (dep: lib.getLib (dep.override { rust = rust; release = release; verbose = verbose; crateOverrides = crateOverrides; }))
buildDependencies_; buildDependencies_;
completeDeps = lib.lists.unique (dependencies ++ lib.lists.concatMap (dep: dep.completeDeps) dependencies); completeDeps = lib.unique (dependencies ++ lib.concatMap (dep: dep.completeDeps) dependencies);
completeBuildDeps = lib.lists.unique ( completeBuildDeps = lib.unique (
buildDependencies buildDependencies
++ lib.lists.concatMap (dep: dep.completeBuildDeps ++ dep.completeDeps) buildDependencies ++ lib.concatMap (dep: dep.completeBuildDeps ++ dep.completeDeps) buildDependencies
); );
crateFeatures = if crate ? features then crateFeatures = lib.optionalString (crate ? features)
lib.concatMapStringsSep " " (f: "--cfg feature=\\\"${f}\\\"") (crate.features ++ features) #" (lib.concatMapStringsSep " " (f: "--cfg feature=\\\"${f}\\\"") (crate.features ++ features));
else "";
libName = if crate ? libName then crate.libName else crate.crateName; libName = if crate ? libName then crate.libName else crate.crateName;
libPath = if crate ? libPath then crate.libPath else ""; libPath = if crate ? libPath then crate.libPath else "";
depsMetadata = builtins.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies); depsMetadata = lib.foldl' (str: dep: str + dep.metadata) "" (dependencies ++ buildDependencies);
metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion + "___" + toString crateFeatures + "___" + depsMetadata )); metadata = lib.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion + "___" + toString crateFeatures + "___" + depsMetadata ));
crateBin = if crate ? crateBin then crateBin = if crate ? crateBin then
builtins.foldl' (bins: bin: let lib.foldl' (bins: bin: let
name = (if bin ? name then bin.name else crateName); name = if bin ? name then bin.name else crateName;
path = if bin ? path then bin.path else ""; path = if bin ? path then bin.path else "";
in in
bins + (if bin == "" then "" else ",") + "${name} ${path}" bins + (if bin == "" then "" else ",") + "${name} ${path}"
@ -142,9 +140,9 @@ stdenv.mkDerivation (rec {
if lib.attrByPath ["plugin"] false crate then ["dylib"] else if lib.attrByPath ["plugin"] false crate then ["dylib"] else
(crate.type or ["lib"]); (crate.type or ["lib"]);
colors = lib.attrByPath [ "colors" ] "always" crate; colors = lib.attrByPath [ "colors" ] "always" crate;
extraLinkFlags = builtins.concatStringsSep " " (crate.extraLinkFlags or []); extraLinkFlags = lib.concatStringsSep " " (crate.extraLinkFlags or []);
edition = crate.edition or null; edition = crate.edition or null;
extraRustcOpts = (if crate ? extraRustcOpts then crate.extraRustcOpts else []) ++ extraRustcOpts_ ++ (lib.optional (edition != null) "--edition ${edition}"); extraRustcOpts = lib.optionals (crate ? extraRustcOpts) crate.extraRustcOpts ++ extraRustcOpts_ ++ (lib.optional (edition != null) "--edition ${edition}");
configurePhase = configureCrate { configurePhase = configureCrate {
inherit crateName buildDependencies completeDeps completeBuildDeps crateDescription inherit crateName buildDependencies completeDeps completeBuildDeps crateDescription

View File

@ -3,23 +3,23 @@
kernel = stdenv.hostPlatform.parsed.kernel.name; kernel = stdenv.hostPlatform.parsed.kernel.name;
abi = stdenv.hostPlatform.parsed.abi.name; abi = stdenv.hostPlatform.parsed.abi.name;
cpu = stdenv.hostPlatform.parsed.cpu.name; cpu = stdenv.hostPlatform.parsed.cpu.name;
updateFeatures = f: up: functions: builtins.deepSeq f (lib.lists.foldl' (features: fun: fun features) (lib.attrsets.recursiveUpdate f up) functions); updateFeatures = f: up: functions: lib.deepSeq f (lib.foldl' (features: fun: fun features) (lib.attrsets.recursiveUpdate f up) functions);
mapFeatures = features: map (fun: fun { features = features; }); mapFeatures = features: map (fun: fun { features = features; });
mkFeatures = feat: lib.lists.foldl (features: featureName: mkFeatures = feat: lib.foldl (features: featureName:
if feat.${featureName} or false then if feat.${featureName} or false then
[ featureName ] ++ features [ featureName ] ++ features
else else
features features
) [] (builtins.attrNames feat); ) [] (lib.attrNames feat);
include = includedFiles: src: builtins.filterSource (path: type: include = includedFiles: src: lib.filterSource (path: type:
lib.lists.any (f: lib.any (f:
let p = toString (src + ("/" + f)); let p = toString (src + ("/" + f));
in in
p == path || (lib.strings.hasPrefix (p + "/") path) p == path || (lib.strings.hasPrefix (p + "/") path)
) includedFiles ) includedFiles
) src; ) src;
exclude = excludedFiles: src: builtins.filterSource (path: type: exclude = excludedFiles: src: lib.filterSource (path: type:
lib.lists.all (f: lib.all (f:
!lib.strings.hasPrefix (toString (src + ("/" + f))) path !lib.strings.hasPrefix (toString (src + ("/" + f))) path
) excludedFiles ) excludedFiles
) src; ) src;