How to embed a build timestamp into the firmware

I’d like to embed a build timestamp into the firmware, but without forcing a recompilation of the entire world each time I invoke pio run. I first used


and while that does allow me to embed the timestamp into the firmware it forces a recompilation.

The idea I’ve come up with so far is to define a symbol on the linker commandline and set the value of the symbol to the timestamp or firmware version. In GLD I believe this can be done using --defsym symbol=expression. This way if the firmware gets relinked it gets a fresh timestamp, but if it doesn’t nothing is forced to be linked or compiled. I have tried this, so perhaps it doesn’t actually work the way I’m thinking…

Any thoughts, other suggestions? How could I try the above idea out?

1 Like

Linker flags can still be passed via build_flags (docs) so e.g. build_flags = -Wl,-T/path/to/ld_script.ld passes options there.

The other option would be to go with GCC predefined macros for __DATE__ and __TIME__, which are given as strings (Standard Predefined Macros (The C Preprocessor)) .

You could then parse it back to a unix timestamp or just use it directly. Then PIO shouldn’t retrigger the compilation by itself, but you have to take care that this source file is recompied…