[CMake] How to properly handle build version number in CMake script for project

Robert Dailey rcdailey.lists at gmail.com
Mon Apr 10 09:29:20 EDT 2017


I have a file called version.cmake that my root CMakeLists.txt
includes. There is only a single line in this file:

set( BUILD_VERSION 1.2.3.4 CACHE STRING "Version of the product" )

I have two scenarios where this version number setting needs to work
slightly differently from a CMake perspective:

1. Our CI build server does a fresh clone of our repo, and generates
from scratch. It sometimes needs to override the version number from
the command line via arguments. So it will do:

-D BUILD_VERSION=99.99.1.2

This works since it's a cache variable. In fact, the current solution
ONLY works well for this scenario (since it will allow overriding from
command line, but also allow the file to set the value if it is not
specified as a -D argument).

2. Local work machine builds. Local builds never override using -D,
they always use what is in the version.cmake file. However, because
it's a cache variable and I'm not using FORCE with set(), it never
updates if I change the version and push it to origin. This is the
missing requirement: it needs to update when the number (value)
changes in version control

I thought of making a BUILD_VERSION_OVERRIDE that is only accepted on
the command line, and if specified, it will set the BUILD_VERSION
value to the overridden value. However, I don't like the idea of 2
separate variables to manage this. Using a non-cache variable doesn't
allow overriding with the same variable value on the command line
either.

What's the best solution here for my situation?


More information about the CMake mailing list