| @@ -577,13 +577,17 @@ pub fn build_in( |
| 577 | 577 | (true, None) => "release", |
| 578 | 578 | (false, _) => "debug", |
| 579 | 579 | }; |
| 580 | | − let mut jobs = Vec::new(); |
| 581 | | − for (unit, text) in units { |
| 582 | | − // `<name>.c` for a program in one unit, else `<name>.<unit>.c`. |
| 583 | | − let stem = match *unit { |
| 580 | + let stems: Vec<String> = units |
| 581 | + .iter() |
| 582 | + .map(|(unit, _)| match *unit { |
| 583 | + // `<name>.c` for a program in one unit, else `<name>.<unit>.c`. |
| 584 | 584 | "" => name.to_string(), |
| 585 | 585 | unit => format!("{name}.{unit}"), |
| 586 | | − }; |
| 586 | + }) |
| 587 | + .collect(); |
| 588 | + remove_stale_units(build, &format!("{name}.{profile}"), profile, &stems)?; |
| 589 | + let mut jobs = Vec::new(); |
| 590 | + for ((_, text), stem) in units.iter().zip(&stems) { |
| 587 | 591 | let c_file = build.join(format!("{stem}.c")); |
| 588 | 592 | let object = cache.join(format!("{stem}.{profile}.o")); |
| 589 | 593 | write_if_changed(&c_file, text)?; |
| @@ -694,6 +698,35 @@ pub fn build_in( |
| 694 | 698 | Ok(built) |
| 695 | 699 | } |
| 696 | 700 | |
| 701 | +/// Remove the units a build of `output.profile` wrote last time and no |
| 702 | +/// longer writes (a module the program lost), with their objects, then |
| 703 | +/// record the ones it writes now in `cache/<output.profile>.units` (issue |
| 704 | +/// #316). Only that record says which units are this build's: a name |
| 705 | +/// prefix cannot, since `foo` with a module `bin/x` and `foo.bin` with a |
| 706 | +/// module `x` both write `foo.bin.x.c`. A unit's `.c` is shared by the |
| 707 | +/// profiles; removing it costs at most writing it again, and its object, |
| 708 | +/// keyed by content, is still reused. |
| 709 | +fn remove_stale_units( |
| 710 | + build: &Path, |
| 711 | + record: &str, |
| 712 | + profile: &str, |
| 713 | + stems: &[String], |
| 714 | +) -> std::io::Result<()> { |
| 715 | + let cache = build.join("cache"); |
| 716 | + let file = cache.join(format!("{record}.units")); |
| 717 | + if let Ok(last) = std::fs::read_to_string(&file) { |
| 718 | + for stale in last.lines().filter(|l| !stems.iter().any(|s| s == l)) { |
| 719 | + let _ = std::fs::remove_file(build.join(format!("{stale}.c"))); |
| 720 | + for extension in ["o", "d", "key"] { |
| 721 | + let _ = std::fs::remove_file(cache.join(format!("{stale}.{profile}.{extension}"))); |
| 722 | + } |
| 723 | + } |
| 724 | + } |
| 725 | + let mut now = stems.join("\n"); |
| 726 | + now.push('\n'); |
| 727 | + write_if_changed(&file, &now) |
| 728 | +} |
| 729 | + |
| 697 | 730 | /// The `lto` archive `opts` link, if there is one the toolchain can read |
| 698 | 731 | /// (ADR 0032). Asked for by `LOUISE_RUNTIME=lto`, it must be usable. |
| 699 | 732 | fn usable_lto(opts: &CcOptions, tools: &Toolchain) -> Result<Option<LtoArchive>, BuildError> { |