[CMake] deleting a cache variable from within CMake script

Philip Lowman philip at yhbt.com
Tue Aug 12 23:27:15 EDT 2008


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)


-- 
Philip Lowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20080812/ee6299f6/attachment.htm>


More information about the CMake mailing list