Skip to main content

cargo/util/
machine_message.rs

1use std::path::PathBuf;
2
3use serde::ser;
4use serde::Serialize;
5use serde_json::{self, json, value::RawValue};
6
7use crate::core::{PackageId, Target};
8
9pub trait Message: ser::Serialize {
10    fn reason(&self) -> &str;
11}
12
13pub fn emit<T: Message>(t: &T) {
14    let json = serde_json::to_string(t).unwrap();
15    assert!(json.starts_with("{\""));
16    let reason = json!(t.reason());
17    println!("{{\"reason\":{},{}", reason, &json[1..]);
18}
19
20#[derive(Serialize)]
21pub struct FromCompiler<'a> {
22    pub package_id: PackageId,
23    pub target: &'a Target,
24    pub message: Box<RawValue>,
25}
26
27impl<'a> Message for FromCompiler<'a> {
28    fn reason(&self) -> &str {
29        "compiler-message"
30    }
31}
32
33#[derive(Serialize)]
34pub struct Artifact<'a> {
35    pub package_id: PackageId,
36    pub target: &'a Target,
37    pub profile: ArtifactProfile,
38    pub features: Vec<String>,
39    pub filenames: Vec<PathBuf>,
40    pub executable: Option<PathBuf>,
41    pub fresh: bool,
42}
43
44impl<'a> Message for Artifact<'a> {
45    fn reason(&self) -> &str {
46        "compiler-artifact"
47    }
48}
49
50/// This is different from the regular `Profile` to maintain backwards
51/// compatibility (in particular, `test` is no longer in `Profile`, but we
52/// still want it to be included here).
53#[derive(Serialize)]
54pub struct ArtifactProfile {
55    pub opt_level: &'static str,
56    pub debuginfo: Option<u32>,
57    pub debug_assertions: bool,
58    pub overflow_checks: bool,
59    pub test: bool,
60}
61
62#[derive(Serialize)]
63pub struct BuildScript<'a> {
64    pub package_id: PackageId,
65    pub linked_libs: &'a [String],
66    pub linked_paths: &'a [String],
67    pub cfgs: &'a [String],
68    pub env: &'a [(String, String)],
69}
70
71impl<'a> Message for BuildScript<'a> {
72    fn reason(&self) -> &str {
73        "build-script-executed"
74    }
75}