[CMake] How is ARGN passed to a macro

Andreas Naumann Andreas-Naumann at gmx.net
Wed Nov 21 14:18:26 EST 2018


Hey Torsten,

Am 21.11.18 um 13:15 schrieb Torsten Robitzki:
> Hi,
> I’ve stumbled over following behavior and now I’m searching for the rules, that explains that behavior:
>
> test.cmake:
>
>    macro(check_argn)
>      message("in macro check_argn ARGN: ${ARGN}")
>
>      if(ARGN)
>        foreach(item IN LISTS ARGN)
>          message("ARG: ${item}")
>        endforeach(item)
>        message("ARGN: ${ARGN}")
>      endif()
>    endmacro()
>
>    function(f1 a)
>      message("in function ARGN: ${ARGN}")
>      check_argn()
>    endfunction()
>
>    f1(1)
>    f1(1 2)
>
> This will yield the following output:
>
>    $ cmake -P ./test.cmake
>    in function ARGN:
>    in macro check_argn ARGN:
>    in function ARGN: 2
>    in macro check_argn ARGN:
>    ARG: 2
>    ARGN:
>
> I would expect ARGN to behave exactly the same in the macro, as in the function, but apparently there is a difference. Can someone of you explain that to me?

I think the behavior is explained in [1] and [2]. In particular the 
second section of [2] states that the parameter " [...] such as ARGN are 
not variables in the usual CMake sense. They are string replacements 
[...]".  And the example at the end of [2] seems to match your test 
perfectly.

Hope that helps a bit,
Andreas

[1] https://cmake.org/cmake/help/latest/command/function.html

[2] https://cmake.org/cmake/help/latest/command/macro.html

>
> TIA and kind regards,
>
> Torsten




More information about the CMake mailing list