Skip to content

Commit 282f238

Browse files
committedNov 30, 2018
Include executable in JSON output.
1 parent 982b9e8 commit 282f238

File tree

7 files changed

+195
-4
lines changed

7 files changed

+195
-4
lines changed
 

‎src/cargo/core/compiler/context/compilation_files.rs

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ pub struct OutputFile {
4949
pub flavor: FileFlavor,
5050
}
5151

52+
impl OutputFile {
53+
/// Gets the hardlink if present. Otherwise returns the path.
54+
pub fn bindst(&self) -> &PathBuf {
55+
return match self.hardlink {
56+
Some(ref link_dst) => link_dst,
57+
None => &self.path,
58+
};
59+
}
60+
}
61+
5262
impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
5363
pub(super) fn new(
5464
roots: &[Unit<'a>],

‎src/cargo/core/compiler/context/mod.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
163163
continue;
164164
}
165165

166-
let bindst = match output.hardlink {
167-
Some(ref link_dst) => link_dst,
168-
None => &output.path,
169-
};
166+
let bindst = output.bindst();
170167

171168
if unit.mode == CompileMode::Test {
172169
self.compilation.tests.push((
@@ -274,6 +271,23 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
274271
Ok(self.compilation)
275272
}
276273

274+
/// Returns the executable for the specified unit (if any).
275+
pub fn get_executable(&mut self, unit: &Unit<'a>) -> CargoResult<Option<PathBuf>> {
276+
for output in self.outputs(unit)?.iter() {
277+
if output.flavor == FileFlavor::DebugInfo {
278+
continue;
279+
}
280+
281+
let is_binary = unit.target.is_bin() || unit.target.is_bin_example();
282+
let is_test = unit.mode.is_any_test() && !unit.mode.is_check();
283+
284+
if is_binary || is_test {
285+
return Ok(Option::Some(output.bindst().clone()));
286+
}
287+
}
288+
return Ok(None);
289+
}
290+
277291
pub fn prepare_units(
278292
&mut self,
279293
export_dir: Option<PathBuf>,

‎src/cargo/core/compiler/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ fn link_targets<'a, 'cfg>(
409409
.map(|s| s.to_owned())
410410
.collect();
411411
let json_messages = bcx.build_config.json_messages();
412+
let executable = cx.get_executable(unit)?;
412413
let mut target = unit.target.clone();
413414
if let TargetSourcePath::Metabuild = target.src_path() {
414415
// Give it something to serialize.
@@ -463,6 +464,7 @@ fn link_targets<'a, 'cfg>(
463464
profile: art_profile,
464465
features,
465466
filenames: destinations,
467+
executable,
466468
fresh,
467469
});
468470
}

‎src/cargo/util/machine_message.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use serde::ser;
24
use serde_json::{self, value::RawValue};
35

@@ -34,6 +36,7 @@ pub struct Artifact<'a> {
3436
pub profile: ArtifactProfile,
3537
pub features: Vec<String>,
3638
pub filenames: Vec<PathBuf>,
39+
pub executable: Option<PathBuf>,
3740
pub fresh: bool,
3841
}
3942

‎tests/testsuite/bench.rs

+78
Original file line numberDiff line numberDiff line change
@@ -1485,3 +1485,81 @@ fn bench_virtual_manifest_all_implied() {
14851485
.with_stdout_contains("test bench_bar ... bench: [..]")
14861486
.run();
14871487
}
1488+
1489+
#[test]
1490+
fn json_artifact_includes_executable_for_benchmark() {
1491+
if !is_nightly() {
1492+
return;
1493+
}
1494+
1495+
let p = project()
1496+
.file("src/main.rs", "fn main() {}")
1497+
.file(
1498+
"benches/benchmark.rs",
1499+
r#"
1500+
#![feature(test)]
1501+
extern crate test;
1502+
1503+
use test::Bencher;
1504+
1505+
#[bench]
1506+
fn bench_foo(_: &mut Bencher) -> () { () }
1507+
"#,
1508+
)
1509+
.build();
1510+
1511+
p.cargo("bench --no-run --message-format=json")
1512+
.with_json(r#"
1513+
{
1514+
"executable": "[..]/foo/target/release/foo[EXE]",
1515+
"features": [],
1516+
"filenames": "{...}",
1517+
"fresh": false,
1518+
"package_id": "foo 0.0.1 ([..])",
1519+
"profile": "{...}",
1520+
"reason": "compiler-artifact",
1521+
"target": {
1522+
"crate_types": [ "bin" ],
1523+
"kind": [ "bin" ],
1524+
"edition": "2015",
1525+
"name": "foo",
1526+
"src_path": "[..]/foo/src/main.rs"
1527+
}
1528+
}
1529+
1530+
{
1531+
"executable": "[..]/foo/target/release/foo-[..][EXE]",
1532+
"features": [],
1533+
"filenames": [ "[..]/foo/target/release/foo-[..][EXE]" ],
1534+
"fresh": false,
1535+
"package_id": "foo 0.0.1 ([..])",
1536+
"profile": "{...}",
1537+
"reason": "compiler-artifact",
1538+
"target": {
1539+
"crate_types": [ "bin" ],
1540+
"kind": [ "bin" ],
1541+
"edition": "2015",
1542+
"name": "foo",
1543+
"src_path": "[..]/foo/src/main.rs"
1544+
}
1545+
}
1546+
1547+
{
1548+
"executable": "[..]/foo/target/release/benchmark-[..][EXE]",
1549+
"features": [],
1550+
"filenames": [ "[..]/foo/target/release/benchmark-[..][EXE]" ],
1551+
"fresh": false,
1552+
"package_id": "foo 0.0.1 ([..])",
1553+
"profile": "{...}",
1554+
"reason": "compiler-artifact",
1555+
"target": {
1556+
"crate_types": [ "bin" ],
1557+
"kind": [ "bench" ],
1558+
"edition": "2015",
1559+
"name": "benchmark",
1560+
"src_path": "[..]/foo/benches/benchmark.rs"
1561+
}
1562+
}
1563+
"#)
1564+
.run();
1565+
}

‎tests/testsuite/build.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3083,6 +3083,7 @@ fn compiler_json_error_format() {
30833083
"overflow_checks": true,
30843084
"test": false
30853085
},
3086+
"executable": null,
30863087
"features": [],
30873088
"filenames": "{...}",
30883089
"fresh": false
@@ -3110,6 +3111,7 @@ fn compiler_json_error_format() {
31103111
"overflow_checks": true,
31113112
"test": false
31123113
},
3114+
"executable": null,
31133115
"features": [],
31143116
"package_id":"bar 0.5.0 ([..])",
31153117
"target":{
@@ -3162,6 +3164,7 @@ fn compiler_json_error_format() {
31623164
"overflow_checks": true,
31633165
"test": false
31643166
},
3167+
"executable": "[..]/foo/target/debug/foo[EXE]",
31653168
"features": [],
31663169
"filenames": "{...}",
31673170
"fresh": false
@@ -3191,6 +3194,7 @@ fn compiler_json_error_format() {
31913194
"overflow_checks": true,
31923195
"test": false
31933196
},
3197+
"executable": null,
31943198
"features": [],
31953199
"filenames": "{...}",
31963200
"fresh": true
@@ -3205,6 +3209,7 @@ fn compiler_json_error_format() {
32053209
"overflow_checks": true,
32063210
"test": false
32073211
},
3212+
"executable": null,
32083213
"features": [],
32093214
"package_id":"bar 0.5.0 ([..])",
32103215
"target":{
@@ -3244,6 +3249,7 @@ fn compiler_json_error_format() {
32443249
"overflow_checks": true,
32453250
"test": false
32463251
},
3252+
"executable": "[..]/foo/target/debug/foo[EXE]",
32473253
"features": [],
32483254
"filenames": "{...}",
32493255
"fresh": true
@@ -3309,6 +3315,7 @@ fn message_format_json_forward_stderr() {
33093315
"overflow_checks": false,
33103316
"test":false
33113317
},
3318+
"executable": "{...}",
33123319
"features":[],
33133320
"filenames": "{...}",
33143321
"fresh": false

‎tests/testsuite/test.rs

+77
Original file line numberDiff line numberDiff line change
@@ -3004,6 +3004,7 @@ fn json_artifact_includes_test_flag() {
30043004
"overflow_checks": true,
30053005
"test": false
30063006
},
3007+
"executable": null,
30073008
"features": [],
30083009
"package_id":"foo 0.0.1 ([..])",
30093010
"target":{
@@ -3026,6 +3027,7 @@ fn json_artifact_includes_test_flag() {
30263027
"overflow_checks": true,
30273028
"test": true
30283029
},
3030+
"executable": "[..]/foo-[..]",
30293031
"features": [],
30303032
"package_id":"foo 0.0.1 ([..])",
30313033
"target":{
@@ -3042,6 +3044,81 @@ fn json_artifact_includes_test_flag() {
30423044
).run();
30433045
}
30443046

3047+
#[test]
3048+
fn json_artifact_includes_executable_for_library_tests() {
3049+
let p = project()
3050+
.file("src/main.rs", "fn main() { }")
3051+
.file("src/lib.rs", r#"#[test] fn lib_test() {}"#)
3052+
.build();
3053+
3054+
p.cargo("test --lib -v --no-run --message-format=json")
3055+
.with_json(r#"
3056+
{
3057+
"executable": "[..]/foo/target/debug/foo-[..][EXE]",
3058+
"features": [],
3059+
"filenames": "{...}",
3060+
"fresh": false,
3061+
"package_id": "foo 0.0.1 ([..])",
3062+
"profile": "{...}",
3063+
"reason": "compiler-artifact",
3064+
"target": {
3065+
"crate_types": [ "lib" ],
3066+
"kind": [ "lib" ],
3067+
"edition": "2015",
3068+
"name": "foo",
3069+
"src_path": "[..]/foo/src/lib.rs"
3070+
}
3071+
}
3072+
"#)
3073+
.run();
3074+
}
3075+
3076+
#[test]
3077+
fn json_artifact_includes_executable_for_integration_tests() {
3078+
let p = project()
3079+
.file("src/main.rs", "fn main() {}")
3080+
.file("tests/integration_test.rs", r#"#[test] fn integration_test() {}"#)
3081+
.build();
3082+
3083+
p.cargo("test -v --no-run --message-format=json --test integration_test")
3084+
.with_json(r#"
3085+
{
3086+
"executable": "[..]/foo/target/debug/foo[EXE]",
3087+
"features": [],
3088+
"filenames": "{...}",
3089+
"fresh": false,
3090+
"package_id": "foo 0.0.1 ([..])",
3091+
"profile": "{...}",
3092+
"reason": "compiler-artifact",
3093+
"target": {
3094+
"crate_types": [ "bin" ],
3095+
"kind": [ "bin" ],
3096+
"edition": "2015",
3097+
"name": "foo",
3098+
"src_path": "[..]/foo/src/main.rs"
3099+
}
3100+
}
3101+
3102+
{
3103+
"executable": "[..]/foo/target/debug/integration_test-[..][EXE]",
3104+
"features": [],
3105+
"filenames": "{...}",
3106+
"fresh": false,
3107+
"package_id": "foo 0.0.1 ([..])",
3108+
"profile": "{...}",
3109+
"reason": "compiler-artifact",
3110+
"target": {
3111+
"crate_types": [ "bin" ],
3112+
"kind": [ "test" ],
3113+
"edition": "2015",
3114+
"name": "integration_test",
3115+
"src_path": "[..]/foo/tests/integration_test.rs"
3116+
}
3117+
}
3118+
"#)
3119+
.run();
3120+
}
3121+
30453122
#[test]
30463123
fn test_build_script_links() {
30473124
let p = project()

0 commit comments

Comments
 (0)
Please sign in to comment.