Assertions are powerful tools for verifying function preconditions and postconditions, catching bugs early in development, and ultimately leading to more robust code. Many popular programming languages include built-in support for assertions. Unfortunately, CMake does not currently provide a built-in assert
command. While it's arguable that CMake's if
command can already cover much of this functionality, a dedicated assert
command would be a valuable addition. It offers a more concise and expressive syntax, improving both readability and intent. This post explores several approaches to implementing assert
in the CMake language and discusses why it would be best implemented as a built-in CMake command.
Our goal is to implement an assert
command that can be used by other CMake code. This command should accept the same arguments as the existing if
command. It should raise an error if the arguments are invalid or if the evaluated condition is false. In s