| @@ -960,17 +960,38 @@ fn in_parallel<J: Send, R: Send>(jobs: Vec<J>, f: impl Fn(J) -> R + Sync) -> Vec |
| 960 | 960 | results |
| 961 | 961 | } |
| 962 | 962 | |
| 963 | | −/// Run the C compiler or linker; on failure, its output. |
| 963 | +/// Run the C compiler or linker; on failure, its output. A failure |
| 964 | +/// without output is described by how the process ended instead: a |
| 965 | +/// compiler the OOM killer stops has no output to show (the empty "the |
| 966 | +/// C compiler failed:" a killed clang left in the tsan workflow). |
| 964 | 967 | fn run_cc(cmd: &mut Command) -> Result<(), BuildError> { |
| 965 | 968 | let result = cmd.output()?; |
| 966 | | − if !result.status.success() { |
| 967 | | − return Err(BuildError::Cc(format!( |
| 968 | | − "{}{}", |
| 969 | | − String::from_utf8_lossy(&result.stdout), |
| 970 | | − String::from_utf8_lossy(&result.stderr) |
| 971 | | − ))); |
| 969 | + if result.status.success() { |
| 970 | + return Ok(()); |
| 972 | 971 | } |
| 973 | | − Ok(()) |
| 972 | + let out = format!( |
| 973 | + "{}{}", |
| 974 | + String::from_utf8_lossy(&result.stdout), |
| 975 | + String::from_utf8_lossy(&result.stderr) |
| 976 | + ); |
| 977 | + if out.trim().is_empty() { |
| 978 | + return Err(BuildError::Cc(ended_silently(result.status))); |
| 979 | + } |
| 980 | + Err(BuildError::Cc(out)) |
| 981 | +} |
| 982 | + |
| 983 | +/// How a failed process that said nothing ended (for [`run_cc`]): the |
| 984 | +/// exit code, or the signal that killed it. Signal 9 with no output is |
| 985 | +/// the OOM killer's signature. |
| 986 | +fn ended_silently(status: std::process::ExitStatus) -> String { |
| 987 | + #[cfg(unix)] |
| 988 | + if let Some(signal) = std::os::unix::process::ExitStatusExt::signal(&status) { |
| 989 | + return match signal { |
| 990 | + 9 => "it said nothing and was killed by signal 9 (SIGKILL): out of memory?".to_owned(), |
| 991 | + _ => format!("it said nothing and was killed by signal {signal}"), |
| 992 | + }; |
| 993 | + } |
| 994 | + format!("it said nothing and ended with {status}") |
| 974 | 995 | } |
| 975 | 996 | |
| 976 | 997 | fn run(cmd: &mut Command) -> Result<String, BuildError> { |
| @@ -1356,6 +1377,34 @@ mod tests { |
| 1356 | 1377 | } |
| 1357 | 1378 | |
| 1358 | 1379 | #[test] |
| 1380 | + fn a_failed_compile_shows_the_compilers_words_or_how_it_ended() { |
| 1381 | + let cc = find_cc().expect("a C compiler"); |
| 1382 | + let dir = std::env::temp_dir().join(format!("louise-cc-error-test-{}", std::process::id())); |
| 1383 | + std::fs::create_dir_all(&dir).unwrap(); |
| 1384 | + std::fs::write(dir.join("bad.c"), "int f(void) { return oops; }\n").unwrap(); |
| 1385 | + let err = run_cc( |
| 1386 | + Command::new(&cc) |
| 1387 | + .arg("-c") |
| 1388 | + .arg(dir.join("bad.c")) |
| 1389 | + .arg("-o") |
| 1390 | + .arg(dir.join("bad.o")), |
| 1391 | + ) |
| 1392 | + .unwrap_err(); |
| 1393 | + match err { |
| 1394 | + BuildError::Cc(out) => assert!(out.contains("oops"), "the compiler's stderr: {out}"), |
| 1395 | + other => panic!("expected Cc, got {other:?}"), |
| 1396 | + } |
| 1397 | + // A compiler killed before it could write (the OOM killer, say) |
| 1398 | + // is reported by how it ended, not by an empty message. |
| 1399 | + #[cfg(unix)] |
| 1400 | + match run_cc(Command::new("sh").args(["-c", "kill -9 $$"])).unwrap_err() { |
| 1401 | + BuildError::Cc(out) => assert!(out.contains("signal 9"), "{out}"), |
| 1402 | + other => panic!("expected Cc, got {other:?}"), |
| 1403 | + } |
| 1404 | + let _ = std::fs::remove_dir_all(&dir); |
| 1405 | + } |
| 1406 | + |
| 1407 | + #[test] |
| 1359 | 1408 | fn llvm_versions_are_read_from_version_banners() { |
| 1360 | 1409 | // Parsed, not run: a script written and run at once by a test can |
| 1361 | 1410 | // fail with ETXTBSY while another test thread forks. |