Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect current CMake version using CMake

Tags:

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?

like image 753
Vertexwahn Avatar asked Apr 29 '17 15:04

Vertexwahn


People also ask

How use CMake command line?

Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.

What is Cmakelist?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.

Where is CMakeLists TXT located?

CMakeLists. txt is placed at the root of the source tree of any application, library it will work for. If there are multiple modules, and each module can be compiled and built separately, CMakeLists. txt can be inserted into the sub folder.


1 Answers

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.

like image 101
oLen Avatar answered Oct 18 '22 00:10

oLen