[CMake] Integrating Build Date/Time into Binary

Torsten Robitzki Torsten at Robitzki.de
Wed Jan 16 03:50:20 EST 2019


Hello,

we are porting a larger set of projects (~70) to CMake. The current build has a feature to integrate the current timestamp of the build into the final binary / target. If a target is relinked, a header (id.h) is created with the current timestamp, a user provided C file (id.c), including that header is recompiled and linked into the target as well.

While thinking about how to provide a similar solutions, we came up with some ideas:

1) add_custom_command()

The projects have to move id.c into a separate library. For the main target, all depending targets (excluding the library containing id.c) are determined. A add_custom_command() creates id.h and depends on the list of determined, depending targets.

The drawback that I see here, is that other, additional dependencies (like introduced with add_custom_command()) are not taken into account and that we have to recreate the algorithm to determine all direct and indirect depending targets.

2) add_custom_command(TARGET <target> PRE_LINK…)

The generation of both, id.h and id.c are excluded from the main target and are build by the add_custom_command(). The resulting object file is added as GENERATED, EXTERNAL_OBJECT source file to the main target.

The drawback here is, that the custom command have to compile the id.c file, using the right compiler, the right flags, include paths etc.

To me it doesn’t sound to be a very special requirement to have some kind of hash (timestamp in this case) over the compiled files in the resulting binary. How do others solve this or similar problems? Any comments to our ideas?

TIA,

Torsten
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <https://cmake.org/pipermail/cmake/attachments/20190116/ab69ae15/attachment.sig>


More information about the CMake mailing list