1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! Serialization formats for cargo messages.

use std::borrow;
use std::path;

pub mod diagnostic;

#[cfg(feature = "test_unstable")]
pub mod test;

type CowPath<'a> = borrow::Cow<'a, path::Path>;
type CowStr<'a> = borrow::Cow<'a, str>;

/// A cargo message
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "reason", rename_all = "kebab-case")]
pub enum Message<'a> {
    /// The compiler generated an artifact
    #[serde(borrow)]
    CompilerArtifact(Artifact<'a>),
    /// The compiler wants to display a message
    #[serde(borrow)]
    CompilerMessage(FromCompiler<'a>),
    /// A build script successfully executed.
    #[serde(borrow)]
    BuildScriptExecuted(BuildScript<'a>),
    #[cfg(not(feature = "strict_unstable"))]
    #[doc(hidden)]
    #[serde(other)]
    Unknown,
}

/// A compiler-generated file.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "strict_unstable", serde(deny_unknown_fields))]
pub struct Artifact<'a> {
    /// The workspace member this artifact belongs to
    #[serde(borrow)]
    pub package_id: WorkspaceMember<'a>,
    /// The target this artifact was compiled for
    #[serde(borrow)]
    pub target: Target<'a>,
    /// The profile this artifact was compiled with
    #[serde(borrow)]
    pub profile: ArtifactProfile<'a>,
    /// The enabled features for this artifact
    #[serde(borrow)]
    pub features: Vec<CowStr<'a>>,
    /// The full paths to the generated artifacts
    #[serde(borrow)]
    pub filenames: Vec<CowPath<'a>>,
    /// The full paths to the generated artifacts
    #[cfg(feature = "cargo_unstable")]
    #[serde(borrow)]
    pub executable: Option<CowPath<'a>>,
    /// If true, then the files were already generated
    pub fresh: bool,
    #[doc(hidden)]
    #[serde(skip)]
    __do_not_match_exhaustively: (),
}

/// A single target (lib, bin, example, ...) provided by a crate
#[derive(Clone, Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "strict_unstable", serde(deny_unknown_fields))]
pub struct Target<'a> {
    /// Name as given in the `Cargo.toml` or generated from the file name
    #[serde(borrow)]
    pub name: CowStr<'a>,
    /// Kind of target ("bin", "example", "test", "bench", "lib")
    #[serde(borrow)]
    pub kind: Vec<CowStr<'a>>,
    /// Almost the same as `kind`, except when an example is a library instad of an executable.
    /// In that case `crate_types` contains things like `rlib` and `dylib` while `kind` is `example`
    #[serde(default)]
    #[serde(borrow)]
    pub crate_types: Vec<CowStr<'a>>,

    #[serde(default)]
    #[serde(rename = "required-features")]
    /// This target is built only if these features are enabled.
    /// It doesn't apply to `lib` targets.
    #[serde(borrow)]
    pub required_features: Vec<CowStr<'a>>,
    /// Path to the main source file of the target
    #[serde(borrow)]
    pub src_path: CowPath<'a>,
    /// Rust edition for this target
    #[serde(default = "edition_default")]
    #[serde(borrow)]
    pub edition: CowStr<'a>,
    #[doc(hidden)]
    #[serde(skip)]
    __do_not_match_exhaustively: (),
}

fn edition_default() -> CowStr<'static> {
    "2015".into()
}

/// A workspace member. This is basically identical to `cargo::core::package_id::PackageId`, except
/// that this does not use `Arc` internally.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
#[cfg_attr(feature = "strict_unstable", serde(deny_unknown_fields))]
pub struct WorkspaceMember<'a> {
    /// The raw package id as given by cargo
    #[serde(borrow)]
    raw: CowStr<'a>,
}

/// Profile settings used to determine which compiler flags to use for a
/// target.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "strict_unstable", serde(deny_unknown_fields))]
pub struct ArtifactProfile<'a> {
    /// Optimization level. Possible values are 0-3, s or z.
    #[serde(borrow)]
    pub opt_level: CowStr<'a>,
    /// The amount of debug info. 0 for none, 1 for limited, 2 for full
    pub debuginfo: Option<u32>,
    /// State of the `cfg(debug_assertions)` directive, enabling macros like
    /// `debug_assert!`
    pub debug_assertions: bool,
    /// State of the overflow checks.
    pub overflow_checks: bool,
    /// Whether this profile is a test
    pub test: bool,
    #[doc(hidden)]
    #[serde(skip)]
    __do_not_match_exhaustively: (),
}

/// Message left by the compiler
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "strict_unstable", serde(deny_unknown_fields))]
pub struct FromCompiler<'a> {
    /// The workspace member this message belongs to
    #[serde(borrow)]
    pub package_id: WorkspaceMember<'a>,
    /// The target this message is aimed at
    #[serde(borrow)]
    pub target: Target<'a>,
    /// The message the compiler sent.
    #[serde(borrow)]
    pub message: diagnostic::Diagnostic<'a>,
    #[doc(hidden)]
    #[serde(skip)]
    __do_not_match_exhaustively: (),
}

/// Output of a Build Script execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "strict_unstable", serde(deny_unknown_fields))]
pub struct BuildScript<'a> {
    /// The workspace member this build script execution belongs to
    #[serde(borrow)]
    pub package_id: WorkspaceMember<'a>,
    /// The libs to link
    #[serde(borrow)]
    pub linked_libs: Vec<CowStr<'a>>,
    /// The paths to search when resolving libs
    #[serde(borrow)]
    pub linked_paths: Vec<CowPath<'a>>,
    /// The paths to search when resolving libs
    #[serde(borrow)]
    pub cfgs: Vec<CowPath<'a>>,
    /// The environment variables to add to the compilation
    #[serde(borrow)]
    pub env: Vec<(CowStr<'a>, CowStr<'a>)>,
    #[doc(hidden)]
    #[serde(skip)]
    __do_not_match_exhaustively: (),
}

#[cfg(not(feature = "print"))]
pub(crate) fn log_message(msg: &Message) {
    match msg {
        Message::CompilerArtifact(ref art) => {
            trace!("Building {:#?}", art.package_id,);
        }
        Message::CompilerMessage(ref comp) => {
            let content = comp
                .message
                .rendered
                .as_ref()
                .map(|s| s.as_ref())
                .unwrap_or(comp.message.message.as_ref());
            match comp.message.level {
                diagnostic::DiagnosticLevel::Ice => error!("{}", content),
                diagnostic::DiagnosticLevel::Error => error!("{}", content),
                diagnostic::DiagnosticLevel::Warning => warn!("{}", content),
                diagnostic::DiagnosticLevel::Note => info!("{}", content),
                diagnostic::DiagnosticLevel::Help => info!("{}", content),
                #[cfg(not(feature = "strict_unstable"))]
                _ => warn!("Unknown message: {:#?}", msg),
            }
        }
        Message::BuildScriptExecuted(ref script) => {
            trace!("Ran script from {:#?}", script.package_id);
        }
        #[cfg(not(feature = "strict_unstable"))]
        _ => {
            warn!("Unknown message: {:#?}", msg);
        }
    }
}

#[cfg(feature = "print")]
pub(crate) fn log_message(msg: &Message) {
    match msg {
        Message::CompilerArtifact(ref art) => {
            println!("Building {:#?}", art.package_id,);
        }
        Message::CompilerMessage(ref comp) => {
            let content = comp
                .message
                .rendered
                .as_ref()
                .map(|s| s.as_ref())
                .unwrap_or(comp.message.message.as_ref());
            match comp.message.level {
                diagnostic::DiagnosticLevel::Ice => println!("{}", content),
                diagnostic::DiagnosticLevel::Error => println!("{}", content),
                diagnostic::DiagnosticLevel::Warning => println!("{}", content),
                diagnostic::DiagnosticLevel::Note => println!("{}", content),
                diagnostic::DiagnosticLevel::Help => println!("{}", content),
                #[cfg(not(feature = "strict_unstable"))]
                _ => warn!("Unknown message: {:#?}", msg),
            }
        }
        Message::BuildScriptExecuted(ref script) => {
            println!("Ran script from {:#?}", script.package_id);
        }
        #[cfg(not(feature = "strict_unstable"))]
        _ => {
            println!("Unknown message: {:#?}", msg);
        }
    }
}