CMAKE_EXPORT_COMPILE_COMMANDS defaulting to "false" instead of "unset"
This link seems to have the associated code.
https://github.com/Kitware/CMake/blob/v3.26.2/Modules/CMakeGenericSystem.cmake#L74-L79
Most CMAKE_ prefixed variables, like CMAKE_PCH_WARN_INVALID, or CMAKE_COLOR_DIAGNOSTICS, or CMAKE_POSITION_INDEPENDENT_CODE and so on default to "unset" instead of defaulting to having a specific value.
I have a function that gets used to query whether a variable is set, either in an actual cmake variable, or in an envrionment variable.
function(query_option_variables OUTPUT_VARIABLE DEFAULT_VALUE)
# Note that each ARGN is a variable *name*, and not a value.
# So VARIABLE will hold the NAME of a variable
# So ${VARIABLE} is a name, and ${${VARIABLE}} is the value of that variable.
foreach(VARIABLE ${ARGN})
if(DEFINED ${VARIABLE})
set(${OUTPUT_VARIABLE} ${${VARIABLE}} PARENT_SCOPE)
return()
endif()
if(DEFINED ENV{${VARIABLE}})
set(${OUTPUT_VARIABLE} $ENV{${VARIABLE}} PARENT_SCOPE)
return()
endif()
endforeach()
set(${OUTPUT_VARIABLE} ${DEFAULT_VALUE} PARENT_SCOPE)
endfunction()
It gets used like
query_option_variables(MYORG_POSITION_INDEPENDENT_CODE ON MYORG_POSITION_INDEPENDENT_CODE CMAKE_POSITION_INDEPENDENT_CODE)
or
query_option_variables(CMAKE_UNITY_BUILD_BATCH_SIZE 8 MYORG_UNITY_BUILD_BATCH_SIZE CMAKE_UNITY_BUILD_BATCH_SIZE)
or
query_option_variables(CMAKE_C_VISIBILITY_PRESET "hidden" MYORG_C_VISIBILITY_PRESET CMAKE_C_VISIBILITY_PRESET)
CMAKE_EXPORT_COMPILE_COMMANDS is the only such variable that i've run into where the default is not "unset". As such it's the only variable that my function provided above doesn't work for, as it always sets the result to OFF unless I explicitly set the value on the commandline.