How can I tell which minimum CMake version to require in CMakeLists.txt?

5k views Asked by At

In CMakeLists.txt scripts, it is customary to have a:

cmake_minimum_required(VERSION x.y)

for the appropriate x.y version number. But how can I tell what that minimum version is? I don't work with older versions; and as my distribution gets updated, so does CMake, so I may have even introduced commands requiring newer versions over time and I just don't know it.

I could theoretically require the versions I've tested this CMakeLists.txt with, but they're pretty new, and I don't want to restrict users of my code to just that.

3

There are 3 answers

0
usr1234567 On BEST ANSWER

tl;dr: Try building with different CMake versions.

Download the CMake binary from https://cmake.org/files/ according to the claimed minimal required version. You can unpack them in some directory and use this CMake to ensure it is still the minimal required version. You can have as many versions as you wish in parallel. Don't forget to delete the build directory.

A problem you did not mention, but is also important: Check your code with newer version. New policies can lead to dozens of warnings, many projects try avoiding warnings in releases. So it might be good to have the latest version with the same procedure.

3
John Zwinck On

There is no automatic way to do this. Here are some approaches I've used:

  1. If you have used CMake for a while, you may roughly remember what features are newer and older. Select some of the features you use which you think are newer, and read the documentation to discover what version supports them.
  2. If you can install just one or two old versions, it may be enough. CMake 2.8.x is very popular, so installing that and testing to see if it works (perhaps fixing it so it does) would be a nice service to some users.
  3. Don't worry about it. Set the minimum to 2.8.10 and then accept patches or bug reports if users have specific issues. It's not likely that setting this number too low will cause any serious harm--typically it will just result in a different error message than "The CMake version is too old."
0
Eike On

You can simply use nlohmann's software for checking cmake minimal version.

It will do the tedious binary search on cmake versions for you and do the download of different cmake versions.