Skip to content

Commit

Permalink
mkDummySrc: also consider main.rs when deduping bins (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipetkov authored Dec 5, 2024
1 parent af1556e commit 62e5013
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
3 changes: 3 additions & 0 deletions checks/mkDummySrcTests/multibin/expected/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ path = "src/bin/hello.rs"
[[bin]]
name = "bye"

[[bin]]
name = "multibin"

[features]
default = ["some"]
some = []
Expand Down
14 changes: 14 additions & 0 deletions checks/mkDummySrcTests/multibin/expected/src/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() {}
5 changes: 4 additions & 1 deletion checks/mkDummySrcTests/multibin/input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "multibin"
name = "multibin" # Loadbearing name matching bin
version = "0.1.0"
edition = "2021"

Expand All @@ -14,3 +14,6 @@ path = "src/bin/hello.rs"
[[bin]]
name = "bye"
required-features = ["some"]

[[bin]]
name = "multibin" # Name of the package
12 changes: 8 additions & 4 deletions lib/mkDummySrc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ let
autobins = trimmedCargoToml.package.autobins or true;
hasMainrs = autobins && hasFile srcDir "main.rs";
srcBinDir = lib.optionalAttrs (autobins && hasDir srcDir "bin") (builtins.readDir (shallowJoinPath "src/bin"));
srcMainrs = "src/main.rs";

candidatePathsForBin = name: rec {
short = "src/bin/${name}";
Expand Down Expand Up @@ -231,9 +232,12 @@ let
inherit (candidates) long;
short = "${candidates.short}.rs";
in
if lib.any (i: i == short || i == long) discoveredBins
then null
else short
if t.name == trimmedCargoToml.package.name
then (if hasMainrs then null else srcMainrs)
else
if lib.any (i: i == short || i == long) discoveredBins
then null
else short
));

allBins = concatStringsSep " " (discoveredBins ++ declaredBins);
Expand Down Expand Up @@ -261,7 +265,7 @@ let
cp ${writeTOML "Cargo.toml" trimmedCargoToml} $out/${cargoTomlDest}
'' + optionalString (trimmedCargoToml ? package) ''
# To build regular and dev dependencies (cargo build + cargo test)
${lib.optionalString hasMainrs (cpDummy parentDir "src/main.rs")}
${lib.optionalString hasMainrs (cpDummy parentDir srcMainrs)}
${stubBins}
# Stub all other targets in case they have particular feature combinations
Expand Down

0 comments on commit 62e5013

Please sign in to comment.