[CMake] Explanation....

Theodore Papadopoulo Theodore.Papadopoulo at inria.fr
Mon Apr 14 13:23:19 EDT 2014


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

First thank's a lot for your answer

On 04/14/2014 12:03 PM, Johannes Zarl wrote:> Hello Theo,
> 
> On Friday, 11. April 2014, 14:20:36, Theodore Papadopoulo wrote:
>> ::-B-::  <- This shows that B is indeed parsed as OPTIONAL
>> 
>> A A        <- Fine I'm in the else part of the if OPTIONAL HERE
>> <- Fine.
> 
> ... I guess "OPTIONAL" should read "TOTO". You say it's fine, but
> it's probably not what you expected.

Yes. The previous version was with OPTIONAL, but since the word might
have been used elsewhere in cmake I decided to put another name and
forgot to change the output.

But the Fine after the OPTIONAL was what I intended.

> Let's take this step by step:
> 
> if ("${arg}" STREQUAL "TOTO") -> if ("TOTO" STREQUAL "TOTO") -> if
> (B STREQUAL B)

What I do not follow is why there is an implicit evaluation of "TOTO"
into "B" (in both this case or the next I explicitely asked for a
string containing the content of the variable ${arg}, if I had
intended the content of the variable which name is in ${arg} I would
have written ${${arg}}.

Of course, you are right about what happened, it is just very unsafe.
That means that if I'm using as a variable name the name of a constant
used in some other module, there will be very unexpected failures....

For example taking /usr/share/cmake/Modules/FeatureSummary.cmake as an
example, if I set a variable PROPERTIES with a value might (untested)
provoke quite unexpected results (if(NOT "${_props}" STREQUAL
"PROPERTIES")). Or if I do set(0 1) before calling
CheckLanguage.cmake (if(CMAKE_${lang}_COMPILER AND "${result}"
STREQUAL "0")).

I realize now that this is what probably means the
 if(<variable|string> STREQUAL <variable|string>)
in the cmake manual. But in my reading that was meaning some
overloading so that

TOTO is a variable so evaluated into ${TOTO}
but "TOTO" remains a string and is not evaluated any further.

At least that what I thought (basically at some type system
differentiating variables and strings)... I'm clearly wrong as:

set(TOTO B)
if ("B" STREQUAL "TOTO")
    message("AhAh")
endif()

The rule seems a little bit awkward as it seem to be sthg like: if
this is a variable name evaluate its value otherwise keep its name as
a string (otherwise the evaluation of strings that do not correspond
to variables would give empty strings...)

In any case, we have to play with the current rule whatever it is, but
I do not think this rule helps making cmake a safe language....


> You could add a prefix to your if expression (I'm sure you have
> seen
this
> technique in configure scripts*g*):
> 
> if ( "_ASDF_${arg}" STREQUAL "_ASDF_TOTO")

Well I knew the technique, but thought it was mainly for protection
againts empty strings (which "${arg}" should be equivalent). Not sure
it makes the thing safer though.

Well I guess I need to go and see cmake source code, as I cannot
understand cery well its evaluation rules.

But the manual could be made clearer here....

> P.S.: To get better visibility on mailing lists, it's best if you
start a new
> thread instead of just replying to a message and changing the
subject line.

Sorry I did not realized that doing so was keeping the thread
information... Normally this message should be a new thread...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlNMGYcACgkQEr8WrU8nPV3TqACgkWkgb1vT3ZQojBGaJq5cCw3X
2ZoAn3LOs/JDDR1PdkkgiTb3drSXvS9M
=MhIC
-----END PGP SIGNATURE-----


More information about the CMake mailing list