Add build date/time (and maybe repository revision) information to compiled binary

Hi,

I want to add some build information (hard coded) into my binary. Is there any way how I can access the current date/time and maybe the revision number of a SVN repository during the build process and use this information to initialize a constant variable/string in flash memory?

If I remember correctly, one of our old projects, using IAR Embedded Workbench, auto-generated an include file which provided macros for variable initialization during compilation (before compilation started). Can Visual Studio Code and the nRF Connect plugin do this, too?

Best regards,
Michael

Parents
  • Hi Michael,

    You can access the build date and time as __DATE__ and __TIME__, and you can see an example of thtat in zephyr/samples/subsys/mgmt/mcumgr/smp_svr/src/main.c. Regarding revision information this video shows how you can get information from Git. I am not aware of any instructions for SVN, but I expect you can do something similar for that.

    Br,

    Einar

  • Hi Einar,

    the __DATE__ and __TIME__ macros work fine, although the video is a bit confusing to understand... too much talking and too little information. I took the information from the video that I can configure calls to external tools in the build system. I added the following command to CMakeLists.txt, right after the find_package() and project() statements:

    execute_process(COMMAND get_ver.bat)

    This statement calls my batch script that parses the SVN information into a .h file, just like in our old projects.

    The only problem is that this command apparently only is executed during a pristine build, and not each time I perform a normal build. How can I execute the command each time? Just like "pre-build actions" which can be configured in other development environments...

    Best regards,
    Michael

  • Hi Michael,

    Can you try to do something like this instead?

    add_custom_target(update_repo_info
      execute_process(COMMAND get_ver.bat)
    )
    add_dependencies(app update_repo_info)

    This should be run for every build.

  • Hi Einar,

    I had to change something in your code:

    1. The execute_process() statement must be removed

    2. The script is now executed in the build direcotry, not in the project directory. So I had to add the path to the script.

    Further, my script needs a parameter with a path directive. While I can add the path to the script file with a slash, the parameter needs a backslash. It seems to work when I just write two backslashes (escape sequence?). This is how my code looks now:

    add_custom_target(update_repo_info COMMAND ../getver.bat .. ..\\src\\generated)

    Now, I still have two probelms:

    1. When I perform a normal build, the .c file that uses __DATA__ and __TIME__ to initialize a const uint8_t[] array does not re-compile (even though the macro values have changed).

    2. When my repository has changed, the custom target call updates my generated header file, but does not re-compile the .c file which includes the generated header file. It is re-compiled during the next build operation, though. It seems like the build system acquires the 'changed' state (for re-compiling source files) before performing the custom target operation, even though the source file is compiled after executing the custom target. (This could explain why my new repository info only makes into the binary one build step later.)

    Could this be a bug in the build system?

    Best regards,
    Michael

Reply
  • Hi Einar,

    I had to change something in your code:

    1. The execute_process() statement must be removed

    2. The script is now executed in the build direcotry, not in the project directory. So I had to add the path to the script.

    Further, my script needs a parameter with a path directive. While I can add the path to the script file with a slash, the parameter needs a backslash. It seems to work when I just write two backslashes (escape sequence?). This is how my code looks now:

    add_custom_target(update_repo_info COMMAND ../getver.bat .. ..\\src\\generated)

    Now, I still have two probelms:

    1. When I perform a normal build, the .c file that uses __DATA__ and __TIME__ to initialize a const uint8_t[] array does not re-compile (even though the macro values have changed).

    2. When my repository has changed, the custom target call updates my generated header file, but does not re-compile the .c file which includes the generated header file. It is re-compiled during the next build operation, though. It seems like the build system acquires the 'changed' state (for re-compiling source files) before performing the custom target operation, even though the source file is compiled after executing the custom target. (This could explain why my new repository info only makes into the binary one build step later.)

    Could this be a bug in the build system?

    Best regards,
    Michael

Children
  • Hi Michael,

    What you see is expected. You will need to perform a pristine build in order to get the date and time updated.

    I am not sure if you can categorize this as a bug or not, but this is how the build system work. I am not aware of any way around that. That said, because of this and other differences between an incremental re-building and pistine builds, you shoudl alway do pristine builds whenever making releases (and often during development as well), and then this should not be a problem.

    Br,

    Einar

Related