Debian Codegraph @

.cargo/vendor/cc/src/tool.rs

raw
1 use std::{
2 borrow::Cow,
3 collections::HashMap,
4 env,
5 ffi::{OsStr, OsString},
6 io::Write,
7 path::{Path, PathBuf},
8 process::{Command, Stdio},
9 sync::Mutex,
10 };
11
12 use crate::{
13 command_helpers::{run_output, CargoOutput},
14 run,
15 tempfile::NamedTempfile,
16 Error, ErrorKind,
17 };
18
19 /// Configuration used to represent an invocation of a C compiler.
20 ///
21 /// This can be used to figure out what compiler is in use, what the arguments
22 /// to it are, and what the environment variables look like for the compiler.
23 /// This can be used to further configure other build systems (e.g. forward
24 /// along CC and/or CFLAGS) or the `to_command` method can be used to run the
25 /// compiler itself.
26 #[derive(Clone, Debug)]
27 #[allow(missing_docs)]
28 pub struct Tool {
29 pub(crate) path: PathBuf,
30 pub(crate) cc_wrapper_path: Option<PathBuf>,
31 pub(crate) cc_wrapper_args: Vec<OsString>,
32 pub(crate) args: Vec<OsString>,
33 pub(crate) env: Vec<(OsString, OsString)>,
34 pub(crate) family: ToolFamily,
35 pub(crate) cuda: bool,
36 pub(crate) removed_args: Vec<OsString>,
37 pub(crate) has_internal_target_arg: bool,
38 }
39
40 impl Tool {
41 pub(crate) fn new(
42 path: PathBuf,
43 cached_compiler_family: &Mutex<HashMap<Box<Path>, ToolFamily>>,
44 cargo_output: &CargoOutput,
45 out_dir: Option<&Path>,
46 ) -> Self {
47 Self::with_features(
48 path,
49 None,
50 false,
51 cached_compiler_family,
52 cargo_output,
53 out_dir,
54 )
55 }
56
57 pub(crate) fn with_clang_driver(
58 path: PathBuf,
59 clang_driver: Option<&str>,
60 cached_compiler_family: &Mutex<HashMap<Box<Path>, ToolFamily>>,
61 cargo_output: &CargoOutput,
62 out_dir: Option<&Path>,
63 ) -> Self {
64 Self::with_features(
65 path,
66 clang_driver,
67 false,
68 cached_compiler_family,
69 cargo_output,
70 out_dir,
71 )
72 }
73
74 /// Explicitly set the `ToolFamily`, skipping name-based detection.
75 pub(crate) fn with_family(path: PathBuf, family: ToolFamily) -> Self {
76 Self {
77 path,
78 cc_wrapper_path: None,
79 cc_wrapper_args: Vec::new(),
80 args: Vec::new(),
81 env: Vec::new(),
82 family,
83 cuda: false,
84 removed_args: Vec::new(),
85 has_internal_target_arg: false,
86 }
87 }
88
89 pub(crate) fn with_features(
90 path: PathBuf,
91 clang_driver: Option<&str>,
92 cuda: bool,
93 cached_compiler_family: &Mutex<HashMap<Box<Path>, ToolFamily>>,
94 cargo_output: &CargoOutput,
95 out_dir: Option<&Path>,
96 ) -> Self {
97 fn is_zig_cc(path: &Path, cargo_output: &CargoOutput) -> bool {
98 run_output(
99 Command::new(&path).arg("--version"),
100 path,
101 // tool detection issues should always be shown as warnings
102 cargo_output,
103 )
104 .map(|o| String::from_utf8_lossy(&o).contains("ziglang"))
105 .unwrap_or_default()
106 }
107
108 fn detect_family_inner(
109 path: &Path,
110 cargo_output: &CargoOutput,
111 out_dir: Option<&Path>,
112 ) -> Result<ToolFamily, Error> {
113 let tmp = NamedTempfile::new(
114 &out_dir
115 .map(Cow::Borrowed)
116 .unwrap_or_else(|| Cow::Owned(env::temp_dir())),
117 "detect_compiler_family.c",
118 )?;
119 tmp.file()
120 .write_all(include_bytes!("detect_compiler_family.c"))?;
121
122 let stdout = run_output(
123 Command::new(path).arg("-E").arg(tmp.path()),
124 path,
125 // When expanding the file, the compiler prints a lot of information to stderr
126 // that it is not an error, but related to expanding itself.
127 //
128 // cc would have to disable warning here to prevent generation of too many warnings.
129 &{
130 let mut cargo_output = cargo_output.clone();
131 cargo_output.warnings = cargo_output.debug;
132 cargo_output
133 },
134 )?;
135 let stdout = String::from_utf8_lossy(&stdout);
136
137 cargo_output.print_debug(&stdout);
138
139 // https://gitlab.kitware.com/cmake/cmake/-/blob/69a2eeb9dff5b60f2f1e5b425002a0fd45b7cadb/Modules/CMakeDetermineCompilerId.cmake#L267-271
140 let accepts_cl_style_flags =
141 run(Command::new(path).arg("-?").stdout(Stdio::null()), path, &{
142 // the errors are not errors!
143 let mut cargo_output = cargo_output.clone();
144 cargo_output.warnings = cargo_output.debug;
145 cargo_output
146 })
147 .is_ok();
148
149 let clang = stdout.contains(r#""clang""#);
150 let gcc = stdout.contains(r#""gcc""#);
151
152 match (clang, accepts_cl_style_flags, gcc) {
153 (clang_cl, true, _) => Ok(ToolFamily::Msvc { clang_cl }),
154 (true, false, _) => Ok(ToolFamily::Clang {
155 zig_cc: is_zig_cc(path, cargo_output),
156 }),
157 (false, false, true) => Ok(ToolFamily::Gnu),
158 (false, false, false) => {
159 cargo_output.print_warning(&"Compiler family detection failed since it does not define `__clang__`, `__GNUC__` or `_MSC_VER`, fallback to treating it as GNU");
160 Err(Error::new(
161 ErrorKind::ToolFamilyMacroNotFound,
162 "Expects macro `__clang__`, `__GNUC__` or `_MSC_VER`, but found none",
163 ))
164 }
165 }
166 }
167 let detect_family = |path: &Path| -> Result<ToolFamily, Error> {
168 if let Some(family) = cached_compiler_family.lock().unwrap().get(path) {
169 return Ok(*family);
170 }
171
172 let family = detect_family_inner(path, cargo_output, out_dir)?;
173 cached_compiler_family
174 .lock()
175 .unwrap()
176 .insert(path.into(), family);
177 Ok(family)
178 };
179
180 let family = detect_family(&path).unwrap_or_else(|e| {
181 cargo_output.print_warning(&format_args!(
182 "Compiler family detection failed due to error: {}",
183 e
184 ));
185 match path.file_name().map(OsStr::to_string_lossy) {
186 Some(fname) if fname.contains("clang-cl") => ToolFamily::Msvc { clang_cl: true },
187 Some(fname) if fname.ends_with("cl") || fname == "cl.exe" => {
188 ToolFamily::Msvc { clang_cl: false }
189 }
190 Some(fname) if fname.contains("clang") => match clang_driver {
191 Some("cl") => ToolFamily::Msvc { clang_cl: true },
192 _ => ToolFamily::Clang {
193 zig_cc: is_zig_cc(&path, cargo_output),
194 },
195 },
196 Some(fname) if fname.contains("zig") => ToolFamily::Clang { zig_cc: true },
197 _ => ToolFamily::Gnu,
198 }
199 });
200
201 Tool {
202 path,
203 cc_wrapper_path: None,
204 cc_wrapper_args: Vec::new(),
205 args: Vec::new(),
206 env: Vec::new(),
207 family,
208 cuda,
209 removed_args: Vec::new(),
210 has_internal_target_arg: false,
211 }
212 }
213
214 /// Add an argument to be stripped from the final command arguments.
215 pub(crate) fn remove_arg(&mut self, flag: OsString) {
216 self.removed_args.push(flag);
217 }
218
219 /// Push an "exotic" flag to the end of the compiler's arguments list.
220 ///
221 /// Nvidia compiler accepts only the most common compiler flags like `-D`,
222 /// `-I`, `-c`, etc. Options meant specifically for the underlying
223 /// host C++ compiler have to be prefixed with `-Xcompiler`.
224 /// [Another possible future application for this function is passing
225 /// clang-specific flags to clang-cl, which otherwise accepts only
226 /// MSVC-specific options.]
227 pub(crate) fn push_cc_arg(&mut self, flag: OsString) {
228 if self.cuda {
229 self.args.push("-Xcompiler".into());
230 }
231 self.args.push(flag);
232 }
233
234 /// Checks if an argument or flag has already been specified or conflicts.
235 ///
236 /// Currently only checks optimization flags.
237 pub(crate) fn is_duplicate_opt_arg(&self, flag: &OsString) -> bool {
238 let flag = flag.to_str().unwrap();
239 let mut chars = flag.chars();
240
241 // Only duplicate check compiler flags
242 if self.is_like_msvc() {
243 if chars.next() != Some('/') {
244 return false;
245 }
246 } else if (self.is_like_gnu() || self.is_like_clang()) && chars.next() != Some('-') {
247 return false;
248 }
249
250 // Check for existing optimization flags (-O, /O)
251 if chars.next() == Some('O') {
252 return self
253 .args()
254 .iter()
255 .any(|a| a.to_str().unwrap_or("").chars().nth(1) == Some('O'));
256 }
257
258 // TODO Check for existing -m..., -m...=..., /arch:... flags
259 false
260 }
261
262 /// Don't push optimization arg if it conflicts with existing args.
263 pub(crate) fn push_opt_unless_duplicate(&mut self, flag: OsString) {
264 if self.is_duplicate_opt_arg(&flag) {
265 println!("Info: Ignoring duplicate arg {:?}", &flag);
266 } else {
267 self.push_cc_arg(flag);
268 }
269 }
270
271 /// Converts this compiler into a `Command` that's ready to be run.
272 ///
273 /// This is useful for when the compiler needs to be executed and the
274 /// command returned will already have the initial arguments and environment
275 /// variables configured.
276 pub fn to_command(&self) -> Command {
277 let mut cmd = match self.cc_wrapper_path {
278 Some(ref cc_wrapper_path) => {
279 let mut cmd = Command::new(cc_wrapper_path);
280 cmd.arg(&self.path);
281 cmd
282 }
283 None => Command::new(&self.path),
284 };
285 cmd.args(&self.cc_wrapper_args);
286
287 let value = self
288 .args
289 .iter()
290 .filter(|a| !self.removed_args.contains(a))
291 .collect::<Vec<_>>();
292 cmd.args(&value);
293
294 for (k, v) in self.env.iter() {
295 cmd.env(k, v);
296 }
297 cmd
298 }
299
300 /// Returns the path for this compiler.
301 ///
302 /// Note that this may not be a path to a file on the filesystem, e.g. "cc",
303 /// but rather something which will be resolved when a process is spawned.
304 pub fn path(&self) -> &Path {
305 &self.path
306 }
307
308 /// Returns the default set of arguments to the compiler needed to produce
309 /// executables for the target this compiler generates.
310 pub fn args(&self) -> &[OsString] {
311 &self.args
312 }
313
314 /// Returns the set of environment variables needed for this compiler to
315 /// operate.
316 ///
317 /// This is typically only used for MSVC compilers currently.
318 pub fn env(&self) -> &[(OsString, OsString)] {
319 &self.env
320 }
321
322 /// Returns the compiler command in format of CC environment variable.
323 /// Or empty string if CC env was not present
324 ///
325 /// This is typically used by configure script
326 pub fn cc_env(&self) -> OsString {
327 match self.cc_wrapper_path {
328 Some(ref cc_wrapper_path) => {
329 let mut cc_env = cc_wrapper_path.as_os_str().to_owned();
330 cc_env.push(" ");
331 cc_env.push(self.path.to_path_buf().into_os_string());
332 for arg in self.cc_wrapper_args.iter() {
333 cc_env.push(" ");
334 cc_env.push(arg);
335 }
336 cc_env
337 }
338 None => OsString::from(""),
339 }
340 }
341
342 /// Returns the compiler flags in format of CFLAGS environment variable.
343 /// Important here - this will not be CFLAGS from env, its internal gcc's flags to use as CFLAGS
344 /// This is typically used by configure script
345 pub fn cflags_env(&self) -> OsString {
346 let mut flags = OsString::new();
347 for (i, arg) in self.args.iter().enumerate() {
348 if i > 0 {
349 flags.push(" ");
350 }
351 flags.push(arg);
352 }
353 flags
354 }
355
356 /// Whether the tool is GNU Compiler Collection-like.
357 pub fn is_like_gnu(&self) -> bool {
358 self.family == ToolFamily::Gnu
359 }
360
361 /// Whether the tool is Clang-like.
362 pub fn is_like_clang(&self) -> bool {
363 matches!(self.family, ToolFamily::Clang { .. })
364 }
365
366 /// Whether the tool is AppleClang under .xctoolchain
367 #[cfg(target_vendor = "apple")]
368 pub(crate) fn is_xctoolchain_clang(&self) -> bool {
369 let path = self.path.to_string_lossy();
370 path.contains(".xctoolchain/")
371 }
372 #[cfg(not(target_vendor = "apple"))]
373 pub(crate) fn is_xctoolchain_clang(&self) -> bool {
374 false
375 }
376
377 /// Whether the tool is MSVC-like.
378 pub fn is_like_msvc(&self) -> bool {
379 match self.family {
380 ToolFamily::Msvc { .. } => true,
381 _ => false,
382 }
383 }
384 }
385
386 /// Represents the family of tools this tool belongs to.
387 ///
388 /// Each family of tools differs in how and what arguments they accept.
389 ///
390 /// Detection of a family is done on best-effort basis and may not accurately reflect the tool.
391 #[derive(Copy, Clone, Debug, PartialEq)]
392 pub enum ToolFamily {
393 /// Tool is GNU Compiler Collection-like.
394 Gnu,
395 /// Tool is Clang-like. It differs from the GCC in a sense that it accepts superset of flags
396 /// and its cross-compilation approach is different.
397 Clang { zig_cc: bool },
398 /// Tool is the MSVC cl.exe.
399 Msvc { clang_cl: bool },
400 }
401
402 impl ToolFamily {
403 /// What the flag to request debug info for this family of tools look like
404 pub(crate) fn add_debug_flags(&self, cmd: &mut Tool, dwarf_version: Option<u32>) {
405 match *self {
406 ToolFamily::Msvc { .. } => {
407 cmd.push_cc_arg("-Z7".into());
408 }
409 ToolFamily::Gnu | ToolFamily::Clang { .. } => {
410 cmd.push_cc_arg(
411 dwarf_version
412 .map_or_else(|| "-g".into(), |v| format!("-gdwarf-{}", v))
413 .into(),
414 );
415 }
416 }
417 }
418
419 /// What the flag to force frame pointers.
420 pub(crate) fn add_force_frame_pointer(&self, cmd: &mut Tool) {
421 match *self {
422 ToolFamily::Gnu | ToolFamily::Clang { .. } => {
423 cmd.push_cc_arg("-fno-omit-frame-pointer".into());
424 }
425 _ => (),
426 }
427 }
428
429 /// What the flags to enable all warnings
430 pub(crate) fn warnings_flags(&self) -> &'static str {
431 match *self {
432 ToolFamily::Msvc { .. } => "-W4",
433 ToolFamily::Gnu | ToolFamily::Clang { .. } => "-Wall",
434 }
435 }
436
437 /// What the flags to enable extra warnings
438 pub(crate) fn extra_warnings_flags(&self) -> Option<&'static str> {
439 match *self {
440 ToolFamily::Msvc { .. } => None,
441 ToolFamily::Gnu | ToolFamily::Clang { .. } => Some("-Wextra"),
442 }
443 }
444
445 /// What the flag to turn warning into errors
446 pub(crate) fn warnings_to_errors_flag(&self) -> &'static str {
447 match *self {
448 ToolFamily::Msvc { .. } => "-WX",
449 ToolFamily::Gnu | ToolFamily::Clang { .. } => "-Werror",
450 }
451 }
452
453 pub(crate) fn verbose_stderr(&self) -> bool {
454 matches!(*self, ToolFamily::Clang { .. })
455 }
456 }