2020-03-28 19:45:46 +00:00
|
|
|
{ lib
|
2020-09-29 19:33:49 +01:00
|
|
|
, buildPackages
|
2020-03-28 19:45:46 +00:00
|
|
|
, buildRustCrate
|
2020-03-28 14:23:12 +00:00
|
|
|
, callPackage
|
|
|
|
, releaseTools
|
2020-03-28 19:45:46 +00:00
|
|
|
, runCommand
|
|
|
|
, runCommandCC
|
2020-03-28 14:23:12 +00:00
|
|
|
, stdenv
|
2020-03-28 19:45:46 +00:00
|
|
|
, symlinkJoin
|
2020-03-28 14:23:12 +00:00
|
|
|
, writeTextFile
|
2020-03-28 19:45:46 +00:00
|
|
|
}:
|
2020-03-28 14:23:12 +00:00
|
|
|
|
2018-09-09 08:21:51 +01:00
|
|
|
let
|
2020-09-29 19:33:49 +01:00
|
|
|
mkCrate = buildRustCrate: args: let
|
2020-01-21 16:32:48 +00:00
|
|
|
p = {
|
|
|
|
crateName = "nixtestcrate";
|
|
|
|
version = "0.1.0";
|
|
|
|
authors = [ "Test <test@example.com>" ];
|
|
|
|
} // args;
|
|
|
|
in buildRustCrate p;
|
2020-09-29 19:33:49 +01:00
|
|
|
mkHostCrate = mkCrate buildRustCrate;
|
2018-09-09 08:21:51 +01:00
|
|
|
|
2020-03-01 12:34:36 +00:00
|
|
|
mkCargoToml =
|
|
|
|
{ name, crateVersion ? "0.1.0", path ? "Cargo.toml" }:
|
|
|
|
mkFile path ''
|
|
|
|
[package]
|
|
|
|
name = ${builtins.toJSON name}
|
|
|
|
version = ${builtins.toJSON crateVersion}
|
|
|
|
'';
|
|
|
|
|
2020-01-21 16:32:48 +00:00
|
|
|
mkFile = destination: text: writeTextFile {
|
|
|
|
name = "src";
|
|
|
|
destination = "/${destination}";
|
|
|
|
inherit text;
|
2018-09-09 08:21:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
mkBin = name: mkFile name ''
|
|
|
|
use std::env;
|
|
|
|
fn main() {
|
|
|
|
let name: String = env::args().nth(0).unwrap();
|
|
|
|
println!("executed {}", name);
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2019-09-08 07:18:09 +01:00
|
|
|
mkBinExtern = name: extern: mkFile name ''
|
|
|
|
extern crate ${extern};
|
|
|
|
fn main() {
|
|
|
|
assert_eq!(${extern}::test(), 23);
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2019-12-17 20:15:53 +00:00
|
|
|
mkTestFile = name: functionName: mkFile name ''
|
|
|
|
#[cfg(test)]
|
|
|
|
#[test]
|
|
|
|
fn ${functionName}() {
|
|
|
|
assert!(true);
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
mkTestFileWithMain = name: functionName: mkFile name ''
|
|
|
|
#[cfg(test)]
|
|
|
|
#[test]
|
|
|
|
fn ${functionName}() {
|
|
|
|
assert!(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
2018-09-09 08:21:51 +01:00
|
|
|
mkLib = name: mkFile name "pub fn test() -> i32 { return 23; }";
|
|
|
|
|
|
|
|
mkTest = crateArgs: let
|
2020-09-29 19:33:49 +01:00
|
|
|
crate = mkHostCrate (builtins.removeAttrs crateArgs ["expectedTestOutput"]);
|
2019-12-17 20:15:53 +00:00
|
|
|
hasTests = crateArgs.buildTests or false;
|
|
|
|
expectedTestOutputs = crateArgs.expectedTestOutputs or null;
|
2020-09-29 19:33:49 +01:00
|
|
|
binaries = map (v: lib.escapeShellArg v.name) (crateArgs.crateBin or []);
|
2018-09-09 08:21:51 +01:00
|
|
|
isLib = crateArgs ? libName || crateArgs ? libPath;
|
|
|
|
crateName = crateArgs.crateName or "nixtestcrate";
|
|
|
|
libName = crateArgs.libName or crateName;
|
|
|
|
|
2020-09-29 19:33:49 +01:00
|
|
|
libTestBinary = if !isLib then null else mkHostCrate {
|
2018-09-09 08:21:51 +01:00
|
|
|
crateName = "run-test-${crateName}";
|
|
|
|
dependencies = [ crate ];
|
2019-09-08 07:18:09 +01:00
|
|
|
src = mkBinExtern "src/main.rs" libName;
|
2018-09-09 08:21:51 +01:00
|
|
|
};
|
|
|
|
|
2019-12-17 20:15:53 +00:00
|
|
|
in
|
|
|
|
assert expectedTestOutputs != null -> hasTests;
|
|
|
|
assert hasTests -> expectedTestOutputs != null;
|
|
|
|
|
|
|
|
runCommand "run-buildRustCrate-${crateName}-test" {
|
|
|
|
nativeBuildInputs = [ crate ];
|
|
|
|
} (if !hasTests then ''
|
2020-09-29 19:33:49 +01:00
|
|
|
${lib.concatMapStringsSep "\n" (binary:
|
|
|
|
# Can't actually run the binary when cross-compiling
|
|
|
|
(lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) "type ") + binary
|
|
|
|
) binaries}
|
2019-12-17 20:15:53 +00:00
|
|
|
${lib.optionalString isLib ''
|
|
|
|
test -e ${crate}/lib/*.rlib || exit 1
|
2020-09-29 19:33:49 +01:00
|
|
|
${lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) "test -x "} \
|
|
|
|
${libTestBinary}/bin/run-test-${crateName}
|
2019-12-17 20:15:53 +00:00
|
|
|
''}
|
|
|
|
touch $out
|
2020-09-29 19:33:49 +01:00
|
|
|
'' else if stdenv.hostPlatform == stdenv.buildPlatform then ''
|
2019-12-17 20:15:53 +00:00
|
|
|
for file in ${crate}/tests/*; do
|
|
|
|
$file 2>&1 >> $out
|
|
|
|
done
|
|
|
|
set -e
|
|
|
|
${lib.concatMapStringsSep "\n" (o: "grep '${o}' $out || { echo 'output \"${o}\" not found in:'; cat $out; exit 23; }") expectedTestOutputs}
|
2020-09-29 19:33:49 +01:00
|
|
|
'' else ''
|
|
|
|
for file in ${crate}/tests/*; do
|
|
|
|
test -x "$file"
|
|
|
|
done
|
|
|
|
touch "$out"
|
2019-12-17 20:15:53 +00:00
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2020-03-28 14:23:12 +00:00
|
|
|
/* Returns a derivation that asserts that the crate specified by `crateArgs`
|
|
|
|
has the specified files as output.
|
|
|
|
|
|
|
|
`name` is used as part of the derivation name that performs the checking.
|
|
|
|
|
2020-09-29 19:33:49 +01:00
|
|
|
`crateArgs` is passed to `mkHostCrate` to build the crate with `buildRustCrate`.
|
2020-03-28 14:23:12 +00:00
|
|
|
|
|
|
|
`expectedFiles` contains a list of expected file paths in the output. E.g.
|
|
|
|
`[ "./bin/my_binary" ]`.
|
|
|
|
|
|
|
|
`output` specifies the name of the output to use. By default, the default
|
|
|
|
output is used but e.g. `output = "lib";` will cause the lib output
|
|
|
|
to be checked instead. You do not need to specify any directories.
|
|
|
|
*/
|
|
|
|
assertOutputs = { name, crateArgs, expectedFiles, output? null }:
|
|
|
|
assert (builtins.isString name);
|
|
|
|
assert (builtins.isAttrs crateArgs);
|
|
|
|
assert (builtins.isList expectedFiles);
|
|
|
|
|
|
|
|
let
|
2020-09-29 19:33:49 +01:00
|
|
|
crate = mkHostCrate (builtins.removeAttrs crateArgs ["expectedTestOutput"]);
|
2020-03-28 14:23:12 +00:00
|
|
|
crateOutput = if output == null then crate else crate."${output}";
|
|
|
|
expectedFilesFile = writeTextFile {
|
|
|
|
name = "expected-files-${name}";
|
|
|
|
text =
|
|
|
|
let sorted = builtins.sort (a: b: a<b) expectedFiles;
|
|
|
|
concatenated = builtins.concatStringsSep "\n" sorted;
|
|
|
|
in "${concatenated}\n";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
runCommand "assert-outputs-${name}" {
|
2020-11-29 16:28:12 +00:00
|
|
|
} (''
|
2020-03-28 14:23:12 +00:00
|
|
|
local actualFiles=$(mktemp)
|
|
|
|
|
|
|
|
cd "${crateOutput}"
|
2020-11-29 16:28:12 +00:00
|
|
|
find . -type f \
|
|
|
|
| sort \
|
|
|
|
''
|
|
|
|
# sed out the hash because it differs per platform
|
|
|
|
+ ''
|
|
|
|
| sed -E -e 's/-[0-9a-fA-F]{10}\.rlib/-HASH.rlib/g' \
|
|
|
|
> "$actualFiles"
|
|
|
|
diff -q ${expectedFilesFile} "$actualFiles" > /dev/null || {
|
2020-03-28 14:23:12 +00:00
|
|
|
echo -e "\033[0;1;31mERROR: Difference in expected output files in ${crateOutput} \033[0m" >&2
|
|
|
|
echo === Got:
|
|
|
|
sed -e 's/^/ /' $actualFiles
|
|
|
|
echo === Expected:
|
|
|
|
sed -e 's/^/ /' ${expectedFilesFile}
|
|
|
|
echo === Diff:
|
|
|
|
diff -u ${expectedFilesFile} $actualFiles |\
|
|
|
|
tail -n +3 |\
|
|
|
|
sed -e 's/^/ /'
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
touch $out
|
2020-11-29 16:28:12 +00:00
|
|
|
'')
|
2020-03-28 14:23:12 +00:00
|
|
|
;
|
|
|
|
|
2018-09-09 08:21:51 +01:00
|
|
|
in rec {
|
|
|
|
|
|
|
|
tests = let
|
2020-03-01 12:34:36 +00:00
|
|
|
cases = rec {
|
2018-09-09 08:21:51 +01:00
|
|
|
libPath = { libPath = "src/my_lib.rs"; src = mkLib "src/my_lib.rs"; };
|
|
|
|
srcLib = { src = mkLib "src/lib.rs"; };
|
2020-01-16 11:51:47 +00:00
|
|
|
|
|
|
|
# This used to be supported by cargo but as of 1.40.0 I can't make it work like that with just cargo anymore.
|
|
|
|
# This might be a regression or deprecated thing they finally removed…
|
|
|
|
# customLibName = { libName = "test_lib"; src = mkLib "src/test_lib.rs"; };
|
|
|
|
# rustLibTestsCustomLibName = {
|
|
|
|
# libName = "test_lib";
|
|
|
|
# src = mkTestFile "src/test_lib.rs" "foo";
|
|
|
|
# buildTests = true;
|
|
|
|
# expectedTestOutputs = [ "test foo ... ok" ];
|
|
|
|
# };
|
|
|
|
|
2018-09-09 08:21:51 +01:00
|
|
|
customLibNameAndLibPath = { libName = "test_lib"; libPath = "src/best-lib.rs"; src = mkLib "src/best-lib.rs"; };
|
|
|
|
crateBinWithPath = { crateBin = [{ name = "test_binary1"; path = "src/foobar.rs"; }]; src = mkBin "src/foobar.rs"; };
|
|
|
|
crateBinNoPath1 = { crateBin = [{ name = "my-binary2"; }]; src = mkBin "src/my_binary2.rs"; };
|
|
|
|
crateBinNoPath2 = {
|
|
|
|
crateBin = [{ name = "my-binary3"; } { name = "my-binary4"; }];
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "buildRustCrateMultipleBinariesCase";
|
|
|
|
paths = [ (mkBin "src/bin/my_binary3.rs") (mkBin "src/bin/my_binary4.rs") ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
crateBinNoPath3 = { crateBin = [{ name = "my-binary5"; }]; src = mkBin "src/bin/main.rs"; };
|
|
|
|
crateBinNoPath4 = { crateBin = [{ name = "my-binary6"; }]; src = mkBin "src/main.rs";};
|
2019-09-08 07:18:09 +01:00
|
|
|
crateBinRename1 = {
|
|
|
|
crateBin = [{ name = "my-binary-rename1"; }];
|
|
|
|
src = mkBinExtern "src/main.rs" "foo_renamed";
|
2020-09-29 19:33:49 +01:00
|
|
|
dependencies = [ (mkHostCrate { crateName = "foo"; src = mkLib "src/lib.rs"; }) ];
|
2019-09-08 07:18:09 +01:00
|
|
|
crateRenames = { "foo" = "foo_renamed"; };
|
|
|
|
};
|
|
|
|
crateBinRename2 = {
|
|
|
|
crateBin = [{ name = "my-binary-rename2"; }];
|
|
|
|
src = mkBinExtern "src/main.rs" "foo_renamed";
|
2020-09-29 19:33:49 +01:00
|
|
|
dependencies = [ (mkHostCrate { crateName = "foo"; libName = "foolib"; src = mkLib "src/lib.rs"; }) ];
|
2019-09-08 07:18:09 +01:00
|
|
|
crateRenames = { "foo" = "foo_renamed"; };
|
|
|
|
};
|
2020-04-09 23:55:44 +01:00
|
|
|
crateBinRenameMultiVersion = let
|
2020-09-29 19:33:49 +01:00
|
|
|
crateWithVersion = version: mkHostCrate {
|
2020-04-09 23:55:44 +01:00
|
|
|
crateName = "my_lib";
|
|
|
|
inherit version;
|
|
|
|
src = mkFile "src/lib.rs" ''
|
|
|
|
pub const version: &str = "${version}";
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
depCrate01 = crateWithVersion "0.1.2";
|
|
|
|
depCrate02 = crateWithVersion "0.2.1";
|
|
|
|
in {
|
|
|
|
crateName = "my_bin";
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "my_bin_src";
|
|
|
|
paths = [
|
|
|
|
(mkFile "src/main.rs" ''
|
|
|
|
#[test]
|
|
|
|
fn my_lib_01() { assert_eq!(lib01::version, "0.1.2"); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn my_lib_02() { assert_eq!(lib02::version, "0.2.1"); }
|
|
|
|
|
|
|
|
fn main() { }
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
|
|
|
dependencies = [ depCrate01 depCrate02 ];
|
|
|
|
crateRenames = {
|
|
|
|
"my_lib" = [
|
|
|
|
{
|
|
|
|
version = "0.1.2";
|
|
|
|
rename = "lib01";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
version = "0.2.1";
|
|
|
|
rename = "lib02";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [
|
|
|
|
"test my_lib_01 ... ok"
|
|
|
|
"test my_lib_02 ... ok"
|
|
|
|
];
|
|
|
|
};
|
2019-12-17 20:15:53 +00:00
|
|
|
rustLibTestsDefault = {
|
|
|
|
src = mkTestFile "src/lib.rs" "baz";
|
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [ "test baz ... ok" ];
|
|
|
|
};
|
|
|
|
rustLibTestsCustomLibPath = {
|
|
|
|
libPath = "src/test_path.rs";
|
|
|
|
src = mkTestFile "src/test_path.rs" "bar";
|
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [ "test bar ... ok" ];
|
|
|
|
};
|
|
|
|
rustLibTestsCustomLibPathWithTests = {
|
|
|
|
libPath = "src/test_path.rs";
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "rust-lib-tests-custom-lib-path-with-tests-dir";
|
|
|
|
paths = [
|
|
|
|
(mkTestFile "src/test_path.rs" "bar")
|
|
|
|
(mkTestFile "tests/something.rs" "something")
|
|
|
|
];
|
|
|
|
};
|
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [
|
|
|
|
"test bar ... ok"
|
|
|
|
"test something ... ok"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
rustBinTestsCombined = {
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "rust-bin-tests-combined";
|
|
|
|
paths = [
|
|
|
|
(mkTestFileWithMain "src/main.rs" "src_main")
|
|
|
|
(mkTestFile "tests/foo.rs" "tests_foo")
|
|
|
|
(mkTestFile "tests/bar.rs" "tests_bar")
|
|
|
|
];
|
|
|
|
};
|
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [
|
|
|
|
"test src_main ... ok"
|
|
|
|
"test tests_foo ... ok"
|
|
|
|
"test tests_bar ... ok"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
rustBinTestsSubdirCombined = {
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "rust-bin-tests-subdir-combined";
|
|
|
|
paths = [
|
|
|
|
(mkTestFileWithMain "src/main.rs" "src_main")
|
|
|
|
(mkTestFile "tests/foo/main.rs" "tests_foo")
|
|
|
|
(mkTestFile "tests/bar/main.rs" "tests_bar")
|
|
|
|
];
|
|
|
|
};
|
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [
|
|
|
|
"test src_main ... ok"
|
|
|
|
"test tests_foo ... ok"
|
|
|
|
"test tests_bar ... ok"
|
|
|
|
];
|
|
|
|
};
|
2020-01-21 16:33:01 +00:00
|
|
|
linkAgainstRlibCrate = {
|
|
|
|
crateName = "foo";
|
|
|
|
src = mkFile "src/main.rs" ''
|
|
|
|
extern crate somerlib;
|
|
|
|
fn main() {}
|
|
|
|
'';
|
|
|
|
dependencies = [
|
2020-09-29 19:33:49 +01:00
|
|
|
(mkHostCrate {
|
2020-01-21 16:33:01 +00:00
|
|
|
crateName = "somerlib";
|
|
|
|
type = [ "rlib" ];
|
|
|
|
src = mkLib "src/lib.rs";
|
|
|
|
})
|
|
|
|
];
|
|
|
|
};
|
2020-03-12 11:29:31 +00:00
|
|
|
buildScriptDeps = let
|
2020-09-29 19:33:49 +01:00
|
|
|
depCrate = buildRustCrate: boolVal: mkCrate buildRustCrate {
|
2020-03-12 11:29:31 +00:00
|
|
|
crateName = "bar";
|
|
|
|
src = mkFile "src/lib.rs" ''
|
|
|
|
pub const baz: bool = ${boolVal};
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
crateName = "foo";
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "build-script-and-main";
|
|
|
|
paths = [
|
|
|
|
(mkFile "src/main.rs" ''
|
|
|
|
extern crate bar;
|
|
|
|
#[cfg(test)]
|
|
|
|
#[test]
|
|
|
|
fn baz_false() { assert!(!bar::baz); }
|
|
|
|
fn main() { }
|
|
|
|
'')
|
|
|
|
(mkFile "build.rs" ''
|
|
|
|
extern crate bar;
|
|
|
|
fn main() { assert!(bar::baz); }
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
2020-09-29 19:33:49 +01:00
|
|
|
buildDependencies = [ (depCrate buildPackages.buildRustCrate "true") ];
|
|
|
|
dependencies = [ (depCrate buildRustCrate "false") ];
|
2020-03-12 11:29:31 +00:00
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [ "test baz_false ... ok" ];
|
|
|
|
};
|
2020-06-12 16:09:45 +01:00
|
|
|
buildScriptFeatureEnv = {
|
|
|
|
crateName = "build-script-feature-env";
|
|
|
|
features = [ "some-feature" "crate/another_feature" ];
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "build-script-feature-env";
|
|
|
|
paths = [
|
|
|
|
(mkFile "src/main.rs" ''
|
|
|
|
#[cfg(test)]
|
|
|
|
#[test]
|
|
|
|
fn feature_not_visible() {
|
|
|
|
assert!(std::env::var("CARGO_FEATURE_SOME_FEATURE").is_err());
|
|
|
|
assert!(option_env!("CARGO_FEATURE_SOME_FEATURE").is_none());
|
|
|
|
}
|
|
|
|
fn main() {}
|
|
|
|
'')
|
|
|
|
(mkFile "build.rs" ''
|
|
|
|
fn main() {
|
|
|
|
assert!(std::env::var("CARGO_FEATURE_SOME_FEATURE").is_ok());
|
|
|
|
assert!(option_env!("CARGO_FEATURE_SOME_FEATURE").is_none());
|
|
|
|
}
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [ "test feature_not_visible ... ok" ];
|
|
|
|
};
|
2020-05-26 19:52:18 +01:00
|
|
|
# Regression test for https://github.com/NixOS/nixpkgs/pull/88054
|
|
|
|
# Build script output should be rewritten as valid env vars.
|
|
|
|
buildScriptIncludeDirDeps = let
|
2020-09-29 19:33:49 +01:00
|
|
|
depCrate = mkHostCrate {
|
2020-05-26 19:52:18 +01:00
|
|
|
crateName = "bar";
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "build-script-and-include-dir-bar";
|
|
|
|
paths = [
|
|
|
|
(mkFile "src/lib.rs" ''
|
|
|
|
fn main() { }
|
|
|
|
'')
|
|
|
|
(mkFile "build.rs" ''
|
|
|
|
use std::path::PathBuf;
|
|
|
|
fn main() { println!("cargo:include-dir={}/src", std::env::current_dir().unwrap_or(PathBuf::from(".")).to_str().unwrap()); }
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
crateName = "foo";
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "build-script-and-include-dir-foo";
|
|
|
|
paths = [
|
|
|
|
(mkFile "src/main.rs" ''
|
|
|
|
fn main() { }
|
|
|
|
'')
|
|
|
|
(mkFile "build.rs" ''
|
|
|
|
fn main() { assert!(std::env::var_os("DEP_BAR_INCLUDE_DIR").is_some()); }
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
|
|
|
buildDependencies = [ depCrate ];
|
|
|
|
dependencies = [ depCrate ];
|
|
|
|
};
|
2020-01-28 13:00:38 +00:00
|
|
|
# Regression test for https://github.com/NixOS/nixpkgs/issues/74071
|
|
|
|
# Whenevever a build.rs file is generating files those should not be overlayed onto the actual source dir
|
|
|
|
buildRsOutDirOverlay = {
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "buildrs-out-dir-overlay";
|
|
|
|
paths = [
|
|
|
|
(mkLib "src/lib.rs")
|
|
|
|
(mkFile "build.rs" ''
|
|
|
|
use std::env;
|
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
fn main() {
|
|
|
|
let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR not set");
|
|
|
|
let out_file = Path::new(&out_dir).join("lib.rs");
|
|
|
|
fs::write(out_file, "invalid rust code!").expect("failed to write lib.rs");
|
|
|
|
}
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2020-03-28 12:00:00 +00:00
|
|
|
# Regression test for https://github.com/NixOS/nixpkgs/pull/83379
|
|
|
|
# link flag order should be preserved
|
|
|
|
linkOrder = {
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "buildrs-out-dir-overlay";
|
|
|
|
paths = [
|
|
|
|
(mkFile "build.rs" ''
|
|
|
|
fn main() {
|
|
|
|
// in the other order, linkage will fail
|
|
|
|
println!("cargo:rustc-link-lib=b");
|
|
|
|
println!("cargo:rustc-link-lib=a");
|
|
|
|
}
|
|
|
|
'')
|
|
|
|
(mkFile "src/main.rs" ''
|
|
|
|
extern "C" {
|
|
|
|
fn hello_world();
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
hello_world();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
|
|
|
buildInputs = let
|
2020-03-28 19:45:46 +00:00
|
|
|
compile = name: text: let
|
|
|
|
src = writeTextFile {
|
2020-03-28 12:00:00 +00:00
|
|
|
name = "${name}-src.c";
|
|
|
|
inherit text;
|
2020-03-28 19:45:46 +00:00
|
|
|
};
|
|
|
|
in runCommandCC name {} ''
|
|
|
|
mkdir -p $out/lib
|
|
|
|
# Note: On darwin (which defaults to clang) we have to add
|
|
|
|
# `-undefined dynamic_lookup` as otherwise the compilation fails.
|
2020-09-29 19:33:49 +01:00
|
|
|
$CC -shared \
|
2020-03-28 19:45:46 +00:00
|
|
|
${lib.optionalString stdenv.isDarwin "-undefined dynamic_lookup"} \
|
|
|
|
-o $out/lib/${name}${stdenv.hostPlatform.extensions.sharedLibrary} ${src}
|
2020-03-28 12:00:00 +00:00
|
|
|
'';
|
|
|
|
b = compile "libb" ''
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void hello();
|
|
|
|
|
|
|
|
void hello_world() {
|
|
|
|
hello();
|
|
|
|
printf(" world!\n");
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
a = compile "liba" ''
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void hello() {
|
|
|
|
printf("hello");
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
in [ a b ];
|
|
|
|
};
|
2020-03-01 12:34:36 +00:00
|
|
|
rustCargoTomlInSubDir = {
|
|
|
|
# The "workspace_member" can be set to the sub directory with the crate to build.
|
|
|
|
# By default ".", meaning the top level directory is assumed.
|
|
|
|
# Using null will trigger a search.
|
|
|
|
workspace_member = null;
|
|
|
|
src = symlinkJoin rec {
|
|
|
|
name = "find-cargo-toml";
|
|
|
|
paths = [
|
|
|
|
(mkCargoToml { name = "ignoreMe"; })
|
|
|
|
(mkTestFileWithMain "src/main.rs" "ignore_main")
|
|
|
|
|
|
|
|
(mkCargoToml { name = "rustCargoTomlInSubDir"; path = "subdir/Cargo.toml"; })
|
|
|
|
(mkTestFileWithMain "subdir/src/main.rs" "src_main")
|
|
|
|
(mkTestFile "subdir/tests/foo/main.rs" "tests_foo")
|
|
|
|
(mkTestFile "subdir/tests/bar/main.rs" "tests_bar")
|
|
|
|
];
|
|
|
|
};
|
|
|
|
buildTests = true;
|
|
|
|
expectedTestOutputs = [
|
|
|
|
"test src_main ... ok"
|
|
|
|
"test tests_foo ... ok"
|
|
|
|
"test tests_bar ... ok"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
rustCargoTomlInTopDir =
|
|
|
|
let
|
|
|
|
withoutCargoTomlSearch = builtins.removeAttrs rustCargoTomlInSubDir [ "workspace_member" ];
|
|
|
|
in
|
|
|
|
withoutCargoTomlSearch // {
|
|
|
|
expectedTestOutputs = [
|
|
|
|
"test ignore_main ... ok"
|
|
|
|
];
|
|
|
|
};
|
2020-03-17 15:33:13 +00:00
|
|
|
procMacroInPrelude = {
|
|
|
|
procMacro = true;
|
|
|
|
edition = "2018";
|
|
|
|
src = symlinkJoin {
|
|
|
|
name = "proc-macro-in-prelude";
|
|
|
|
paths = [
|
|
|
|
(mkFile "src/lib.rs" ''
|
|
|
|
use proc_macro::TokenTree;
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2018-09-09 08:21:51 +01:00
|
|
|
};
|
2018-09-13 20:12:14 +01:00
|
|
|
brotliCrates = (callPackage ./brotli-crates.nix {});
|
2020-03-28 14:23:12 +00:00
|
|
|
tests = lib.mapAttrs (key: value: mkTest (value // lib.optionalAttrs (!value?crateName) { crateName = key; })) cases;
|
|
|
|
in tests // rec {
|
|
|
|
|
|
|
|
crateBinWithPathOutputs = assertOutputs {
|
|
|
|
name="crateBinWithPath";
|
|
|
|
crateArgs = {
|
|
|
|
crateBin = [{ name = "test_binary1"; path = "src/foobar.rs"; }];
|
|
|
|
src = mkBin "src/foobar.rs";
|
|
|
|
};
|
|
|
|
expectedFiles = [
|
|
|
|
"./bin/test_binary1"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
crateBinWithPathOutputsDebug = assertOutputs {
|
|
|
|
name="crateBinWithPath";
|
|
|
|
crateArgs = {
|
|
|
|
release = false;
|
|
|
|
crateBin = [{ name = "test_binary1"; path = "src/foobar.rs"; }];
|
|
|
|
src = mkBin "src/foobar.rs";
|
|
|
|
};
|
|
|
|
expectedFiles = [
|
|
|
|
"./bin/test_binary1"
|
|
|
|
] ++ lib.optionals stdenv.isDarwin [
|
|
|
|
# On Darwin, the debug symbols are in a seperate directory.
|
|
|
|
"./bin/test_binary1.dSYM/Contents/Info.plist"
|
|
|
|
"./bin/test_binary1.dSYM/Contents/Resources/DWARF/test_binary1"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
crateBinNoPath1Outputs = assertOutputs {
|
|
|
|
name="crateBinNoPath1";
|
|
|
|
crateArgs = {
|
|
|
|
crateBin = [{ name = "my-binary2"; }];
|
|
|
|
src = mkBin "src/my_binary2.rs";
|
|
|
|
};
|
|
|
|
expectedFiles = [
|
|
|
|
"./bin/my-binary2"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
crateLibOutputs = assertOutputs {
|
|
|
|
name="crateLib";
|
|
|
|
output="lib";
|
|
|
|
crateArgs = {
|
|
|
|
libName = "test_lib";
|
|
|
|
type = [ "rlib" ];
|
|
|
|
libPath = "src/lib.rs";
|
|
|
|
src = mkLib "src/lib.rs";
|
|
|
|
};
|
|
|
|
expectedFiles = [
|
|
|
|
"./nix-support/propagated-build-inputs"
|
2020-11-29 16:28:12 +00:00
|
|
|
"./lib/libtest_lib-HASH.rlib"
|
2020-03-28 14:23:12 +00:00
|
|
|
"./lib/link"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
crateLibOutputsDebug = assertOutputs {
|
|
|
|
name="crateLib";
|
|
|
|
output="lib";
|
|
|
|
crateArgs = {
|
|
|
|
release = false;
|
|
|
|
libName = "test_lib";
|
|
|
|
type = [ "rlib" ];
|
|
|
|
libPath = "src/lib.rs";
|
|
|
|
src = mkLib "src/lib.rs";
|
|
|
|
};
|
|
|
|
expectedFiles = [
|
|
|
|
"./nix-support/propagated-build-inputs"
|
2020-11-29 16:28:12 +00:00
|
|
|
"./lib/libtest_lib-HASH.rlib"
|
2020-03-28 14:23:12 +00:00
|
|
|
"./lib/link"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2018-09-13 20:12:14 +01:00
|
|
|
brotliTest = let
|
|
|
|
pkg = brotliCrates.brotli_2_5_0 {};
|
|
|
|
in runCommand "run-brotli-test-cmd" {
|
|
|
|
nativeBuildInputs = [ pkg ];
|
2020-09-29 19:33:49 +01:00
|
|
|
} (if stdenv.hostPlatform == stdenv.buildPlatform then ''
|
2018-09-13 20:12:14 +01:00
|
|
|
${pkg}/bin/brotli -c ${pkg}/bin/brotli > /dev/null && touch $out
|
2020-09-29 19:33:49 +01:00
|
|
|
'' else ''
|
|
|
|
test -x '${pkg}/bin/brotli' && touch $out
|
|
|
|
'');
|
2018-09-13 20:12:14 +01:00
|
|
|
allocNoStdLibTest = let
|
|
|
|
pkg = brotliCrates.alloc_no_stdlib_1_3_0 {};
|
|
|
|
in runCommand "run-alloc-no-stdlib-test-cmd" {
|
|
|
|
nativeBuildInputs = [ pkg ];
|
|
|
|
} ''
|
|
|
|
test -e ${pkg}/bin/example && touch $out
|
|
|
|
'';
|
|
|
|
brotliDecompressorTest = let
|
|
|
|
pkg = brotliCrates.brotli_decompressor_1_3_1 {};
|
|
|
|
in runCommand "run-brotli-decompressor-test-cmd" {
|
|
|
|
nativeBuildInputs = [ pkg ];
|
|
|
|
} ''
|
|
|
|
test -e ${pkg}/bin/brotli-decompressor && touch $out
|
|
|
|
'';
|
|
|
|
};
|
2020-01-16 12:19:50 +00:00
|
|
|
test = releaseTools.aggregate {
|
|
|
|
name = "buildRustCrate-tests";
|
|
|
|
meta = {
|
|
|
|
description = "Test cases for buildRustCrate";
|
2022-01-19 23:24:52 +00:00
|
|
|
maintainers = [ ];
|
2020-01-16 12:19:50 +00:00
|
|
|
};
|
|
|
|
constituents = builtins.attrValues tests;
|
|
|
|
};
|
2018-09-09 08:21:51 +01:00
|
|
|
}
|