| @@ -339,6 +339,52 @@ fn build_reports_the_time_of_each_phase() { |
| 339 | 339 | ); |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | +/// Release executables leave out the runtime's debug information, unless |
| 343 | +/// the C flags ask for debug information (issue #115). |
| 344 | +#[test] |
| 345 | +fn release_builds_strip_the_runtime_debug_information() { |
| 346 | + let lib = std::path::Path::new(env!("CARGO_BIN_EXE_louise")).with_file_name("liblouise_rt.a"); |
| 347 | + if !lib.is_file() { |
| 348 | + eprintln!("skipping: {} not built", lib.display()); |
| 349 | + return; |
| 350 | + } |
| 351 | + let dir = std::path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("stripped"); |
| 352 | + // Whether an ELF-64 executable has a `.debug_info` section: the names |
| 353 | + // in its section-header string table (std holds the name as a string |
| 354 | + // too, for its own backtraces, so the bytes alone don't tell). |
| 355 | + let has_debug = |exe: &std::path::Path| { |
| 356 | + let b = std::fs::read(exe).unwrap(); |
| 357 | + let u16_at = |o: usize| u16::from_le_bytes([b[o], b[o + 1]]) as usize; |
| 358 | + let u32_at = |o: usize| u32::from_le_bytes(b[o..o + 4].try_into().unwrap()) as usize; |
| 359 | + let u64_at = |o: usize| u64::from_le_bytes(b[o..o + 8].try_into().unwrap()) as usize; |
| 360 | + let (shoff, shentsize, shnum, shstrndx) = |
| 361 | + (u64_at(0x28), u16_at(0x3a), u16_at(0x3c), u16_at(0x3e)); |
| 362 | + let strtab = u64_at(shoff + shstrndx * shentsize + 0x18); |
| 363 | + (0..shnum).any(|i| { |
| 364 | + let name = strtab + u32_at(shoff + i * shentsize); |
| 365 | + b[name..].starts_with(b".debug_info\0") |
| 366 | + }) |
| 367 | + }; |
| 368 | + for (cflags, debug) in [("", false), ("-g", true)] { |
| 369 | + let exe = dir.join(if debug { "with-g/hello" } else { "hello" }); |
| 370 | + let out = Command::new(env!("CARGO_BIN_EXE_louise")) |
| 371 | + .args(["build", "--release", "../../tests/run/hello.lse", "-o"]) |
| 372 | + .arg(&exe) |
| 373 | + .env("LOUISE_CFLAGS", cflags) |
| 374 | + .current_dir(env!("CARGO_MANIFEST_DIR")) |
| 375 | + .output() |
| 376 | + .unwrap(); |
| 377 | + assert!( |
| 378 | + out.status.success(), |
| 379 | + "{}", |
| 380 | + String::from_utf8_lossy(&out.stderr) |
| 381 | + ); |
| 382 | + assert_eq!(has_debug(&exe), debug, "LOUISE_CFLAGS={cflags:?}"); |
| 383 | + let run = Command::new(&exe).output().unwrap(); |
| 384 | + assert_eq!(String::from_utf8_lossy(&run.stdout), "Hello, Louise!\n"); |
| 385 | + } |
| 386 | +} |
| 387 | + |
| 342 | 388 | #[test] |
| 343 | 389 | fn check_reports_the_time_of_each_phase() { |
| 344 | 390 | let (ok, _, stderr) = louise(&["check", "--timings", "../../tests/run/hello.lse"]); |