[CMake] Version embedding - how to cause a command when make is run?

Atwood, Robert C r.atwood at imperial.ac.uk
Thu Oct 4 10:34:59 EDT 2007


Hi:
I have applied Cmake to a pre-existing project that used to use plain
make-files that I crafted with great amounts of hair-tearing, and so far
cmake was relatively easy and I am now a convert :=> Maybe my hair will
even grow back...


However, there's a little thing I have not quite figured out. I have had
a policy of trying to keep enough information embedded in the executable
to ensure that the exact code used to build it may be obtained, using a
script embedded in the Makefile to place some strings, provided by
version control software etc., into the code.

I've tried the following in CMakeLists, placing the output of
'svnversion' and similar commands into an included source file and it
works .. But.. The information applies to the state when 'cmake' is run,
not the state when 'make' is actually run. Therefore it is possible that
source files could be changed and an executable built without updating
this information. 

Can some form of dependency be set so that if any source file is
changed, then these commands get run again when invoking Make, or
alternately that a message is displayed requesting the user to run cmake
again? 

Or, should I use another way to do what I want?

Thanks

Robert


#create an include-file for incorporating the build details in the
executable
#this should also somehow include a dependency for getcflags.inc ?
#
set(INCFILE getcflags.inc)
   file(WRITE ${INCFILE} "Cflags = strdup(\"Configured using
${CMAKE_COMMAND} and ${CMAKE_C_COMPILER}\\")
   file(APPEND ${INCFILE} "\n\\n")
#this line should include the test for GNU C compiler and whether the
command 'head' is present
   exec_program("${CMAKE_C_COMPILER} --version | head -1"
OUTPUT_VARIABLE CCOUT)
   file(APPEND ${INCFILE} "${CCOUT}\\")
   file(APPEND ${INCFILE} "\n\\nCFLAGS used:\\n${ALL_DEFS}\\")
   file(APPEND ${INCFILE} "\n\\nBuild Username:\\t$ENV{USER}\\")
   file(APPEND ${INCFILE} "\n\\nBuild Host:\\t$ENV{HOST}\\")
#this line should include the test whether the command 'svnversion' is
present
   exec_program("svnversion ${uMatIC_SOURCE_DIR}" OUTPUT_VARIABLE CCOUT)
   file(APPEND ${INCFILE} "\n\\nSVN version:\\t${CCOUT}\\")
#this line should include a test for whether the command 'date' is
present
   exec_program("date" OUTPUT_VARIABLE CCOUT)
   file(APPEND ${INCFILE} "\n\\nBuild date:\\t${CCOUT}\");")
   file(APPEND ${INCFILE} "\n")


More information about the CMake mailing list