I use CMAKE on Ubuntu 22. I have the following CMAKE code:
cmake_minimum_required(VERSION 3.19)
<...snip...>
# Read the configuration file
file(READ "${CMAKE_CURRENT_LIST_DIR}/config.json" CONFIG)
string(JSON SW_VER_MAJOR GET ${CONFIG} SW VER_MAJOR)
string(JSON SW_VER_MINOR GET ${CONFIG} SW VER_MINOR)
string(JSON SW_BUILD GET ${CONFIG} SW BUILD)
message(STATUS "SW version: ${SW_VER_MAJOR}.${SW_VER_MINOR}.${SW_BUILD}")
add_compile_definitions(_VER_MAJOR=${SW_VER_MAJOR})
add_compile_definitions(_VER_MINOR=${SW_VER_MINOR})
add_compile_definitions(_VER_BUILD=${SW_BUILD})
When one of the parameters is missing in the JSON file, I get the error like:
CMake Error at CMakeLists.txt:41 (string):
string sub-command JSON member 'SW_VER_MAJOR' not found.
After the error, the CMAKE build continues. I expect that this error will be a fatal error and CMAKE will stop. According to doc https://cmake.org/cmake/help/latest/command/string.html If the ERROR_VARIABLE optional parameter is not defined, the error should be handled as a fatal error. But actually, it is not happening. What I did wrong?