[CMake] Caching calculated values.

Josef Karthauser joe.karthauser at geomerics.com
Tue Oct 23 04:01:46 EDT 2007


> -----Original Message-----
> From: Amitha Perera [mailto:perera at cs.rpi.edu]
> Sent: 22 October 2007 18:53
> To: Josef Karthauser
> Cc: cmake at cmake.org
> Subject: Re: [CMake] Caching calculated values.
> 
> On Mon 22 Oct 2007, Josef Karthauser wrote:
> > As each set of calculations are connected to a single CMakeList.txt
> > file, it should be possible to avoid recalculating, and useing
cached
> > values where possible.  In order to do that however I need to know
> > whether a CMakeLists.txt file is stale, with respect to the last
time
> it
> > ran, or not.  Is there an easy way for me to determine from within a
> > CMakeLists.txt file, whether it is out-of-date?
> 
> In the VXL project, we use a serial number for that purpose.
> Something like
> 
> # The serial number below will allow the maintainers to force builds
> # to update cached results. Whenever you make a change that makes it
> # necessary for cached values to be updated, increment the serial
> # number. The format follows a DNS-style numbering: the current date
> # followed by a modification time within the day.
> #
> SET( VXL_CONFIG_SERIAL_CURRENT "2006-16-03-002" )

In the end I've come up with a method based around dropping a file in
each project build directory, and then comparing its time stamp with
that of the associated CMakeLists.txt file.  Instead of calling PROJECT,
I just call GEO_PROJECT.  This then manages the correct setting of the
relevant USE_CACHE setting for each project, which is utilised elsewhere
by the code that needs to avoid doing the work.

Joe


MACRO(GEO_PROJECT PROJECTNAME)
    PROJECT(${PROJECTNAME})
    DEBUG_MESSAGE(GEO_CACHE_VERBOSE "Configuring project
${PROJECTNAME}")

    # We record the source files in per-project source variablesa
    # with this prefix.
    SET(GEO_SOURCE GEO_SOURCE_${PROJECTNAME})

    # Create the cache file.
    SET(GEO_CACHE_FILE "${PROJECT_BINARY_DIR}/source.cached")

    # If cached file exists, and is younger than CMakeLists.txt then
    # use the cache.
    SET(${GEO_SOURCE}_USE_CACHE 0)
    IF(EXISTS ${GEO_CACHE_FILE})
        DEBUG_MESSAGE(GEO_CACHE_VERBOSE "Cache exists")
        DEBUG_MESSAGE(GEO_CACHE_VERBOSE "Cmakelists file:
${PROJECT_SOURCE_DIR} CMakeLists.txt")
        DEBUG_MESSAGE(GEO_CACHE_VERBOSE "Geocache file:
${GEO_CACHE_FILE}")
        IF(NOT "${PROJECT_SOURCE_DIR}/CMakeLists.txt" IS_NEWER_THAN
${GEO_CACHE_FILE})
            DEBUG_MESSAGE(GEO_CACHE_VERBOSE "CMakelists newer")
            SET(${GEO_SOURCE}_USE_CACHE 1)
        ENDIF(NOT "${PROJECT_SOURCE_DIR}/CMakeLists.txt" IS_NEWER_THAN
${GEO_CACHE_FILE})
    ENDIF(EXISTS ${GEO_CACHE_FILE})
    DEBUG_MESSAGE(GEO_CACHE_VERBOSE "Use cache:
${${GEO_SOURCE}_USE_CACHE}")

    # If not using the cache, then erase the previously cached list
    # and start again.
    IF(NOT ${GEO_SOURCE}_USE_CACHE)
        # Clear the file names
        SET(${GEO_SOURCE} "" CACHE INTERNAL "" FORCE)
        FOREACH(PFORM ${ALL_PLATFORMS})
            SET(${GEO_SOURCE}_${PFORM} "" CACHE INTERNAL "" FORCE)
        ENDFOREACH(PFORM)

        # Create the cache file.
        MESSAGE("(Re-)Configuring project ${PROJECTNAME}")
        FILE(WRITE ${GEO_CACHE_FILE} "Cache exists")
    ELSE(NOT ${GEO_SOURCE}_USE_CACHE)
        DEBUG_MESSAGE(GEO_CACHE_VERBOSE "\tUsing cached file list")
    ENDIF(NOT ${GEO_SOURCE}_USE_CACHE)
ENDMACRO(GEO_PROJECT)




More information about the CMake mailing list