diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 6c71cd9b94b..4ab5ea4f9f1 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -112,19 +112,34 @@ pub enum TargetKind { CustomBuild, } -impl Encodable for TargetKind { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { +impl TargetKind { + /// Returns a vector of crate types as specified in a manifest with one difference. + /// For ExampleLib it returns "example" instead of crate types + pub fn kinds(&self) -> Vec<&str> { + use self::TargetKind::*; match *self { - TargetKind::Lib(ref kinds) | - TargetKind::ExampleLib(ref kinds) => { + Lib(ref kinds) => kinds.iter().map(LibKind::crate_type).collect(), + Bin => vec!["bin"], + ExampleBin | ExampleLib(_) => vec!["example"], + Test => vec!["test"], + CustomBuild => vec!["custom-build"], + Bench => vec!["bench"] + } + } + + /// Returns a vector of crate types as specified in a manifest + pub fn crate_types(&self) -> Vec<&str> { + use self::TargetKind::*; + match *self { + Lib(ref kinds) | ExampleLib(ref kinds) => { kinds.iter().map(LibKind::crate_type).collect() } - TargetKind::Bin => vec!["bin"], - TargetKind::ExampleBin => vec!["example"], - TargetKind::Test => vec!["test"], - TargetKind::CustomBuild => vec!["custom-build"], - TargetKind::Bench => vec!["bench"], - }.encode(s) + Bin => vec!["bin"], + ExampleBin => vec!["example"], + Test => vec!["test"], + CustomBuild => vec!["custom-build"], + Bench => vec!["bench"] + } } } @@ -196,7 +211,8 @@ pub struct Target { #[derive(RustcEncodable)] struct SerializedTarget<'a> { - kind: &'a TargetKind, + kind: Vec<&'a str>, + crate_types: Vec<&'a str>, name: &'a str, src_path: &'a str, } @@ -204,7 +220,8 @@ struct SerializedTarget<'a> { impl Encodable for Target { fn encode(&self, s: &mut S) -> Result<(), S::Error> { SerializedTarget { - kind: &self.kind, + kind: self.kind.kinds(), + crate_types: self.kind.crate_types(), name: &self.name, src_path: &self.src_path.display().to_string(), }.encode(s) diff --git a/tests/build.rs b/tests/build.rs index 75ca7dcee53..5d012b862fc 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -2510,7 +2510,12 @@ fn compiler_json_error_format() { { "reason":"compiler-message", "package_id":"bar 0.5.0 ([..])", - "target":{"kind":["lib"],"name":"bar","src_path":"[..]lib.rs"}, + "target":{ + "kind":["lib"], + "crate_types":["lib"], + "name":"bar", + "src_path":"[..]lib.rs" + }, "message":"{...}" } @@ -2524,21 +2529,36 @@ fn compiler_json_error_format() { }, "features": [], "package_id":"bar 0.5.0 ([..])", - "target":{"kind":["lib"],"name":"bar","src_path":"[..]lib.rs"}, + "target":{ + "kind":["lib"], + "crate_types":["lib"], + "name":"bar", + "src_path":"[..]lib.rs" + }, "filenames":["[..].rlib"] } { "reason":"compiler-message", "package_id":"foo 0.5.0 ([..])", - "target":{"kind":["bin"],"name":"foo","src_path":"[..]main.rs"}, + "target":{ + "kind":["bin"], + "crate_types":["bin"], + "name":"foo", + "src_path":"[..]main.rs" + }, "message":"{...}" } { "reason":"compiler-artifact", "package_id":"foo 0.5.0 ([..])", - "target":{"kind":["bin"],"name":"foo","src_path":"[..]main.rs"}, + "target":{ + "kind":["bin"], + "crate_types":["bin"], + "name":"foo", + "src_path":"[..]main.rs" + }, "profile": { "debug_assertions": true, "debuginfo": 2, @@ -2580,14 +2600,24 @@ fn message_format_json_forward_stderr() { { "reason":"compiler-message", "package_id":"foo 0.5.0 ([..])", - "target":{"kind":["bin"],"name":"foo","src_path":"[..]"}, + "target":{ + "kind":["bin"], + "crate_types":["bin"], + "name":"foo", + "src_path":"[..]" + }, "message":"{...}" } { "reason":"compiler-artifact", "package_id":"foo 0.5.0 ([..])", - "target":{"kind":["bin"],"name":"foo","src_path":"[..]"}, + "target":{ + "kind":["bin"], + "crate_types":["bin"], + "name":"foo", + "src_path":"[..]" + }, "profile":{ "debug_assertions":true, "debuginfo":2, diff --git a/tests/metadata.rs b/tests/metadata.rs index 349de9fc195..f7dbbc414f1 100644 --- a/tests/metadata.rs +++ b/tests/metadata.rs @@ -28,6 +28,9 @@ fn cargo_metadata_simple() { "kind": [ "bin" ], + "crate_types": [ + "bin" + ], "name": "foo", "src_path": "[..][/]foo[/]src[/]foo.rs" } @@ -50,6 +53,62 @@ fn cargo_metadata_simple() { }"#)); } +#[test] +fn library_with_several_crate_types() { + let p = project("foo") + .file("src/lib.rs", "") + .file("Cargo.toml", r#" +[package] +name = "foo" +version = "0.5.0" + +[lib] +crate-type = ["lib", "staticlib"] + "#); + + assert_that(p.cargo_process("metadata"), execs().with_json(r#" + { + "packages": [ + { + "name": "foo", + "version": "0.5.0", + "id": "foo[..]", + "source": null, + "dependencies": [], + "license": null, + "license_file": null, + "description": null, + "targets": [ + { + "kind": [ + "lib", + "staticlib" + ], + "crate_types": [ + "lib", + "staticlib" + ], + "name": "foo", + "src_path": "[..][/]foo[/]src[/]lib.rs" + } + ], + "features": {}, + "manifest_path": "[..]Cargo.toml" + } + ], + "workspace_members": ["foo 0.5.0 (path+file:[..]foo)"], + "resolve": { + "nodes": [ + { + "dependencies": [], + "id": "foo 0.5.0 (path+file:[..]foo)" + } + ], + "root": "foo 0.5.0 (path+file:[..]foo)" + }, + "version": 1 + }"#)); +} #[test] fn cargo_metadata_with_deps_and_version() { @@ -93,6 +152,9 @@ fn cargo_metadata_with_deps_and_version() { "kind": [ "lib" ], + "crate_types": [ + "lib" + ], "name": "baz", "src_path": "[..]lib.rs" } @@ -125,6 +187,9 @@ fn cargo_metadata_with_deps_and_version() { "kind": [ "lib" ], + "crate_types": [ + "lib" + ], "name": "bar", "src_path": "[..]lib.rs" } @@ -157,6 +222,9 @@ fn cargo_metadata_with_deps_and_version() { "kind": [ "bin" ], + "crate_types": [ + "bin" + ], "name": "foo", "src_path": "[..]foo.rs" } @@ -190,6 +258,127 @@ fn cargo_metadata_with_deps_and_version() { }"#)); } +#[test] +fn example() { + let p = project("foo") + .file("src/lib.rs", "") + .file("examples/ex.rs", "") + .file("Cargo.toml", r#" +[package] +name = "foo" +version = "0.1.0" + +[[example]] +name = "ex" + "#); + + assert_that(p.cargo_process("metadata"), execs().with_json(r#" + { + "packages": [ + { + "name": "foo", + "version": "0.1.0", + "id": "foo[..]", + "license": null, + "license_file": null, + "description": null, + "source": null, + "dependencies": [], + "targets": [ + { + "kind": [ "lib" ], + "crate_types": [ "lib" ], + "name": "foo", + "src_path": "[..][/]foo[/]src[/]lib.rs" + }, + { + "kind": [ "example" ], + "crate_types": [ "example" ], + "name": "ex", + "src_path": "[..][/]foo[/]examples[/]ex.rs" + } + ], + "features": {}, + "manifest_path": "[..]Cargo.toml" + } + ], + "workspace_members": [ + "foo 0.1.0 (path+file:[..]foo)" + ], + "resolve": { + "root": "foo 0.1.0 (path+file://[..]foo)", + "nodes": [ + { + "id": "foo 0.1.0 (path+file:[..]foo)", + "dependencies": [] + } + ] + }, + "version": 1 + }"#)); +} + +#[test] +fn example_lib() { + let p = project("foo") + .file("src/lib.rs", "") + .file("examples/ex.rs", "") + .file("Cargo.toml", r#" +[package] +name = "foo" +version = "0.1.0" + +[[example]] +name = "ex" +crate-type = ["rlib", "dylib"] + "#); + + assert_that(p.cargo_process("metadata"), execs().with_json(r#" + { + "packages": [ + { + "name": "foo", + "version": "0.1.0", + "id": "foo[..]", + "license": null, + "license_file": null, + "description": null, + "source": null, + "dependencies": [], + "targets": [ + { + "kind": [ "lib" ], + "crate_types": [ "lib" ], + "name": "foo", + "src_path": "[..][/]foo[/]src[/]lib.rs" + }, + { + "kind": [ "example" ], + "crate_types": [ "rlib", "dylib" ], + "name": "ex", + "src_path": "[..][/]foo[/]examples[/]ex.rs" + } + ], + "features": {}, + "manifest_path": "[..]Cargo.toml" + } + ], + "workspace_members": [ + "foo 0.1.0 (path+file:[..]foo)" + ], + "resolve": { + "root": "foo 0.1.0 (path+file://[..]foo)", + "nodes": [ + { + "id": "foo 0.1.0 (path+file:[..]foo)", + "dependencies": [] + } + ] + }, + "version": 1 + }"#)); +} + #[test] fn workspace_metadata() { let p = project("foo") @@ -218,6 +407,7 @@ fn workspace_metadata() { "targets": [ { "kind": [ "lib" ], + "crate_types": [ "lib" ], "name": "bar", "src_path": "[..]bar[/]src[/]lib.rs" } @@ -237,6 +427,7 @@ fn workspace_metadata() { "targets": [ { "kind": [ "lib" ], + "crate_types": [ "lib" ], "name": "baz", "src_path": "[..]baz[/]src[/]lib.rs" } @@ -291,6 +482,7 @@ fn workspace_metadata_no_deps() { "targets": [ { "kind": [ "lib" ], + "crate_types": [ "lib" ], "name": "bar", "src_path": "[..]bar[/]src[/]lib.rs" } @@ -310,6 +502,7 @@ fn workspace_metadata_no_deps() { "targets": [ { "kind": [ "lib" ], + "crate_types": ["lib"], "name": "baz", "src_path": "[..]baz[/]src[/]lib.rs" } @@ -351,6 +544,7 @@ const MANIFEST_OUTPUT: &'static str= "description": null, "targets":[{ "kind":["bin"], + "crate_types":["bin"], "name":"foo", "src_path":"[..][/]foo[/]src[/]foo.rs" }], @@ -429,7 +623,7 @@ fn cargo_metadata_no_deps_cwd() { } #[test] -fn carg_metadata_bad_version() { +fn cargo_metadata_bad_version() { let p = project("foo") .file("Cargo.toml", &basic_bin_manifest("foo")) .file("src/foo.rs", &main_file(r#""i am foo""#, &[])); diff --git a/tests/read-manifest.rs b/tests/read-manifest.rs index ab29399a560..f656ab2108c 100644 --- a/tests/read-manifest.rs +++ b/tests/read-manifest.rs @@ -16,6 +16,7 @@ static MANIFEST_OUTPUT: &'static str = r#" "dependencies":[], "targets":[{ "kind":["bin"], + "crate_types":["bin"], "name":"foo", "src_path":"[..][/]foo[/]src[/]foo.rs" }],