From 6e913b15d796dd512116a691b53ab46a584c6803 Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 3 Mar 2025 15:57:16 +0000 Subject: [PATCH] Update macro to use relative working dirs, Spans --- kernel/src/resources/font/mod.rs | 4 ++-- libm/src/lib.rs | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/kernel/src/resources/font/mod.rs b/kernel/src/resources/font/mod.rs index 0b27bc2..ebe52ed 100644 --- a/kernel/src/resources/font/mod.rs +++ b/kernel/src/resources/font/mod.rs @@ -1,9 +1,9 @@ use libm::include_font; pub static FONT_SPLEEN_8X16: Font = - Font::new(include_font!("./kernel/resources/font/spleen-8x16.psf")); + Font::new(include_font!("../../../resources/font/spleen-8x16.psf")); pub static FONT_CP850_8X16: Font = - Font::new(include_font!("./kernel/resources/font/cp850-8x16.psf")); + Font::new(include_font!("../../../resources/font/cp850-8x16.psf")); // pub struct Font(pub [[u8; 16]; 512]); diff --git a/libm/src/lib.rs b/libm/src/lib.rs index 376fb0b..0c9103b 100644 --- a/libm/src/lib.rs +++ b/libm/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(proc_macro_span)] #![warn( clippy::correctness, clippy::nursery, @@ -12,18 +13,32 @@ use std::fs::File; use std::io::{Read, Seek, SeekFrom}; -use proc_macro::TokenStream; +use proc_macro::{Span, TokenStream}; use quote::quote; +use std::path::PathBuf; use syn::{LitStr, parse_macro_input}; - extern crate proc_macro; #[proc_macro] +/// Expects the file path to be relative to the current file so this works +/// similarly to the standard Rust include! macros. pub fn include_font(item: TokenStream) -> TokenStream { - let filename = parse_macro_input!(item as LitStr); - let file_path = filename.value(); + let span = Span::call_site(); + let source_file = span.source_file(); - println!("Loading font: [{}]", file_path); + if !source_file.is_real() { + panic!( + "We can't handle finding files if the source file does not exist. TODO: Can we?" + ) + } + + let filename = parse_macro_input!(item as LitStr); + let source_filepath: PathBuf = source_file.path(); + let file_path = format!( + "{}/{}", + source_filepath.parent().unwrap_or_else(|| panic!("Expected to find the calling source file in a folder like src! Got: {}", source_filepath.display())).display(), + filename.value() + ); let font_bytes = match load_file(file_path) { Ok(bytes) => bytes,