You can add PARENT_SCOPE to set command to set the value on the calling code.

Regards,
Dvir

From: CMake [mailto:[email protected]] On Behalf Of J Decker
Sent: Tuesday, January 30, 2018 18:07
To: Petr Kmoch <[email protected]>
Cc: CMake Mail List <[email protected]>
Subject: [Digital Signature Failure] Re: [CMake] Hard to do if in Macro

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 
<[email protected]<mailto:[email protected]>> 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 
<[email protected]<mailto:[email protected]>> 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<http://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


-- 

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

Reply via email to