How to properly fail the CMake project files generation at "toolchain file execution time"
When the toolchain file executes a statement like:
message(FATAL_ERROR "ERROR")
the error reported by CMake is about the generator, e.g.:
cmake .. -DVCPKG_VERBOSE=1 -DCMAKE_TOOLCHAIN_FILE=...../x.cmake
CMake Error at x.cmake:1 (message):
ERROR
Call Stack (most recent call first):
..../cmake/Modules/CMakeDetermineSystem.cmake:123 (include)
CMakeLists.txt:3 (project)
CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
I would expect instead that CMake would just emit the "ERROR" message, and stop there, without adding any unrelated error message afterwards.
This is quite confusing when running CMake along with vcpkg
: an error about the generator is being reported, but the cause is that vcpkg
uses the message(FATAL_ERROR ...
to report an error at toolchain execution time. This has been reported here for example: https://github.com/lukka/CppBuildTasks-Validation/issues/9
To reproduce this problem, it suffices to use a toolchain file that contains a single MESSAGE(FATAL_ERROR ...)
and pass it to CMake
as usual with cmake path/to/src/dir -DCMAKE_TOOLCHAIN_FILE=path/to/toolchain.cmake