[CMake] how to remove a variable??

Brad King brad.king at kitware.com
Wed Dec 15 08:22:00 EST 2004


Sergio Andrés wrote:
> Hi all,
> 
> a simple question... How could I remove a variable from the CACHE??
> 
> I want to include VTK depending on an OPTION. If this OPTION is OFF, I 
> would like to remove VTK_DIR varible from de CACHE (or at least from the 
> CMakeSetup GUI).
> 
> I tried the following, but when the option is ON the VTK_DIR is INTERNAL 
> and it is not shown.
> ----------------------------------
> OPTION(SMOLIB_VTK "Use VTK classes" OFF)
> 
> IF (SMOLIB_VTK)    FIND_PACKAGE(VTK)
>    IF (USE_VTK_FILE)
>        INCLUDE(${USE_VTK_FILE})
>    ENDIF (USE_VTK_FILE)
> ELSE (SMOLIB_VTK)
>    SET (VTK_DIR ${VTK_DIR} CACHE INTERNAL "" FORCE)
> ENDIF (SMOLIB_VTK)
> ----------------------------------
> 
> Can anyone help me??

The simplest change is just to make VTK_DIR an advanced variable:

MARK_AS_ADVANCED(VTK_DIR)

It won't do exactly what you want but it will keep the setup dialog more 
clean.  When a user turns on the option that needs VTK then he/she will 
get an error if VTK_DIR is not set.

To really do what you want you can do this:

# Set proper not-found value for cache.
IF(NOT VTK_DIR)
   SET(VTK_DIR "VTK_DIR-NOTFOUND")
ENDIF(NOT VTK_DIR)

IF(SMOLIB_VTK)
   # Show VTK_DIR to the user.
   SET(VTK_DIR "${VTK_DIR}" CACHE PATH "" FORCE)

   # Find and use VTK.
   FIND_PACKAGE(VTK)
   IF(VTK_USE_FILE)
     INCLUDE(${VTK_USE_FILE})
   ENDIF(VTK_USE_FILE)
ELSE(SMOLIB_VTK)
   # Hide VTK_DIR from the user but preserve value.
   SET(VTK_DIR "${VTK_DIR}" CACHE INTERNAL "")
ENDIF(SMOLIB_VTK)

-Brad


More information about the CMake mailing list