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

Make sure targets always use absolute path #3436

Merged
merged 1 commit into from
Dec 26, 2016
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
35 changes: 15 additions & 20 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,12 @@ impl VirtualManifest {
}

impl Target {
fn blank() -> Target {
fn with_path(src_path: PathBuf) -> Target {
assert!(src_path.is_absolute());
Target {
kind: TargetKind::Bin,
name: String::new(),
src_path: PathBuf::new(),
src_path: src_path,
doc: false,
doctest: false,
harness: true,
Expand All @@ -309,67 +310,61 @@ impl Target {

pub fn lib_target(name: &str,
crate_targets: Vec<LibKind>,
src_path: &Path) -> Target {
src_path: PathBuf) -> Target {
Target {
kind: TargetKind::Lib(crate_targets),
name: name.to_string(),
src_path: src_path.to_path_buf(),
doctest: true,
doc: true,
..Target::blank()
..Target::with_path(src_path)
}
}

pub fn bin_target(name: &str, src_path: &Path) -> Target {
pub fn bin_target(name: &str, src_path: PathBuf) -> Target {
Target {
kind: TargetKind::Bin,
name: name.to_string(),
src_path: src_path.to_path_buf(),
doc: true,
..Target::blank()
..Target::with_path(src_path)
}
}

/// Builds a `Target` corresponding to the `build = "build.rs"` entry.
pub fn custom_build_target(name: &str, src_path: &Path) -> Target {
pub fn custom_build_target(name: &str, src_path: PathBuf) -> Target {
Target {
kind: TargetKind::CustomBuild,
name: name.to_string(),
src_path: src_path.to_path_buf(),
for_host: true,
benched: false,
tested: false,
..Target::blank()
..Target::with_path(src_path)
}
}

pub fn example_target(name: &str, src_path: &Path) -> Target {
pub fn example_target(name: &str, src_path: PathBuf) -> Target {
Target {
kind: TargetKind::Example,
name: name.to_string(),
src_path: src_path.to_path_buf(),
benched: false,
..Target::blank()
..Target::with_path(src_path)
}
}

pub fn test_target(name: &str, src_path: &Path) -> Target {
pub fn test_target(name: &str, src_path: PathBuf) -> Target {
Target {
kind: TargetKind::Test,
name: name.to_string(),
src_path: src_path.to_path_buf(),
benched: false,
..Target::blank()
..Target::with_path(src_path)
}
}

pub fn bench_target(name: &str, src_path: &Path) -> Target {
pub fn bench_target(name: &str, src_path: PathBuf) -> Target {
Target {
kind: TargetKind::Bench,
name: name.to_string(),
src_path: src_path.to_path_buf(),
tested: false,
..Target::blank()
..Target::with_path(src_path)
}
}

Expand Down
57 changes: 30 additions & 27 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Layout {
examples: Vec<PathBuf>,
tests: Vec<PathBuf>,
benches: Vec<PathBuf>,

}

impl Layout {
Expand Down Expand Up @@ -549,7 +550,8 @@ impl TomlManifest {
let new_build = self.maybe_custom_build(&project.build, &layout.root, &mut warnings);

// Get targets
let targets = normalize(&lib,
let targets = normalize(&layout.root,
&lib,
&bins,
new_build,
&examples,
Expand Down Expand Up @@ -1090,7 +1092,8 @@ impl fmt::Debug for PathValue {
}
}

fn normalize(lib: &Option<TomlLibTarget>,
fn normalize(package_root: &Path,
lib: &Option<TomlLibTarget>,
bins: &[TomlBinTarget],
custom_build: Option<PathBuf>,
examples: &[TomlExampleTarget],
Expand All @@ -1110,7 +1113,7 @@ fn normalize(lib: &Option<TomlLibTarget>,
});
}

fn lib_target(dst: &mut Vec<Target>, l: &TomlLibTarget) {
let lib_target = |dst: &mut Vec<Target>, l: &TomlLibTarget| {
let path = l.path.clone().unwrap_or(
PathValue::Path(Path::new("src").join(&format!("{}.rs", l.name())))
);
Expand All @@ -1124,71 +1127,71 @@ fn normalize(lib: &Option<TomlLibTarget>,
};

let mut target = Target::lib_target(&l.name(), crate_types,
&path.to_path());
package_root.join(path.to_path()));
configure(l, &mut target);
dst.push(target);
}
};

fn bin_targets(dst: &mut Vec<Target>, bins: &[TomlBinTarget],
default: &mut FnMut(&TomlBinTarget) -> PathBuf) {
let bin_targets = |dst: &mut Vec<Target>, bins: &[TomlBinTarget],
default: &mut FnMut(&TomlBinTarget) -> PathBuf| {
for bin in bins.iter() {
let path = bin.path.clone().unwrap_or_else(|| {
PathValue::Path(default(bin))
});
let mut target = Target::bin_target(&bin.name(), &path.to_path());
let mut target = Target::bin_target(&bin.name(), package_root.join(path.to_path()));
configure(bin, &mut target);
dst.push(target);
}
}
};

fn custom_build_target(dst: &mut Vec<Target>, cmd: &Path) {
let custom_build_target = |dst: &mut Vec<Target>, cmd: &Path| {
let name = format!("build-script-{}",
cmd.file_stem().and_then(|s| s.to_str()).unwrap_or(""));

dst.push(Target::custom_build_target(&name, cmd));
}
dst.push(Target::custom_build_target(&name, package_root.join(cmd)));
};

fn example_targets(dst: &mut Vec<Target>,
examples: &[TomlExampleTarget],
default: &mut FnMut(&TomlExampleTarget) -> PathBuf) {
let example_targets = |dst: &mut Vec<Target>,
examples: &[TomlExampleTarget],
default: &mut FnMut(&TomlExampleTarget) -> PathBuf| {
for ex in examples.iter() {
let path = ex.path.clone().unwrap_or_else(|| {
PathValue::Path(default(ex))
});

let mut target = Target::example_target(&ex.name(), &path.to_path());
let mut target = Target::example_target(&ex.name(), package_root.join(path.to_path()));
configure(ex, &mut target);
dst.push(target);
}
}
};

fn test_targets(dst: &mut Vec<Target>,
tests: &[TomlTestTarget],
default: &mut FnMut(&TomlTestTarget) -> PathBuf) {
let test_targets = |dst: &mut Vec<Target>,
tests: &[TomlTestTarget],
default: &mut FnMut(&TomlTestTarget) -> PathBuf| {
for test in tests.iter() {
let path = test.path.clone().unwrap_or_else(|| {
PathValue::Path(default(test))
});

let mut target = Target::test_target(&test.name(), &path.to_path());
let mut target = Target::test_target(&test.name(), package_root.join(path.to_path()));
configure(test, &mut target);
dst.push(target);
}
}
};

fn bench_targets(dst: &mut Vec<Target>,
benches: &[TomlBenchTarget],
default: &mut FnMut(&TomlBenchTarget) -> PathBuf) {
let bench_targets = |dst: &mut Vec<Target>,
benches: &[TomlBenchTarget],
default: &mut FnMut(&TomlBenchTarget) -> PathBuf| {
for bench in benches.iter() {
let path = bench.path.clone().unwrap_or_else(|| {
PathValue::Path(default(bench))
});

let mut target = Target::bench_target(&bench.name(), &path.to_path());
let mut target = Target::bench_target(&bench.name(), package_root.join(path.to_path()));
configure(bench, &mut target);
dst.push(target);
}
}
};

let mut ret = Vec::new();

Expand Down
4 changes: 2 additions & 2 deletions tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn cargo_metadata_simple() {
"bin"
],
"name": "foo",
"src_path": "src[/]foo.rs"
"src_path": "[..][/]foo[/]src[/]foo.rs"
}
],
"features": {},
Expand Down Expand Up @@ -341,7 +341,7 @@ const MANIFEST_OUTPUT: &'static str=
"targets":[{
"kind":["bin"],
"name":"foo",
"src_path":"src[/]foo.rs"
"src_path":"[..][/]foo[/]src[/]foo.rs"
}],
"features":{},
"manifest_path":"[..]Cargo.toml"
Expand Down
18 changes: 6 additions & 12 deletions tests/read-manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ extern crate hamcrest;
use cargotest::support::{project, execs, main_file, basic_bin_manifest};
use hamcrest::{assert_that};

fn remove_all_whitespace(s: &str) -> String {
s.split_whitespace().collect()
}

fn read_manifest_output() -> String {
remove_all_whitespace(r#"
static MANIFEST_OUTPUT: &'static str = r#"
{
"name":"foo",
"version":"0.5.0",
Expand All @@ -21,12 +16,11 @@ fn read_manifest_output() -> String {
"targets":[{
"kind":["bin"],
"name":"foo",
"src_path":"src[..]foo.rs"
"src_path":"[..][/]foo[/]src[/]foo.rs"
}],
"features":{},
"manifest_path":"[..]Cargo.toml"
}"#)
}
}"#;

#[test]
fn cargo_read_manifest_path_to_cargo_toml_relative() {
Expand All @@ -38,7 +32,7 @@ fn cargo_read_manifest_path_to_cargo_toml_relative() {
.arg("--manifest-path").arg("foo/Cargo.toml")
.cwd(p.root().parent().unwrap()),
execs().with_status(0)
.with_stdout(read_manifest_output()));
.with_json(MANIFEST_OUTPUT));
}

#[test]
Expand All @@ -51,7 +45,7 @@ fn cargo_read_manifest_path_to_cargo_toml_absolute() {
.arg("--manifest-path").arg(p.root().join("Cargo.toml"))
.cwd(p.root().parent().unwrap()),
execs().with_status(0)
.with_stdout(read_manifest_output()));
.with_json(MANIFEST_OUTPUT));
}

#[test]
Expand Down Expand Up @@ -91,5 +85,5 @@ fn cargo_read_manifest_cwd() {
assert_that(p.cargo_process("read-manifest")
.cwd(p.root()),
execs().with_status(0)
.with_stdout(read_manifest_output()));
.with_json(MANIFEST_OUTPUT));
}