[CMake] macro/function and ARGN weird behavior

David Cole david.cole at kitware.com
Fri May 15 10:29:13 EDT 2009


The other difference (besides scoping) between a macro and a function
explains the behavior you are seeing:
The parameters to a function are automatically local variables inside the
function scope. The parameters to a macro are not actually variables. They
are merely expanded *as if* they were variables anywhere you see the ${}
de-referencing syntax. Think of it like #define in C++ : the arguments are
substituted where they are referenced, but they are not actually local
variables.

It's equivalent to the difference between these in C++:

// there are not actually variables named x and y here:
#define min(x, y) ......

// but there are variables here:
int min(int x, int y)
{
 .......
}


HTH,
David



2009/5/15 Nicolas Desprès <nicolas.despres at gmail.com>

> Hi there,
>
> I'm experiencing a weird behavior with cmake 2.6.4 (Win32) playing
> with macro, function and ARGN.
>
> === script
> function(print_var)
>  foreach(i ${ARGV})
>    message("${i}='${${i}}'")
>  endforeach(i)
> endfunction(print_var)
>
> macro(print_var_macro)
>  foreach(i ${ARGV})
>    message("macro ${i}='${${i}}'")
>  endforeach(i)
> endmacro(print_var_macro)
>
> function(foo a1 a2)
>  message("begin foo")
>  message("a1=${a1}")
>  message("a2=${a2}")
>  message("ARGN=${ARGN}")
>  print_var(a1 a2 ARGN) # 1
>  print_var_macro(a1 a2 ARGN) # 2
>  message("end foo")
> endfunction(foo)
>
> macro(bar a1 a2)
>  message("begin bar")
>  message("a1=${a1}")
>  message("a2=${a2}")
>  message("ARGN=${ARGN}")
>  print_var(a1 a2 ARGN) # 3
>  print_var_macro(a1 a2 ARGN) # 4
>  message("end bar")
> endmacro(bar)
>
> foo("a" "b" "c" "d" "e")
> bar("a" "b" "c" "d" "e")
> === end script
> === output
> begin foo
> a1=a
> a2=b
> ARGN=c;d;e
> a1='a'
> a2='b'
> ARGN='a1;a2;ARGN'
> macro a1='a'
> macro a2='b'
> macro ARGN='c;d;e'
> end foo
> begin bar
> a1=a
> a2=b
> ARGN=c;d;e
> a1=''
> a2=''
> ARGN='a1;a2;ARGN'
> macro a1=''
> macro a2=''
> macro ARGN=''
> end bar
> === end output
>
> 1) I undestand it perfectly. ARGN is interpreted in the context of the
> `print_var' function.
> 2) I don't understand why the behavior is not similar to #1
> 3) I don't understand why `a1' and `a2' are "eaten"
> 4) This one is very weird.
>
> I though the only difference between macro and function was that
> variables are scoped in a function and not in a macro. In all cases, I
> find those behavior weird compared to what I'm used to in other
> languages.
>
> Any light?
>
> Cheers,
>
> --
> Nicolas Desprès
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090515/df8553fd/attachment.htm>


More information about the CMake mailing list