From fc5e5950031d8af57f8b5b9e55187f3e4cb4f063 Mon Sep 17 00:00:00 2001 From: Andreas Rammhold Date: Thu, 13 Sep 2018 21:12:14 +0200 Subject: [PATCH] buildRustCrate: added some edge cases with binaries This commit adds test based on real-world crates (brotli). There were a few more edge cases that were missing beforehand. Also it turned out that we can get rid of the `finalBins` list since that will now be handled during runtime. --- .../rust/build-rust-crate/build-crate.nix | 7 +- .../rust/build-rust-crate/default.nix | 17 +--- .../build-rust-crate/test/brotli-crates.nix | 95 +++++++++++++++++++ .../rust/build-rust-crate/test/default.nix | 27 +++++- 4 files changed, 125 insertions(+), 21 deletions(-) create mode 100644 pkgs/build-support/rust/build-rust-crate/test/brotli-crates.nix diff --git a/pkgs/build-support/rust/build-rust-crate/build-crate.nix b/pkgs/build-support/rust/build-rust-crate/build-crate.nix index bd9d96d96cd6..f65118ba4a64 100644 --- a/pkgs/build-support/rust/build-rust-crate/build-crate.nix +++ b/pkgs/build-support/rust/build-rust-crate/build-crate.nix @@ -2,7 +2,7 @@ { crateName, dependencies, crateFeatures, libName, release, libPath, - crateType, metadata, crateBin, finalBins, + crateType, metadata, crateBin, extraRustcOpts, verbose, colors }: let @@ -117,11 +117,11 @@ # the first two cases are the "new" default IIRC BIN_NAME_=$(echo $BIN_NAME | sed -e 's/-/_/g') - FILES=( "src/bin/$BIN_NAME_.rs" "src/bin/$BIN_NAME_/main.rs" "src/bin/main.rs" "src/main.rs" ) + FILES=( "src/bin/$BIN_NAME.rs" "src/bin/$BIN_NAME/main.rs" "src/bin/$BIN_NAME_.rs" "src/bin/$BIN_NAME_/main.rs" "src/bin/main.rs" "src/main.rs" ) if ! [ -e "${libPath}" -o -e src/lib.rs -o -e "src/${libName}.rs" ]; then # if this is not a library the following path is also valid - FILES=( "src/$BIN_NAME_.rs" "''${FILES[@]}" ) + FILES=( "src/$BIN_NAME.rs" "src/$BIN_NAME_.rs" "''${FILES[@]}" ) fi for file in "''${FILES[@]}"; @@ -153,6 +153,5 @@ ''} # Remove object files to avoid "wrong ELF type" find target -type f -name "*.o" -print0 | xargs -0 rm -f - '' + finalBins + '' runHook postBuild '' diff --git a/pkgs/build-support/rust/build-rust-crate/default.nix b/pkgs/build-support/rust/build-rust-crate/default.nix index db7f72acfb99..a11cef9f1f46 100644 --- a/pkgs/build-support/rust/build-rust-crate/default.nix +++ b/pkgs/build-support/rust/build-rust-crate/default.nix @@ -115,8 +115,7 @@ stdenv.mkDerivation (rec { crateBin = if crate ? crateBin then builtins.foldl' (bins: bin: let - _name = (if bin ? name then bin.name else crateName); - name = lib.strings.replaceStrings ["-"] ["_"] _name; + name = (if bin ? name then bin.name else crateName); path = if bin ? path then bin.path else ""; in bins + (if bin == "" then "" else ",") + "${name} ${path}" @@ -124,18 +123,6 @@ stdenv.mkDerivation (rec { ) "" crate.crateBin else ""; - finalBins = if crate ? crateBin then - builtins.foldl' (bins: bin: - let name = lib.strings.replaceStrings ["-"] ["_"] - (if bin ? name then bin.name else crateName); - new_name = if bin ? name then bin.name else crateName; - in - if name == new_name then bins else - (bins + "mv target/bin/${name} target/bin/${new_name};") - - ) "" crate.crateBin - else ""; - build = crate.build or ""; workspace_member = crate.workspace_member or "."; crateVersion = crate.version; @@ -156,7 +143,7 @@ stdenv.mkDerivation (rec { buildPhase = buildCrate { inherit crateName dependencies crateFeatures libName release libPath crateType - metadata crateBin finalBins verbose colors + metadata crateBin verbose colors extraRustcOpts; }; installPhase = installCrate crateName metadata; diff --git a/pkgs/build-support/rust/build-rust-crate/test/brotli-crates.nix b/pkgs/build-support/rust/build-rust-crate/test/brotli-crates.nix new file mode 100644 index 000000000000..068cc5a98843 --- /dev/null +++ b/pkgs/build-support/rust/build-rust-crate/test/brotli-crates.nix @@ -0,0 +1,95 @@ +{ lib, buildPlatform, buildRustCrate, fetchgit }: +let kernel = buildPlatform.parsed.kernel.name; + abi = buildPlatform.parsed.abi.name; + include = includedFiles: src: builtins.filterSource (path: type: + lib.lists.any (f: + let p = toString (src + ("/" + f)); in + (path == p) || (type == "directory" && lib.strings.hasPrefix path p) + ) includedFiles + ) src; + updateFeatures = f: up: functions: builtins.deepSeq f (lib.lists.foldl' (features: fun: fun features) (lib.attrsets.recursiveUpdate f up) functions); + mapFeatures = features: map (fun: fun { features = features; }); + mkFeatures = feat: lib.lists.foldl (features: featureName: + if feat.${featureName} or false then + [ featureName ] ++ features + else + features + ) [] (builtins.attrNames feat); +in +rec { + alloc_no_stdlib_1_3_0_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate { + crateName = "alloc-no-stdlib"; + version = "1.3.0"; + authors = [ "Daniel Reiter Horn " ]; + sha256 = "1jcp27pzmqdszgp80y484g4kwbjbg7x8a589drcwbxg0i8xwkir9"; + crateBin = [ { name = "example"; } ]; + inherit dependencies buildDependencies features; + }; + brotli_2_5_0_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate { + crateName = "brotli"; + version = "2.5.0"; + authors = [ "Daniel Reiter Horn " "The Brotli Authors" ]; + sha256 = "1ynw4hkdwnp0kj30p86ls44ahv4s99258s019bqrq4mya8hlsb5b"; + crateBin = [ { name = "brotli"; } ]; + inherit dependencies buildDependencies features; + }; + brotli_decompressor_1_3_1_ = { dependencies?[], buildDependencies?[], features?[] }: buildRustCrate { + crateName = "brotli-decompressor"; + version = "1.3.1"; + authors = [ "Daniel Reiter Horn " "The Brotli Authors" ]; + sha256 = "022g69q1xzwdj0130qm3fa4qwpn4q1jx3lc8yz0v0v201p7bm8fb"; + crateBin = [ { name = "brotli-decompressor"; } ]; + inherit dependencies buildDependencies features; + }; + alloc_no_stdlib_1_3_0 = { features?(alloc_no_stdlib_1_3_0_features {}) }: alloc_no_stdlib_1_3_0_ { + features = mkFeatures (features.alloc_no_stdlib_1_3_0 or {}); + }; + alloc_no_stdlib_1_3_0_features = f: updateFeatures f (rec { + alloc_no_stdlib_1_3_0.default = (f.alloc_no_stdlib_1_3_0.default or true); + }) []; + brotli_2_5_0 = { features?(brotli_2_5_0_features {}) }: brotli_2_5_0_ { + dependencies = mapFeatures features ([ alloc_no_stdlib_1_3_0 brotli_decompressor_1_3_1 ]); + features = mkFeatures (features.brotli_2_5_0 or {}); + }; + brotli_2_5_0_features = f: updateFeatures f (rec { + alloc_no_stdlib_1_3_0."no-stdlib" = + (f.alloc_no_stdlib_1_3_0."no-stdlib" or false) || + (brotli_2_5_0."no-stdlib" or false) || + (f.brotli_2_5_0."no-stdlib" or false); + alloc_no_stdlib_1_3_0.default = true; + brotli_2_5_0.default = (f.brotli_2_5_0.default or true); + brotli_decompressor_1_3_1."disable-timer" = + (f.brotli_decompressor_1_3_1."disable-timer" or false) || + (brotli_2_5_0."disable-timer" or false) || + (f.brotli_2_5_0."disable-timer" or false); + brotli_decompressor_1_3_1."no-stdlib" = + (f.brotli_decompressor_1_3_1."no-stdlib" or false) || + (brotli_2_5_0."no-stdlib" or false) || + (f.brotli_2_5_0."no-stdlib" or false); + brotli_decompressor_1_3_1.benchmark = + (f.brotli_decompressor_1_3_1.benchmark or false) || + (brotli_2_5_0.benchmark or false) || + (f.brotli_2_5_0.benchmark or false); + brotli_decompressor_1_3_1.default = true; + brotli_decompressor_1_3_1.seccomp = + (f.brotli_decompressor_1_3_1.seccomp or false) || + (brotli_2_5_0.seccomp or false) || + (f.brotli_2_5_0.seccomp or false); + }) [ alloc_no_stdlib_1_3_0_features brotli_decompressor_1_3_1_features ]; + brotli_decompressor_1_3_1 = { features?(brotli_decompressor_1_3_1_features {}) }: brotli_decompressor_1_3_1_ { + dependencies = mapFeatures features ([ alloc_no_stdlib_1_3_0 ]); + features = mkFeatures (features.brotli_decompressor_1_3_1 or {}); + }; + brotli_decompressor_1_3_1_features = f: updateFeatures f (rec { + alloc_no_stdlib_1_3_0."no-stdlib" = + (f.alloc_no_stdlib_1_3_0."no-stdlib" or false) || + (brotli_decompressor_1_3_1."no-stdlib" or false) || + (f.brotli_decompressor_1_3_1."no-stdlib" or false); + alloc_no_stdlib_1_3_0.default = true; + alloc_no_stdlib_1_3_0.unsafe = + (f.alloc_no_stdlib_1_3_0.unsafe or false) || + (brotli_decompressor_1_3_1.unsafe or false) || + (f.brotli_decompressor_1_3_1.unsafe or false); + brotli_decompressor_1_3_1.default = (f.brotli_decompressor_1_3_1.default or true); + }) [ alloc_no_stdlib_1_3_0_features ]; +} diff --git a/pkgs/build-support/rust/build-rust-crate/test/default.nix b/pkgs/build-support/rust/build-rust-crate/test/default.nix index e5e77c93555d..08f7238c1fda 100644 --- a/pkgs/build-support/rust/build-rust-crate/test/default.nix +++ b/pkgs/build-support/rust/build-rust-crate/test/default.nix @@ -1,4 +1,4 @@ -{ lib, stdenv, buildRustCrate, runCommand, writeTextFile, symlinkJoin }: +{ lib, stdenv, buildRustCrate, runCommand, writeTextFile, symlinkJoin, callPackage }: let mkCrate = args: let p = { @@ -72,7 +72,30 @@ let crateBinNoPath3 = { crateBin = [{ name = "my-binary5"; }]; src = mkBin "src/bin/main.rs"; }; crateBinNoPath4 = { crateBin = [{ name = "my-binary6"; }]; src = mkBin "src/main.rs";}; }; - in lib.mapAttrs (key: value: mkTest (value // lib.optionalAttrs (!value?crateName) { crateName = key; })) cases; + brotliCrates = (callPackage ./brotli-crates.nix {}); + in lib.mapAttrs (key: value: mkTest (value // lib.optionalAttrs (!value?crateName) { crateName = key; })) cases // { + brotliTest = let + pkg = brotliCrates.brotli_2_5_0 {}; + in runCommand "run-brotli-test-cmd" { + nativeBuildInputs = [ pkg ]; + } '' + ${pkg}/bin/brotli -c ${pkg}/bin/brotli > /dev/null && touch $out + ''; + 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 + ''; + }; test = runCommand "run-buildRustCrate-tests" { nativeBuildInputs = builtins.attrValues tests; } "