[CMake] ARGVn madness?!

Michael Wild themiwi at gmail.com
Fri Nov 13 07:17:32 EST 2009


On 13. Nov, 2009, at 12:33 , Marcel Loose wrote:

> Hi all,
>
> I've been bitten by an IMHO weird behaviour with argument  
> substitution.
> When I call a macro from within a function, then unused ARGVn  
> variables
> (where n=1..9) retain the value that they got when entering the
> function. Is this a bug?!
>
> This example demonstrates what I mean:
>
> $ cat ../CMakeLists.txt
> project(Dummy)
> cmake_minimum_required(VERSION 2.6)
>
> function(my_function)
>  message(STATUS "my_function: ARGC=${ARGC}")
>  message(STATUS "my_function: ARGV=${ARGV}")
>  message(STATUS "my_function: ARGV0=${ARGV0}")
>  message(STATUS "my_function: ARGV1=${ARGV1}")
>  my_macro(mies)
> endfunction(my_function)
>
> macro(my_macro)
>  message(STATUS "my_macro: ARGC=${ARGC}")
>  message(STATUS "my_macro: ARGV=${ARGV}")
>  message(STATUS "my_macro: ARGV0=${ARGV0}")
>  message(STATUS "my_macro: ARGV1=${ARGV1}")
> endmacro(my_macro)
>
> my_function(aap noot)
> my_macro(wim)
>
> $ cmake ..
> -- my_function: ARGC=2
> -- my_function: ARGV=aap;noot
> -- my_function: ARGV0=aap
> -- my_function: ARGV1=noot
> -- my_macro: ARGC=1
> -- my_macro: ARGV=mies
> -- my_macro: ARGV0=mies
> -- my_macro: ARGV1=noot    <---
> -- my_macro: ARGC=1
> -- my_macro: ARGV=wim
> -- my_macro: ARGV0=wim
> -- my_macro: ARGV1=
> -- Configuring done
> -- Generating done
> -- Build files have been written to: /tmp/loose/cmake/build
>
> I expected to find "ARGV1=" in the line marked with "<---", not
> "ARGV1=noot".
>
> Best regards,
> Marcel Loose.
>

Looks like the scoping algorithm only "overwrites" these automatic  
variables only if they are associated with actual arguments, otherwise  
it just inherits them. Same thing happens if my_macro is a function.  
Smells like bug to me ;-)

This probably means, always check ARGC and only use ARGN to retrieve  
optional arguments...

Michael



More information about the CMake mailing list