Fixing incremental build issue caused by cache variables
This issue is in response to this long discourse usage chat: https://discourse.cmake.org/t/incremental-build-bug-changing-project-option-default-isnt-registering-on-incremental-builds/695
The issue is highlighted by this git repo: https://github.com/YMba9g8j9CJp0wLoQf5y/cmake_default_option_issue
Please take the time to reproduce the issue highlighted in the above github link.
This isn't very intuitive for people coming to cmake.
This function below solves the problem quite nicely.
function(set_with_default variable help_text type value)
set("${variable}" "<DEFAULT>" CACHE "${type}" "${help_text}")
if (${variable} STREQUAL "<DEFAULT>")
set("${variable}" "${value}" PARENT_SCOPE)
endif ()
endfunction()
However, I think this functionality should be exposed more obviously to users. Rather than a helper function.
set(<variable> <value>... CACHE <type> <docstring> [FORCE | DEFAULT])
option(<variable> "<help_text>" [value] [DEFAULT])
IE this default keyword would accomplish the same goal as the above function "set_with_default".
However going one step further I believe, you should opt out of this functionality. Rather than opt in. This will make working with cache variables much more intuitive. And will prevent unnecessarily having to clean the build.
set(<variable> <value>... CACHE <type> <docstring> [FORCE | NO_DEFAULT])
option(<variable> "<help_text>" [value] [NO_DEFAULT])
IE specifying NO_DEFAULT makes cache variables act like they always have.
While not specifying NO_DEFAULT will have the equivalent functionality as the function set_with_default.
As for cmake-gui and ccmake I believe this can be visible be shown by having 3 columns instead of just 2
Name,Default,Value
FOO,1,24 # <- DEFAULT
BAR,0,ON # <- NO_DEFAULT
Please leave your thoughts and ideas.