Update compile-time constant whenever part of a component is rebuilt

squire
Posts: 7
Joined: Tue Oct 15, 2024 8:25 pm

Update compile-time constant whenever part of a component is rebuilt

Postby squire » Mon Mar 10, 2025 11:30 pm

I have a project where I need to know the data/time a certian component was compiled. I have another instance of the program running on another ESP32S3, and if a spesific component was updated on the master ESP32, it flashes its whole app partition onto the slave device, thus keeping the application code for both devices in sync, at least for that spesific component. If any of the other components change, re-flashing is not nessecary. The spesifics aren't really important, I'm just adding this for context.

Currently I have a consteval function BuildNumer() generates the build date (an integer) of a translation unit by parsing the __DATE__ and __TIME__ macros. The problem is, my component has multiple source files and I need that integer to be the same in every translation unit within my component. I modify only one file in the component, then only that file (translation unit) gets the updated integer; files that weren't modified still use the old integer. If I instead don't make BuildNumber() static, the date/time will never be updated unless I modify the source file that contains it. I need the integer to be updated whenever any of the files in the component are recompiled. I've noticed that CMake only compiles source files that have been modified since the last build. How can I get it to either build all or none of the files, or always build one spesific file whenever part of the component is changed? Is there a better way to do this? I'd like to do this on a component-level basis since cleaning and rebuilding the whole project would take too long to be practical (and it's a bit too manual for my taste—one day I'll forget to clean and the whole application will break at runtime :P ).

squire
Posts: 7
Joined: Tue Oct 15, 2024 8:25 pm

Re: Update compile-time constant whenever part of a component is rebuilt

Postby squire » Tue Mar 11, 2025 9:26 pm

I figured it out! All I had to do was un-static BuildNumber() and use CMake to touch the file that contains it whenever any of the other source files are updated. I added the following to the end of the component's CMake.txt:

Code: Select all

add_custom_command(
    OUTPUT ${ROOT_DIR}/BuildNumber.cpp
    COMMAND ${CMAKE_COMMAND} -E touch ${ROOT_DIR}/BuildNumber.cpp
    DEPENDS ${SRCS}
)

Who is online

Users browsing this forum: Amazon [Bot], Baidu [Spider], Bing [Bot], Qwantbot and 2 guests