-
Notifications
You must be signed in to change notification settings - Fork 23
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
jannic
wants to merge
2
commits into
rp-rs:main
Choose a base branch
from
jannic:issue-53
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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>() { | ||||||
|
@@ -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(), | ||||||
|
@@ -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, | ||||||
}) | ||||||
} | ||||||
} | ||||||
|
@@ -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(), | ||||||
) | ||||||
.into() | ||||||
} | ||||||
|
||||||
/// A macro which invokes the PIO assembler at compile time. | ||||||
|
@@ -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, | ||||||
|
@@ -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 );}, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could also be:
Suggested change
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, | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 resultsThere was a problem hiding this comment.
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 thepio_file
macro would work with non-UTF-8 filenames. I didn't want to break that, but there's no way to pass aPath
orOsString
that's not also a validString
toinclude_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?