Skip to content

Commit

Permalink
mkDummySrc: dedup discovered and declared bin targets (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipetkov authored Dec 4, 2024
1 parent 76d64e7 commit 3af1e1f
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions checks/mkDummySrcTests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions checks/mkDummySrcTests/multibin/expected/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions checks/mkDummySrcTests/multibin/expected/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
14 changes: 14 additions & 0 deletions checks/mkDummySrcTests/multibin/expected/src/bin/bye/main.rs
Original file line number Diff line number Diff line change
@@ -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() {}
14 changes: 14 additions & 0 deletions checks/mkDummySrcTests/multibin/expected/src/bin/hello.rs
Original file line number Diff line number Diff line change
@@ -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() {}
7 changes: 7 additions & 0 deletions checks/mkDummySrcTests/multibin/input/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions checks/mkDummySrcTests/multibin/input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
3 changes: 3 additions & 0 deletions checks/mkDummySrcTests/multibin/input/src/bin/bye/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Bye, world!");
}
3 changes: 3 additions & 0 deletions checks/mkDummySrcTests/multibin/input/src/bin/hello.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
31 changes: 25 additions & 6 deletions lib/mkDummySrc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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"}
''
Expand Down

0 comments on commit 3af1e1f

Please sign in to comment.