[CMake] deleting a cache variable from within CMake script

Yuri V. Timenkov ytimenkov at parallels.com
Wed Aug 13 03:50:21 EDT 2008


On Wednesday 13 August 2008 07:27:15 Philip Lowman wrote:
> Is there a way to have CMake script delete a cache variable?  I do realize
> CMake allows me to do this:
>
> 1. hide it away as an internal cache variable
> 2. mark it as advanced and bury it
> 3. set it to FOO-NOTFOUND prior to calling FIND_LIBRARY()
>
> None of these will work for my use case.  Option 3 makes it impossible for
> the user to set the cache variable manually, option 1 makes it impossible
> to bring it back from an internal state without using force (which is not
> acceptable), option 2 hides the problem but doesn't solve it.
>
> The use case is:
>
> 1. On MSVC or when a particular CMake variable is set (let's call it
> FOO_HUNT_FOR_DEBUG), hunt for and expose FOO_LIBRARY_DEBUG,
> BAR_LIBRARY_DEBUG, etc. using find_library()
> 2. Otherwise set FOO_LIBRARY_DEBUG to FOO_LIBRARY, BAR_LIBRARY to
> BAR_LIBRARY_DEBUG, etc. but don't expose these via the cache since they
> will not be editable and exposing them will at best clutter up the cache,
> at worst confuse people when they try to set them, to no avail.
> 3. The user should be able to toggle FOO_HUNT_FOR_DEBUG between true/false
> on non-MSVC platforms and have the DEBUG cache variables go away when set
> to false
>
> Relevant code snippit:
>
>    if(MSVC OR OSG_FIND_LIBRARY_SEARCH_DEBUG)
>        set(${module_uc}_LIBRARY_DEBUG
> "${module_uc}_LIBRARY_DEBUG-NOTFOUND") # When compiling with VS, search for
> debug libraries since they are # nearly always needed at runtime.
>        find_library(${module_uc}_LIBRARY_DEBUG
>            NAMES ${library}d
>            HINTS
>                 $ENV{${module_uc}_DIR}
>                 $ENV{OSG_DIR}
>                 $ENV{OSGDIR}
>            PATH_SUFFIXES lib64 lib
>            PATHS
>                 <snip>
>        )
>    else(MSVC OR OSG_FIND_LIBRARY_SEARCH_DEBUG)
>        #
>        # On all other platforms there is no requirement to link
>        # debug targets against debug libraries and release targets
>        # against release libraries so just set the FOO_LIBRARY_DEBUG
>        # for the users' convenience in calling target_link_libraries()
>        # once.
>        #
>        set(${module_uc}_LIBRARY_DEBUG ${${module_uc}_LIBRARY})
>     endif(MSVC OR OSG_FIND_LIBRARY_SEARCH_DEBUG)
>
> endfunction(_OSG_FIND_LIBRARY module library)
Why not find two libraries always and and then use
set(${module_uc}_LIBRARY debug ${module_uc}_LIBRARY_DEBUG optimized ${module_uc}_LIBRARY_RELEASE)
This way CMake will use proper libraries for debug and release modes in studio.

The other problem if you want to use static/shared libraries same way. I suggest using kind of internal value, for example (didn't tested for compilability):

option(OSG_FIND_LIBRARY_SEARCH_STATIC "Look for static libs" OFF)

if(NOT "${OSG_FIND_LIBRARY_SEARCH_STATIC}" STREQUAL "${OSG_FIND_LIBRARY_SEARCH_STATIC_INTERNAL}")
	set(OSG_FIND_LIBRARY_SEARCH_STATIC_INTERNAL "${OSG_FIND_LIBRARY_SEARCH_STATIC}" CACHE INTERNAL "" FORCE)
	set(${module_uc}_LIBRARY "NOTFOUND" CACHE FILEPATH "${module_uc} library" FORCE)
endif()

if(OSG_FIND_LIBRARY_SEARCH_STATIC)
	# Push variables to search for static libs
endif()

find_library(${module_uc}_LIBRARY ...)


More information about the CMake mailing list