]>
Commit | Line | Data |
---|---|---|
ba9703b0 | 1 | use super::{Diagnostic, PackageId, Target}; |
3dfed10e | 2 | use serde::{Deserialize, Serialize}; |
ba9703b0 | 3 | use std::fmt; |
f035d41b | 4 | use std::io::{self, BufRead, Lines, Read}; |
ba9703b0 XL |
5 | use std::path::PathBuf; |
6 | ||
7 | /// Profile settings used to determine which compiler flags to use for a | |
8 | /// target. | |
9 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
10 | pub struct ArtifactProfile { | |
11 | /// Optimization level. Possible values are 0-3, s or z. | |
12 | pub opt_level: String, | |
13 | /// The amount of debug info. 0 for none, 1 for limited, 2 for full | |
14 | pub debuginfo: Option<u32>, | |
15 | /// State of the `cfg(debug_assertions)` directive, enabling macros like | |
16 | /// `debug_assert!` | |
17 | pub debug_assertions: bool, | |
18 | /// State of the overflow checks. | |
19 | pub overflow_checks: bool, | |
20 | /// Whether this profile is a test | |
21 | pub test: bool, | |
22 | #[doc(hidden)] | |
23 | #[serde(skip)] | |
24 | __do_not_match_exhaustively: (), | |
25 | } | |
26 | ||
27 | /// A compiler-generated file. | |
28 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
29 | pub struct Artifact { | |
30 | /// The package this artifact belongs to | |
31 | pub package_id: PackageId, | |
32 | /// The target this artifact was compiled for | |
33 | pub target: Target, | |
34 | /// The profile this artifact was compiled with | |
35 | pub profile: ArtifactProfile, | |
36 | /// The enabled features for this artifact | |
37 | pub features: Vec<String>, | |
38 | /// The full paths to the generated artifacts | |
39 | /// (e.g. binary file and separate debug info) | |
40 | pub filenames: Vec<PathBuf>, | |
41 | /// Path to the executable file | |
42 | pub executable: Option<PathBuf>, | |
43 | /// If true, then the files were already generated | |
44 | pub fresh: bool, | |
45 | #[doc(hidden)] | |
46 | #[serde(skip)] | |
47 | __do_not_match_exhaustively: (), | |
48 | } | |
49 | ||
50 | /// Message left by the compiler | |
51 | // TODO: Better name. This one comes from machine_message.rs | |
52 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
53 | pub struct CompilerMessage { | |
54 | /// The package this message belongs to | |
55 | pub package_id: PackageId, | |
56 | /// The target this message is aimed at | |
57 | pub target: Target, | |
58 | /// The message the compiler sent. | |
59 | pub message: Diagnostic, | |
60 | #[doc(hidden)] | |
61 | #[serde(skip)] | |
62 | __do_not_match_exhaustively: (), | |
63 | } | |
64 | ||
65 | /// Output of a build script execution. | |
66 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
67 | pub struct BuildScript { | |
68 | /// The package this build script execution belongs to | |
69 | pub package_id: PackageId, | |
70 | /// The libs to link | |
71 | pub linked_libs: Vec<PathBuf>, | |
72 | /// The paths to search when resolving libs | |
73 | pub linked_paths: Vec<PathBuf>, | |
74 | /// Various `--cfg` flags to pass to the compiler | |
f035d41b | 75 | pub cfgs: Vec<String>, |
ba9703b0 XL |
76 | /// The environment variables to add to the compilation |
77 | pub env: Vec<(String, String)>, | |
78 | /// The `OUT_DIR` environment variable where this script places its output | |
79 | /// | |
80 | /// Added in Rust 1.41. | |
81 | #[serde(default)] | |
82 | pub out_dir: PathBuf, | |
83 | #[doc(hidden)] | |
84 | #[serde(skip)] | |
85 | __do_not_match_exhaustively: (), | |
86 | } | |
87 | ||
f035d41b XL |
88 | /// Final result of a build. |
89 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
90 | pub struct BuildFinished { | |
91 | /// Whether or not the build finished successfully. | |
92 | pub success: bool, | |
93 | #[doc(hidden)] | |
94 | #[serde(skip)] | |
95 | __do_not_match_exhaustively: (), | |
96 | } | |
97 | ||
ba9703b0 XL |
98 | /// A cargo message |
99 | #[derive(Debug, Clone, Serialize, Deserialize)] | |
100 | #[serde(tag = "reason", rename_all = "kebab-case")] | |
101 | pub enum Message { | |
102 | /// The compiler generated an artifact | |
103 | CompilerArtifact(Artifact), | |
104 | /// The compiler wants to display a message | |
105 | CompilerMessage(CompilerMessage), | |
106 | /// A build script successfully executed. | |
107 | BuildScriptExecuted(BuildScript), | |
f035d41b XL |
108 | /// The build has finished. |
109 | /// | |
110 | /// This is emitted at the end of the build as the last message. | |
111 | /// Added in Rust 1.44. | |
112 | BuildFinished(BuildFinished), | |
113 | /// A line of text which isn't a cargo or compiler message. | |
114 | /// Line separator is not included | |
115 | #[serde(skip)] | |
116 | TextLine(String), | |
ba9703b0 XL |
117 | #[doc(hidden)] |
118 | #[serde(other)] | |
119 | Unknown, | |
120 | } | |
121 | ||
f035d41b XL |
122 | impl Message { |
123 | /// Creates an iterator of Message from a Read outputting a stream of JSON | |
124 | /// messages. For usage information, look at the top-level documentation. | |
125 | pub fn parse_stream<R: BufRead>(input: R) -> MessageIter<R> { | |
126 | MessageIter { | |
127 | lines: input.lines(), | |
128 | } | |
129 | } | |
130 | } | |
131 | ||
ba9703b0 XL |
132 | impl fmt::Display for CompilerMessage { |
133 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
134 | write!(f, "{}", self.message) | |
135 | } | |
136 | } | |
137 | ||
f035d41b XL |
138 | /// An iterator of Messages. |
139 | pub struct MessageIter<R> { | |
140 | lines: Lines<R>, | |
141 | } | |
142 | ||
143 | impl<R: BufRead> Iterator for MessageIter<R> { | |
144 | type Item = io::Result<Message>; | |
145 | fn next(&mut self) -> Option<Self::Item> { | |
146 | let line = self.lines.next()?; | |
147 | let message = line.map(|it| serde_json::from_str(&it).unwrap_or(Message::TextLine(it))); | |
148 | Some(message) | |
149 | } | |
150 | } | |
151 | ||
ba9703b0 XL |
152 | /// An iterator of Message. |
153 | type MessageIterator<R> = | |
154 | serde_json::StreamDeserializer<'static, serde_json::de::IoRead<R>, Message>; | |
155 | ||
156 | /// Creates an iterator of Message from a Read outputting a stream of JSON | |
157 | /// messages. For usage information, look at the top-level documentation. | |
f035d41b | 158 | #[deprecated(note = "Use Message::parse_stream instead")] |
ba9703b0 XL |
159 | pub fn parse_messages<R: Read>(input: R) -> MessageIterator<R> { |
160 | serde_json::Deserializer::from_reader(input).into_iter::<Message>() | |
161 | } |