[CMake] foreach: bug or feature?

Marcel Loose loose at astron.nl
Wed Apr 29 11:02:21 EDT 2009


Hi Andreas,

The thing is that I was hoping that I would not have to check explicitly
for any missing arguments to DEPENDS. But of course it is very wise to
check and report this error. It was more that I was baffled by the
behaviour of foreach(). I'll have to work around this feature ;-)

Best regards,
Marcel Loose.

On Wed, 2009-04-29 at 15:11 +0200, Andreas Pakulat wrote:
> 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
>  



More information about the CMake mailing list