Question

I have a variable X. X can be generated by the CMakeList. So what I've typically done is:

set(X_OVERRIDE CACHE STRING "" "Overrides the generated value for X")
MARK_AS_ADVANCED(FORCE X_OVERRIDE)
IF("${X_OVERRIDE}" STREQUAL "")
set(X ${GENERATED_X})
ELSE()
SET(X ${X_OVERRIDE})
ENDIF()

Is this how people typically do this?

Was it helpful?

Solution

The distinctive property of your approach is that the user will never see the generated value of X. If this was intended, your approach is fine, but it should not be the default.

The idiomatic way to do this is a simple:

set(X ${GENERATED_X} CACHE STRING "The value X")

This has the following effects:

  • If there is no entry for X in the cache, CMake will create one and set it to the value of GENERATED_X.
  • If there is already an entry for X in the cache, this command does nothing. This is true regardless of whether the value you're trying to set it to matches the one that is present in the cache.

So the user can override the value by manually setting it to a different one (just as before), but if they chose not to do so, they will see the generated value in the cache (unlike before).

Also, if a user did override the value and wants to go back to the default, they need to remove the changed value from the cache (for instance, by clicking the Remove Entry button in cmake-gui) and re-run CMake. Setting it to empty, as with your current approach, will simply use "" as the overriden value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top