Skip to main content

cabin_build/
graph.rs

1use std::path::PathBuf;
2
3/// Backend-independent description of everything that needs to happen to
4/// realize a build. A backend (currently `cabin-ninja`) walks this graph and
5/// emits the equivalent build-system-specific representation.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct BuildGraph {
8    /// All actions to execute, in topological order. Earlier actions in the
9    /// vector never depend on later actions.
10    pub actions: Vec<Action>,
11    /// Output paths that should be marked as default targets.
12    pub default_outputs: Vec<PathBuf>,
13    /// One entry per C/C++ source compile, used to emit
14    /// `compile_commands.json`. Both languages contribute entries
15    /// with their language-appropriate compiler driver and flags
16    /// recorded in `arguments`.
17    pub compile_commands: Vec<CompileCommand>,
18}
19
20/// A single buildable step.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Action {
23    pub kind: ActionKind,
24    /// Inputs that participate in the command (e.g. `.cc` for compile,
25    /// `.o`/`.a` for link).
26    pub inputs: Vec<PathBuf>,
27    /// Inputs the action implicitly depends on but that are not arguments.
28    /// Used by C/C++ compiles whose source is generated by an
29    /// upstream action.
30    pub implicit_inputs: Vec<PathBuf>,
31    /// Files this action produces.
32    pub outputs: Vec<PathBuf>,
33    /// Optional Makefile-style depfile path; populated for C/C++
34    /// compiles so Ninja can wire `-MMD -MF $depfile` into its
35    /// `deps = gcc` machinery.
36    pub depfile: Option<PathBuf>,
37    /// Argv-style command, ready to be shell-quoted by the backend.
38    pub command: Vec<String>,
39    /// Short, human-readable description for build output (`CXX foo.o`).
40    pub description: String,
41}
42
43/// Categorization of a build action. Currently a closed set; new variants
44/// require explicit handling by every backend.
45#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum ActionKind {
47    /// Compile a single C translation unit (`.c`) to an object
48    /// file. Driven by the C compiler with `-std=c11` plus the
49    /// language-neutral profile flags.
50    CompileC,
51    /// Compile a single C++ translation unit (`.cc` / `.cpp` /
52    /// `.cxx` / `.c++` / `.C`) to an object file. Driven by the
53    /// C++ compiler with `-std=c++17` plus the language-neutral
54    /// profile flags.
55    CompileCpp,
56    /// Archive a set of object files into a static library.
57    ArchiveStaticLibrary,
58    /// Link object files plus static archives into an executable.
59    /// The link-driver language (C vs. C++) is encoded in the
60    /// action's `command` (the first argument is the chosen
61    /// compiler driver), so backends do not need to consult any
62    /// extra metadata to render the action.
63    LinkExecutable,
64}
65
66/// One entry of a Clang JSON Compilation Database.
67///
68/// `arguments` is kept as a list so each backend / consumer can render it
69/// however the format requires (LLVM accepts both `command` and
70/// `arguments` keys; `cabin-ninja` emits `command`).
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct CompileCommand {
73    pub directory: PathBuf,
74    pub file: PathBuf,
75    pub arguments: Vec<String>,
76    pub output: PathBuf,
77}