Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rust-lang/rust 40955 #3898

Merged
merged 2 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,17 +1273,7 @@ fn normalize(package_root: &Path,
default: &mut FnMut(&TomlBinTarget) -> PathBuf| {
for bin in bins.iter() {
let path = bin.path.clone().unwrap_or_else(|| {
let default_bin_path = PathValue(default(bin));
if package_root.join(&default_bin_path.0).exists() {
default_bin_path // inferred from bin's name
} else {
let path = PathValue(Path::new("src").join("main.rs"));
if package_root.join(&path.0).exists() {
path
} else {
PathValue(Path::new("src").join("bin").join("main.rs"))
}
}
PathValue(default(bin))
});
let mut target = Target::bin_target(&bin.name(), package_root.join(&path.0),
bin.required_features.clone());
Expand Down Expand Up @@ -1359,14 +1349,13 @@ fn normalize(package_root: &Path,
if let Some(ref lib) = *lib {
lib_target(&mut ret, lib);
bin_targets(&mut ret, bins,
&mut |bin| Path::new("src").join("bin")
.join(&format!("{}.rs", bin.name())));
&mut |bin| inferred_bin_path(bin, package_root, true, bins.len()));
} else if bins.len() > 0 {
bin_targets(&mut ret, bins,
&mut |bin| Path::new("src")
.join(&format!("{}.rs", bin.name())));
&mut |bin| inferred_bin_path(bin, package_root, false, bins.len()));
}


if let Some(custom_build) = custom_build {
custom_build_target(&mut ret, &custom_build);
}
Expand Down Expand Up @@ -1394,6 +1383,59 @@ fn normalize(package_root: &Path,
ret
}

fn inferred_bin_path(bin: &TomlBinTarget,
package_root: &Path,
lib: bool,
bin_len: usize) -> PathBuf {
// we have a lib with multiple bins, so the bins are expected to be located
// inside src/bin
if lib && bin_len > 1 {
return Path::new("src").join("bin").join(&format!("{}.rs", bin.name()))
.to_path_buf()
}

// we have a lib with one bin, so it's either src/main.rs, src/bin/foo.rs or
// src/bin/main.rs
if lib && bin_len == 1 {
let path = Path::new("src").join(&format!("main.rs"));
if package_root.join(&path).exists() {
return path.to_path_buf()
}

let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
if package_root.join(&path).exists() {
return path.to_path_buf()
}

return Path::new("src").join("bin").join(&format!("main.rs")).to_path_buf()
}

// here we have a single bin, so it may be located in src/main.rs, src/foo.rs,
// srb/bin/foo.rs or src/bin/main.rs
if bin_len == 1 {
let path = Path::new("src").join(&format!("main.rs"));
if package_root.join(&path).exists() {
return path.to_path_buf()
}

let path = Path::new("src").join(&format!("{}.rs", bin.name()));
if package_root.join(&path).exists() {
return path.to_path_buf()
}

let path = Path::new("src").join("bin").join(&format!("{}.rs", bin.name()));
if package_root.join(&path).exists() {
return path.to_path_buf()
}

return Path::new("src").join("bin").join(&format!("main.rs")).to_path_buf()

}

// here we have multiple bins, so they are expected to be located inside src/bin
Path::new("src").join("bin").join(&format!("{}.rs", bin.name())).to_path_buf()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcrichton not sure about this, maybe we should support multiple bin targets (for example: foo and bar) in src/foo.rs and src.bar.rs... but what should we do with src/main.rs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah so the intention was that if you have multiple binaries they're all in src/bin/*.rs and if you have one binary it has a few possible locations it could be at, but only one or the other. We didn't intend to support src/main.rs as well as src/bin/*.rs (at least not with automatic inference).

In that sense this looks good to me, thanks!

}

fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
let profiles = profiles.as_ref();
let mut profiles = Profiles {
Expand Down
25 changes: 25 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2870,3 +2870,28 @@ fn run_proper_binary_main_rs() {
assert_that(p.cargo_process("run").arg("--bin").arg("foo"),
execs().with_status(0));
}

#[test]
fn run_proper_binary_main_rs_as_foo() {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
authors = []
version = "0.0.0"
[[bin]]
name = "foo"
"#)
.file("src/foo.rs", r#"
fn main() {
panic!("This should never be run.");
}
"#)
.file("src/main.rs", r#"
fn main() {
}
"#);

assert_that(p.cargo_process("run").arg("--bin").arg("foo"),
execs().with_status(0));
}