[CMake] macro/function and ARGN weird behavior

Nicolas Desprès nicolas.despres at gmail.com
Fri May 15 10:10:40 EDT 2009


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


More information about the CMake mailing list