nixpkgs-suyu/pkgs/build-support/rust/build-rust-crate/install-crate.nix
Andreas Rammhold a3a51763f9
buildRustCrate: add buildTests flag to tell rustc to build tests instead of binaries
This helps us instruct rustc to build tests instead of binaries. The
actual build will then ONLY produce test executables. This is a first
step towards having rust crate tests within nixpkgs.

We default back to only a single output in test cases since that is the
only reasonable thing to do here.

Producing libraries or binaries in addition to tests would theoretically
be feasible but usually generates different dependency trees. It is very
common to have some libraries in `[dev-depdendencies]` within Cargo.toml
just for your tests. To not start mixing things up going with a
dedicated derivation for the test build sounds like the best choice for
now.

To use this you must provide a proper test dependency chain to
`buildRustCrate` (as you would usually do with your non-test inputs).
And then set the `buildTests` attribute to `true`. The derivation will
then contain all tests that were built in `$out/tests`. All common test
patterns and directories should be supported and tested by this change.

Below is an example how you would run a single test from the derivation.
This commit contains some more examples in the `buildRustCrateTests`
attribute set that might be helpful.

```
let
  drv = buildRustCrate {
     …
     buildTests true;
  };
in runCommand "test-my-crate" {} ''
  touch $out
  exec ${drv}/tests/my-test
''
```
2020-01-07 11:57:34 +01:00

51 lines
1.4 KiB
Nix

{ stdenv }:
crateName: metadata: buildTests:
if !buildTests then ''
runHook preInstall
# always create $out even if we do not have binaries. We are detecting binary targets during compilation, if those are missing there is no way to only have $lib
mkdir $out
if [[ -s target/env ]]; then
mkdir -p $lib
cp target/env $lib/env
fi
if [[ -s target/link.final ]]; then
mkdir -p $lib/lib
cp target/link.final $lib/lib/link
fi
if [[ "$(ls -A target/lib)" ]]; then
mkdir -p $lib/lib
cp target/lib/* $lib/lib #*/
for library in $lib/lib/*.so $lib/lib/*.dylib; do #*/
ln -s $library $(echo $library | sed -e "s/-${metadata}//")
done
fi
if [[ "$(ls -A target/build)" ]]; then # */
mkdir -p $lib/lib
cp -r target/build/* $lib/lib # */
fi
if [[ -d target/bin ]]; then
if [[ "$(ls -A target/bin)" ]]; then
mkdir -p $out/bin
cp -P target/bin/* $out/bin # */
fi
fi
runHook postInstall
'' else
# for tests we just put them all in the output. No execution.
''
runHook preInstall
mkdir -p $out/tests
if [ -e target/bin ]; then
find target/bin/ -type f -executable -exec cp {} $out/tests \;
fi
if [ -e target/lib ]; then
find target/lib/ -type f \! -name '*.rlib' \
-a \! -name '*${stdenv.hostPlatform.extensions.sharedLibrary}' \
-a \! -name '*.d' \
-executable \
-print0 | xargs --no-run-if-empty --null install --target $out/tests;
fi
runHook postInstall
''