mirror of
https://github.com/nix-community/naersk
synced 2024-11-26 05:20:18 +00:00
Fix features rebuild
This commit is contained in:
parent
fc13d3d7b8
commit
51e8f903c4
4 changed files with 181 additions and 179 deletions
66
build.nix
66
build.nix
|
@ -2,11 +2,13 @@ src:
|
|||
{ #| What command to run during the build phase
|
||||
cargoBuild
|
||||
, #| What command to run during the test phase
|
||||
cargoTest ? "cargo test --$CARGO_BUILD_PROFILE"
|
||||
cargoTest
|
||||
#| Whether or not to forward build artifacts to $out
|
||||
, copyBuildArtifacts ? false
|
||||
, doCheck ? true
|
||||
, doDoc ? true
|
||||
#| Whether or not the rustdoc can fail the build
|
||||
, doDocFail ? false
|
||||
, copyDocsToSeparateOutput ? true
|
||||
#| Whether to remove references to source code from the generated cargo docs
|
||||
# to reduce Nix closure size. By default cargo doc includes snippets like the
|
||||
|
@ -32,8 +34,7 @@ src:
|
|||
, buildInputs ? []
|
||||
, nativeBuildInputs ? []
|
||||
, builtDependencies ? []
|
||||
, cargolock ? null
|
||||
, cargotoml ? null
|
||||
, replaceToml ? true
|
||||
, release ? true
|
||||
, stdenv
|
||||
, lib
|
||||
|
@ -46,6 +47,7 @@ src:
|
|||
, runCommand
|
||||
, remarshal
|
||||
, crateDependencies
|
||||
# TODO: rename to "members"
|
||||
, cratePaths
|
||||
}:
|
||||
|
||||
|
@ -67,18 +69,18 @@ with rec
|
|||
name
|
||||
version;
|
||||
|
||||
cargoconfig = builtinz.toTOML
|
||||
{ source =
|
||||
{ crates-io = { replace-with = "nix-sources"; } ;
|
||||
nix-sources =
|
||||
{ directory = symlinkJoin
|
||||
{ name = "crates-io";
|
||||
paths = map (v: unpackCrate v.name v.version v.sha256)
|
||||
crateDependencies;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
cargoconfig = builtinz.toTOML
|
||||
{ source =
|
||||
{ crates-io = { replace-with = "nix-sources"; } ;
|
||||
nix-sources =
|
||||
{ directory = symlinkJoin
|
||||
{ name = "crates-io";
|
||||
paths = map (v: unpackCrate v.name v.version v.sha256)
|
||||
crateDependencies;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
outputs = [ "out" ] ++ lib.optional (doDoc && copyDocsToSeparateOutput) "doc";
|
||||
preInstallPhases = lib.optional doDoc [ "docPhase" ];
|
||||
|
@ -114,26 +116,6 @@ with rec
|
|||
''
|
||||
runHook preConfigure
|
||||
|
||||
if [ -n "$cargolock" ]
|
||||
then
|
||||
echo "Setting Cargo.lock"
|
||||
if [ -f "Cargo.lock" ]
|
||||
then
|
||||
echo "WARNING: replacing existing Cargo.lock"
|
||||
fi
|
||||
install -m 644 "$cargolock" Cargo.lock
|
||||
fi
|
||||
|
||||
if [ -n "$cargotoml" ]
|
||||
then
|
||||
echo "Setting Cargo.toml"
|
||||
if [ -f "Cargo.toml" ]
|
||||
then
|
||||
echo "WARNING: replacing existing Cargo.toml"
|
||||
fi
|
||||
install -m 644 "$cargotoml" Cargo.toml
|
||||
fi
|
||||
|
||||
mkdir -p target
|
||||
|
||||
cat ${builtinz.writeJSON "dependencies-json" builtDependencies} |\
|
||||
|
@ -165,8 +147,7 @@ with rec
|
|||
''
|
||||
runHook preBuild
|
||||
|
||||
echo "Running build command:"
|
||||
echo " ${cargoBuild}"
|
||||
echo "Building..."
|
||||
${cargoBuild}
|
||||
|
||||
runHook postBuild
|
||||
|
@ -176,8 +157,7 @@ with rec
|
|||
''
|
||||
runHook preCheck
|
||||
|
||||
echo "Running test command:"
|
||||
echo " ${cargoTest}"
|
||||
echo "Testing..."
|
||||
${cargoTest}
|
||||
|
||||
runHook postCheck
|
||||
|
@ -200,7 +180,7 @@ with rec
|
|||
cargoDoc="cargo doc --offline $doc_arg"
|
||||
echo "Running doc command:"
|
||||
echo " $cargoDoc"
|
||||
$cargoDoc
|
||||
$cargoDoc || ${if doDocFail then "false" else "true" }
|
||||
|
||||
${lib.optionalString removeReferencesToSrcFromDocs ''
|
||||
# Remove references to the source derivation to reduce closure size
|
||||
|
@ -253,11 +233,7 @@ with rec
|
|||
|
||||
runHook postInstall
|
||||
'';
|
||||
} //
|
||||
lib.optionalAttrs (! isNull cargolock )
|
||||
{ cargolock = builtinz.writeTOML "Cargo.lock" cargolock; } //
|
||||
lib.optionalAttrs (! isNull cargotoml )
|
||||
{ cargotoml = builtinz.writeTOML "Cargo.toml" cargotoml; }
|
||||
}
|
||||
)
|
||||
;
|
||||
|
||||
|
|
234
default.nix
234
default.nix
|
@ -15,8 +15,8 @@
|
|||
with
|
||||
{ libb = import ./lib.nix { inherit lib writeText runCommand remarshal; }; };
|
||||
|
||||
with
|
||||
{ defaultBuildAttrs =
|
||||
let
|
||||
defaultBuildAttrs =
|
||||
{ inherit
|
||||
llvmPackages
|
||||
jq
|
||||
|
@ -30,65 +30,84 @@ with
|
|||
symlinkJoin
|
||||
cargo
|
||||
rustc;
|
||||
};
|
||||
};
|
||||
}; in
|
||||
|
||||
with
|
||||
{ builtinz =
|
||||
let
|
||||
builtinz =
|
||||
builtins //
|
||||
import ./builtins
|
||||
{ inherit lib writeText remarshal runCommand ; };
|
||||
};
|
||||
{ inherit lib writeText remarshal runCommand ; }; in
|
||||
|
||||
# Crate building
|
||||
with rec
|
||||
{
|
||||
commonAttrs = src: attrs: rec
|
||||
{ usePureFromTOML = attrs.usePureFromTOML or true;
|
||||
cargolock = attrs.cargolock or null;
|
||||
cargotoml = attrs.cargotoml or null;
|
||||
cargolock' =
|
||||
if isNull cargolock then
|
||||
builtinz.readTOML usePureFromTOML "${src}/Cargo.lock"
|
||||
else cargolock;
|
||||
rootCargotoml =
|
||||
if isNull cargotoml then
|
||||
builtinz.readTOML usePureFromTOML "${src}/Cargo.toml"
|
||||
else cargotoml;
|
||||
readTOML = builtinz.readTOML usePureFromTOML;
|
||||
|
||||
# All the Cargo.tomls, including the top-level one
|
||||
# The members we want to build
|
||||
wantedMembers =
|
||||
lib.mapAttrsToList (member: _cargotoml: member) wantedMemberCargotomls;
|
||||
|
||||
# Member path to cargotoml
|
||||
wantedMemberCargotomls =
|
||||
let pred =
|
||||
if ! isWorkspace
|
||||
then (_member: _cargotoml: true)
|
||||
else
|
||||
if builtins.hasAttr "targets" attrs
|
||||
then (_member: cargotoml: lib.elem cargotoml.package.name attrs.targets)
|
||||
else (member: _cargotoml: member != "."); in
|
||||
lib.filterAttrs pred cargotomls;
|
||||
|
||||
# All cargotoml, from path to nix object
|
||||
cargotomls =
|
||||
if builtins.hasAttr "package" rootCargotoml then
|
||||
[rootCargotoml]
|
||||
else
|
||||
with { members = rootCargotoml.workspace.members or []; };
|
||||
lib.filter (cargotoml:
|
||||
if builtins.hasAttr "targets" attrs then
|
||||
lib.elem cargotoml.package.name attrs.targets
|
||||
else true
|
||||
) ( map
|
||||
(member: (builtinz.readTOML usePureFromTOML "${src}/${member}/Cargo.toml"))
|
||||
members );
|
||||
let readTOML = builtinz.readTOML usePureFromTOML; in
|
||||
|
||||
{ "." = toplevelCargotoml; } //
|
||||
lib.optionalAttrs isWorkspace
|
||||
(lib.listToAttrs
|
||||
(map
|
||||
(member:
|
||||
{ name = member;
|
||||
value = readTOML (src + "/${member}/Cargo.toml");
|
||||
}
|
||||
)
|
||||
(toplevelCargotoml.workspace.members or [])
|
||||
)
|
||||
);
|
||||
|
||||
# Are we building a workspace (or is this a simple crate) ?
|
||||
isWorkspace = builtins.hasAttr "workspace" toplevelCargotoml;
|
||||
|
||||
# The top level Cargo.toml, either a workspace or package
|
||||
toplevelCargotoml = readTOML (src + "/Cargo.toml");
|
||||
|
||||
# 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 =
|
||||
with rec
|
||||
{ workspaceMembers = rootCargotoml.workspace.members or null;
|
||||
};
|
||||
|
||||
if isNull workspaceMembers then "."
|
||||
else
|
||||
let pred = if builtins.hasAttr "targets" attrs then
|
||||
(member: lib.elem member attrs.targets)
|
||||
else (_member: true);
|
||||
in lib.concatStringsSep "\n" (lib.filter pred workspaceMembers);
|
||||
crateDependencies = libb.mkVersions cargolock';
|
||||
cratePaths = lib.concatStringsSep "\n" wantedMembers;
|
||||
crateDependencies = libb.mkVersions cargolock;
|
||||
cargoBuild = attrs.cargoBuild or
|
||||
''
|
||||
# Cargo uses mtime, and we write `src/main.rs` in the dep build
|
||||
# step, so make sure cargo rebuilds stuff
|
||||
find . -type f -name '*.rs' -exec touch {} +
|
||||
for p in $cratePaths; do
|
||||
pushd "$p"
|
||||
echo "Building $p"
|
||||
cargo build --$CARGO_BUILD_PROFILE -j $NIX_BUILD_CORES
|
||||
popd
|
||||
done
|
||||
'';
|
||||
cargoTest = attrs.cargoTest or
|
||||
''
|
||||
for p in $cratePaths; do
|
||||
pushd "$p"
|
||||
cargo build --$CARGO_BUILD_PROFILE -j $NIX_BUILD_CORES
|
||||
echo "Running tests for $p"
|
||||
cargo test --$CARGO_BUILD_PROFILE -j $NIX_BUILD_CORES
|
||||
popd
|
||||
done
|
||||
'';
|
||||
|
@ -97,93 +116,58 @@ with rec
|
|||
with (commonAttrs src attrs);
|
||||
import ./build.nix src
|
||||
( defaultBuildAttrs //
|
||||
{ name =
|
||||
if lib.length cargotomls == 0 then
|
||||
abort "Found no cargotomls"
|
||||
else if lib.length cargotomls == 1 then
|
||||
(lib.head cargotomls).package.name
|
||||
else
|
||||
"${(lib.head cargotomls).package.name}-and-others";
|
||||
version = (lib.head cargotomls).package.version;
|
||||
inherit cratePaths crateDependencies cargoBuild;
|
||||
{ name = "some-name";
|
||||
version = "some-version";
|
||||
inherit cratePaths crateDependencies cargoBuild cargoTest;
|
||||
} //
|
||||
(removeAttrs attrs [ "targets" "usePureFromTOML" ])
|
||||
(removeAttrs attrs [ "targets" "usePureFromTOML" "cargotomls" ])
|
||||
);
|
||||
|
||||
buildPackageIncremental = src: attrs:
|
||||
with (commonAttrs src attrs);
|
||||
with rec
|
||||
# FIXME: directDependencies should be built on a per-cargotoml basis.
|
||||
# All dependencies are not available in every member.
|
||||
# Also, if a dependency is shared between two cargotomls, there's
|
||||
# (most of the time) no point recompiling it
|
||||
{ buildDepsScript = writeText "prebuild-script"
|
||||
''
|
||||
cat ${builtinz.writeJSON "crates" ((directDependenciesList))} |\
|
||||
jq -r \
|
||||
--arg cbp $CARGO_BUILD_PROFILE \
|
||||
--arg nbc $NIX_BUILD_CORES \
|
||||
'.[] | "cargo build --\($cbp) -j \($nbc) -p \(.name):\(.version)"' |\
|
||||
while IFS= read -r c
|
||||
do
|
||||
echo "Running build command '$c'"
|
||||
$c || echo "WARNING: one some dependencies failed to build: $c"
|
||||
done
|
||||
'';
|
||||
isMember = name:
|
||||
lib.elem name (map (ctoml: ctoml.package.name) cargotomls);
|
||||
|
||||
isLocal = v:
|
||||
! builtins.hasAttr "path" v;
|
||||
|
||||
versions =
|
||||
lib.listToAttrs (
|
||||
map (v: { name = v.name; value = v.version; })
|
||||
crateDependencies);
|
||||
|
||||
directDependenciesList =
|
||||
lib.filter (c:
|
||||
builtins.hasAttr c.name directDependencies) crateDependencies;
|
||||
|
||||
directDependencies =
|
||||
lib.filterAttrs (_: v:
|
||||
lib.isString v ||
|
||||
(! builtins.hasAttr "path" v)
|
||||
) (
|
||||
lib.foldr (x: y: x // y) {}
|
||||
(map (cargotoml:
|
||||
(lib.optionalAttrs (builtins.hasAttr "dependencies" cargotoml)
|
||||
cargotoml.dependencies) //
|
||||
(lib.optionalAttrs (builtins.hasAttr "dev-dependencies" cargotoml)
|
||||
cargotoml.dev-dependencies)
|
||||
) cargotomls
|
||||
));
|
||||
};
|
||||
buildPackageSingleStep src
|
||||
((attrs) //
|
||||
{ builtDependencies =
|
||||
[(
|
||||
buildPackageSingleStep (libb.dummySrc src)
|
||||
(attrs //
|
||||
{ cargoBuild = "source ${buildDepsScript}";
|
||||
doCheck = false;
|
||||
copyBuildArtifacts = true;
|
||||
copyDocsToSeparateOutput = false;
|
||||
cargolock = cargolock';
|
||||
cargotoml =
|
||||
{ package = { name = "dummy"; version = "0.0.0"; }; } //
|
||||
{ dependencies = directDependencies; }
|
||||
;
|
||||
name =
|
||||
if lib.length cargotomls == 0 then
|
||||
abort "Found no cargotomls"
|
||||
else if lib.length cargotomls == 1 then
|
||||
"${(lib.head cargotomls).package.name}-deps"
|
||||
else
|
||||
"${(lib.head cargotomls).package.name}-and-others-deps";
|
||||
})
|
||||
)];
|
||||
});
|
||||
import ./build.nix src
|
||||
(defaultBuildAttrs //
|
||||
{ name = "foo";
|
||||
version = "bar";
|
||||
inherit cratePaths crateDependencies cargoBuild cargoTest;
|
||||
} //
|
||||
(removeAttrs attrs [ "targets" "usePureFromTOML" "cargotomls" ]) //
|
||||
{ builtDependencies =
|
||||
[(
|
||||
import ./build.nix
|
||||
(libb.dummySrc
|
||||
{ cargoconfig =
|
||||
if builtinz.pathExists (src + "/.cargo/config")
|
||||
then builtins.readFile (src + "/.cargo/config")
|
||||
else "";
|
||||
cargolock = cargolock;
|
||||
cargotomls = cargotomls;
|
||||
}
|
||||
)
|
||||
(defaultBuildAttrs //
|
||||
{ name = "foo-deps";
|
||||
version = "bar";
|
||||
inherit cratePaths crateDependencies ;
|
||||
} //
|
||||
(removeAttrs attrs [ "targets" "usePureFromTOML" "cargotomls" ]) //
|
||||
{ cargoBuild =
|
||||
''
|
||||
for p in $cratePaths; do
|
||||
pushd "$p"
|
||||
cargo build --$CARGO_BUILD_PROFILE -j $NIX_BUILD_CORES || true
|
||||
cargo test --$CARGO_BUILD_PROFILE -j $NIX_BUILD_CORES || true
|
||||
popd
|
||||
done
|
||||
'';
|
||||
cargoTest = "echo no tests for deps";
|
||||
doCheck = false;
|
||||
copyBuildArtifacts = true;
|
||||
copyDocsToSeparateOutput = false;
|
||||
name = "some-name";
|
||||
}
|
||||
)
|
||||
)];
|
||||
});
|
||||
};
|
||||
|
||||
{ inherit buildPackageSingleStep buildPackageIncremental crates;
|
||||
|
|
56
lib.nix
56
lib.nix
|
@ -82,20 +82,62 @@ rec
|
|||
};
|
||||
|
||||
# A very minimal 'src' which makes cargo happy nonetheless
|
||||
dummySrc = src:
|
||||
dummySrc =
|
||||
{ cargoconfig # string
|
||||
, cargotomls # attrset
|
||||
, cargolock # attrset
|
||||
}:
|
||||
let
|
||||
configContent =
|
||||
if builtinz.pathExists "${src}/.cargo/config"
|
||||
then builtins.readFile "${src}/.cargo/config" else "";
|
||||
config = writeText "config" configContent;
|
||||
config = writeText "config" cargoconfig;
|
||||
cargolock' = builtinz.writeTOML "Cargo.toml" cargolock;
|
||||
cargotomlss = writeText "foo"
|
||||
(lib.concatStrings (lib.mapAttrsToList
|
||||
(k: v: "${k}\n${builtinz.writeTOML "Cargo.toml-ds-aa" v}\n")
|
||||
cargotomls
|
||||
));
|
||||
|
||||
in
|
||||
runCommand "dummy-src" {}
|
||||
''
|
||||
mkdir -p $out/.cargo
|
||||
cp -r ${config} $out/.cargo/config
|
||||
cp ${cargolock'} $out/Cargo.lock
|
||||
|
||||
mkdir -p $out/src
|
||||
touch $out/src/main.rs
|
||||
cat ${cargotomlss} | \
|
||||
while IFS= read -r member; do
|
||||
read -r cargotoml
|
||||
final_dir="$out/$member"
|
||||
mkdir -p "$final_dir"
|
||||
final_path="$final_dir/Cargo.toml"
|
||||
cp $cargotoml "$final_path"
|
||||
|
||||
# make sure cargo is happy
|
||||
pushd $out/$member > /dev/null
|
||||
|
||||
# TODO: get rid of all these hacks
|
||||
|
||||
sed -i '/.*=\s*{.*path\s*=.*/d' Cargo.toml
|
||||
sed -i '/^build\s*=/d' Cargo.toml
|
||||
|
||||
mkdir -p src
|
||||
touch src/main.rs
|
||||
touch src/lib.rs
|
||||
# Dirty hack to "touch" any bench, test, etc path.
|
||||
cat Cargo.toml | \
|
||||
grep -oP '^path\s*=\s*"\K\w+.rs' | \
|
||||
while IFS= read -r path; do
|
||||
mkdir -p $(dirname $path)
|
||||
touch $path
|
||||
done || true
|
||||
cat Cargo.toml | \
|
||||
grep -oP '^name\s*=\s*"\K\w+' | \
|
||||
while IFS= read -r name; do
|
||||
mkdir -p $name
|
||||
mkdir -p benches
|
||||
touch benches/$name.rs
|
||||
done || true
|
||||
popd > /dev/null
|
||||
done
|
||||
'';
|
||||
|
||||
mkPackages = cargolock:
|
||||
|
|
4
test.nix
4
test.nix
|
@ -18,14 +18,14 @@ with
|
|||
{ builtinz = builtins // pkgs.callPackage ./builtins {}; };
|
||||
|
||||
rec
|
||||
{ rustfmt = naersk.buildPackage sources.rustfmt {};
|
||||
{ rustfmt = naersk.buildPackage sources.rustfmt { doDocFail = false; };
|
||||
rustfmt_test = pkgs.runCommand "rustfmt-test"
|
||||
{ buildInputs = [ rustfmt ]; }
|
||||
"rustfmt --help && cargo-fmt --help && touch $out";
|
||||
|
||||
ripgrep = naersk.buildPackage sources.ripgrep { usePureFromTOML = false; };
|
||||
# XXX: the `rg` executable is missing because we don't do `cargo install
|
||||
# --path`.
|
||||
# --path .`.
|
||||
# ripgrep_test = pkgs.runCommand "ripgrep-test"
|
||||
# { buildInputs = [ ripgrep ]; }
|
||||
# "rg --help && touch $out";
|
||||
|
|
Loading…
Reference in a new issue