[CMake] Embedding up-to-date version info in built library

Brad King brad.king at kitware.com
Mon Dec 14 11:08:18 EST 2009


Kolja Waschk wrote:
> Beside the SVN revision number of the working base, our embedded
> revision information also tells whether the library was built from code
> with local modifications, i.e. whether it equals the source code stored
> in the SVN repository or was changed afterwards.  Changing a single byte
> in a header file already affects that state, so all headers need to be
> watched.

So you need some combination of "svn info" and "svn status".

> Try #1: add_custom_command(OUTPUT version.cc DEPENDS ${SOURCES} ...)

You need the custom command to run every time the build starts:

  add_custom_target(update_version
    COMMAND ${CMAKE_COMMAND} -P update_version.cmake
    )

The update_version.cmake script should compute the version and then
use configure_file() to store the result in "version.h" with a
copy-if-different check.  The key is then to make the library depend
on this rule at a *target* level, but only on version.h at a *file*
level.

  add_library(mylib version.c) # version.c does #include "version.h"
  add_dependencies(mylib update_version)

The update_version target will always run the command before the build
system considers mylib.  Then the version.c source will recompile only
if the version update rule actually changed version.h content.

> Try #2: as above plus IMPLICIT_DEPENDS CXX ${SOURCES} ...

FYI, any solution based on IMPLICIT_DEPENDS will not work with VS IDE
and other non-Makefile generators.  It is documented to work only with
Makefile generators.

-Brad


More information about the CMake mailing list