Generating custom output files with Cargo

⚓ Rust    📅 2025-12-09    👤 surdeus    👁️ 2      

surdeus

fish shell uses gettext (and in future fluent) for storing translations.
To be able to check and auto-update translation files,
we use a proc-macro to extract the set of translatable strings.

It looks like the easiest way to do this is by having each macro invocation like localizable_string!("foo") write to a temporary file, and finally concat all files.
Writing to the same file would break parallel compilation.

Writing a new file for each translatable string literal seems fine,
but if there is a better way or if if anyone else does anything like this.
It looks like Cargo/rustc are only meant to produce object files, not custom text files.

Prior to the extraction macro, we used parse the output of cargo-expand with regex but that was quite brittle though (since cargo-expand doesn't give structured data).
Maybe there is a way to write a "compiler plugin".. I'm thinking something like clang-query that allows to collect all interesting identifiers inside specific function calls.

(For our use case, we might be able to get away with a different appproach of using the translation files themselves as source of truth, rather than the string literals in Rust code. But that's a bit worse)

4 posts - 2 participants

Read full topic

🏷️ Rust_feed