Debian Codegraph @

.cargo/vendor/cc/src/command_helpers.rs

raw
1 //! Miscellaneous helpers for running commands
2
3 use std::{
4 collections::hash_map,
5 ffi::OsString,
6 fmt::Display,
7 fs,
8 hash::Hasher,
9 io::{self, Read, Write},
10 path::Path,
11 process::{Child, ChildStderr, Command, Stdio},
12 sync::{
13 atomic::{AtomicBool, Ordering},
14 Arc,
15 },
16 };
17
18 use crate::{Error, ErrorKind, Object};
19
20 #[derive(Clone, Debug)]
21 pub(crate) struct CargoOutput {
22 pub(crate) metadata: bool,
23 pub(crate) warnings: bool,
24 pub(crate) debug: bool,
25 checked_dbg_var: Arc<AtomicBool>,
26 }
27
28 impl CargoOutput {
29 pub(crate) fn new() -> Self {
30 Self {
31 metadata: true,
32 warnings: true,
33 debug: std::env::var_os("CC_ENABLE_DEBUG_OUTPUT").is_some(),
34 checked_dbg_var: Arc::new(AtomicBool::new(false)),
35 }
36 }
37
38 pub(crate) fn print_metadata(&self, s: &dyn Display) {
39 if self.metadata {
40 println!("{}", s);
41 }
42 }
43
44 pub(crate) fn print_warning(&self, arg: &dyn Display) {
45 if self.warnings {
46 println!("cargo:warning={}", arg);
47 }
48 }
49
50 pub(crate) fn print_debug(&self, arg: &dyn Display) {
51 if self.metadata && !self.checked_dbg_var.load(Ordering::Relaxed) {
52 self.checked_dbg_var.store(true, Ordering::Relaxed);
53 println!("cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT");
54 }
55 if self.debug {
56 println!("{}", arg);
57 }
58 }
59
60 fn stdio_for_warnings(&self) -> Stdio {
61 if self.warnings {
62 Stdio::piped()
63 } else {
64 Stdio::null()
65 }
66 }
67 }
68
69 pub(crate) struct StderrForwarder {
70 inner: Option<(ChildStderr, Vec<u8>)>,
71 #[cfg(feature = "parallel")]
72 is_non_blocking: bool,
73 #[cfg(feature = "parallel")]
74 bytes_available_failed: bool,
75 }
76
77 const MIN_BUFFER_CAPACITY: usize = 100;
78
79 impl StderrForwarder {
80 pub(crate) fn new(child: &mut Child) -> Self {
81 Self {
82 inner: child
83 .stderr
84 .take()
85 .map(|stderr| (stderr, Vec::with_capacity(MIN_BUFFER_CAPACITY))),
86 #[cfg(feature = "parallel")]
87 is_non_blocking: false,
88 #[cfg(feature = "parallel")]
89 bytes_available_failed: false,
90 }
91 }
92
93 #[allow(clippy::uninit_vec)]
94 fn forward_available(&mut self) -> bool {
95 if let Some((stderr, buffer)) = self.inner.as_mut() {
96 loop {
97 let old_data_end = buffer.len();
98
99 // For non-blocking we check to see if there is data available, so we should try to
100 // read at least that much. For blocking, always read at least the minimum amount.
101 #[cfg(not(feature = "parallel"))]
102 let to_reserve = MIN_BUFFER_CAPACITY;
103 #[cfg(feature = "parallel")]
104 let to_reserve = if self.is_non_blocking && !self.bytes_available_failed {
105 match crate::parallel::stderr::bytes_available(stderr) {
106 #[cfg(windows)]
107 Ok(0) => return false,
108 #[cfg(unix)]
109 Ok(0) => {
110 // On Unix, depending on the implementation, we may sometimes get 0 in a
111 // loop (either there is data available or the pipe is broken), so
112 // continue with the non-blocking read anyway.
113 MIN_BUFFER_CAPACITY
114 }
115 #[cfg(windows)]
116 Err(_) => {
117 // On Windows, if we get an error then the pipe is broken, so flush
118 // the buffer and bail.
119 if !buffer.is_empty() {
120 write_warning(&buffer[..]);
121 }
122 self.inner = None;
123 return true;
124 }
125 #[cfg(unix)]
126 Err(_) => {
127 // On Unix, depending on the implementation, we may get spurious
128 // errors so make a note not to use bytes_available again and try
129 // the non-blocking read anyway.
130 self.bytes_available_failed = true;
131 MIN_BUFFER_CAPACITY
132 }
133 Ok(bytes_available) => MIN_BUFFER_CAPACITY.max(bytes_available),
134 }
135 } else {
136 MIN_BUFFER_CAPACITY
137 };
138 buffer.reserve(to_reserve);
139
140 // SAFETY: 1) the length is set to the capacity, so we are never using memory beyond
141 // the underlying buffer and 2) we always call `truncate` below to set the len back
142 // to the initialized data.
143 unsafe {
144 buffer.set_len(buffer.capacity());
145 }
146 match stderr.read(&mut buffer[old_data_end..]) {
147 Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
148 // No data currently, yield back.
149 buffer.truncate(old_data_end);
150 return false;
151 }
152 Err(err) if err.kind() == std::io::ErrorKind::Interrupted => {
153 // Interrupted, try again.
154 buffer.truncate(old_data_end);
155 }
156 Ok(0) | Err(_) => {
157 // End of stream: flush remaining data and bail.
158 if old_data_end > 0 {
159 write_warning(&buffer[..old_data_end]);
160 }
161 self.inner = None;
162 return true;
163 }
164 Ok(bytes_read) => {
165 buffer.truncate(old_data_end + bytes_read);
166 let mut consumed = 0;
167 for line in buffer.split_inclusive(|&b| b == b'\n') {
168 // Only forward complete lines, leave the rest in the buffer.
169 if let Some((b'\n', line)) = line.split_last() {
170 consumed += line.len() + 1;
171 write_warning(line);
172 }
173 }
174 buffer.drain(..consumed);
175 }
176 }
177 }
178 } else {
179 true
180 }
181 }
182
183 #[cfg(feature = "parallel")]
184 pub(crate) fn set_non_blocking(&mut self) -> Result<(), Error> {
185 assert!(!self.is_non_blocking);
186
187 #[cfg(unix)]
188 if let Some((stderr, _)) = self.inner.as_ref() {
189 crate::parallel::stderr::set_non_blocking(stderr)?;
190 }
191
192 self.is_non_blocking = true;
193 Ok(())
194 }
195
196 #[cfg(feature = "parallel")]
197 fn forward_all(&mut self) {
198 while !self.forward_available() {}
199 }
200
201 #[cfg(not(feature = "parallel"))]
202 fn forward_all(&mut self) {
203 let forward_result = self.forward_available();
204 assert!(forward_result, "Should have consumed all data");
205 }
206 }
207
208 fn write_warning(line: &[u8]) {
209 let stdout = io::stdout();
210 let mut stdout = stdout.lock();
211 stdout.write_all(b"cargo:warning=").unwrap();
212 stdout.write_all(line).unwrap();
213 stdout.write_all(b"\n").unwrap();
214 }
215
216 fn wait_on_child(
217 cmd: &Command,
218 program: &Path,
219 child: &mut Child,
220 cargo_output: &CargoOutput,
221 ) -> Result<(), Error> {
222 StderrForwarder::new(child).forward_all();
223
224 let status = match child.wait() {
225 Ok(s) => s,
226 Err(e) => {
227 return Err(Error::new(
228 ErrorKind::ToolExecError,
229 format!(
230 "Failed to wait on spawned child process, command {:?} with args {}: {}.",
231 cmd,
232 program.display(),
233 e
234 ),
235 ));
236 }
237 };
238
239 cargo_output.print_debug(&status);
240
241 if status.success() {
242 Ok(())
243 } else {
244 Err(Error::new(
245 ErrorKind::ToolExecError,
246 format!(
247 "Command {:?} with args {} did not execute successfully (status code {}).",
248 cmd,
249 program.display(),
250 status
251 ),
252 ))
253 }
254 }
255
256 /// Find the destination object path for each file in the input source files,
257 /// and store them in the output Object.
258 pub(crate) fn objects_from_files(files: &[Arc<Path>], dst: &Path) -> Result<Vec<Object>, Error> {
259 let mut objects = Vec::with_capacity(files.len());
260 for file in files {
261 let basename = file
262 .file_name()
263 .ok_or_else(|| {
264 Error::new(
265 ErrorKind::InvalidArgument,
266 "No file_name for object file path!",
267 )
268 })?
269 .to_string_lossy();
270 let dirname = file
271 .parent()
272 .ok_or_else(|| {
273 Error::new(
274 ErrorKind::InvalidArgument,
275 "No parent for object file path!",
276 )
277 })?
278 .to_string_lossy();
279
280 // Hash the dirname. This should prevent conflicts if we have multiple
281 // object files with the same filename in different subfolders.
282 let mut hasher = hash_map::DefaultHasher::new();
283 hasher.write(dirname.to_string().as_bytes());
284 let obj = dst
285 .join(format!("{:016x}-{}", hasher.finish(), basename))
286 .with_extension("o");
287
288 match obj.parent() {
289 Some(s) => fs::create_dir_all(s)?,
290 None => {
291 return Err(Error::new(
292 ErrorKind::InvalidArgument,
293 "dst is an invalid path with no parent",
294 ));
295 }
296 };
297
298 objects.push(Object::new(file.to_path_buf(), obj));
299 }
300
301 Ok(objects)
302 }
303
304 pub(crate) fn run(
305 cmd: &mut Command,
306 program: impl AsRef<Path>,
307 cargo_output: &CargoOutput,
308 ) -> Result<(), Error> {
309 let program = program.as_ref();
310
311 let mut child = spawn(cmd, program, cargo_output)?;
312 wait_on_child(cmd, program, &mut child, cargo_output)
313 }
314
315 pub(crate) fn run_output(
316 cmd: &mut Command,
317 program: impl AsRef<Path>,
318 cargo_output: &CargoOutput,
319 ) -> Result<Vec<u8>, Error> {
320 let program = program.as_ref();
321
322 cmd.stdout(Stdio::piped());
323
324 let mut child = spawn(cmd, program, cargo_output)?;
325
326 let mut stdout = vec![];
327 child
328 .stdout
329 .take()
330 .unwrap()
331 .read_to_end(&mut stdout)
332 .unwrap();
333
334 wait_on_child(cmd, program, &mut child, cargo_output)?;
335
336 Ok(stdout)
337 }
338
339 pub(crate) fn spawn(
340 cmd: &mut Command,
341 program: &Path,
342 cargo_output: &CargoOutput,
343 ) -> Result<Child, Error> {
344 struct ResetStderr<'cmd>(&'cmd mut Command);
345
346 impl Drop for ResetStderr<'_> {
347 fn drop(&mut self) {
348 // Reset stderr to default to release pipe_writer so that print thread will
349 // not block forever.
350 self.0.stderr(Stdio::inherit());
351 }
352 }
353
354 cargo_output.print_debug(&format_args!("running: {:?}", cmd));
355
356 let cmd = ResetStderr(cmd);
357 let child = cmd.0.stderr(cargo_output.stdio_for_warnings()).spawn();
358 match child {
359 Ok(child) => Ok(child),
360 Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
361 let extra = if cfg!(windows) {
362 " (see https://github.com/rust-lang/cc-rs#compile-time-requirements \
363 for help)"
364 } else {
365 ""
366 };
367 Err(Error::new(
368 ErrorKind::ToolNotFound,
369 format!(
370 "Failed to find tool. Is `{}` installed?{}",
371 program.display(),
372 extra
373 ),
374 ))
375 }
376 Err(e) => Err(Error::new(
377 ErrorKind::ToolExecError,
378 format!(
379 "Command {:?} with args {} failed to start: {:?}",
380 cmd.0,
381 program.display(),
382 e
383 ),
384 )),
385 }
386 }
387
388 pub(crate) struct CmdAddOutputFileArgs {
389 pub(crate) cuda: bool,
390 pub(crate) is_assembler_msvc: bool,
391 pub(crate) msvc: bool,
392 pub(crate) clang: bool,
393 pub(crate) gnu: bool,
394 pub(crate) is_asm: bool,
395 pub(crate) is_arm: bool,
396 }
397
398 pub(crate) fn command_add_output_file(cmd: &mut Command, dst: &Path, args: CmdAddOutputFileArgs) {
399 if args.is_assembler_msvc
400 || (args.msvc && !args.clang && !args.gnu && !args.cuda && !(args.is_asm && args.is_arm))
401 {
402 let mut s = OsString::from("-Fo");
403 s.push(dst);
404 cmd.arg(s);
405 } else {
406 cmd.arg("-o").arg(dst);
407 }
408 }
409
410 #[cfg(feature = "parallel")]
411 pub(crate) fn try_wait_on_child(
412 cmd: &Command,
413 program: &Path,
414 child: &mut Child,
415 stdout: &mut dyn io::Write,
416 stderr_forwarder: &mut StderrForwarder,
417 ) -> Result<Option<()>, Error> {
418 stderr_forwarder.forward_available();
419
420 match child.try_wait() {
421 Ok(Some(status)) => {
422 stderr_forwarder.forward_all();
423
424 let _ = writeln!(stdout, "{}", status);
425
426 if status.success() {
427 Ok(Some(()))
428 } else {
429 Err(Error::new(
430 ErrorKind::ToolExecError,
431 format!(
432 "Command {:?} with args {} did not execute successfully (status code {}).",
433 cmd,
434 program.display(),
435 status
436 ),
437 ))
438 }
439 }
440 Ok(None) => Ok(None),
441 Err(e) => {
442 stderr_forwarder.forward_all();
443 Err(Error::new(
444 ErrorKind::ToolExecError,
445 format!(
446 "Failed to wait on spawned child process, command {:?} with args {}: {}.",
447 cmd,
448 program.display(),
449 e
450 ),
451 ))
452 }
453 }
454 }