Reputation: 613
I need to be able to stop CMake processing from within a CMake function or macro. I don't want to stop processing just the current file, but stop ALL CMake processing. Right now, I am using message(FATAL_ERROR "Foo")
. The documentation for message
states it does exactly what I need:
CMake Error, stop processing and generation
However, I would like to be able to do this without generating an error. I really don't need a message either. I just want to completely exit. Is there any way to do this?
Upvotes: 3
Views: 6413
Reputation: 18243
If your CMakeLists.txt hierarchy is fairly shallow, you can call return()
once or twice (depending on if you called add_subdirectory()
or if you're in a macro/function) to return to the calling file or function and exit CMake processing.
If your project hierarchy is more complex, and you want to exit your CMakes from an arbitrary location, there is no native CMake command to support that. However, you could roll your own (admittedly, a bit scary) solution to terminate cmake
:
function(exit_cmake)
if(UNIX)
set(KILL_COMMAND "killall")
execute_process(COMMAND ${KILL_COMMAND} -9 cmake
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
else()
set(KILL_COMMAND "taskkill")
execute_process(COMMAND ${KILL_COMMAND} /IM cmake.exe /F
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
endif()
endfunction()
If you include this in your top-level CMake file, you can call this from anywhere in your CMakes to exit silently.
WARNING: This will terminate ALL currently running CMake processes, not just your current process. Because you are forcing the process the terminate, your CMake files/cache may be left in a weird state.
Upvotes: 4