From 3af1e1fe89ea22da0a99e1bd8b8d10744e2f5823 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Wed, 4 Dec 2024 04:18:21 +0000 Subject: [PATCH] mkDummySrc: dedup discovered and declared bin targets (#755) --- CHANGELOG.md | 4 +++ checks/mkDummySrcTests/default.nix | 2 ++ .../multibin/expected/Cargo.lock | 7 +++++ .../multibin/expected/Cargo.toml | 16 ++++++++++ .../multibin/expected/src/bin/bye/main.rs | 14 +++++++++ .../multibin/expected/src/bin/hello.rs | 14 +++++++++ .../mkDummySrcTests/multibin/input/Cargo.lock | 7 +++++ .../mkDummySrcTests/multibin/input/Cargo.toml | 16 ++++++++++ .../multibin/input/src/bin/bye/main.rs | 3 ++ .../multibin/input/src/bin/hello.rs | 3 ++ lib/mkDummySrc.nix | 31 +++++++++++++++---- 11 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 checks/mkDummySrcTests/multibin/expected/Cargo.lock create mode 100644 checks/mkDummySrcTests/multibin/expected/Cargo.toml create mode 100644 checks/mkDummySrcTests/multibin/expected/src/bin/bye/main.rs create mode 100644 checks/mkDummySrcTests/multibin/expected/src/bin/hello.rs create mode 100644 checks/mkDummySrcTests/multibin/input/Cargo.lock create mode 100644 checks/mkDummySrcTests/multibin/input/Cargo.toml create mode 100644 checks/mkDummySrcTests/multibin/input/src/bin/bye/main.rs create mode 100644 checks/mkDummySrcTests/multibin/input/src/bin/hello.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index bd0944a9..6fd567f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. rebuilding all dependencies once more. (If this is a frequent enough occurrence for your project to cause headaches, please open an issue!) +### Fixed +* `mkDummySrc` will deduplicate discovered and declared binary targets when + dummifying sources + ## [0.19.4] - 2024-11-30 ### Fixed diff --git a/checks/mkDummySrcTests/default.nix b/checks/mkDummySrcTests/default.nix index 0a71e518..c3cd6dbe 100644 --- a/checks/mkDummySrcTests/default.nix +++ b/checks/mkDummySrcTests/default.nix @@ -102,6 +102,8 @@ in linkFarmFromDrvs "cleanCargoToml" (lib.flatten [ (cmpDummySrc "single" ./single) (cmpDummySrc "single-alt" ./single-alt) + # https://github.com/ipetkov/crane/issues/753 + (cmpDummySrc "multibin" ./multibin) (cmpDummySrc "workspace" ./workspace) (cmpDummySrc "workspace-bindeps" ./workspace-bindeps) (cmpDummySrc "workspace-inheritance" ./workspace-inheritance) diff --git a/checks/mkDummySrcTests/multibin/expected/Cargo.lock b/checks/mkDummySrcTests/multibin/expected/Cargo.lock new file mode 100644 index 00000000..45402763 --- /dev/null +++ b/checks/mkDummySrcTests/multibin/expected/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "multibin" +version = "0.1.0" diff --git a/checks/mkDummySrcTests/multibin/expected/Cargo.toml b/checks/mkDummySrcTests/multibin/expected/Cargo.toml new file mode 100644 index 00000000..dfc92361 --- /dev/null +++ b/checks/mkDummySrcTests/multibin/expected/Cargo.toml @@ -0,0 +1,16 @@ +[[bin]] +name = "alt" +path = "src/bin/hello.rs" + +[[bin]] +name = "bye" + +[features] +default = ["some"] +some = [] + +[package] +build = "cranespecific-dummy.rs" +edition = "2021" +name = "multibin" +version = "0.1.0" diff --git a/checks/mkDummySrcTests/multibin/expected/src/bin/bye/main.rs b/checks/mkDummySrcTests/multibin/expected/src/bin/bye/main.rs new file mode 100644 index 00000000..b6a90ffb --- /dev/null +++ b/checks/mkDummySrcTests/multibin/expected/src/bin/bye/main.rs @@ -0,0 +1,14 @@ +#![allow(clippy::all)] +#![allow(dead_code)] +#![cfg_attr(any(target_os = "none", target_os = "uefi"), no_std)] +#![cfg_attr(any(target_os = "none", target_os = "uefi"), no_main)] + +#[allow(unused_extern_crates)] +extern crate core; + +#[cfg_attr(any(target_os = "none", target_os = "uefi"), panic_handler)] +fn panic(_info: &::core::panic::PanicInfo<'_>) -> ! { + loop {} +} + +pub fn main() {} diff --git a/checks/mkDummySrcTests/multibin/expected/src/bin/hello.rs b/checks/mkDummySrcTests/multibin/expected/src/bin/hello.rs new file mode 100644 index 00000000..b6a90ffb --- /dev/null +++ b/checks/mkDummySrcTests/multibin/expected/src/bin/hello.rs @@ -0,0 +1,14 @@ +#![allow(clippy::all)] +#![allow(dead_code)] +#![cfg_attr(any(target_os = "none", target_os = "uefi"), no_std)] +#![cfg_attr(any(target_os = "none", target_os = "uefi"), no_main)] + +#[allow(unused_extern_crates)] +extern crate core; + +#[cfg_attr(any(target_os = "none", target_os = "uefi"), panic_handler)] +fn panic(_info: &::core::panic::PanicInfo<'_>) -> ! { + loop {} +} + +pub fn main() {} diff --git a/checks/mkDummySrcTests/multibin/input/Cargo.lock b/checks/mkDummySrcTests/multibin/input/Cargo.lock new file mode 100644 index 00000000..45402763 --- /dev/null +++ b/checks/mkDummySrcTests/multibin/input/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "multibin" +version = "0.1.0" diff --git a/checks/mkDummySrcTests/multibin/input/Cargo.toml b/checks/mkDummySrcTests/multibin/input/Cargo.toml new file mode 100644 index 00000000..1b3ebe94 --- /dev/null +++ b/checks/mkDummySrcTests/multibin/input/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "multibin" +version = "0.1.0" +edition = "2021" + +[features] +default = ["some"] +some = [] + +[[bin]] +name = "alt" +path = "src/bin/hello.rs" + +[[bin]] +name = "bye" +required-features = ["some"] diff --git a/checks/mkDummySrcTests/multibin/input/src/bin/bye/main.rs b/checks/mkDummySrcTests/multibin/input/src/bin/bye/main.rs new file mode 100644 index 00000000..5fb67886 --- /dev/null +++ b/checks/mkDummySrcTests/multibin/input/src/bin/bye/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Bye, world!"); +} diff --git a/checks/mkDummySrcTests/multibin/input/src/bin/hello.rs b/checks/mkDummySrcTests/multibin/input/src/bin/hello.rs new file mode 100644 index 00000000..e7a11a96 --- /dev/null +++ b/checks/mkDummySrcTests/multibin/input/src/bin/hello.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/lib/mkDummySrc.nix b/lib/mkDummySrc.nix index 4a7ef466..f45e3081 100644 --- a/lib/mkDummySrc.nix +++ b/lib/mkDummySrc.nix @@ -200,13 +200,17 @@ let hasMainrs = autobins && hasFile srcDir "main.rs"; srcBinDir = lib.optionalAttrs (autobins && hasDir srcDir "bin") (builtins.readDir (shallowJoinPath "src/bin")); + candidatePathsForBin = name: rec { + short = "src/bin/${name}"; + long = "${short}/main.rs"; + }; + # NB: sort the result here to be as deterministic as possible and avoid rebuilds if # directory listings happen to change their order - discoveredBins = concatStringsSep " " (lib.sortOn (x: x) (lib.filter (p: p != null) + discoveredBins = lib.sortOn (x: x) (lib.filter (p: p != null) (lib.flip map (lib.attrsToList srcBinDir) ({ name, value }: let - short = "src/bin/${name}"; - long = "${short}/main.rs"; + inherit (candidatePathsForBin name) short long; in lib.escapeShellArg ( if value == "regular" @@ -216,12 +220,28 @@ let else null ) )) + ); + + declaredBins = lib.filter (p: p != null) (lib.flip map (trimmedCargoToml.bin or [ ]) (t: + if t ? path + then (if lib.elem t.path discoveredBins then null else t.path) + else + let + candidates = candidatePathsForBin t.name; + inherit (candidates) long; + short = "${candidates.short}.rs"; + in + if lib.any (i: i == short || i == long) discoveredBins + then null + else short )); + allBins = concatStringsSep " " (discoveredBins ++ declaredBins); + stubBins = ''( cd ${parentDir} - echo ${discoveredBins} | xargs --no-run-if-empty -n1 dirname | sort -u | xargs --no-run-if-empty mkdir -p - echo ${discoveredBins} | xargs --no-run-if-empty -n1 cp -f ${dummyrs} + echo ${allBins} | xargs --no-run-if-empty -n1 dirname | sort -u | xargs --no-run-if-empty mkdir -p + echo ${allBins} | xargs --no-run-if-empty -n1 cp -f ${dummyrs} )''; safeStubLib = lib.optionalString @@ -247,7 +267,6 @@ let # Stub all other targets in case they have particular feature combinations ${safeStubLib} ${safeStubList "bench" "benches"} - ${safeStubList "bin" "src/bin"} ${safeStubList "example" "examples"} ${safeStubList "test" "tests"} ''