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//! Memory layout lives in generated source, not the environment: controller firmware gets
71//! its IPC region base and its `[dbg]` ring baked into generated `init()`, and the Linux
72//! application reaches the same memory through its UIO devices. What still travels as
73//! environment variables is what generated code cannot know: the on-target HMI asset
74//! paths ([`HmiConfig`], read with `option_env!`), the TEE UUID, and the `CSTI_*`
75//! build-host directories ([`CstiWorkspace`]). Numeric values are emitted as decimal
76//! strings.
77//!
78//! # Firmware post-processing
79//!
80//! Linux remoteproc expects a coprocessor's vector table in a section named
81//! `.isr_vectors`, but `cortex-m-rt` emits `.vector_table`. The pipeline renames it, and
82//! keeps the debug/original artifacts alongside the deployable ELF.
83//!
84//! # Staging
85//!
86//! The assembled tree can contain `app/`, `firmware/`, `ta/`, `www/`, `dts/linux/`,
87//! `kernel/`, `boot/`, and `systemd/`, plus a generated BitBake recipe. CLI flags can
88//! stage that tree into a recipe's `files/` directory and invoke or dry-run BitBake —
89//! the dry-run path exists for the split-machine flow, where `dist/` is produced on one
90//! host and the Yocto build runs on another.
91//!
92//! Keep the planning, compilation, staging, and external BitBake hand-off boundaries
93//! distinct when extending this crate; and prefer extending `consortium-cfg` lowering
94//! first, then the pipeline.
95
96mod compile;
97pub mod pipeline;
98pub mod progress;
99
100// Trait
101pub use compile::Compiler;
102pub use progress::{BuildReporter, BuildStep};
103
104// Deployment artifact rendering
105pub use compile::deploy::systemd::{
106    SystemdRestartPolicy, SystemdUnitConfig, render_unit as render_systemd_unit,
107    write_unit as write_systemd_unit,
108};
109
110// End-to-end pipeline
111pub use pipeline::{
112    CargoOptions, PipelineConfig, PipelineError, PipelinePlan, run as run_pipeline,
113};
114
115// Type-check without building
116pub use pipeline::check::{CheckConfig, CheckPlan, run as run_check};
117
118// Pre-build dependency download
119pub use pipeline::fetch::{FetchConfig, FetchPlan, run as run_fetch};
120
121// Compile-time environment-injection carriers
122pub use compile::env::{CortexMcuCore, CstiWorkspace, HmiConfig};
123
124// Backward-compatible builders (String-error API)
125pub use compile::application::ApplicationBuilder;
126pub use compile::firmware::FirmwareBuilder;
127pub use compile::optee::{OpteeBuilder, OpteeEnvironment};
128
129// Typed compiler API
130pub use compile::application::{
131    ApplicationCompileError, ApplicationCompiler, ApplicationConfiguration,
132};
133pub use compile::firmware::{FirmwareCompileError, FirmwareCompiler, FirmwareConfiguration};
134pub use compile::optee::{OPTEE_TARGET, OpteeCompileError, OpteeCompiler, OpteeConfiguration};
135pub use compile::web::{HmiBuildCommand, HmiCompileError, HmiCompiler, HmiConfiguration};