Vertexwahn
Vertexwahn

Reputation: 8142

Detect current CMake version using CMake

I am working on a project that uses CMake. The top CMakeLists.txt file contains the following line:

cmake_minimum_required(VERSION 3.7.2) # Kittens will die if you switch to an earlier version of CMake. We suggest using CMake 3.8.0.

I want to force all developers to switch to CMake 3.8.0, but for some reasons, not all developers have administration rights and are not able to switch from 3.7.2 to 3.8.0 immediately. Actually, we do not need any new features of version 3.8.0, but our policy is to use always the newest and greatest tools to prevent "porting up" problems in the future - for instance switching fast from Qt4 to Qt5 was a good decission in the past - I know switching always to the newest libraries and tools has also some drawbacks as discussed here, but we want to do it this way.

Because of this, instead of forcing everyone to use version 3.8.0, I'd like to output a warning message if CMake 3.7.2 is used. Somehow like this:

# not working - just pseudocode
if(CMAKE_VERSION == "3.7.2") 
    message("Please consider to switch to CMake 3.8.0")
endif()

I tried to read the VERSION variable, but this does not work. Does anyone now how this check can be achieved?

Upvotes: 38

Views: 52610

Answers (1)

oLen
oLen

Reputation: 5547

There exist a few variables for that, described here:

CMAKE_MAJOR_VERSION
major version number for CMake, e.g. the "2" in CMake 2.4.3
CMAKE_MINOR_VERSION
minor version number for CMake, e.g. the "4" in CMake 2.4.3
CMAKE_PATCH_VERSION
patch version number for CMake, e.g. the "3" in CMake 2.4.3

Also, the variable CMAKE_VERSION contains the string for the version. In your case, you would, for instance, use the following:

if(CMAKE_VERSION VERSION_LESS "3.8.0") 
    message("Please consider to switch to CMake 3.8.0")
endif()

Other comparison operators are VERSION_EQUAL and VERSION_GREATER.

Upvotes: 66

Related Questions