provide useful message when cmake <3.19 is used with --preset flag

69 Views Asked by At

I'm trying to return a useful error message when cmake < 3.19 is used by the user to compile a project that has presets. The problem is that, for cmakes older than 3.19 the flag --preset is meaningless and, i believe, it is completely ignored.

Here is an example:

cmake --preset ValidPreset
> CMake Error: The source directory "/absolute/path/ValidPreset" does not exist.

which is misleading at best and has nothing to do with the version of cmake. Is there any way to signal this to the users?

Putting

cmake_minimum_required(VERSION 3.19)

in the CMakeLists.txt is useless as the file is not even open, the same goes for something like:

  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 19,
    "patch": 0
  },

in the CMakePresets.json as earlier version of cmake have no idea what that file is.

Is there an hack or workaround to signal the wrong version to the users?

1

There are 1 best solutions below

0
On

You may create for every preset a directory with the same name, where put the CMakeLists.txt file. That file would just emit an error message. E.g.

message(FATAL_ERROR "You use CMake of version ${CMAKE_VERSION}, but minimum required is 3.19. After upgrading CMake remove the file '${CMAKE_BINARY_DIR}/CMakeCache.txt' and try again")

So, instead of emitting a message about non-existence directory <...>/ValidPreset CMake will find CMakeLists.txt in that directory and execute it.

Downside of this approach is that CMake will organize a source directory of your project as a build one, so every further attempt to build your project will silently(!) use the same build directory, until a user removes at least CMakeCache.txt file from it.


I have seen a project which contains CMakeLists.txt in some of its subdirectories, which was a legacy way for run CMake. In that project "fake" CMakeLists.txt just emits a warning, but then calls root CMakeLists.txt.

But in your case I don't find useful for fake CMakeLists.txt to attempt to perform a regular build. The main reason is that root directory will be used as a build one, which is definitely not desired neither by you (as the project developer) nor by a user. So it is better to explicitly ask user to invoke cmake without --preset option.