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

Include executable in JSON output. #5517

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ pub struct OutputFile {
pub flavor: FileFlavor,
}

impl OutputFile {
/// Gets the hardlink if present. Otherwise returns the path.
pub fn bindist(&self) -> &PathBuf {
return match self.hardlink {
Some(ref link_dst) => link_dst,
None => &self.path,
};
}
}

impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
pub(super) fn new(
roots: &[Unit<'a>],
Expand Down
30 changes: 22 additions & 8 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,21 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
}

for unit in units.iter() {

for output in self.outputs(unit)?.iter() {
if output.flavor == FileFlavor::DebugInfo {
continue;
}

let bindst = match output.hardlink {
Some(ref link_dst) => link_dst,
None => &output.path,
};

if unit.mode == CompileMode::Test {
if unit.mode.is_any_test() && !unit.mode.is_check() {
self.compilation.tests.push((
unit.pkg.clone(),
unit.target.kind().clone(),
unit.target.name().to_string(),
output.path.clone(),
));
} else if unit.target.is_bin() || unit.target.is_bin_example() {
self.compilation.binaries.push(bindst.clone());
let bindist = output.bindist();
self.compilation.binaries.push(bindist.clone());
} else if unit.target.is_lib() {
let pkgid = unit.pkg.package_id().clone();
self.compilation
Expand Down Expand Up @@ -305,6 +301,24 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
Ok(self.compilation)
}

/// Returns the executable for the specified unit (if any).
pub fn get_executable(&mut self, unit: &Unit<'a>) -> CargoResult<Option<PathBuf>> {
for output in self.outputs(unit)?.iter() {
if output.flavor == FileFlavor::DebugInfo {
continue;
}

let is_binary = unit.target.is_bin() || unit.target.is_bin_example();
let is_test = unit.mode.is_any_test() && !unit.mode.is_check();

if is_binary || is_test {
let bindist = output.bindist();
return Ok(Option::Some(bindist.clone()));
}
}
return Ok(None);
}

pub fn prepare_units(
&mut self,
export_dir: Option<PathBuf>,
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ fn link_targets<'a, 'cfg>(
.map(|s| s.to_owned())
.collect();
let json_messages = bcx.build_config.json_messages();
let executable = cx.get_executable(unit)?;

Ok(Work::new(move |_| {
// If we're a "root crate", e.g. the target of this compilation, then we
Expand Down Expand Up @@ -439,6 +440,7 @@ fn link_targets<'a, 'cfg>(
profile: art_profile,
features,
filenames: destinations,
executable,
fresh,
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/cargo/util/machine_message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use serde::ser;
use serde_json::{self, Value};

Expand Down Expand Up @@ -33,6 +35,7 @@ pub struct Artifact<'a> {
pub profile: ArtifactProfile,
pub features: Vec<String>,
pub filenames: Vec<String>,
pub executable: Option<PathBuf>,
pub fresh: bool,
}

Expand Down
81 changes: 81 additions & 0 deletions tests/testsuite/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1917,3 +1917,84 @@ fn bench_virtual_manifest_all_implied() {
.with_stdout_contains("test bench_foo ... bench: [..]"),
);
}

#[test]
fn json_artifact_includes_executable_for_benchmark() {
let p = project("foo")
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#,
)
.file("src/main.rs", "fn main() { }")
.file(
"benches/benchmark.rs",
r#"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
fn bench_foo(_: &mut Bencher) -> () { () }
"#,
)
.build();

assert_that(
p.cargo("bench --no-run --message-format=json"),
execs().with_status(0).with_json(
r#"
{
"executable": "[..]foo[/]target[/]release[/]foo[EXE]",
"features": [],
"filenames": "{...}",
"fresh": false,
"package_id": "foo 0.0.1 ([..])",
"profile": "{...}",
"reason": "compiler-artifact",
"target": {
"crate_types": [ "bin" ],
"kind": [ "bin" ],
"name": "foo",
"src_path": "[..]main.rs"
}
}

{
"executable": "[..]foo-[..][EXE]",
"features": [],
"filenames": [ "[..]foo-[..][EXE]", "[..]" ],
"fresh": false,
"package_id": "foo 0.0.1 ([..])",
"profile": "{...}",
"reason": "compiler-artifact",
"target": {
"crate_types": [ "bin" ],
"kind": [ "bin" ],
"name": "foo",
"src_path": "[..]main.rs"
}
}

{
"executable": "[..]benchmark-[..][EXE]",
"features": [],
"filenames": [ "[..]benchmark-[..][EXE]" ],
"fresh": false,
"package_id": "foo 0.0.1 ([..])",
"profile": "{...}",
"reason": "compiler-artifact",
"target": {
"crate_types": [ "bin" ],
"kind": [ "bench" ],
"name": "benchmark",
"src_path": "[..]benchmark.rs"
}
}
"#,
),
);
}
19 changes: 5 additions & 14 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,7 @@ fn compiler_json_error_format() {
"overflow_checks": true,
"test": false
},
"executable": null,
"features": [],
"package_id":"bar 0.5.0 ([..])",
"target":{
Expand Down Expand Up @@ -4092,6 +4093,7 @@ fn compiler_json_error_format() {
"overflow_checks": true,
"test": false
},
"executable": "[..]foo[/]target[/]debug[/]foo[EXE]",
"features": [],
"filenames": "{...}",
"fresh": false
Expand Down Expand Up @@ -4122,20 +4124,7 @@ fn compiler_json_error_format() {
"overflow_checks": true,
"test": false
},
"features": [],
"filenames": "{...}",
"fresh": true
}

{
"reason":"compiler-artifact",
"profile": {
"debug_assertions": true,
"debuginfo": 2,
"opt_level": "0",
"overflow_checks": true,
"test": false
},
"executable": null,
"features": [],
"package_id":"bar 0.5.0 ([..])",
"target":{
Expand Down Expand Up @@ -4173,6 +4162,7 @@ fn compiler_json_error_format() {
"overflow_checks": true,
"test": false
},
"executable": "[..]foo[/]target[/]debug[/]foo[EXE]",
"features": [],
"filenames": "{...}",
"fresh": true
Expand Down Expand Up @@ -4244,6 +4234,7 @@ fn message_format_json_forward_stderr() {
"overflow_checks": false,
"test":false
},
"executable": "{...}",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not verifying the executable here since that's not what's being tested here. Is that OK?

Copy link
Member

Choose a reason for hiding this comment

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

Yep, that's ok!

"features":[],
"filenames": "{...}",
"fresh": false
Expand Down
110 changes: 110 additions & 0 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4102,6 +4102,7 @@ fn json_artifact_includes_test_flag() {
"overflow_checks": true,
"test": false
},
"executable": null,
"features": [],
"package_id":"foo 0.0.1 ([..])",
"target":{
Expand All @@ -4123,6 +4124,7 @@ fn json_artifact_includes_test_flag() {
"overflow_checks": true,
"test": true
},
"executable": "[..][/]foo-[..]",
"features": [],
"package_id":"foo 0.0.1 ([..])",
"target":{
Expand All @@ -4138,3 +4140,111 @@ fn json_artifact_includes_test_flag() {
),
);
}

#[test]
fn json_artifact_includes_executable_for_library_tests() {
let p = project("foo")
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#,
)
.file("src/main.rs", "fn main() { }")
.file(
"src/lib.rs",
r#"
#[test]
fn lib_test() { }
"#,
)
.build();

assert_that(
p.cargo("test --lib -v --no-run --message-format=json"),
execs().with_status(0).with_json(
r#"
{
"executable": "[..]foo[/]target[/]debug[/]foo-[..][EXE]",
"features": [],
"filenames": "{...}",
"fresh": false,
"package_id": "foo 0.0.1 ([..])",
"profile": "{...}",
"reason": "compiler-artifact",
"target": {
"crate_types": [ "lib" ],
"kind": [ "lib" ],
"name": "foo",
"src_path": "[..]foo[/]src[/]lib.rs"
}
}
"#,
),
);
}

#[test]
fn json_artifact_includes_executable_for_integration_tests() {
let p = project("foo")
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
"#,
)
.file("src/main.rs", "fn main() { }")
.file(
"tests/integration_test.rs",
r#"
#[test]
fn integration_test() { }
"#,
)
.build();

assert_that(
p.cargo("test -v --no-run --message-format=json --test integration_test"),
execs().with_status(0).with_json(
r#"
{
"executable": "[..]foo[/]target[/]debug[/]foo[EXE]",
"features": [],
"filenames": "{...}",
"fresh": false,
"package_id": "foo 0.0.1 ([..])",
"profile": "{...}",
"reason": "compiler-artifact",
"target": {
"crate_types": [ "bin" ],
"kind": [ "bin" ],
"name": "foo",
"src_path": "[..]main.rs"
}
}

{
"executable": "[..]integration_test-[..][EXE]",
"features": [],
"filenames": "{...}",
"fresh": false,
"package_id": "foo 0.0.1 ([..])",
"profile": "{...}",
"reason": "compiler-artifact",
"target": {
"crate_types": [ "bin" ],
"kind": [ "test" ],
"name": "integration_test",
"src_path": "[..]integration_test.rs"
}
}
"#,
),
);
}