Skip to main content

consortium_builder/
lib.rs

1// Copyright 2026 Ethan Wu
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17//! Compiles, post-processes, and stages every artifact a `Consortium.toml` describes.
18//!
19//! `consortium-cfg` answers "what does this system look like, and is it valid?" and emits
20//! generated source. This crate answers "now actually build it": four compilers, a
21//! post-processing step remoteproc requires, and a staging tree a packaging recipe can
22//! consume.
23//!
24//! It stops short of deployment. Nothing here writes to a live target filesystem — the
25//! output is a local `dist/` tree plus an optional BitBake hand-off.
26//!
27//! # Layout
28//!
29//! | Module         | Role                                                        |
30//! | -------------- | ----------------------------------------------------------- |
31//! | [`pipeline`]   | The end-to-end `Consortium.toml` → `dist/` flow, stage by stage. |
32//! | `compile`      | One compiler per artifact kind, behind the [`Compiler`] trait. |
33//! | [`progress`]   | Reporting hooks the CLI uses to keep a step prompt visible.    |
34//!
35//! # Fetch, then build offline
36//!
37//! [`pipeline::fetch`] resolves the same Cargo projects the build plan does and
38//! downloads their dependencies (plus each target's `std` and the frontend's
39//! packages) up front. A subsequent run with [`PipelineConfig::offline`] passes
40//! `--offline` to every cargo invocation, so the build reads only local caches —
41//! useful for air-gapped hosts, reproducible CI, and container images that should
42//! not re-resolve the registry on each build.
43//!
44//! # Check without building
45//!
46//! [`pipeline::check`] is the pipeline's `cargo check`: it validates the manifest,
47//! writes every endpoint's generated module, and runs `cargo check` in each Cargo
48//! project with the same targets and injected environment a build uses — no
49//! linking, objcopy, signing, HMI build, or staging.
50//!
51//! # The `Compiler` trait
52//!
53//! Firmware, the Linux application, the OP-TEE TA, and the HMI frontend are built by
54//! very different toolchains, but callers drive them identically: each implementor owns
55//! a `Configuration` carrying all its parameters, a typed `Error`, and an `Output`
56//! (usually the path to the built artifact). See [`Compiler`].
57//!
58//! # Plan, then run
59//!
60//! [`pipeline::plan`] resolves every stage's inputs — paths, env vars, artifact
61//! locations — **without running any build**, threading the data flow between stages.
62//! [`pipeline::run`] plans, then executes.
63//!
64//! That split is what makes the pipeline debuggable: a wrong sysroot or a missing
65//! firmware ELF surfaces as a planning failure naming the stage, rather than as a cargo
66//! error several minutes into a cross-compile.
67//!
68//! # Compile-time configuration
69//!
70//! Much of what generated code needs is passed as `CONSORTIUM_*` environment variables
71//! read with `option_env!`/`env!`, not as generated source. The carriers in
72//! `compile::env` describe what gets injected: [`SharedMemConfig`], [`DbgConfig`],
73//! [`HmiConfig`].
74//!
75//! Numeric values are emitted as **decimal strings**, so a firmware-side consumer that
76//! needs const parsing can use `const-str` without depending on the std-oriented schema
77//! crates. Note that there is deliberately no `CONSORTIUM_IPC_BASE`: controller firmware
78//! gets its region base baked into generated init code, and the Linux application reaches
79//! the same memory through its UIO device, so neither side needs the physical address
80//! from the environment.
81//!
82//! # Firmware post-processing
83//!
84//! Linux remoteproc expects a coprocessor's vector table in a section named
85//! `.isr_vectors`, but `cortex-m-rt` emits `.vector_table`. The pipeline renames it, and
86//! keeps the debug/original artifacts alongside the deployable ELF.
87//!
88//! # Staging
89//!
90//! The assembled tree can contain `app/`, `firmware/`, `ta/`, `www/`, `dts/linux/`,
91//! `kernel/`, `boot/`, and `systemd/`, plus a generated BitBake recipe. CLI flags can
92//! stage that tree into a recipe's `files/` directory and invoke or dry-run BitBake —
93//! the dry-run path exists for the split-machine flow, where `dist/` is produced on one
94//! host and the Yocto build runs on another.
95//!
96//! Keep the planning, compilation, staging, and external BitBake hand-off boundaries
97//! distinct when extending this crate; and prefer extending `consortium-cfg` lowering
98//! first, then the pipeline.
99
100mod compile;
101pub mod pipeline;
102pub mod progress;
103
104// Trait
105pub use compile::Compiler;
106pub use progress::{BuildReporter, BuildStep};
107
108// Deployment artifact rendering
109pub use compile::deploy::systemd::{
110    SystemdRestartPolicy, SystemdUnitConfig, render_unit as render_systemd_unit,
111    write_unit as write_systemd_unit,
112};
113
114// End-to-end pipeline
115pub use pipeline::{
116    CargoOptions, PipelineConfig, PipelineError, PipelinePlan, run as run_pipeline,
117};
118
119// Type-check without building
120pub use pipeline::check::{CheckConfig, CheckPlan, run as run_check};
121
122// Pre-build dependency download
123pub use pipeline::fetch::{FetchConfig, FetchPlan, run as run_fetch};
124
125// Compile-time environment-injection carriers
126pub use compile::env::{CortexMcuCore, CstiWorkspace, DbgConfig, HmiConfig, SharedMemConfig};
127
128// Backward-compatible builders (String-error API)
129pub use compile::application::ApplicationBuilder;
130pub use compile::firmware::FirmwareBuilder;
131pub use compile::optee::{OpteeBuilder, OpteeEnvironment};
132
133// Typed compiler API
134pub use compile::application::{
135    ApplicationCompileError, ApplicationCompiler, ApplicationConfiguration,
136};
137pub use compile::firmware::{FirmwareCompileError, FirmwareCompiler, FirmwareConfiguration};
138pub use compile::optee::{OPTEE_TARGET, OpteeCompileError, OpteeCompiler, OpteeConfiguration};
139pub use compile::web::{HmiBuildCommand, HmiCompileError, HmiCompiler, HmiConfiguration};