Skip to content

Commit f16c88c

Browse files
authored
Merge pull request #54 from jannic/issue-53
Call include_file! on loaded pio file
2 parents fe1f763 + 199f984 commit f16c88c

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

pio-proc/src/lib.rs

+39-11
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ struct PioFileMacroArgs {
121121
max_program_size: Expr,
122122
program: String,
123123
program_name: Option<(String, LitStr)>,
124+
file_path: PathBuf,
124125
}
125126

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

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

157+
file_path = pathbuf.to_owned();
158+
155159
match fs::read(pathbuf) {
156160
Ok(content) => match std::str::from_utf8(&content) {
157161
Ok(prog) => program = prog.to_string(),
@@ -203,6 +207,7 @@ impl syn::parse::Parse for PioFileMacroArgs {
203207
program_name: select_program.map(|v| (v.name, v.ident)),
204208
max_program_size,
205209
program,
210+
file_path,
206211
})
207212
}
208213
}
@@ -279,7 +284,17 @@ pub fn pio_file(item: TokenStream) -> TokenStream {
279284
Err(e) => return parse_error(e, &args.program).into(),
280285
};
281286

282-
to_codegen(program, args.max_program_size).into()
287+
to_codegen(
288+
program,
289+
args.max_program_size,
290+
Some(
291+
args.file_path
292+
.into_os_string()
293+
.into_string()
294+
.expect("file path must be valid UTF-8"),
295+
),
296+
)
297+
.into()
283298
}
284299

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

298-
to_codegen(program, args.max_program_size).into()
313+
to_codegen(program, args.max_program_size, None).into()
299314
}
300315

301316
fn to_codegen(
302317
program: &pio::ProgramWithDefines<HashMap<String, i32>, { MAX_PROGRAM_SIZE }>,
303318
max_program_size: Expr,
319+
file: Option<String>,
304320
) -> proc_macro2::TokenStream {
305321
let pio::ProgramWithDefines {
306322
program,
@@ -381,18 +397,30 @@ fn to_codegen(
381397
.parse()
382398
.unwrap();
383399
let program_size = max_program_size;
400+
401+
// This makes sure the file is added to the list
402+
// of tracked files, so a change of that file triggers
403+
// a recompile. Should be replaced by
404+
// `proc_macro::tracked_path::path` when it is stable.
405+
let dummy_include = match file {
406+
Some(file_path) => quote! {let _ = include_bytes!( #file_path );},
407+
None => quote!(),
408+
};
384409
quote! {
385410
{
386411
#defines_struct
387-
::pio::ProgramWithDefines {
388-
program: ::pio::Program::<{ #program_size }> {
389-
code: #code,
390-
origin: #origin,
391-
wrap: #wrap,
392-
side_set: #side_set,
393-
version: #version,
394-
},
395-
public_defines: #defines_init,
412+
{
413+
#dummy_include;
414+
::pio::ProgramWithDefines {
415+
program: ::pio::Program::<{ #program_size }> {
416+
code: #code,
417+
origin: #origin,
418+
wrap: #wrap,
419+
side_set: #side_set,
420+
version: #version,
421+
},
422+
public_defines: #defines_init,
423+
}
396424
}
397425
}
398426
}

0 commit comments

Comments
 (0)