Skip to content

Commit

Permalink
Assume that a build.rs file is a build script
Browse files Browse the repository at this point in the history
If cargo sees a `build.rs` file in the same directory as the current
`Cargo.toml`, it will assume that the `build.rs` file is a build script,
_unless there is_ `build = false` _in the _ `Cargo.toml` _file_.

Closes #3391
  • Loading branch information
Paul Woolcock committed Feb 7, 2017
1 parent de2919f commit 952b0ab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
17 changes: 5 additions & 12 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl TomlManifest {
}

// processing the custom build script
let new_build = self.maybe_custom_build(&project.build, &layout.root, &mut warnings);
let new_build = self.maybe_custom_build(&project.build, &layout.root);

// Get targets
let targets = normalize(&layout.root,
Expand Down Expand Up @@ -768,8 +768,7 @@ impl TomlManifest {

fn maybe_custom_build(&self,
build: &Option<StringOrBool>,
project_dir: &Path,
warnings: &mut Vec<String>)
project_dir: &Path)
-> Option<PathBuf> {
let build_rs = project_dir.join("build.rs");
match *build {
Expand All @@ -778,15 +777,9 @@ impl TomlManifest {
Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)),
None => {
match fs::metadata(&build_rs) {
// Enable this after the warning has been visible for some time
// Ok(ref e) if e.is_file() => Some(build_rs.into()),
Ok(ref e) if e.is_file() => {
warnings.push("`build.rs` files in the same directory \
as your `Cargo.toml` will soon be treated \
as build scripts. Add `build = false` to \
your `Cargo.toml` to prevent this".into());
None
},
// If there is a build.rs file next to the Cargo.toml, assume it is
// a build script
Ok(ref e) if e.is_file() => Some(build_rs.into()),
Ok(_) => None,
Err(_) => None,
}
Expand Down
13 changes: 3 additions & 10 deletions tests/build-script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2350,8 +2350,8 @@ fn assume_build_script_when_build_rs_present() {
"#)
.file("src/main.rs", r#"
fn main() {
if cfg!(foo) {
panic!("the build script was run");
if ! cfg!(foo) {
panic!("the build script was not run");
}
}
"#)
Expand All @@ -2363,14 +2363,7 @@ fn assume_build_script_when_build_rs_present() {
p.build();

assert_that(p.cargo("run").arg("-v"),
execs().with_status(0).with_stderr("\
warning: `build.rs` files in the same directory as your `Cargo.toml` will soon be treated \
as build scripts. Add `build = false` to your `Cargo.toml` to prevent this
Compiling builder v0.0.1 ([..])
Running [..]
Finished [..]
Running [..]
"));
execs().with_status(0));
}

#[test]
Expand Down

0 comments on commit 952b0ab

Please sign in to comment.