[CMake] Bug in macro command

Filipe Sousa filipe at ipb.pt
Fri Jul 1 10:42:28 EDT 2005


On Friday 01 July 2005 15:15, Ken Martin wrote:
> > Subject: [CMake] Bug in macro command
> >
> > The following code displays
> > --
> > -- xxx
> > -- Configuring done
> > -- Generating done
> >
> > instead of
> > -- xxx
> > -- xxx
> > -- Configuring done
> > -- Generating done
> >
> > MACRO(foo var)
> >   SET(${var} "xxx")
> > ENDMACRO(foo)
> >
> > foo(var)
> > MESSAGE(STATUS ${var})
> >
> > foo(hello)
> > MESSAGE(STATUS ${hello})
>
> Hmmm, very interesting. Here is what is going on. The original MacroCommand
> did a simple string replacement on its arguments and ARGV ARGC etc. So in
> the above example:  SET (${var} "xxx")  would get sent to the parser as:
> SET (var "xxx") in the first call. In the new implementation the formal
> arguments and ARGC ARGV etc are set as variables that are expanded like any
> other variable. To be safe I restore any variables set by the macro command
> to their original value at the end of the macro execution. So.. in your
> example you happen to be setting a formal argument to a new value in your
> macro command AND the invocation of the macro is passing in a variable with
> the same name as the formal argument. So the macro sets var to xxx and at
> the end of the invocation the macro command sets it back to its original
> value. This doesn't happen for the second invocation because you are
> passing hello in.
>
> (whew)
>
> So, I'm thinking of reverting back to the original implementation. The only
> advantage of the new implementation (other than shorter code) is that you
> can do something like
>
> MACRO(foo var)
>   SET (A v)
>   SET (B ar)
>   MESSAGE ("${${A}${B}} is var")
> ENDMACRO(foo)
>
> And ${A} and ${B} will expand to var and then ${var} will expand to
> whatever the formal argument to the macro was. Not a very useful feature
> and not a traditional behavior of a macro in the C/C++ concept of a macro.
> Any comments?
>
> Thanks
> Ken

I'm using macros more like functions than c/c++ macros. Here is one example of 
a macro that I'm using

MACRO(QT_GET_MOCABLE_FILES moc_list)
  FOREACH(source ${ARGN})
    # is this a header file?
    SET(header_ext "\\.h|\\.hpp|\\.H")
    GET_FILENAME_COMPONENT(ext ${source} EXT)
    IF("${ext}" MATCHES "${header_ext}")
      SET(header ${CMAKE_CURRENT_SOURCE_DIR}/${source})
      FILE(READ ${header} header_contents)
      SET(moc_regex "class.*{.*Q_OBJECT.*}")
      IF("${header_contents}" MATCHES "${moc_regex}")
	SET(${moc_list} ${${moc_list}} ${source})
      ENDIF("${header_contents}" MATCHES "${moc_regex}")
    ENDIF("${ext}" MATCHES "${header_ext}")
  ENDFOREACH(source)
ENDMACRO(QT_GET_MOCABLE_FILES)

SET(sources a.h a.cc b.h b.cc ....)
QT_GET_MOCABLE_FILES(moc ${sources})
SET(sources ${moc} ${sources})

With the new macro command behavior ${moc} is empty.

-- 
Filipe Sousa
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://public.kitware.com/pipermail/cmake/attachments/20050701/999f7fb6/attachment-0001.pgp


More information about the CMake mailing list