[CMake] How to get around a CACHE problem?

Christian Convey christian.convey at gmail.com
Sun Aug 5 11:25:52 EDT 2007


In this project I'm working on, the user is supposed to be able to
specify where the project's programs and libraries are placed when
they're built.  So the top-level lists

SET( LIBRARY_OUTPUT_PATH ${MOOSBIN} CACHE PATH
     "Output directory for the MOOS libraries" )

But I have a problem:  If some directive earlier in the lists file
causes a MESSAGE(FATAL_ERROR ...) to execute, my SET command above
doesn't work as intended.

I think what happens is that when the fatal error occurs, the cache
file has already been created.  So when the person using ccmake
addresses the error (while staying within ccmake the whole time),
LIBRARY_OUTPUT_PATH has a value: the empty string.

Is this really a desirable way for cmake to work?  It seems to me that
the best solution would be for cmake to realize that is never actually
got around to issuing the SET( LIBRARY_OUTPUT_PATH ...) command, and
therefore should NOT rely on the cache for its value; the value should
be computed and written to the cache.

My work-around for now is to do it like this:

SET( LIBRARY_OUTPUT_PATH ${MOOSBIN} CACHE PATH
     "Output directory for the MOOS libraries" )

IF("${LIBRARY_OUTPUT_PATH}" STREQUAL "")
   SET( LIBRARY_OUTPUT_PATH ${MOOSBIN} CACHE PATH
        "Output directory for the MOOS libraries"
      FORCE)  <-- Note the FORCE directive
ENDIF("${LIBRARY_OUTPUT_PATH}" STREQUAL "")

The "IF" statement should only be true if (a) the fatal error
situation occurred, or (b) the user explicitly set the value to the
empty string.

But this seems lame because I have to do this following *every*
SET(... CACHE ...)
command in my project's and subprojects' lists files!  Also, (b)
should be permitted, but my technique will blow away a user-supplied
empty string.  Is there a better way?

Thanks,
Christain


More information about the CMake mailing list