Skip to main content

cargo/util/
machine_message.rs

1use std::path::{Path, PathBuf};
2
3use serde::ser;
4use serde::Serialize;
5use serde_json::{self, json, value::RawValue};
6
7use crate::core::{compiler::CompileMode, PackageId, Target};
8
9pub trait Message: ser::Serialize {
10    fn reason(&self) -> &str;
11
12    fn to_json_string(&self) -> String {
13        let json = serde_json::to_string(self).unwrap();
14        assert!(json.starts_with("{\""));
15        let reason = json!(self.reason());
16        format!("{{\"reason\":{},{}", reason, &json[1..])
17    }
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    pub out_dir: &'a Path,
70}
71
72impl<'a> Message for BuildScript<'a> {
73    fn reason(&self) -> &str {
74        "build-script-executed"
75    }
76}
77
78#[derive(Serialize)]
79pub struct TimingInfo<'a> {
80    pub package_id: PackageId,
81    pub target: &'a Target,
82    pub mode: CompileMode,
83    pub duration: f64,
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub rmeta_time: Option<f64>,
86}
87
88impl<'a> Message for TimingInfo<'a> {
89    fn reason(&self) -> &str {
90        "timing-info"
91    }
92}