cargo/core/compiler/
build_config.rs1use crate::core::compiler::{CompileKind, CompileTarget};
2use crate::core::interning::InternedString;
3use crate::util::ProcessBuilder;
4use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
5use serde::ser;
6use std::cell::RefCell;
7
8#[derive(Debug)]
10pub struct BuildConfig {
11 pub requested_kind: CompileKind,
13 pub jobs: u32,
15 pub requested_profile: InternedString,
17 pub mode: CompileMode,
19 pub message_format: MessageFormat,
21 pub force_rebuild: bool,
23 pub build_plan: bool,
25 pub unit_graph: bool,
27 pub primary_unit_rustc: Option<ProcessBuilder>,
29 pub rustfix_diagnostic_server: RefCell<Option<RustfixDiagnosticServer>>,
30}
31
32impl BuildConfig {
33 pub fn new(
42 config: &Config,
43 jobs: Option<u32>,
44 requested_target: &Option<String>,
45 mode: CompileMode,
46 ) -> CargoResult<BuildConfig> {
47 let cfg = config.build_config()?;
48 let requested_kind = match requested_target {
49 Some(s) => CompileKind::Target(CompileTarget::new(s)?),
50 None => match &cfg.target {
51 Some(val) => {
52 let value = if val.raw_value().ends_with(".json") {
53 let path = val.clone().resolve_path(config);
54 path.to_str().expect("must be utf-8 in toml").to_string()
55 } else {
56 val.raw_value().to_string()
57 };
58 CompileKind::Target(CompileTarget::new(&value)?)
59 }
60 None => CompileKind::Host,
61 },
62 };
63
64 if jobs == Some(0) {
65 anyhow::bail!("jobs must be at least 1")
66 }
67 if jobs.is_some() && config.jobserver_from_env().is_some() {
68 config.shell().warn(
69 "a `-j` argument was passed to Cargo but Cargo is \
70 also configured with an external jobserver in \
71 its environment, ignoring the `-j` parameter",
72 )?;
73 }
74 let jobs = jobs.or(cfg.jobs).unwrap_or(::num_cpus::get() as u32);
75
76 Ok(BuildConfig {
77 requested_kind,
78 jobs,
79 requested_profile: InternedString::new("dev"),
80 mode,
81 message_format: MessageFormat::Human,
82 force_rebuild: false,
83 build_plan: false,
84 unit_graph: false,
85 primary_unit_rustc: None,
86 rustfix_diagnostic_server: RefCell::new(None),
87 })
88 }
89
90 pub fn emit_json(&self) -> bool {
93 match self.message_format {
94 MessageFormat::Json { .. } => true,
95 _ => false,
96 }
97 }
98
99 pub fn test(&self) -> bool {
100 self.mode == CompileMode::Test || self.mode == CompileMode::Bench
101 }
102}
103
104#[derive(Clone, Copy, Debug, PartialEq, Eq)]
105pub enum MessageFormat {
106 Human,
107 Json {
108 render_diagnostics: bool,
111 short: bool,
114 ansi: bool,
117 },
118 Short,
119}
120
121#[derive(Clone, Copy, PartialEq, Debug, Eq, Hash, PartialOrd, Ord)]
127pub enum CompileMode {
128 Test,
130 Build,
132 Check { test: bool },
136 Bench,
141 Doc { deps: bool },
144 Doctest,
146 RunCustomBuild,
148}
149
150impl ser::Serialize for CompileMode {
151 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
152 where
153 S: ser::Serializer,
154 {
155 use self::CompileMode::*;
156 match *self {
157 Test => "test".serialize(s),
158 Build => "build".serialize(s),
159 Check { .. } => "check".serialize(s),
160 Bench => "bench".serialize(s),
161 Doc { .. } => "doc".serialize(s),
162 Doctest => "doctest".serialize(s),
163 RunCustomBuild => "run-custom-build".serialize(s),
164 }
165 }
166}
167
168impl CompileMode {
169 pub fn is_check(self) -> bool {
171 match self {
172 CompileMode::Check { .. } => true,
173 _ => false,
174 }
175 }
176
177 pub fn is_doc(self) -> bool {
179 match self {
180 CompileMode::Doc { .. } => true,
181 _ => false,
182 }
183 }
184
185 pub fn is_doc_test(self) -> bool {
187 self == CompileMode::Doctest
188 }
189
190 pub fn is_any_test(self) -> bool {
193 match self {
194 CompileMode::Test
195 | CompileMode::Bench
196 | CompileMode::Check { test: true }
197 | CompileMode::Doctest => true,
198 _ => false,
199 }
200 }
201
202 pub fn is_rustc_test(self) -> bool {
204 match self {
205 CompileMode::Test | CompileMode::Bench | CompileMode::Check { test: true } => true,
206 _ => false,
207 }
208 }
209
210 pub fn is_run_custom_build(self) -> bool {
212 self == CompileMode::RunCustomBuild
213 }
214
215 pub fn all_modes() -> &'static [CompileMode] {
218 static ALL: [CompileMode; 9] = [
219 CompileMode::Test,
220 CompileMode::Build,
221 CompileMode::Check { test: true },
222 CompileMode::Check { test: false },
223 CompileMode::Bench,
224 CompileMode::Doc { deps: true },
225 CompileMode::Doc { deps: false },
226 CompileMode::Doctest,
227 CompileMode::RunCustomBuild,
228 ];
229 &ALL
230 }
231}