Getting source and build version with CMake.

These are some useful snippets of CMake code that I wrote today, so I thought that I's share them. They aren't CMake macros or functions or anything tidy like that, in fact I have them in the main CMakeLists.txt file of a project that I'm currently working on. The purpose of this code is to make it possible for my programs to print out full version information, so that even if I am working on some strange branch and haven't tagged a release, I'll always be able to check to see what source I used to produce that program:

myprogram --version
myprogram version 1.0.0 (master 1a8e6107, 16 Jun 2013, 17:50:26)

The first thing is to grab the git ref for the current head. I read ".git/HEAD" directly, rather than calling upon git itself (the "git describe" command is the more official way of getting such info, but it requires that at least one tag exists in the repository).

# Store the git hash of the current head
    string(REGEX REPLACE "^ref: *([^ \n\r]*).*" "\\1"

Now that we have a PROJECT_SOURCE_VERSION variable set in CMake, the next thing is to grab the date and time and put them into PROJECT_BUILD_DATE and PROJECT_BUILD_TIME. This can be done easily on Linux and OS X by calling the "date" command, but Windows does not provide any way of formatting the output of it's date command so some string manipulation is required:

# Store the build date
  execute_process(COMMAND "cmd" " /c date /t"
  string(REGEX REPLACE "[^0-9]*(..).*" "\\1" MONTH "${DATE}")
  set(MONTHS ""
    "Jan" "Feb" "Mar" "Apr" "May" "Jun"
    "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
  string(REGEX REPLACE "[^/]*/(..)/(....).*" "\\1 ${MONTH} \\2"
  execute_process(COMMAND "cmd" " /c echo %TIME%"
  string(REGEX REPLACE "[^0-9]*(..:..:..).*" "\\1"
  execute_process(COMMAND "date" "+%d %b %Y/%H:%M:%S"
  string(REGEX REPLACE "([^/]*)/.*" "\\1"
  string(REGEX REPLACE "[^/]*/([0-9:]*).*" "\\1"

This isn't quite everything: this only puts the needed information into CMake variables. It still necessary to add a file that can be configured by CMake to make these values available to your C++ code:

/* Source and Build version info. */

Now your program has a set of string macros that it can print when the user asks what version of the source was used to build it.

2 thoughts on “Getting source and build version with CMake.

  1. Great intro for stamping my project with git version. I’m using something similar, but it was not executing each build. I think your solution gave me the clue to fix it. Thanks!!

  2. Glad to hear that you found it useful. There is a caveat for the WIN32 section of the “Store the build date” section: I received an error report from a user that on Windows it only works for the standard north-american date format, so international users might have to change the code to make it work for them.

Comments are closed.