Logging file dependency like include_bytes! in custom macro

I was only able to google up include_bytes! now registers the file included by tbelaire · Pull Request #24423 · rust-lang/rust · GitHub related to this idea. As I explain there, I'm looking for a way to log a file dependency from custom, user-written macros?

I'd like to write a macro that parses an xml spec and generates code on-the-fly (returning a TokenStream), but I'd want to be able to log this kind of dependency so that when the spec is edited, the rust file that includes it is recompiled. In the linked pull request, that was added for include_bytes! such that if the included file changes, the .rs that includes it gets recompiled. Is there a way to do that for my own macro?

Usually the trick is to emit a const _: &[u8] = include_bytes!("file-to-depend-on...");

Depending on your project's architecture, you could use a build.rs script to generate a file, because:

  • build.rs scripts have a way to properly tell Cargo about file dependencies;

  • IDEs might appreciate only having to walk through included files rather than having to expand macros themselves.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.