173

How can I get CMAKE to generate an error on a particular condition. That is, I want something like this:

if( SOME_COND )
  error( "You can't do that" )
endif()
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267

1 Answers1

250

The message() method has an optional argument for the mode, allowing STATUS, WARNING, AUTHOR_WARNING, SEND_ERROR, and FATAL_ERROR. STATUS messages go to stdout. Every other mode of message, including none, goes to stderr.

You want SEND_ERROR if you want to output an error, but continue processing. You want FATAL_ERROR if you want to exit CMake processing.

Something like:

if( SOME_COND )
  message( SEND_ERROR "You can't do that" )
elseif( SOME_CRITICAL_COND )
  message( FATAL_ERROR "You can not do this at all, CMake will exit." )
endif()
starball
  • 20,030
  • 7
  • 43
  • 238
André
  • 18,348
  • 6
  • 60
  • 74
  • 1
    What the heck is an `AUTHOR_WARNING`? – Alexis Wilke Feb 01 '14 at 07:47
  • 2
    @AlexisWilke: the CMake docs state `AUTHOR_WARNING = CMake Warning (dev), continue processing`, suggesting that it is to be used for debugging CMake scripts. Just a wild guess. – pauluss86 Feb 09 '14 at 22:40
  • 6
    @AlexisWilke The `AUTHOR_WARNING` indicates a warning that is not useful to the end user but to the developers. It can either remind them to fix some hack / todos or indicate warnings that should be fixed but does not affect the CMake run for the user. Thats why the user can suppress these warnings with `--Wno-dev`. – usr1234567 May 08 '14 at 06:16
  • 3
    CMake now has *many* more message modes that accompany [`message()`](https://cmake.org/cmake/help/latest/command/message.html), including `VERBOSE`, `DEBUG`, and `TRACE`. – Kevin Oct 05 '19 at 13:05