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

Assume build.rs in the same directory as Cargo.toml is a build script (unless explicitly told not to) #3361

Merged
merged 5 commits into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 29 additions & 2 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,18 @@ pub struct TomlProfile {
panic: Option<String>,
}

#[derive(RustcDecodable, Clone, Debug)]
pub enum StringOrBool {
String(String),
Bool(bool),
}

#[derive(RustcDecodable)]
pub struct TomlProject {
name: String,
version: TomlVersion,
authors: Vec<String>,
build: Option<String>,
build: Option<StringOrBool>,
links: Option<String>,
exclude: Option<Vec<String>>,
include: Option<Vec<String>>,
Expand Down Expand Up @@ -540,7 +546,11 @@ impl TomlManifest {
}

// processing the custom build script
let new_build = project.build.as_ref().map(PathBuf::from);
let manifest_file = util::important_paths::find_root_manifest_for_wd(None, &layout.root)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking this over again, I'm wondering if I actually have to do this, or if I can just assume base_dir == layout.root?

.chain_error(|| human("Could not find root manifest location"))?;
let base_dir = manifest_file.parent()
.ok_or(human("Could not get parent directory of manifest"))?;
let new_build = self.maybe_custom_build(&project.build, &base_dir);

// Get targets
let targets = normalize(&lib,
Expand Down Expand Up @@ -767,6 +777,23 @@ impl TomlManifest {
}
Ok(replace)
}

fn maybe_custom_build(&self, build: &Option<StringOrBool>, project_dir: &Path)
-> Option<PathBuf> {
let build_rs = project_dir.join("build.rs");
match *build {
Some(StringOrBool::Bool(false)) => None, // explicitly no build script
Some(StringOrBool::Bool(true)) => Some(build_rs.into()),
Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)),
None => {
match fs::metadata(&build_rs) {
Ok(ref e) if e.is_file() => Some(build_rs.into()),
Ok(_) => None,
Err(_) => None,
}
}
}
}
}

/// Will check a list of toml targets, and make sure the target names are unique within a vector.
Expand Down
71 changes: 71 additions & 0 deletions tests/build-script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,3 +2334,74 @@ fn switch_features_rerun() {
assert_that(build.cargo("run").arg("-v").arg("--features=foo"),
execs().with_status(0).with_stdout("foo\n"));
}

#[test]
fn assume_build_script_when_build_rs_present() {
let p = project("builder")
.file("Cargo.toml", r#"
[package]
name = "builder"
version = "0.0.1"
authors = []
"#)
.file("src/main.rs", r#"
fn main() {
println!(include_str!(concat!(env!("OUT_DIR"), "/output")));
}
"#)
.file("build.rs", r#"
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir).join("output");
let mut f = File::create(&out_dir).unwrap();
f.write_all(b"foo").unwrap();
}
"#);
p.build();

assert_that(p.cargo("run").arg("-v"),
execs().with_status(0).with_stdout("foo\n"));
}

#[test]
fn if_build_set_to_false_dont_tread_build_rs_as_build_script() {
let p = project("builder")
.file("Cargo.toml", r#"
[package]
name = "builder"
version = "0.0.1"
authors = []
build = false
"#)
.file("src/main.rs", r#"
use std::path::Path;
fn main() {
let f = env!("OUT_DIR");
assert!(
! Path::new(f).join("output").exists()
)
}
"#)
.file("build.rs", r#"
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir).join("output");
let mut f = File::create(&out_dir).unwrap();
f.write_all(b"foo").unwrap();
}
"#);
p.build();

assert_that(p.cargo("run").arg("-v"),
execs().with_status(0));
}