Skip to main content

rucc_driver/
phase.rs

1//! The phase graph: what has to happen to each input file, in what order, and where the
2//! result goes.
3//!
4//! Design: `spec/04-driver-and-cli.md` section 4.2.
5//!
6//! The plan is computed before anything runs and is a plain data structure with no side
7//! effects, which is what makes `-###` possible and what makes this testable without a file
8//! system. Nothing in here reads a file or spawns a process. Executing the plan is M3, when
9//! there is something for the phases to do.
10
11use std::fmt::Write as _;
12
13use rucc_session::{EmitKind, Options, SaveTemps};
14use rucc_target::Os;
15
16use crate::link::Item;
17
18/// A step in the compilation of one input.
19///
20/// The order of the variants is the order of the pipeline, and the derived `Ord` is relied on
21/// when a mode flag truncates a sequence. `Compile` covers parsing through code generation,
22/// which is one phase from the driver's point of view because nothing between them can be
23/// stopped at from the command line.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
25pub enum Phase {
26    /// Translation phases 1 to 4, producing preprocessed source.
27    Preprocess,
28    /// Parse, check, optimize and generate code, producing assembly.
29    Compile,
30    /// Assemble, producing an object file.
31    Assemble,
32    /// Collect the objects into a static library.
33    ///
34    /// In front of the link rather than after it because the derived `Ord` is what truncates a
35    /// sequence, and every phase an archive needs is a phase a link needs too. No input's sequence
36    /// has this in it: an archive is one step for the whole command line, the way a link is, and
37    /// what each input contributes to it is an object.
38    Archive,
39    /// Link the objects into an executable or a shared library.
40    Link,
41}
42
43impl Phase {
44    /// The name used in `-###` output and in diagnostics.
45    #[must_use]
46    pub fn as_str(self) -> &'static str {
47        match self {
48            Phase::Preprocess => "preprocess",
49            Phase::Compile => "compile",
50            Phase::Assemble => "assemble",
51            Phase::Archive => "archive",
52            Phase::Link => "link",
53        }
54    }
55}
56
57impl std::fmt::Display for Phase {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        f.write_str(self.as_str())
60    }
61}
62
63/// What an input file is, which decides where in the pipeline it enters.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
65pub enum InputKind {
66    /// C source. Extension `.c`, or `-x c`.
67    C,
68    /// A header compiled on its own. Extension `.h` with `-x c-header`, or `-x c-header`.
69    CHeader,
70    /// Already preprocessed C. Extension `.i`, or `-x cpp-output`.
71    PreprocessedC,
72    /// The IR this compiler prints. Extension `.ir`, or `-x ir`.
73    ///
74    /// Not a GCC input kind, because GCC has no textual IR. It is here because the IR's
75    /// printer and its parser are a pair, and a pair is only known to agree if something reads
76    /// back what was written: `rucc --emit=ir a.c -o a.ir` and then `rucc --emit=ir a.ir` are
77    /// two files a byte comparison has an opinion about, over whatever code is at hand rather
78    /// than over the modules a test happens to build.
79    Ir,
80    /// Assembly. Extension `.s`, or `-x assembler`.
81    Assembler,
82    /// Assembly that still needs the preprocessor. Extension `.S` or `.sx`, or
83    /// `-x assembler-with-cpp`.
84    AssemblerWithCpp,
85    /// An object file, an archive or a shared library. Anything the linker takes directly.
86    LinkerInput,
87}
88
89impl InputKind {
90    /// The name `-x` uses for this kind, where one exists.
91    #[must_use]
92    pub fn as_str(self) -> &'static str {
93        match self {
94            InputKind::C => "c",
95            InputKind::CHeader => "c-header",
96            InputKind::PreprocessedC => "cpp-output",
97            InputKind::Ir => "ir",
98            InputKind::Assembler => "assembler",
99            InputKind::AssemblerWithCpp => "assembler-with-cpp",
100            InputKind::LinkerInput => "linker-input",
101        }
102    }
103
104    /// Parses the argument of `-x`.
105    ///
106    /// # Errors
107    ///
108    /// Returns the offending name when it is not one we accept. C++ gets its own message,
109    /// because "unknown language c++" reads like an oversight and it is a decision.
110    pub fn from_x_arg(name: &str) -> Result<InputKind, XError> {
111        match name {
112            "c" => Ok(InputKind::C),
113            "c-header" => Ok(InputKind::CHeader),
114            "cpp-output" | "c-cpp-output" => Ok(InputKind::PreprocessedC),
115            "ir" => Ok(InputKind::Ir),
116            "assembler" => Ok(InputKind::Assembler),
117            "assembler-with-cpp" => Ok(InputKind::AssemblerWithCpp),
118            "c++" | "c++-header" | "c++-cpp-output" | "objective-c" | "objective-c++" => {
119                Err(XError::Unsupported(name.to_owned()))
120            }
121            _ => Err(XError::Unknown(name.to_owned())),
122        }
123    }
124
125    /// Classifies an input by its extension, the way `spec/04-driver-and-cli.md` section 4.2
126    /// tabulates it.
127    ///
128    /// An unrecognized extension is a linker input, which is GCC's behavior and is what makes
129    /// `rucc foo.o bar.builtin-suffix` work. The exception is a C++ extension, which is a
130    /// hard error rather than a confusing link failure later.
131    ///
132    /// # Errors
133    ///
134    /// Returns the extension when it names a language that is permanently out of scope.
135    pub fn from_path(path: &str) -> Result<InputKind, XError> {
136        let ext = extension(path);
137        match ext {
138            // Matched case-sensitively on purpose: `.S` and `.s` are different languages and
139            // conflating them is a real bug on case-insensitive file systems that GCC also
140            // has. The comment is here so the next person does not "fix" it.
141            "c" => Ok(InputKind::C),
142            "i" => Ok(InputKind::PreprocessedC),
143            "ir" => Ok(InputKind::Ir),
144            "h" => Ok(InputKind::CHeader),
145            "s" => Ok(InputKind::Assembler),
146            "S" | "sx" => Ok(InputKind::AssemblerWithCpp),
147            "cc" | "cpp" | "cxx" | "c++" | "C" | "hpp" | "hxx" | "ii" | "m" | "mm" => {
148                Err(XError::Unsupported(ext.to_owned()))
149            }
150            _ => Ok(InputKind::LinkerInput),
151        }
152    }
153
154    /// The full phase sequence for this kind, before any mode flag truncates it.
155    fn full_sequence(self) -> &'static [Phase] {
156        use Phase::{Assemble, Compile, Link, Preprocess};
157        match self {
158            InputKind::C | InputKind::CHeader => &[Preprocess, Compile, Assemble, Link],
159            InputKind::PreprocessedC | InputKind::Ir => &[Compile, Assemble, Link],
160            // Note the gap: assembly with a preprocessor skips `Compile` entirely. This is why
161            // the sequence is a list rather than a range over the enum.
162            InputKind::AssemblerWithCpp => &[Preprocess, Assemble, Link],
163            InputKind::Assembler => &[Assemble, Link],
164            InputKind::LinkerInput => &[Link],
165        }
166    }
167}
168
169/// Why an input or an `-x` argument was rejected.
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub enum XError {
172    /// A language we do not know at all.
173    Unknown(String),
174    /// A language we know and will not implement.
175    Unsupported(String),
176}
177
178impl std::fmt::Display for XError {
179    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180        match self {
181            XError::Unknown(name) => {
182                write!(
183                    f,
184                    "unknown language `{name}`; \
185                     accepted: c, c-header, cpp-output, ir, assembler, assembler-with-cpp, none"
186                )
187            }
188            XError::Unsupported(name) => {
189                write!(
190                    f,
191                    "`{name}` is not C, and this compiler is only ever going to compile C; \
192                     see the not-in-scope list in spec/00-README.md"
193                )
194            }
195        }
196    }
197}
198
199impl std::error::Error for XError {}
200
201/// What one entry of the input list was written as.
202///
203/// Three things share the list because all three are positional and the position is what they mean.
204/// An archive is searched for what is undefined at the moment the linker reaches it, so a library
205/// named before the object that needs it contributes nothing. A great many of the linker's own
206/// options are a bracket around the files after them, so an option moved away from what it brackets
207/// says nothing at all. Keeping a list of files, a list of libraries and a list of linker words
208/// apart would lose the one fact each of them depends on.
209#[derive(Debug, Clone, Copy, PartialEq, Eq)]
210pub enum Role {
211    /// A path, which is a file this command line may have something to do to.
212    File,
213    /// `-l<name>`, which the linker resolves against its search path.
214    Library,
215    /// One word from `-Wl,` or `-Xlinker`, handed to the linker where the user wrote it.
216    Linker,
217}
218
219/// One input file, with the `-x` setting that was in effect where it appeared.
220#[derive(Debug, Clone, PartialEq, Eq)]
221pub struct Input {
222    /// The path as it was written on the command line, the name of a `-l` library, or one word
223    /// handed straight to the linker.
224    pub path: String,
225    /// The language forced by an earlier `-x`, if any. `-x none` clears it.
226    pub forced: Option<InputKind>,
227    /// Which of the three this is, and so what its position means.
228    pub role: Role,
229}
230
231impl Input {
232    /// An input with no `-x` in effect.
233    #[must_use]
234    pub fn new(path: impl Into<String>) -> Input {
235        Input { path: path.into(), forced: None, role: Role::File }
236    }
237
238    /// `-l<name>`, which is an input to the link and to nothing else.
239    #[must_use]
240    pub fn library(name: impl Into<String>) -> Input {
241        Input { path: name.into(), forced: None, role: Role::Library }
242    }
243
244    /// One word of `-Wl,` or `-Xlinker`, which is read by the linker and by nothing here.
245    #[must_use]
246    pub fn linker(arg: impl Into<String>) -> Input {
247        Input { path: arg.into(), forced: None, role: Role::Linker }
248    }
249
250    /// How this input is named in a message about it.
251    #[must_use]
252    pub fn named(&self) -> String {
253        match self.role {
254            Role::File => self.path.clone(),
255            Role::Library => format!("-l{}", self.path),
256            Role::Linker => format!("-Wl,{}", self.path),
257        }
258    }
259
260    /// What this input is, taking `-x` into account.
261    ///
262    /// # Errors
263    ///
264    /// Returns the extension when it names a language that is out of scope.
265    pub fn kind(&self) -> Result<InputKind, XError> {
266        if self.role != Role::File {
267            return Ok(InputKind::LinkerInput);
268        }
269        match self.forced {
270            Some(k) => Ok(k),
271            None => InputKind::from_path(&self.path),
272        }
273    }
274}
275
276/// Where the result of a job goes.
277#[derive(Debug, Clone, PartialEq, Eq)]
278pub enum Output {
279    /// Standard output, which is where `-E` writes when there is no `-o`.
280    Stdout,
281    /// A path the user can see and named, or that we derived from the input name.
282    File(String),
283    /// A file the link step consumes and nothing else ever sees. The name is a hint for
284    /// `-###` output; the real path is chosen in a temporary directory at execution time.
285    Temporary(String),
286}
287
288impl Output {
289    fn render(&self) -> String {
290        match self {
291            Output::Stdout => "-".to_owned(),
292            Output::File(p) => p.clone(),
293            Output::Temporary(p) => format!("{p} (temporary)"),
294        }
295    }
296
297    /// The path the link step reads, for an output that feeds it.
298    fn as_link_input(&self) -> Option<&str> {
299        match self {
300            Output::File(p) | Output::Temporary(p) => Some(p),
301            Output::Stdout => None,
302        }
303    }
304}
305
306/// Everything that has to happen to one input file.
307#[derive(Debug, Clone, PartialEq, Eq)]
308pub struct Job {
309    /// The input path as written.
310    pub input: String,
311    /// What we decided it is.
312    pub kind: InputKind,
313    /// The phases to run, in order. Empty when the input goes straight to the linker.
314    pub phases: Vec<Phase>,
315    /// Where the last phase writes.
316    pub output: Output,
317    /// What the files `-save-temps` keeps are called, without the suffix that says which one it
318    /// is, or `None` when there is nothing to keep.
319    ///
320    /// Nothing to keep is the usual case: the flag was not given, or it was and this job has no
321    /// step whose result the compilation would have thrown away. `-E -save-temps` is the second
322    /// of those, since the preprocessed text is the output and is already being written.
323    pub aux_base: Option<String>,
324    /// Where `-fstack-usage` writes this job's report, or `None` when it was not asked for or the
325    /// job does not compile C.
326    ///
327    /// gcc's auxiliary base name with `.su` on the end, the same name `-save-temps` builds its
328    /// files from: `-c a.c -o out/x.o` writes `out/x.su`, a bare `-c sub/a.c` writes `a.su` in the
329    /// working directory, and `a.c` linked into `out/prog` writes `out/prog-a.su`. The rest of the
330    /// rules are on `aux_base` in this file.
331    pub stack_usage: Option<String>,
332}
333
334impl Job {
335    /// Where the preprocessed text goes when `-save-temps` asked for it to be kept.
336    ///
337    /// `None` when the flag was not given, when the input arrives preprocessed already and there
338    /// is no phase 4 to keep the result of, or when the text is this job's own output and is
339    /// being written anyway.
340    #[must_use]
341    pub fn saved_text(&self) -> Option<String> {
342        let base = self.aux_base.as_ref()?;
343        self.phases.contains(&Phase::Preprocess).then(|| format!("{base}.i"))
344    }
345
346    /// Where the assembly goes when `-save-temps` asked for it to be kept.
347    ///
348    /// `None` for the same reasons, the last of them being `-S`: the assembly is the output
349    /// there, and a copy of it under a second name is a file nobody asked for.
350    #[must_use]
351    pub fn saved_asm(&self) -> Option<String> {
352        let base = self.aux_base.as_ref()?;
353        let past = self.phases.last().is_some_and(|last| *last > Phase::Compile);
354        (past && self.phases.contains(&Phase::Compile)).then(|| format!("{base}.s"))
355    }
356}
357
358/// The link step, when there is one.
359#[derive(Debug, Clone, PartialEq, Eq)]
360pub struct LinkJob {
361    /// Objects and libraries, in command line order, because link order is semantic.
362    pub inputs: Vec<Item>,
363    /// The executable.
364    pub output: String,
365}
366
367/// The archive step, when there is one.
368#[derive(Debug, Clone, PartialEq, Eq)]
369pub struct ArchiveJob {
370    /// What the members are called inside the archive, in command line order.
371    ///
372    /// Command line order is the order they are written in, which is the stated order
373    /// `spec/cross-compile/13-distribution.md` section 13.6 asks for: the caller chose it and the
374    /// same command line produces the same archive. The names are the object file names, because
375    /// that is what `ar t` over anybody else's archive shows.
376    pub members: Vec<String>,
377    /// The archive.
378    pub output: String,
379}
380
381/// The whole plan for one invocation.
382#[derive(Debug, Clone, PartialEq, Eq)]
383pub struct Plan {
384    /// One per input, in command line order.
385    pub jobs: Vec<Job>,
386    /// The link step, or `None` when a mode flag stopped short of it.
387    pub link: Option<LinkJob>,
388    /// The archive step, which is there only under `--emit=archive` and is never there beside a
389    /// link: one command line produces one of the two.
390    pub archive: Option<ArchiveJob>,
391    /// Things worth saying under `-v` that are not errors, such as an object file passed on a
392    /// command line that is not linking.
393    pub notes: Vec<String>,
394    /// The argument of `-o` as it was written, if it was given.
395    ///
396    /// Kept alongside the paths it produced because the `-M` family needs the name rather than
397    /// the path: a make rule whose target is the object the build asked for is one the build
398    /// can read back, and a rule naming a temporary directory is one nothing will ever match.
399    pub output: Option<String>,
400}
401
402/// Why a command line could not be turned into a plan.
403#[derive(Debug, Clone, PartialEq, Eq)]
404pub struct PlanError {
405    /// Lowercase, no trailing period, the same shape as every other diagnostic.
406    pub message: String,
407}
408
409impl std::fmt::Display for PlanError {
410    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
411        f.write_str(&self.message)
412    }
413}
414
415impl std::error::Error for PlanError {}
416
417fn plan_err(message: impl Into<String>) -> PlanError {
418    PlanError { message: message.into() }
419}
420
421/// The last phase that runs, given what the user asked to be emitted.
422///
423/// `--emit=tast` and the other intermediate dumps stop where `-S` stops, because they are
424/// produced inside the compile phase and there is nothing after them to run.
425#[must_use]
426pub fn last_phase(emit: EmitKind) -> Phase {
427    match emit {
428        EmitKind::Preprocessed => Phase::Preprocess,
429        EmitKind::Asm
430        | EmitKind::Tast
431        | EmitKind::Ir
432        | EmitKind::MirFinal
433        | EmitKind::SafetySummary
434        | EmitKind::TypeGranules
435        | EmitKind::SyntaxOnly => Phase::Compile,
436        EmitKind::Object => Phase::Assemble,
437        EmitKind::Archive => Phase::Archive,
438        EmitKind::Executable => Phase::Link,
439    }
440}
441
442/// What an input written on the command line is when it means standard input rather than a file.
443pub const STDIN: &str = "-";
444
445/// What a file reads as in a message, in a line marker and in `__FILE__`.
446///
447/// Standard input has no name, so it gets the one gcc gives it. A build that pipes a file in and
448/// then greps the output for a name is reading gcc's name for it, and `-` is not that name.
449#[must_use]
450pub fn source_name(path: &str) -> &str {
451    if path == STDIN { "<stdin>" } else { path }
452}
453
454/// The extension of a path, without the dot, or the empty string when there is none.
455fn extension(path: &str) -> &str {
456    let name = path.rsplit(['/', '\\']).next().unwrap_or(path);
457    match name.rfind('.') {
458        // A leading dot is a hidden file, not an extension, and `.` and `..` are not inputs.
459        Some(0) | None => "",
460        Some(i) => &name[i + 1..],
461    }
462}
463
464/// The last component of a path, which is the whole of it when there is no directory in it.
465fn file_part(path: &str) -> &str {
466    path.rsplit(['/', '\\']).next().unwrap_or(path)
467}
468
469/// The path with its extension taken off and its directory left on.
470///
471/// This is what a name derived from `-o` is built on, since `-o out/a.o` puts the files that go
472/// beside the object in `out` and not in the working directory.
473fn without_extension(path: &str) -> &str {
474    let start = path.rfind(['/', '\\']).map_or(0, |i| i + 1);
475    match path[start..].rfind('.') {
476        // A leading dot is a hidden file, not an extension, and `.` and `..` are not inputs.
477        Some(0) | None => path,
478        Some(i) => &path[..start + i],
479    }
480}
481
482/// The path without its extension, keeping any directory part off, because GCC writes the
483/// output into the current directory rather than next to the source.
484fn stem(path: &str) -> &str {
485    file_part(without_extension(path))
486}
487
488/// The name the files `-save-temps` and `-fstack-usage` keep beside an output are built from,
489/// without the suffix that says which one it is.
490///
491/// GCC calls this the auxiliary base name, and it is the name of the file the compilation
492/// produces with the extension taken off: `-c a.c -o out/a.o` keeps `out/a.i`, `out/a.s` and
493/// `out/a.su`. A command line that links has one output for however many inputs, so the input's
494/// own name goes on the end and `a.c` under `-o out/prog` becomes `out/prog-a`. The output keeps
495/// its extension there, since it is not the name of an object, and only an `.exe` comes off, so
496/// `-o lib.so` gives `lib.so-a`. A link with no `-o` and one input has nothing to tell apart and
497/// uses the input's name alone, and with more than one it uses `a-`, from `a.out`.
498/// `-save-temps=cwd` is the same name with the directory taken off, which is the only thing the
499/// two spellings disagree about.
500///
501/// `-dumpdir` replaces everything in front of the input's name, or of the output's in a compile
502/// that names one, and is a directory when it ends in a slash and the start of a name when it
503/// does not. `-dumpbase` replaces the name, still with the input's on the end when linking, and
504/// keeps the directory of the output or of `-dumpdir` unless it has one of its own.
505/// `-dumpbase-ext` is taken off the end of `-dumpbase`. Every case here was measured against gcc
506/// 16.
507///
508/// `collecting` is that one output, which is the link and the archive both, and `alone` is
509/// whether the command line has a single input. `output` is `None` for an `-o -`, which names no
510/// file.
511fn aux_base(
512    opts: &Options,
513    input: &str,
514    output: Option<&str>,
515    collecting: bool,
516    alone: bool,
517) -> String {
518    let own = stem(input);
519    let named = if let Some(base) = opts.dump_base.as_deref() {
520        let ext = opts.dump_base_ext.as_deref().filter(|ext| !ext.is_empty());
521        let stripped = ext.and_then(|ext| base.strip_suffix(ext)).filter(|b| !b.is_empty());
522        let base = stripped.unwrap_or(base);
523        let base = if collecting { format!("{base}-{own}") } else { base.to_owned() };
524        if base.contains(['/', '\\']) {
525            base
526        } else {
527            let dir = opts.dump_dir.as_deref().unwrap_or_else(|| output.map_or("", directory));
528            format!("{dir}{base}")
529        }
530    } else if let Some(dir) = &opts.dump_dir {
531        let name = match output {
532            Some(o) if !collecting => file_part(without_extension(o)),
533            _ => own,
534        };
535        format!("{dir}{name}")
536    } else if collecting {
537        match output {
538            Some(o) => format!("{}-{own}", o.strip_suffix(".exe").unwrap_or(o)),
539            None if alone => own.to_owned(),
540            None => format!("{}-{own}", stem(default_exe(opts))),
541        }
542    } else {
543        output.map_or(own, without_extension).to_owned()
544    };
545    if opts.save_temps == SaveTemps::Cwd { file_part(&named).to_owned() } else { named }
546}
547
548/// The directory part of a path with the slash that ends it, or nothing when there is none.
549fn directory(path: &str) -> &str {
550    path.rfind(['/', '\\']).map_or("", |i| &path[..=i])
551}
552
553/// The suffix a phase's output carries, for this target.
554fn suffix_for(phase: Phase, opts: &Options) -> &'static str {
555    match phase {
556        Phase::Preprocess => "i",
557        // The compile phase is where every intermediate dump comes out, and each of them is a
558        // different language, so each gets a name of its own. `rucc --emit=tast a.c` writing
559        // `a.s` would be a file that neither an assembler nor a reader could make sense of.
560        Phase::Compile => match opts.emit {
561            EmitKind::Tast => "tast",
562            EmitKind::Ir => "ir",
563            EmitKind::MirFinal => "mir",
564            // Two extensions rather than one, because the content is JSON and a tool that reads
565            // JSON should be able to tell by looking, and because `a.json` next to `a.c` says
566            // nothing about which of a build's several JSON files it is.
567            EmitKind::SafetySummary => "safety.json",
568            // Two extensions for the same reason, and text rather than JSON because this one
569            // is read by a person once and not by a build every time.
570            EmitKind::TypeGranules => "granules.txt",
571            _ => "s",
572        },
573        // MSVC-targeted builds expect `.obj`, and build systems written for that target look
574        // for it by name.
575        Phase::Assemble => {
576            if opts.target.os == Os::Windows {
577                "obj"
578            } else {
579                "o"
580            }
581        }
582        // Neither of the last two is a suffix anything derives: both write one file for the whole
583        // command line and both take its name from `-o`, and the archive requires one.
584        Phase::Archive | Phase::Link => "",
585    }
586}
587
588/// The default name of the linked output, which is GCC's `a.out` everywhere but Windows.
589fn default_exe(opts: &Options) -> &'static str {
590    if opts.target.os == Os::Windows { "a.exe" } else { "a.out" }
591}
592
593impl Plan {
594    /// Builds the plan for one invocation.
595    ///
596    /// `output` is the argument of `-o`, if it was given.
597    ///
598    /// # Errors
599    ///
600    /// Returns a message when the inputs and the mode flags do not describe a compilation:
601    /// an out of scope language, `-o` naming one file for several outputs, or nothing to do.
602    pub fn new(opts: &Options, inputs: &[Input], output: Option<&str>) -> Result<Plan, PlanError> {
603        if inputs.is_empty() {
604            return Err(plan_err("no input files"));
605        }
606        let last = last_phase(opts.emit);
607        let linking = last == Phase::Link;
608        let archiving = last == Phase::Archive;
609        // An archive has no default name. `a.out` is a convention old enough that a build which
610        // gets one knows what happened, and a `libwhatever.a` this made up would be a file the
611        // build then fails to find under the name it asked for.
612        if archiving && output.is_none() {
613            return Err(plan_err("an archive has no default name, so `--emit=archive` needs `-o`"));
614        }
615        // One output for however many inputs, which both of the collecting steps are. What it
616        // decides below is that no input names the output and that the objects are temporary.
617        let collecting = linking || archiving;
618
619        let mut kinds = Vec::with_capacity(inputs.len());
620        for input in inputs {
621            // Standard input has no extension to read a language out of, so either `-x` said what
622            // it is or `-E` did, and gcc refuses the rest rather than guessing. The refusal is
623            // worth keeping: `rucc -` on a terminal with nothing else on the line would otherwise
624            // sit there waiting for a program to be typed at it.
625            if input.role == Role::File && input.path == STDIN && input.forced.is_none() {
626                if opts.emit != EmitKind::Preprocessed {
627                    return Err(plan_err("-E or -x required when input is from standard input"));
628                }
629                kinds.push(InputKind::C);
630                continue;
631            }
632            kinds.push(input.kind().map_err(|e| plan_err(format!("{}: {e}", input.path)))?);
633        }
634
635        // How many inputs actually write an output of their own. A `.o` on a `-c` line
636        // produces nothing, and neither does a `.s` on an `-E` line, so neither may count
637        // toward the `-o` check below. When linking there is exactly one output and it is the
638        // executable, so nothing counts.
639        let producing = if collecting {
640            0
641        } else {
642            kinds
643                .iter()
644                .filter(|k| **k != InputKind::LinkerInput)
645                .filter(|k| k.full_sequence().iter().any(|p| *p <= last))
646                .count()
647        };
648        if output.is_some() && !collecting && producing > 1 {
649            return Err(plan_err("cannot specify -o with multiple inputs when not linking"));
650        }
651
652        // Whether there is one input, which decides whether the files kept beside a link's output
653        // need the input's name on them to tell them apart. A library or a word for the linker is
654        // not an input here and an object is, which is how gcc counts.
655        let alone = inputs.iter().filter(|input| input.role == Role::File).count() == 1;
656
657        let mut notes = Vec::new();
658        let mut jobs = Vec::with_capacity(inputs.len());
659        let mut link_inputs = Vec::new();
660        let mut members = Vec::new();
661
662        for (input, kind) in inputs.iter().zip(kinds) {
663            // A word the user handed the linker is not a file and nothing here does anything to it,
664            // so it is neither a job nor something an archive could hold. It is in this list rather
665            // than beside the other link flags so that it reaches the linker among the files it was
666            // written among, which is the note on [`Role`]. On a command line that does not link it
667            // is dropped without a word, which is what GCC does with one.
668            if input.role == Role::Linker {
669                if linking {
670                    link_inputs.push(Item::Linker(input.path.clone()));
671                }
672                continue;
673            }
674
675            // An object, an archive or a shared library has nothing done to it. It reaches the
676            // linker under the name it was written with, and its name is not derived from
677            // anything, which is why this case is separate rather than falling out of the
678            // sequence below. Deriving it would rewrite `libm.a` into `libm.o`.
679            if kind == InputKind::LinkerInput {
680                // An object, an archive or a library on an archive command line is refused rather
681                // than noted and dropped. The symbol index is why: it needs the names each member
682                // defines, and this compiler knows those for a file it compiled and not for one it
683                // was handed, since nothing here reads an object file back. Carrying on and
684                // leaving the file out would be an archive that is quietly missing half of what
685                // was asked for, which a link finds out about much later.
686                if archiving {
687                    let named = input.named();
688                    return Err(plan_err(format!(
689                        "{named}: an archive is written from the objects this command line \
690                         compiles, and the symbol index in it needs the names each member \
691                         defines, which this compiler knows for a file it compiled and not for \
692                         one it was handed"
693                    )));
694                }
695                if linking {
696                    link_inputs.push(if input.role == Role::Library {
697                        Item::Library(input.path.clone())
698                    } else {
699                        Item::File(input.path.clone())
700                    });
701                } else {
702                    // GCC warns and carries on here, and configure scripts rely on that, so
703                    // this is a note rather than an error.
704                    notes.push(format!(
705                        "{}: linker input unused because linking was not requested",
706                        input.named()
707                    ));
708                }
709                // A library is not a file this compilation does anything to, so it gets no job.
710                // One would print a line under `-###` saying nothing happens to it, next to the
711                // note above already saying so.
712                if input.role == Role::Library {
713                    continue;
714                }
715                jobs.push(Job {
716                    input: input.path.clone(),
717                    kind,
718                    phases: Vec::new(),
719                    output: Output::File(input.path.clone()),
720                    aux_base: None,
721                    stack_usage: None,
722                });
723                continue;
724            }
725
726            let phases: Vec<Phase> =
727                kind.full_sequence().iter().copied().filter(|p| *p <= last).collect();
728            // `rucc -E a.s` lands here: assembly enters at `Assemble`, which is past where
729            // `-E` stops, so there is no phase left to run. GCC carries on rather than
730            // failing, and so do we.
731            let Some(&final_phase) = phases.last() else {
732                notes.push(format!(
733                    "{}: input unused because it enters the pipeline after the last phase \
734                     the mode flags asked for",
735                    input.path
736                ));
737                jobs.push(Job {
738                    input: input.path.clone(),
739                    kind,
740                    phases,
741                    output: Output::File(input.path.clone()),
742                    aux_base: None,
743                    stack_usage: None,
744                });
745                continue;
746            };
747            let named = if producing == 1 { output } else { None };
748            // A job that stops at the preprocessed text has nothing to keep, since that text is
749            // what it writes. Everything past it does: the text and, once there is a back end
750            // step after it, the assembly.
751            let aux = (opts.save_temps.wanted() && final_phase > Phase::Preprocess)
752                .then(|| aux_base(opts, &input.path, output, collecting, alone));
753            let out = if final_phase == Phase::Link || archiving {
754                // The job stops at the object, and the step below takes it from here. Under
755                // `-save-temps` the object is one of the files being kept, so it is written where
756                // the person can see it rather than in a directory that goes away.
757                let ext = suffix_for(Phase::Assemble, opts);
758                match &aux {
759                    Some(base) => Output::File(format!("{base}.{ext}")),
760                    None => Output::Temporary(format!("{}.{ext}", stem(&input.path))),
761                }
762            } else if opts.emit == EmitKind::SyntaxOnly {
763                // Nothing is produced, so nothing is written, and an `-o` is ignored the way gcc
764                // ignores it. Standard output takes the empty artifact without leaving a file.
765                Output::Stdout
766            } else if let Some(o) = named {
767                // `-o -` is standard output rather than a file of that name, which is what gcc
768                // does for everything it compiles, the object file included. Its link step is
769                // the exception and writes a file called `-`, because the name goes to the
770                // linker and the linker takes it literally.
771                if o == "-" { Output::Stdout } else { Output::File(o.to_owned()) }
772            } else if final_phase == Phase::Preprocess {
773                // `-E` writes to standard output unless it was given a name, which is the one
774                // place where the default is not a file.
775                Output::Stdout
776            } else {
777                Output::File(format!("{}.{}", stem(&input.path), suffix_for(final_phase, opts)))
778            };
779            // An input whose output has the name it has itself would be read and then written
780            // over, and what it held would be gone. GCC compares the two names the way they
781            // were written and so does this, which catches `rucc --emit=ir a.ir` and leaves
782            // the same file reached by two different paths to the file system. `/dev/null` is
783            // left out, as gcc leaves out its bit bucket: nothing is lost by writing to it, and
784            // kbuild's cc-option asks whether a flag is taken by compiling `-xc /dev/null` with
785            // `-o /dev/null`, so refusing it refused every flag busybox probed for.
786            if let Output::File(path) = &out {
787                if *path == input.path && path != "/dev/null" {
788                    return Err(plan_err(format!(
789                        "input file `{}` is the same as the output file",
790                        input.path
791                    )));
792                }
793            }
794
795            if linking {
796                if let Some(p) = out.as_link_input() {
797                    link_inputs.push(Item::File(p.to_owned()));
798                }
799            }
800            if archiving {
801                // The name inside the archive rather than the path the object is written to, which
802                // under `-save-temps` is a path with a directory on it and otherwise is a
803                // temporary. What `ar t` shows is a file name, so that is what goes in.
804                members.push(format!(
805                    "{}.{}",
806                    stem(&input.path),
807                    suffix_for(Phase::Assemble, opts)
808                ));
809            }
810            // Only for a job that compiles C, since the numbers are the frames the back end laid
811            // out. An input of assembly has no compile phase and an input of IR is only ever
812            // printed back, so neither has a frame to report, and gcc writes nothing under `-E`.
813            // `-fsyntax-only` gets a file with nothing in it, which is what gcc gives it, named
814            // the way a link's would be since nothing on the command line said to stop at an
815            // object.
816            let syntax = opts.emit == EmitKind::SyntaxOnly;
817            let backed = syntax
818                || matches!(
819                    opts.emit,
820                    EmitKind::Asm | EmitKind::Object | EmitKind::Archive | EmitKind::Executable
821                );
822            let stack_usage = (opts.stack_usage
823                && backed
824                && kind != InputKind::Ir
825                && phases.contains(&Phase::Compile))
826            .then(|| {
827                let named = output.filter(|o| *o != "-");
828                format!("{}.su", aux_base(opts, &input.path, named, collecting || syntax, alone))
829            });
830            jobs.push(Job {
831                input: input.path.clone(),
832                kind,
833                phases,
834                output: out,
835                aux_base: aux,
836                stack_usage,
837            });
838        }
839
840        let link = linking.then(|| LinkJob {
841            inputs: link_inputs,
842            output: output.unwrap_or(default_exe(opts)).to_owned(),
843        });
844        let archive = archiving.then(|| ArchiveJob {
845            members,
846            // Checked at the top, so there is an `-o` here. Saying so is cheaper than an unwrap
847            // that would have to be explained.
848            output: output.unwrap_or_default().to_owned(),
849        });
850
851        Ok(Plan { jobs, link, archive, notes, output: output.map(str::to_owned) })
852    }
853
854    /// Renders the plan the way `-###` prints it.
855    ///
856    /// One line per job, then the link line. This is meant to be read next to `gcc -###`
857    /// output when a build behaves differently under the two compilers, so it says what will
858    /// happen rather than how it is represented.
859    #[must_use]
860    pub fn render(&self) -> String {
861        let mut out = String::new();
862        for note in &self.notes {
863            let _ = writeln!(out, "note: {note}");
864        }
865        for job in &self.jobs {
866            // A linker input has no phases of its own. It shows up in the link line below, or
867            // in a note above when there is no link line, and repeating it here would suggest
868            // something happens to it.
869            if job.phases.is_empty() {
870                continue;
871            }
872            let names: Vec<&str> = job.phases.iter().map(|p| p.as_str()).collect();
873            let _ = writeln!(out, "{}: {} -> {}", job.input, names.join(", "), job.output.render());
874            // The files `-save-temps` keeps, which are as much a part of what will happen as the
875            // output is and are the only reason the flag was passed.
876            let kept: Vec<String> =
877                [job.saved_text(), job.saved_asm()].into_iter().flatten().collect();
878            if !kept.is_empty() {
879                let _ = writeln!(out, "{}: keeping {}", job.input, kept.join(", "));
880            }
881        }
882        if let Some(link) = &self.link {
883            let names: Vec<String> = link.inputs.iter().map(ToString::to_string).collect();
884            let _ = writeln!(out, "link: {} -> {}", names.join(" "), link.output);
885        }
886        if let Some(archive) = &self.archive {
887            let _ = writeln!(out, "archive: {} -> {}", archive.members.join(" "), archive.output);
888        }
889        out
890    }
891}
892
893#[cfg(test)]
894mod tests {
895    use rucc_session::Options;
896
897    use super::*;
898
899    fn opts(triple: &str) -> Options {
900        Options::new(triple.parse().expect("test triple"))
901    }
902
903    fn linux() -> Options {
904        opts("x86_64-unknown-linux-gnu")
905    }
906
907    fn plan(o: &Options, paths: &[&str], output: Option<&str>) -> Plan {
908        let inputs: Vec<Input> = paths.iter().map(|p| Input::new(*p)).collect();
909        Plan::new(o, &inputs, output).expect("expected a plan")
910    }
911
912    #[test]
913    fn extensions_map_to_the_table_in_the_spec() {
914        assert_eq!(InputKind::from_path("a.c").unwrap(), InputKind::C);
915        assert_eq!(InputKind::from_path("a.i").unwrap(), InputKind::PreprocessedC);
916        assert_eq!(InputKind::from_path("a.ir").unwrap(), InputKind::Ir);
917        assert_eq!(InputKind::from_path("a.h").unwrap(), InputKind::CHeader);
918        assert_eq!(InputKind::from_path("a.s").unwrap(), InputKind::Assembler);
919        assert_eq!(InputKind::from_path("a.S").unwrap(), InputKind::AssemblerWithCpp);
920        assert_eq!(InputKind::from_path("a.sx").unwrap(), InputKind::AssemblerWithCpp);
921        assert_eq!(InputKind::from_path("a.o").unwrap(), InputKind::LinkerInput);
922        assert_eq!(InputKind::from_path("libm.a").unwrap(), InputKind::LinkerInput);
923        assert_eq!(InputKind::from_path("libm.so.6").unwrap(), InputKind::LinkerInput);
924    }
925
926    #[test]
927    fn standard_input_is_c_under_dash_e_and_wants_telling_otherwise() {
928        // `$(CPP) $(CFLAGS) -` is how a generated header gets preprocessed in a Makefile, and
929        // there is no extension on `-` to read a language out of. Under `-E` gcc takes it as C
930        // and otherwise it refuses, which is the difference between a build that works and one
931        // that sits waiting for a program to be typed at a terminal.
932        let mut o = linux();
933        o.emit = EmitKind::Preprocessed;
934        let plan = plan(&o, &["-"], None);
935        assert_eq!(plan.jobs.len(), 1);
936        assert_eq!(plan.jobs[0].kind, InputKind::C);
937        assert_eq!(plan.jobs[0].phases, [Phase::Preprocess]);
938        assert_eq!(plan.jobs[0].output, Output::Stdout);
939
940        let o = linux();
941        let error = Plan::new(&o, &[Input::new("-")], None).expect_err("expected this refused");
942        assert_eq!(error.message, "-E or -x required when input is from standard input");
943
944        // `-x` is the other way of saying it, and then the whole pipeline runs and the object
945        // is named after the dash, which is the name gcc derives too.
946        let mut o = linux();
947        o.emit = EmitKind::Object;
948        let inputs = [Input { path: "-".to_owned(), forced: Some(InputKind::C), role: Role::File }];
949        let plan = Plan::new(&o, &inputs, None).expect("expected a plan");
950        assert_eq!(plan.jobs[0].kind, InputKind::C);
951        assert_eq!(plan.jobs[0].output, Output::File("-.o".to_owned()));
952    }
953
954    #[test]
955    fn standard_input_is_named_the_way_gcc_names_it_and_a_file_is_named_after_itself() {
956        assert_eq!(source_name("-"), "<stdin>");
957        assert_eq!(source_name("a.c"), "a.c");
958        assert_eq!(source_name("sub/-"), "sub/-");
959    }
960
961    #[test]
962    fn ir_enters_where_preprocessed_c_does_and_needs_no_preprocessor() {
963        // It is the compiler's own output coming back in, so the phases in front of the walk
964        // have already happened to it and the ones after it are the ones still to run.
965        assert_eq!(InputKind::from_x_arg("ir").unwrap(), InputKind::Ir);
966        assert_eq!(InputKind::Ir.as_str(), "ir");
967        assert_eq!(InputKind::Ir.full_sequence(), InputKind::PreprocessedC.full_sequence());
968        assert!(!InputKind::Ir.full_sequence().contains(&Phase::Preprocess));
969    }
970
971    #[test]
972    fn an_input_whose_output_has_its_own_name_is_refused_rather_than_written_over() {
973        // `rucc --emit=ir a.ir` would read the file and then write the result over it, and
974        // what it held would be gone.
975        let mut o = linux();
976        o.emit = EmitKind::Ir;
977        let inputs = [Input::new("a.ir")];
978        let error = Plan::new(&o, &inputs, None).expect_err("expected this to be refused");
979        assert!(error.message.contains("is the same as the output file"), "{error}");
980        // Naming it something else is fine, and so is the same name reached through `-o`
981        // being refused for the same reason.
982        assert!(Plan::new(&o, &inputs, Some("b.ir")).is_ok());
983        assert!(Plan::new(&o, &inputs, Some("a.ir")).is_err());
984        // The bit bucket is not a file anything is kept in, which is how kbuild asks whether a
985        // flag is taken.
986        let null = Input { forced: Some(InputKind::C), ..Input::new("/dev/null") };
987        assert!(Plan::new(&linux(), &[null], Some("/dev/null")).is_ok());
988    }
989
990    #[test]
991    fn capital_s_and_small_s_are_different_languages() {
992        // On a case-insensitive file system it is tempting to fold these together. They are
993        // not the same: one runs the preprocessor and one does not.
994        let hi = InputKind::from_path("a.S").unwrap();
995        let lo = InputKind::from_path("a.s").unwrap();
996        assert_ne!(hi, lo);
997        assert!(hi.full_sequence().contains(&Phase::Preprocess));
998        assert!(!lo.full_sequence().contains(&Phase::Preprocess));
999    }
1000
1001    #[test]
1002    fn a_cplusplus_source_says_why_rather_than_failing_at_link_time() {
1003        let e = InputKind::from_path("a.cpp").unwrap_err();
1004        assert!(format!("{e}").contains("only ever going to compile C"), "{e}");
1005        let e = InputKind::from_x_arg("c++").unwrap_err();
1006        assert!(matches!(e, XError::Unsupported(_)), "{e:?}");
1007    }
1008
1009    #[test]
1010    fn a_file_with_no_extension_goes_to_the_linker() {
1011        assert_eq!(InputKind::from_path("crt1").unwrap(), InputKind::LinkerInput);
1012        assert_eq!(InputKind::from_path(".bashrc").unwrap(), InputKind::LinkerInput);
1013    }
1014
1015    #[test]
1016    fn the_default_line_compiles_and_links_to_a_out() {
1017        let p = plan(&linux(), &["a.c"], None);
1018        assert_eq!(
1019            p.jobs[0].phases,
1020            vec![Phase::Preprocess, Phase::Compile, Phase::Assemble, Phase::Link]
1021        );
1022        assert_eq!(p.jobs[0].output, Output::Temporary("a.o".into()));
1023        let link = p.link.expect("expected a link step");
1024        assert_eq!(link.inputs, vec![Item::File("a.o".into())]);
1025        assert_eq!(link.output, "a.out");
1026    }
1027
1028    #[test]
1029    fn dash_c_stops_at_the_object_and_names_it_after_the_source() {
1030        let mut o = linux();
1031        o.emit = EmitKind::Object;
1032        let p = plan(&o, &["src/a.c", "src/b.c"], None);
1033        assert!(p.link.is_none());
1034        assert_eq!(p.jobs[0].output, Output::File("a.o".into()));
1035        assert_eq!(p.jobs[1].output, Output::File("b.o".into()));
1036        // Next to the source is what people expect and it is not what GCC does. The object
1037        // lands in the current directory.
1038        assert_eq!(p.jobs[0].phases.last(), Some(&Phase::Assemble));
1039    }
1040
1041    #[test]
1042    fn dash_e_writes_to_stdout_unless_it_is_given_a_name() {
1043        let mut o = linux();
1044        o.emit = EmitKind::Preprocessed;
1045        assert_eq!(plan(&o, &["a.c"], None).jobs[0].output, Output::Stdout);
1046        assert_eq!(plan(&o, &["a.c"], Some("a.i")).jobs[0].output, Output::File("a.i".into()));
1047    }
1048
1049    #[test]
1050    fn a_name_of_one_dash_is_standard_output_and_not_a_file_called_that() {
1051        let mut o = linux();
1052        o.emit = EmitKind::Preprocessed;
1053        assert_eq!(plan(&o, &["a.c"], Some("-")).jobs[0].output, Output::Stdout);
1054        o.emit = EmitKind::Object;
1055        assert_eq!(plan(&o, &["a.c"], Some("-")).jobs[0].output, Output::Stdout);
1056        // The linker is handed the name and makes a file of it, which is gcc's behaviour and
1057        // is the one place the dash is not standard output.
1058        let p = plan(&linux(), &["a.c"], Some("-"));
1059        assert_eq!(p.link.expect("a link step").output, "-");
1060    }
1061
1062    #[test]
1063    fn dash_s_produces_assembly_named_after_the_source() {
1064        let mut o = linux();
1065        o.emit = EmitKind::Asm;
1066        let p = plan(&o, &["dir/a.c"], None);
1067        assert_eq!(p.jobs[0].output, Output::File("a.s".into()));
1068        assert_eq!(p.jobs[0].phases, vec![Phase::Preprocess, Phase::Compile]);
1069    }
1070
1071    #[test]
1072    fn an_already_preprocessed_file_skips_the_preprocessor() {
1073        let p = plan(&linux(), &["a.i"], None);
1074        assert_eq!(p.jobs[0].phases, vec![Phase::Compile, Phase::Assemble, Phase::Link]);
1075    }
1076
1077    #[test]
1078    fn assembly_with_a_capital_s_is_preprocessed_but_not_compiled() {
1079        let p = plan(&linux(), &["a.S"], None);
1080        assert_eq!(p.jobs[0].phases, vec![Phase::Preprocess, Phase::Assemble, Phase::Link]);
1081        assert!(!p.jobs[0].phases.contains(&Phase::Compile));
1082    }
1083
1084    #[test]
1085    fn objects_on_the_line_reach_the_linker_in_the_order_they_were_written() {
1086        // Link order is semantic. A plan that reorders it is a plan that produces a different
1087        // program, and the failure would be a missing symbol nobody could explain.
1088        let p = plan(&linux(), &["a.o", "b.c", "libm.a"], None);
1089        let link = p.link.expect("expected a link step");
1090        assert_eq!(
1091            link.inputs,
1092            vec![Item::File("a.o".into()), Item::File("b.o".into()), Item::File("libm.a".into()),]
1093        );
1094    }
1095
1096    #[test]
1097    fn an_object_on_a_dash_c_line_is_a_note_rather_than_an_error() {
1098        // Configure scripts do this. Erroring here fails builds that work under GCC.
1099        let mut o = linux();
1100        o.emit = EmitKind::Object;
1101        let p = plan(&o, &["a.c", "b.o"], None);
1102        assert!(p.jobs[1].phases.is_empty());
1103        assert_eq!(p.notes.len(), 1);
1104        assert!(p.notes[0].contains("linker input unused"), "{:?}", p.notes);
1105    }
1106
1107    #[test]
1108    fn dash_o_with_several_compilations_is_rejected() {
1109        let mut o = linux();
1110        o.emit = EmitKind::Object;
1111        let inputs = [Input::new("a.c"), Input::new("b.c")];
1112        let e = Plan::new(&o, &inputs, Some("out.o")).unwrap_err();
1113        assert!(e.message.contains("multiple inputs"), "{}", e.message);
1114    }
1115
1116    #[test]
1117    fn dash_o_with_one_compilation_and_some_objects_is_fine() {
1118        // `rucc -c -o out.o a.c b.o` has exactly one thing to write, so the check above must
1119        // not count the object.
1120        let mut o = linux();
1121        o.emit = EmitKind::Object;
1122        let inputs = [Input::new("a.c"), Input::new("b.o")];
1123        let p = Plan::new(&o, &inputs, Some("out.o")).expect("expected a plan");
1124        assert_eq!(p.jobs[0].output, Output::File("out.o".into()));
1125    }
1126
1127    #[test]
1128    fn dash_x_overrides_the_extension() {
1129        let inputs = [Input { path: "a.txt".into(), forced: Some(InputKind::C), role: Role::File }];
1130        let p = Plan::new(&linux(), &inputs, None).expect("expected a plan");
1131        assert_eq!(p.jobs[0].kind, InputKind::C);
1132        assert_eq!(p.jobs[0].phases.first(), Some(&Phase::Preprocess));
1133    }
1134
1135    #[test]
1136    fn windows_gets_obj_and_a_exe() {
1137        let o = opts("x86_64-pc-windows-msvc");
1138        let p = plan(&o, &["a.c"], None);
1139        assert_eq!(p.jobs[0].output, Output::Temporary("a.obj".into()));
1140        assert_eq!(p.link.expect("expected a link step").output, "a.exe");
1141    }
1142
1143    /// The archive is the link's shape rather than `-c`'s: one file out of however many inputs.
1144    #[test]
1145    fn an_archive_is_one_file_however_many_inputs_there_are() {
1146        let mut o = linux();
1147        o.emit = EmitKind::Archive;
1148        let p = plan(&o, &["a.c", "sub/b.c"], Some("out/libx.a"));
1149        assert_eq!(p.jobs.len(), 2);
1150        for job in &p.jobs {
1151            // Each input stops at its object, which is where `-c` stops, and the object is a file
1152            // nobody asked for and nobody sees: what was asked for is the archive.
1153            assert_eq!(job.phases.last(), Some(&Phase::Assemble), "{}", job.input);
1154            assert!(matches!(job.output, Output::Temporary(_)), "{:?}", job.output);
1155        }
1156        let archive = p.archive.expect("an archive step");
1157        assert_eq!(archive.members, ["a.o", "b.o"]);
1158        assert_eq!(archive.output, "out/libx.a");
1159        assert!(p.link.is_none(), "one command line produces one of the two and not both");
1160    }
1161
1162    #[test]
1163    fn a_member_is_called_what_an_object_is_called_on_this_target() {
1164        let mut o = opts("x86_64-pc-windows-msvc");
1165        o.emit = EmitKind::Archive;
1166        let p = plan(&o, &["a.c"], Some("x.lib"));
1167        assert_eq!(p.archive.expect("an archive step").members, ["a.obj"]);
1168    }
1169
1170    /// `a.out` is a convention old enough that a build which gets one knows what happened. A
1171    /// `libsomething.a` invented here would be a file the build then fails to find.
1172    #[test]
1173    fn an_archive_has_no_default_name() {
1174        let mut o = linux();
1175        o.emit = EmitKind::Archive;
1176        let inputs = [Input::new("a.c")];
1177        let error = Plan::new(&o, &inputs, None).expect_err("no name for the archive");
1178        assert!(error.message.contains("needs `-o`"), "{error}");
1179    }
1180
1181    /// An `ar` would take these. This cannot, and says so rather than writing an archive with
1182    /// half of what was asked for in it.
1183    #[test]
1184    fn something_this_compilation_did_not_produce_cannot_go_into_an_archive() {
1185        let mut o = linux();
1186        o.emit = EmitKind::Archive;
1187        for handed in [Input::new("b.o"), Input::library("m")] {
1188            let inputs = [Input::new("a.c"), handed];
1189            let error = Plan::new(&o, &inputs, Some("libx.a")).expect_err("not ours to index");
1190            assert!(error.message.contains("names each member"), "{error}");
1191        }
1192    }
1193
1194    #[test]
1195    fn the_plan_says_what_goes_into_the_archive() {
1196        let mut o = linux();
1197        o.emit = EmitKind::Archive;
1198        let text = plan(&o, &["a.c", "b.c"], Some("libx.a")).render();
1199        assert!(text.contains("archive: a.o b.o -> libx.a"), "{text}");
1200        assert!(!text.contains("link:"), "{text}");
1201    }
1202
1203    #[test]
1204    fn the_intermediate_dumps_stop_where_dash_s_stops() {
1205        for emit in [
1206            EmitKind::Tast,
1207            EmitKind::Ir,
1208            EmitKind::MirFinal,
1209            EmitKind::SafetySummary,
1210            EmitKind::TypeGranules,
1211        ] {
1212            assert_eq!(last_phase(emit), Phase::Compile, "{emit:?}");
1213        }
1214    }
1215
1216    #[test]
1217    fn each_intermediate_dump_is_a_language_of_its_own_and_gets_a_suffix_of_its_own() {
1218        // They all come out of the compile phase and none of them is assembly, so writing any
1219        // of them to `a.s` would leave a file that neither an assembler nor a reader can use.
1220        for (emit, name) in [
1221            (EmitKind::Asm, "a.s"),
1222            (EmitKind::Tast, "a.tast"),
1223            (EmitKind::Ir, "a.ir"),
1224            (EmitKind::MirFinal, "a.mir"),
1225            (EmitKind::SafetySummary, "a.safety.json"),
1226            (EmitKind::TypeGranules, "a.granules.txt"),
1227        ] {
1228            let mut o = linux();
1229            o.emit = emit;
1230            assert_eq!(plan(&o, &["a.c"], None).jobs[0].output, Output::File(name.into()));
1231        }
1232    }
1233
1234    #[test]
1235    fn syntax_only_writes_no_file_even_when_given_a_name() {
1236        let mut o = linux();
1237        o.emit = EmitKind::SyntaxOnly;
1238        assert_eq!(last_phase(o.emit), Phase::Compile);
1239        let p = plan(&o, &["a.c", "b.c"], None);
1240        assert!(p.jobs.iter().all(|job| job.output == Output::Stdout), "{:?}", p.jobs);
1241        assert_eq!(plan(&o, &["a.c"], Some("a.o")).jobs[0].output, Output::Stdout);
1242        assert!(!plan(&o, &["a.c"], None).render().contains("link:"));
1243    }
1244
1245    #[test]
1246    fn an_input_that_enters_after_the_last_phase_is_a_note_rather_than_an_error() {
1247        // `rucc -E a.s` has nothing to preprocess. GCC carries on, and a configure script
1248        // that probes with a mixed input list depends on that.
1249        let mut o = linux();
1250        o.emit = EmitKind::Preprocessed;
1251        let p = plan(&o, &["a.c", "b.s"], None);
1252        assert!(p.jobs[1].phases.is_empty());
1253        assert_eq!(p.notes.len(), 1);
1254        assert!(p.notes[0].contains("after the last phase"), "{:?}", p.notes);
1255        // And it must not count against `-o`, because only one file is being written.
1256        let inputs = [Input::new("a.c"), Input::new("b.s")];
1257        assert!(Plan::new(&o, &inputs, Some("out.i")).is_ok());
1258    }
1259
1260    #[test]
1261    fn no_inputs_is_an_error() {
1262        assert!(Plan::new(&linux(), &[], None).is_err());
1263    }
1264
1265    /// The plan for `paths` under `-save-temps` in the spelling `kind`.
1266    fn keeping(kind: SaveTemps, emit: EmitKind, paths: &[&str], output: Option<&str>) -> Plan {
1267        let mut o = linux();
1268        o.emit = emit;
1269        o.save_temps = kind;
1270        plan(&o, paths, output)
1271    }
1272
1273    /// What one job of that plan keeps, in the order the files are produced.
1274    fn kept(plan: &Plan, at: usize) -> Vec<String> {
1275        [plan.jobs[at].saved_text(), plan.jobs[at].saved_asm()].into_iter().flatten().collect()
1276    }
1277
1278    #[test]
1279    fn the_files_that_are_kept_land_beside_the_output_and_not_where_the_manual_says() {
1280        // gcc 16's bare `-save-temps` is `-save-temps=obj`, whatever its manual says, so
1281        // `-o out/t.o` puts them in `out` and not in the working directory. Both spellings are
1282        // here because the whole of the difference between them is the directory.
1283        let p = keeping(SaveTemps::Object, EmitKind::Object, &["t.c"], Some("out/t.o"));
1284        assert_eq!(kept(&p, 0), vec!["out/t.i", "out/t.s"]);
1285        let p = keeping(SaveTemps::Cwd, EmitKind::Object, &["t.c"], Some("out/t.o"));
1286        assert_eq!(kept(&p, 0), vec!["t.i", "t.s"]);
1287    }
1288
1289    #[test]
1290    fn the_name_comes_off_the_output_rather_than_off_the_input_that_produced_it() {
1291        // `-o out/x.o` keeps `x.i` and not `t.i`, and an output with no extension on it keeps
1292        // the whole of the name it was given.
1293        let p = keeping(SaveTemps::Cwd, EmitKind::Object, &["t.c"], Some("out/x.o"));
1294        assert_eq!(kept(&p, 0), vec!["x.i", "x.s"]);
1295        let p = keeping(SaveTemps::Object, EmitKind::Object, &["t.c"], Some("out/noext"));
1296        assert_eq!(kept(&p, 0), vec!["out/noext.i", "out/noext.s"]);
1297    }
1298
1299    #[test]
1300    fn without_a_name_they_are_called_after_the_input_and_are_where_the_object_would_be() {
1301        // The object of `sub/u.c` is `u.o` in the working directory, so what is kept beside it
1302        // is in the working directory as well, under both spellings.
1303        for kind in [SaveTemps::Object, SaveTemps::Cwd] {
1304            let p = keeping(kind, EmitKind::Object, &["sub/u.c"], None);
1305            assert_eq!(kept(&p, 0), vec!["u.i", "u.s"], "{kind:?}");
1306            assert_eq!(p.jobs[0].output, Output::File("u.o".into()), "{kind:?}");
1307        }
1308    }
1309
1310    #[test]
1311    fn a_command_line_that_links_names_them_after_the_executable_and_the_input() {
1312        // One output for however many inputs, so the input's own name goes on the end and two
1313        // files that would otherwise both be `prog.i` are two files.
1314        let p = keeping(SaveTemps::Object, EmitKind::Executable, &["t.c", "sub/u.c"], Some("o/p"));
1315        assert_eq!(kept(&p, 0), vec!["o/p-t.i", "o/p-t.s"]);
1316        assert_eq!(kept(&p, 1), vec!["o/p-u.i", "o/p-u.s"]);
1317        // With no `-o` the executable is `a.out`, and the `a` of it is what the files are named
1318        // from, which is where `a-t.i` comes from on a command line nobody wrote an `a` on. With
1319        // one input there is nothing to tell apart and gcc uses the input's name alone.
1320        let p = keeping(SaveTemps::Object, EmitKind::Executable, &["t.c", "u.c"], None);
1321        assert_eq!(kept(&p, 0), vec!["a-t.i", "a-t.s"]);
1322        let p = keeping(SaveTemps::Object, EmitKind::Executable, &["t.c"], None);
1323        assert_eq!(kept(&p, 0), vec!["t.i", "t.s"]);
1324        // The executable's name is not an object's, so it keeps its extension, all but `.exe`.
1325        let p = keeping(SaveTemps::Object, EmitKind::Executable, &["t.c"], Some("o/p.so"));
1326        assert_eq!(kept(&p, 0), vec!["o/p.so-t.i", "o/p.so-t.s"]);
1327        let p = keeping(SaveTemps::Object, EmitKind::Executable, &["t.c"], Some("o/p.exe"));
1328        assert_eq!(kept(&p, 0), vec!["o/p-t.i", "o/p-t.s"]);
1329    }
1330
1331    #[test]
1332    fn the_object_a_link_reads_is_kept_rather_than_written_where_it_will_be_removed() {
1333        // Without the flag it goes in a directory that is gone by the end of the run, and that
1334        // is the one thing `-save-temps` cannot leave true: the object is one of the files it
1335        // was asked to keep.
1336        let p = keeping(SaveTemps::Object, EmitKind::Executable, &["t.c"], Some("out/prog"));
1337        assert_eq!(p.jobs[0].output, Output::File("out/prog-t.o".into()));
1338        let plain = plan(&linux(), &["t.c"], Some("out/prog"));
1339        assert_eq!(plain.jobs[0].output, Output::Temporary("t.o".into()));
1340    }
1341
1342    #[test]
1343    fn a_step_whose_result_is_already_being_written_is_not_kept_a_second_time() {
1344        // `-E` writes the preprocessed text, so there is nothing left over to keep, and `-S`
1345        // writes the assembly and keeps only the text that came before it.
1346        let p = keeping(SaveTemps::Object, EmitKind::Preprocessed, &["t.c"], None);
1347        assert_eq!(p.jobs[0].aux_base, None);
1348        assert_eq!(kept(&p, 0), Vec::<String>::new());
1349        let p = keeping(SaveTemps::Object, EmitKind::Asm, &["t.c"], None);
1350        assert_eq!(kept(&p, 0), vec!["t.i"]);
1351    }
1352
1353    #[test]
1354    fn an_input_that_arrives_preprocessed_has_no_text_of_its_own_to_keep() {
1355        // There is no phase 4 to keep the result of, and the file the compilation read is the
1356        // one that would have been written, which is already on the disk under its own name.
1357        let p = keeping(SaveTemps::Object, EmitKind::Object, &["t.i"], None);
1358        assert_eq!(kept(&p, 0), vec!["t.s"]);
1359        // And an input the linker takes directly goes through no step at all.
1360        let p = keeping(SaveTemps::Object, EmitKind::Executable, &["t.o"], None);
1361        assert_eq!(p.jobs[0].aux_base, None);
1362    }
1363
1364    #[test]
1365    fn nothing_is_kept_when_the_flag_was_not_given() {
1366        let p = plan(&linux(), &["t.c"], None);
1367        assert_eq!(p.jobs[0].aux_base, None);
1368        assert_eq!(kept(&p, 0), Vec::<String>::new());
1369    }
1370
1371    #[test]
1372    fn the_rendering_says_what_will_happen() {
1373        let p = plan(&linux(), &["a.c", "b.o"], None);
1374        let text = p.render();
1375        assert!(
1376            text.contains("a.c: preprocess, compile, assemble, link -> a.o (temporary)"),
1377            "{text}"
1378        );
1379        assert!(text.contains("link: a.o b.o -> a.out"), "{text}");
1380        // The object has nothing done to it, so it appears once, in the link line.
1381        assert_eq!(text.matches("b.o").count(), 1, "{text}");
1382    }
1383
1384    #[test]
1385    fn the_rendering_names_the_files_that_will_be_kept() {
1386        // `-###` is what will happen, and under `-save-temps` two more files being written is
1387        // part of that. It is also the only way to see the names without running a compilation.
1388        let p = keeping(SaveTemps::Object, EmitKind::Executable, &["a.c", "b.c"], None);
1389        let text = p.render();
1390        assert!(text.contains("a.c: keeping a-a.i, a-a.s"), "{text}");
1391        assert!(!plan(&linux(), &["a.c"], None).render().contains("keeping"));
1392    }
1393}