[CMake] foreach: bug or feature?

Andreas Pakulat apaku at gmx.de
Wed Apr 29 09:11:23 EDT 2009


On 29.04.09 14:58:00, Marcel Loose wrote:
> Hi Andreas,
> 
> Seems we disagree about the intuitiveness of the API. Maybe I've done
> too much C/C++ programming ;-)

Hmm, I'm doing mostly C++ as well :)

> I stumbled upon this problem while I was trying to parse the input
> arguments to a macro(add_package _name). This macro can be used as
> follows: add_package(name [version] [DEPENDS depend depend ...])
> I wanted to get all the arguments after DEPENDS. Here's a simplified
> version of the macro.
> 
>   macro(add_package _name)
>     set(_args ${ARGN})
>     list(FIND _args DEPENDS _start)
>     list(LENGTH _args _stop)
>     math(EXPR _start "${_start}+1")
>     math(EXPR _stop "${_stop}-1")
>     message(STATUS "_index = ${_start}")
>     message(STATUS "_length = ${_stop}")
>     foreach(idx RANGE ${_start} ${_stop})
>       list(GET _args ${idx} _arg)
>       message(STATUS "_args[${idx}] = ${_arg}")
>     endforeach(idx RANGE ${_start} ${_stop})
>   endmacro(add_package _name)
> 
> While testing boundary condition, I noted that this macro fails when
> there are no arguments after DEPENDS. In that case start > stop, so I
> did not expect CMake to enter the foreach loop at all. But it did, and
> triggered a list index out of range error. Now, that's what I found
> counter-intuitive.

Well, but the bug is in your macro, it doesn't check that there's at least
one dependency listed, while apparently thats a requirement. And in this
case you wouldn't have won a lot if RANGE would bail out if start is larger
than stop or simply switches them, you'd still have to dig into your macro
and add some debug prints to find out why start/stop don't have the
expected value.

Andreas
 
-- 
Never reveal your best argument.


More information about the CMake mailing list