Split build and build-plan configuration

This commit is contained in:
Nicolas Mattia 2019-11-18 16:41:28 +01:00
parent 9829b21bc1
commit 79ccb516e9
3 changed files with 98 additions and 102 deletions

View file

@ -29,7 +29,6 @@ src:
# Which drops the run-time dependency on the crates-io source thereby
# significantly reducing the Nix closure size.
, removeReferencesToSrcFromDocs
, cratePaths
, pname
, version
, rustc
@ -64,7 +63,6 @@ let
inherit
src
doCheck
cratePaths
version
preBuild;

View file

@ -1,10 +1,37 @@
{ lib, libb, builtinz, src, attrs }:
rec
{ usePureFromTOML = attrs.usePureFromTOML or true;
let
usePureFromTOML = attrs.usePureFromTOML or true;
readTOML = builtinz.readTOML usePureFromTOML;
# config used during build the prebuild and the final build
buildConfig =
{
compressTarget = attrs.compressTarget or true;
doCheck = attrs.doCheck or true;
buildInputs = attrs.buildInputs or [];
removeReferencesToSrcFromDocs = attrs.removeReferencesToSrcFromDocs or true;
doDoc = attrs.doDoc or true;
#| Whether or not the rustdoc can fail the build
doDocFail = attrs.doDocFail or false;
release = attrs.release or true;
override = attrs.override or (x: x);
cargoBuild = attrs.cargoBuild or ''
cargo build "''${cargo_release}" -j $NIX_BUILD_CORES -Z unstable-options --out-dir out
'';
# The list of _all_ crates (incl. transitive dependencies) with name,
# version and sha256 of the crate
# Example:
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
crateDependencies = libb.mkVersions buildPlanConfig.cargolock;
};
# config used when planning the builds
buildPlanConfig = rec
{
# Whether we skip pre-building the deps
isSingleStep = attrs.singleStep or false;
@ -63,55 +90,22 @@ rec
# The cargo lock
cargolock = readTOML (src + "/Cargo.lock");
# The list of paths to Cargo.tomls. If this is a workspace, the paths
# are the members. Otherwise, there is a single path, ".".
cratePaths = lib.concatStringsSep "\n" wantedMembers;
packageName = attrs.name or toplevelCargotoml.package.name or
(if isWorkspace then "rust-workspace" else "rust-package");
packageVersion = attrs.version or toplevelCargotoml.package.version or
"unknown";
# The list of _all_ crates (incl. transitive dependencies) with name,
# version and sha256 of the crate
# Example:
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
crateDependencies = libb.mkVersions cargolock;
preBuild = ''
# Cargo uses mtime, and we write `src/lib.rs` and `src/main.rs`in
# the dep build step, so make sure cargo rebuilds stuff
if [ -f src/lib.rs ] ; then touch src/lib.rs; fi
if [ -f src/main.rs ] ; then touch src/main.rs; fi
'';
cargoBuild = attrs.cargoBuild or ''
cargo build "''${cargo_release}" -j $NIX_BUILD_CORES -Z unstable-options --out-dir out
'';
cargoTestCommands = attrs.cargoTestCommands or [
''cargo test "''${cargo_release}" -j $NIX_BUILD_CORES''
];
override = attrs.override or (x: x);
release = attrs.release or true;
#| Whether or not to forward intermediate build artifacts to $out
copyTarget = attrs.copyTarget or false;
#| Whether or not the rustdoc can fail the build
doDocFail = attrs.doDocFail or false;
doDoc = attrs.doDoc or true;
copyBins = attrs.copyBins or true;
copyDocsToSeparateOutput = attrs.copyDocsToSeparateOutput or true;
removeReferencesToSrcFromDocs = attrs.removeReferencesToSrcFromDocs or true;
doCheck = attrs.doCheck or true;
buildInputs = attrs.buildInputs or [];
}
};
in buildPlanConfig // { inherit buildConfig ; }

View file

@ -44,9 +44,14 @@ let
(defaultBuildAttrs //
{ pname = config.packageName;
version = config.packageVersion;
inherit (config) cratePaths crateDependencies preBuild cargoBuild cargoTestCommands compressTarget override release copyTarget doDocFail doDoc copyBins copyDocsToSeparateOutput removeReferencesToSrcFromDocs doCheck buildInputs;
} //
(removeAttrs attrs [ "usePureFromTOML" "cargotomls" "singleStep" ]) //
preBuild = lib.optionalString (!config.isSingleStep) ''
# Cargo uses mtime, and we write `src/lib.rs` and `src/main.rs`in
# the dep build step, so make sure cargo rebuilds stuff
if [ -f src/lib.rs ] ; then touch src/lib.rs; fi
if [ -f src/main.rs ] ; then touch src/main.rs; fi
'';
inherit (config) cargoTestCommands copyTarget copyBins copyDocsToSeparateOutput ;
} // config.buildConfig //
{ builtDependencies = lib.optional (! config.isSingleStep)
(
import ./build.nix
@ -63,10 +68,9 @@ let
(defaultBuildAttrs //
{ pname = "${config.packageName}-deps";
version = config.packageVersion;
inherit (config) cratePaths crateDependencies cargoBuild compressTarget override release doDocFail doDoc removeReferencesToSrcFromDocs doCheck buildInputs;
} //
(removeAttrs attrs [ "usePureFromTOML" "cargotomls" "singleStep"]) //
} // config.buildConfig //
{ preBuild = "";
# TODO: custom cargoTestCommands should not be needed here
cargoTestCommands = map (cmd: "${cmd} || true") config.cargoTestCommands;
copyTarget = true;
copyBins = false;