[CMake] Problem while setting variables in CMakeCache

Stefan Köhnen stefan.khnen at googlemail.com
Tue Sep 7 13:13:47 EDT 2010


2010/9/7 James Bigler <jamesbigler at gmail.com>:
> 2010/9/7 Stefan Köhnen <stefan.khnen at googlemail.com>:
>> 2010/9/6 Alexander Neundorf <a.neundorf-work at gmx.net>:
>>> On Monday 06 September 2010, Stefan Köhnen wrote:
>>>> Ah, okay.
>>>>
>>>> Thanks for your fast reply.
>>>>
>>>> Is there a way to change the value in the cache?
>>>
>>> set(... FORCE)
>>>
>>> Alex
>>>
>>
>> Hello Alex,
>>
>> thanks again for your reply. I tried to use set with FORCE but it didn't work.
>>
>> My CMakeLists.txt looks like this:
>>
>> PROJECT(CMakeTest)
>>
>> SET(VAR_FOR_TEST "firstValue" CACHE STRING "Just for testing")
>>
>> set(VAR_FOR_TEST "secondValue" FORCE)
>
> This second command also needs to set the cache value:
>
> set(VAR_FOR_TEST "secondValue" CACHE STRING "Just for testing" FORCE)
>
> Keep in mind, that this will prevent the user from changing this value
> from the GUI, since you are forcing it.
>
> An alternative that I've employed with FindCUDA is to set the variable
> *before* calling find_package(CUDA).
>
> set(VAR_FOR_TEST "secondValue" CACHE STRING "Just for testing" FORCE)
> find_package(CUDA)
>
> The first set(CACHE) command wins and the one in the FindCUDA script
> doesn't take effect.
>
> A second alternative that I've employed is to keep a flag called
> PASSED_FIRST_CONFIGURE that is set to ON at the end of the top level
> CMakeLists.txt file:
>
> set(PASSED_FIRST_CONFIGURE ON CACHE INTERNAL "Already Configured once?")
>
> During execution of the CMake scripts, I can check for this flag and
> only do set(CACHE FORCE) commands if PASSED_FIRST_CONFIGURE evaluates
> to false:
>
> if(NOT PASSED_FIRST_CONFIGURE)
>  set(flag "--use_fast_math")
>  list(FIND CUDA_NVCC_FLAGS ${flag} index)
>  if(index EQUAL -1)
>    list(APPEND CUDA_NVCC_FLAGS ${flag})
>    set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} CACHE LIST "Semi-colon
> delimit multiple arguments." FORCE)
>  endif()
> endif()
>
> James
>

Hello James,

thanks for your reply, I implemented your first suggestion and this
works perfectly.

I think your idea with PASSED_FIRST_CONFIGURE flag is propably the
most flexible way to implement this, maybe we will use something
similar to allow changes from the GUI.

Stefan


More information about the CMake mailing list