[CMake] Hard to do if in Macro

J Decker d3ck0r at gmail.com
Tue Jan 30 11:07:19 EST 2018


Okay... but then with function I can't set external variables; but if(
__ANDROID__ ) works.

`result` never changes.

---------------------------

set( __ANDROID__ 1 )

function( test __ANDROID__ RETVAL )

  if( ${__ANDROID__} EQUAL 1 OR ${__ANDROID__} STREQUAL "ON")
    set( ${RETVAL} ${${RETVAL}} qwer2 )
    message( "Included..... " )
  endif( ${__ANDROID__} EQUAL 1 OR ${__ANDROID__} STREQUAL "ON")

  if( __ANDROID__ )
    set( ${RETVAL} ${${RETVAL}} asdf )
    message( "ALWAYS Included ${__ANDROID__}" )
  endif( __ANDROID__ )

endfunction( test )


set( result "default" )

test( __ANDROID__ "result" )
message( "REsult:${result}" )

test( ${__ANDROID__} "result" )
message( "REsult:${result}" )

test( OFF  "result")
message( "REsult:${result}" )

---------------------------




On Tue, Jan 30, 2018 at 12:35 AM, Petr Kmoch <petr.kmoch at gmail.com> wrote:

> Macros aren't functions, and macro parameters aren't CMake variables.
> Expanding a macro parameter is effectively *textual* substitution, whereas
> dereferencing a CMake variable is a semantic one.
>
> In other words, inside your macro, the text __ANDROID__ (when not
> expanded) never refers to the macro parameter. Macro parameters "don't
> exist" outside of expansion context. The `if(__ANDROID__)` bit therefore
> always refers to the variable __ANDROID__, never to the macro parameter
> __ANDROID__.
>
> You still don't have to spell out the conditional as explicitly as you're
> doing it now, but you have to expand the parameter:
>
> macro(test __ANDROID__)
>   if(${__ANDROID__})
>     message( "Included..... " )
>   endif()
> endmacro()
>
> This is what the evaluation will look like based on how you call it:
>
> test(__ANDROID__)   -> if(__ANDROID__) -> refers to the variable
> set(__ANDROID__ 1)
> test(${__ANDROID__})   -> if(1)
> set(__ANDROID__ 0)
> test(${__ANDROID__})   -> if(0)
> test(OFF)   -> if(OFF)
>
> CMake macros have a lot of specific and potentially weird/counterintuitive
> behaviour. In general, you should always write your commands as functions
> and only resort to macros if you explicitly need their specific behaviour.
>
> Petr
>
>
>
> On 30 January 2018 at 09:11, J Decker <d3ck0r at gmail.com> wrote:
>
>> Why do I have to do
>>
>> if( ${M__ANDROID__} EQUAL 1 OR ${M__ANDROID__} STREQUAL "ON")
>> endif( ${M__ANDROID__} EQUAL 1 OR ${M__ANDROID__} STREQUAL "ON")
>>
>> in a macro like...
>> ----------------------
>>
>> set( __ANDROID__ 1 )
>>
>> macro( test __ANDROID__ )
>>
>>   if( ${__ANDROID__} EQUAL 1 OR ${__ANDROID__} STREQUAL "ON")
>>     message( "Included..... " )
>>   endif( ${__ANDROID__} EQUAL 1 OR ${__ANDROID__} STREQUAL "ON")
>>
>>   if( __ANDROID__ )
>>     message( "ALWAYS Included ${__ANDROID__}" )
>>   endif( __ANDROID__ )
>>
>> endmacro( test )
>>
>> test( __ANDROID__ )
>> test( ${__ANDROID__} )
>> test( OFF )
>>
>> ----------
>> Output
>>
>> Included.....
>> ALWAYS Included __ANDROID__
>> Included.....
>> ALWAYS Included 1
>> ALWAYS Included OFF
>>
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> https://cmake.org/mailman/listinfo/cmake
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180130/25d63346/attachment-0001.html>


More information about the CMake mailing list