How can I tell which minimum CMake version to require in CMakeLists.txt? [duplicate]
Asked Answered
C

3

18

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.

Connally answered 26/12, 2016 at 22:52 Comment(5)
@wasthishelpful Nope, that's not a duplicate. You cannot check for every CMake command whether it uses a newer features.Mimosa
@Mimosa my bad ^^ maybe more this one: CMake - Which Version as MinimumGause
@wasthishelpful There is some overlap, true. Actually, if you want to close a question, close the other one because it is kind of opinion-based while this is written in a more general way.Mimosa
@usr1234567: Yeah, sorry, I upvoted you but forgot to accept. Also have not had the time actually do as you suggested :-(Connally
In hindsight, and having watched More modern CMake - I wish I could just require 3.15 ...Connally
M
5

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.

Mimosa answered 27/12, 2016 at 9:2 Comment(0)
S
12

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."
Sonyasoo answered 27/12, 2016 at 2:27 Comment(3)
what about just bootstrapping the specific CMake version during build?Spreader
@Spreader That would add complexity to the build system. And for smaller projects it is out of scale. Further, which version would you build? The minimal required one, the latest one, some manually picked and tested one? Do you build CMake everywhere or only on systems with an too old CMake?Mimosa
@usr1234567: Thanks I updated my answer to specify 2.8.10 rather than 2.8.Sonyasoo
M
5

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.

Mimosa answered 27/12, 2016 at 9:2 Comment(0)
L
1

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.

Lingenfelter answered 13/7, 2023 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.