[CMake] [cmake-developers] CMake IR

Ruslan Baratov ruslan_baratov at yahoo.com
Fri Jul 31 13:47:03 EDT 2015


On 31-Jul-15 19:43, Bill Hoffman wrote:
> On 7/31/2015 12:33 PM, Ruslan Baratov wrote:
>>      > rm -rf _builds
>>      > cmake -H. -B_builds
>>      > cmake -H. -B_builds -DA=ON
>>      > grep '\<B\>' _builds/CMakeCache.txt
>>      B:STRING=There is no A
>>
>> I'm not saying cache is a bad idea, I'm just saying that when users hit
>> such kind of situations that's one of the reason why they said CMake is
>> not a good script language. Just like CMake before CMP0054: user see
>> `if("a" STREQUAL "b")` and think "okay, 'a' is not equal to 'b' so this
>> condition is always false"... nope!
> OK, it is a bit confusing.  One solution would be to treat the cache 
> variables with some sort of separate look up.  Basically put all cache 
> stuff into its own namespace.
>
> Something like:
> $CACHE{A}
>
> Then, it would never be confused with the the variable A. However, 
> getting rid of the cache would not be something that could be done.
>
> -Bill
I'm not sure I understand this solution. If you mean that user should 
not mix cache/non-cache variables with the same name, I guess yes (good 
idea for policy by the way). But it will not fix that kind of issues:

     cmake_minimum_required(VERSION 3.0)
     project(Foo)

     set(A "OFF" CACHE BOOL "This is A")

     if(A) # Yes, A is cache variable
       set(B "Hello A" CACHE STRING "Do we use A?")
     else()
       set(B "There is no A" CACHE STRING "Do we use A?")
     endif()

result will be quite the same. The fundamental reason of this is that 
every cache (as a general term) have a context where it can be used, and 
if context changing we can't reuse same cached value. For variable B 
this context is value of A:

     > cmake -H. -B_builds # use default value OFF
     save to cache: B="There is no A" context: A=OFF

     > cmake -H. -B_builds -DA=ON
     load from cache: B="There is no A" context: A=OFF # that's the 
problem, context is not the same, but we still using variable from cache

For more complex example let's use CMAKE_PREFIX_PATH and find_package:

     find_package(Foo CONFIG REQUIRED)
     message("Foo_DIR: ${Foo_DIR}")

     > rm -rf _builds
     > cmake -H. -B_builds -DCMAKE_PREFIX_PATH="location-A"
     Foo_DIR: location-A
     > cmake -H. -B_builds -DCMAKE_PREFIX_PATH="location-B"
     Foo_DIR: location-A # Still location-A!

     > rm -rf _builds
     > cmake -H. -B_builds -DCMAKE_PREFIX_PATH="location-B"
     Foo_DIR: location-B

You can said that it's kind of obvious that you must clear the cache if 
you're changing such critical variables like CMAKE_PREFIX_PATH but in 
this case think about tools that want to do such things automatically 
and efficiently. There is no big difference between A and 
CMAKE_PREFIX_PATH, in both cases it's just a CMake variable.

Ruslan


More information about the CMake mailing list