git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/build.rs
//! Links with the selected board's linker script: each board loads the kernel
//! at its own address.
fn main() {
let board = match (
std::env::var_os("CARGO_FEATURE_BOARD_QEMU_VIRT").is_some(),
std::env::var_os("CARGO_FEATURE_BOARD_RK3399").is_some(),
std::env::var_os("CARGO_FEATURE_BOARD_PC").is_some(),
) {
(true, false, false) => "qemu-virt",
(false, true, false) => "rk3399",
(false, false, true) => "pc",
_ => panic!("select exactly one board feature"),
};
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-link-arg=-L{dir}/link");
println!("cargo:rustc-link-arg=-T{dir}/link/{board}.ld");
println!("cargo:rerun-if-changed=link");
// The programs, packed first by `cargo xtask`; a kernel built without
// them gets an empty archive and fails the self-check.
println!("cargo:rerun-if-env-changed=PINNACLE_PROGRAMS");
let program = std::env::var("PINNACLE_PROGRAMS").unwrap_or_else(|_| {
let empty = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("no-programs");
std::fs::write(&empty, []).unwrap();
empty.display().to_string()
});
println!("cargo:rustc-env=PINNACLE_PROGRAMS={program}");
console_font(&dir);
}
/// Traces an outline into a vector path.
struct Tracer(pinnacle_vector::Path);
impl pinnacle_ttf::Pen for Tracer {
fn move_to(&mut self, x: f32, y: f32) {
self.0.move_to(x, y);
}
fn line_to(&mut self, x: f32, y: f32) {
self.0.line_to(x, y);
}
fn quad_to(&mut self, cx: f32, cy: f32, x: f32, y: f32) {
self.0.quad_to(cx, cy, x, y);
}
fn close(&mut self) {
self.0.close();
}
}
/// The console's font (`drivers::screen_log`): printable ASCII from the
/// system's monospace face, at two sizes, each glyph a cell of coverage
/// (0–255) the size of the face's advance and line. Written to
/// `$OUT_DIR/console_font.rs`.
fn console_font(dir: &str) {
let path = format!("{dir}/../fonts/AdwaitaMono-Regular.ttf");
println!("cargo:rerun-if-changed={path}");
let data = std::fs::read(&path).unwrap();
let font = pinnacle_ttf::Font::parse(&data).expect("the monospace face does not parse");
let mut out = String::new();
for (name, px) in [("SMALL", 16.0f32), ("LARGE", 30.0)] {
let (ascent, descent, _) = font.line_metrics(px);
let width = font.glyph_advance(font.glyph_index('M'), px).round() as usize;
let height = (ascent - descent).ceil() as usize;
let baseline = ascent.round() as i32;
let mut cells = vec![0u8; 95 * width * height];
for (i, c) in (' '..='~').enumerate() {
let mut tracer = Tracer(pinnacle_vector::Path::new());
let m = font.outline(c, px, &mut tracer);
if m.width == 0 || m.height == 0 {
continue;
}
let mask = pinnacle_vector::mask(&tracer.0, m.width, m.height);
// The mask's top-left within the cell: its left edge from the
// pen, its top from the baseline (`ymin` is its bottom, y up).
let (left, top) = (m.xmin, baseline - (m.height as i32 + m.ymin));
let cell = &mut cells[i * width * height..(i + 1) * width * height];
for y in 0..m.height as i32 {
for x in 0..m.width as i32 {
let (cx, cy) = (left + x, top + y);
if (0..width as i32).contains(&cx) && (0..height as i32).contains(&cy) {
cell[cy as usize * width + cx as usize] = mask[y as usize * m.width + x as usize];
}
}
}
}
out += &format!(
"pub const {name}: Font = Font {{ width: {width}, height: {height}, cells: &{cells:?} }};\n"
);
}
let out_dir = std::env::var("OUT_DIR").unwrap();
std::fs::write(format!("{out_dir}/console_font.rs"), out).unwrap();
}