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

Hendrik Sattler post at hendrik-sattler.de
Mon Apr 10 12:10:12 EDT 2017


Hi,

You could make it a non-cache variable and manage the caching yourself. The cache is your version.cmake. Before including it, check if the variable is set and update your version cmake file, possibly with configure_file().  After that, unset() the cache variable and include your cmake file, this having a non-cache variable. Add the cmake file to the list of dependent files to let cmake run when that file changes.

Regards

HS


Am 10. April 2017 15:29:20 MESZ schrieb Robert Dailey <rcdailey.lists at gmail.com>:
>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?

-- 
Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet.


More information about the CMake mailing list