Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call include_file! on loaded pio file #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions pio-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ struct PioFileMacroArgs {
max_program_size: Expr,
program: String,
program_name: Option<(String, LitStr)>,
file_path: PathBuf,
}

impl syn::parse::Parse for PioFileMacroArgs {
fn parse(stream: syn::parse::ParseStream) -> syn::parse::Result<Self> {
let mut program = String::new();
let mut file_path = PathBuf::new();

// Parse the list of instructions
if let Ok(s) = stream.parse::<LitStr>() {
Expand All @@ -152,6 +154,8 @@ impl syn::parse::Parse for PioFileMacroArgs {
abort!(s, "the file '{}' does not exist", pathbuf.display());
}

file_path = pathbuf.to_owned();

match fs::read(pathbuf) {
Ok(content) => match std::str::from_utf8(&content) {
Ok(prog) => program = prog.to_string(),
Expand Down Expand Up @@ -203,6 +207,7 @@ impl syn::parse::Parse for PioFileMacroArgs {
program_name: select_program.map(|v| (v.name, v.ident)),
max_program_size,
program,
file_path,
})
}
}
Expand Down Expand Up @@ -279,7 +284,12 @@ pub fn pio_file(item: TokenStream) -> TokenStream {
Err(e) => return parse_error(e, &args.program).into(),
};

to_codegen(program, args.max_program_size).into()
to_codegen(
program,
args.max_program_size,
args.file_path.into_os_string().into_string().ok(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really ok() that we want? An error would be ignored and yield surprising results

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning for using ok() was that I thought the existing implementation of the pio_file macro would work with non-UTF-8 filenames. I didn't want to break that, but there's no way to pass a Path or OsString that's not also a valid String to include_bytes.

However I now tested this, and noticed that my assumption was wrong: pio_file only works if it gets passed a literal string, not (eg.) a byte-string (b"....").

BTW, the current behavior if the first argument to pio_file! is not a string literal is also quite surprising: The parameter is silently ignored, and an empty program is parsed.

Anyway, it currently doesn't matter if we call .ok() or .unwrap(), the code in question is only called with paths generated from valid strings.

Would you prefer this change to make the code more robust against future changes?

Suggested change
args.file_path.into_os_string().into_string().ok(),
Some(args.file_path.into_os_string().into_string().expect("file path must be valid UTF-8")),

)
.into()
}

/// A macro which invokes the PIO assembler at compile time.
Expand All @@ -295,12 +305,13 @@ pub fn pio_asm(item: TokenStream) -> TokenStream {
Err(e) => return parse_error(e, &args.program).into(),
};

to_codegen(program, args.max_program_size).into()
to_codegen(program, args.max_program_size, None).into()
}

fn to_codegen(
program: &pio::ProgramWithDefines<HashMap<String, i32>, { MAX_PROGRAM_SIZE }>,
max_program_size: Expr,
file: Option<String>,
) -> proc_macro2::TokenStream {
let pio::ProgramWithDefines {
program,
Expand Down Expand Up @@ -378,17 +389,29 @@ fn to_codegen(
.parse()
.unwrap();
let program_size = max_program_size;

// This makes sure the file is added to the list
// of tracked files, so a change of that file triggers
// a recompile. Should be replaced by
// `proc_macro::tracked_path::path` when it is stable.
let dummy_include = match file {
Some(file_path) => quote! {let _ = include_bytes!( #file_path );},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be:

Suggested change
Some(file_path) => quote! {let _ = include_bytes!( #file_path );},
Some(file_path) => quote! {const _:&[u8] = include_bytes!( #file_path );},

Not sure which one is better.

None => quote!(),
};
quote! {
{
#defines_struct
::pio::ProgramWithDefines {
program: ::pio::Program::<{ #program_size }> {
code: #code,
origin: #origin,
wrap: #wrap,
side_set: #side_set,
},
public_defines: #defines_init,
{
#dummy_include;
::pio::ProgramWithDefines {
program: ::pio::Program::<{ #program_size }> {
code: #code,
origin: #origin,
wrap: #wrap,
side_set: #side_set,
},
public_defines: #defines_init,
}
}
}
}
Expand Down
Loading