Skip to content

Commit

Permalink
mkDummySrcTests: avoid unnecessarily adding main.rs (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipetkov authored Dec 21, 2024
1 parent 769bddf commit a83a48a
Show file tree
Hide file tree
Showing 28 changed files with 196 additions and 6 deletions.
4 changes: 4 additions & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ in
'';
};

multiBinSmoke = myLib.buildPackage {
src = myLib.cleanCargoSource ./mkDummySrcTests/multibin/input;
};

multiOutputDerivation = myLib.buildPackage {
src = ./simple;
outputs = [ "out" "doc" ];
Expand Down

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[bin]]
name = "mybin"

[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "mybin"
version = "0.1.0"
14 changes: 14 additions & 0 deletions checks/mkDummySrcTests/declaredBinWithMainrs/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() {}
7 changes: 7 additions & 0 deletions checks/mkDummySrcTests/declaredBinWithMainrs/input/Cargo.lock

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

7 changes: 7 additions & 0 deletions checks/mkDummySrcTests/declaredBinWithMainrs/input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "mybin" # Loadbearing name matching bin
version = "0.1.0"
edition = "2021"

[[bin]]
name = "mybin" # Name of the package
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[bin]]
name = "mybin"

[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "mybin"
version = "0.1.0"
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/declaredBinWithSrcBin/input/Cargo.lock

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

7 changes: 7 additions & 0 deletions checks/mkDummySrcTests/declaredBinWithSrcBin/input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "mybin" # Loadbearing name matching bin
version = "0.1.0"
edition = "2021"

[[bin]]
name = "mybin" # Name of the package
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
6 changes: 6 additions & 0 deletions checks/mkDummySrcTests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ linkFarmFromDrvs "cleanCargoToml" (lib.flatten [

customized
customizedDummyrs

# https://github.com/ipetkov/crane/issues/768
(cmpDummySrc "declaredBinWithMainrs" ./declaredBinWithMainrs)
(cmpDummySrc "declaredBinWithSrcBin" ./declaredBinWithSrcBin)
(cmpDummySrc "omittedBinWithMainrs" ./omittedBinWithMainrs)
(cmpDummySrc "omittedBinWithSrcBin" ./omittedBinWithSrcBin)
])
3 changes: 3 additions & 0 deletions checks/mkDummySrcTests/multibin/input/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "mybin"
version = "0.1.0"
14 changes: 14 additions & 0 deletions checks/mkDummySrcTests/omittedBinWithMainrs/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() {}
7 changes: 7 additions & 0 deletions checks/mkDummySrcTests/omittedBinWithMainrs/input/Cargo.lock

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

4 changes: 4 additions & 0 deletions checks/mkDummySrcTests/omittedBinWithMainrs/input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "mybin" # Loadbearing name matching bin
version = "0.1.0"
edition = "2021"
3 changes: 3 additions & 0 deletions checks/mkDummySrcTests/omittedBinWithMainrs/input/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "mybin"
version = "0.1.0"
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/omittedBinWithSrcBin/input/Cargo.lock

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

4 changes: 4 additions & 0 deletions checks/mkDummySrcTests/omittedBinWithSrcBin/input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "mybin" # Loadbearing name matching bin
version = "0.1.0"
edition = "2021"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
17 changes: 11 additions & 6 deletions lib/mkDummySrc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,18 @@ let
candidates = candidatePathsForBin t.name;
inherit (candidates) long;
short = "${candidates.short}.rs";

# If we have a declared target that matches the package name then exactly one of the
# following needs to be true for a well-formed cargo project:
# * the target has an explictly declared path (handled above)
# * the project has a `src/main.rs` file (detected above and handled further below)
# * the project has a `src/bin/${name}.rs` file which will show up in discoveredBins
# Hence if the target's name matches the package name, we have nothing further to add
nothingToAdd = t.name == trimmedCargoToml.package.name
# Otherwise if the target was already discovered, nothing else for us to add
|| lib.any (i: i == short || i == long) discoveredBins;
in
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
if nothingToAdd then null else short
));

allBins = concatStringsSep " " (discoveredBins ++ declaredBins);
Expand Down

0 comments on commit a83a48a

Please sign in to comment.