[CMake] Dynamic variable name construction doesn't work with ARGV[n] ?

David Cole david.cole at kitware.com
Sat Jul 28 12:00:22 EDT 2007


I've written macros like this that work:

MACRO(abc var)
  # Reset to empty:
  SET(${var} "")

  # Append each optional arg to ${var} if it matches "\\.h$"
  FOREACH(item ${ARGN})
    IF("${item}" MATCHES "\\.h$")
      SET(${var} ${${var}} "${item}")
    ENDIF("${item}" MATCHES "\\.h$")
  ENDFOREACH(item)
ENDMACRO(abc)

# Call it like this:
abc(header_files a.cpp a.h b.cpp x.h)
MESSAGE("header_files='${header_files}'")

# Or like this:
SET(all_files2 "a.cpp;a.h;b.cpp;x.h")
abc(header_files2 ${all_files2})
MESSAGE("all_files2='${all_files2}'")
MESSAGE("header_files2='${header_files2}'")

# Or like this:
SET(all_files3 a.cpp a.h b.cpp x.h)
abc(header_files3 ${all_files3})
MESSAGE("all_files3='${all_files3}'")
MESSAGE("header_files3='${header_files3}'")

# Notes:
#
# all_files2 and all_files3 are equivalent
#
# If you call the macro abc with ${all_files2} it works like you expect, if
you call it with "${all_files2}" (including
# double quotes), then it appears as one optional argument inside the macro
and you will get different results...


HTH and good luck,
David


On 7/28/07, Christian Convey <christian.convey at gmail.com> wrote:
>
> On 7/28/07, David Cole <david.cole at kitware.com> wrote:
> > Try this:
> > MACRO(BAR)
> >    SET(bar_ARGV0 "${ARGV0}")
> >    MESSAGE("bar_ARGV0 = ${bar_ARGV0}")
> >    SET(ARGV0_NAME bar_ARGV0)
> >    MESSAGE("bar_ARGV0 = ${${bar_ARGV0_NAME}}")
> > ENDMACRO(BAR)
> >
> > I'm not *the* expert to explain this behavior, but I can tell you this:
> > When a macro is called, it is evaluated in its entirety in terms of
> variable
> > substitutions. In other words, the text of the macro is scanned and
> variable
> > substitutions for ${ARG*} and any formally named macro parameters are
> done
> > prior to executing the first line of the macro. The only "variables"
> that
> > exist as you execute are those that existed prior to calling the macro
> plus
> > those that you explicitly set within the macro. The formal parameters
> and
> > the ARG* values are not variables. If they were, you could not achieve
> > macros calling macros without having local scope to macro variables.
> >
> > So, in your example, doing a SET as the first line with "${ARGV0}" as
> the
> > value is substituted prior to running and it works as you expect. In
> your
> > original example, ${ARGV0} only appears inside a MESSAGE command, and
> there
> > is no variable named that, so you cannot access it via double
> indirection
> > with ARGV0 as a variable name,,,
> >
> > This has the one nice effect that the macro parameters are hidden and so
> do
> > not pollute the CMake variable space, but it does take some getting used
> > to... (as you were suggesting in your other thread -- it would be nice
> to
> > have a mechanism to achieve variables local in scope to a macro) --
> Perhaps
> > you could see if it's already a feature request in CMake's bug tracker?
> If
> > not, feel free to add a feature request for macro local variables.
> >
> >
> > HTH,
> > David
> >
>
> Thanks David, that's a big help.
>
> Given what you said, do you know how I can achieve my broader goal?
>
> I have a macro like this:
>
> MACRO( EXTRACT_HEADER_FILENAMES OUTPUT_VARNAME)
>
> The idea is that you invoke it like this:
>     EXTRACT_HEADER_FILENAMES(Foo a.cpp;a.h;b.cpp;x.h)
> So that afteward ${Foo} = "a.h;x.h"
>
> I *wanted* to use a FOREACH loop to iterate through ARGV2, ARGV3, ...,
> to test each filename to see if it ends in ".h".  I would append each
> filename ending in ".h" to the ${OUTPUT_VARNAME} variable.
>
> Is there some way  can iterate over that subset of ARGV like I want to?
>
> Or is there some better way to extract the header filenames from a
> variable that contains a list of filenames?  (I had no luck with
> STRING(REGEX MATCH ALL ...) )
>
> Thanks,
> Christian
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/cmake/attachments/20070728/fa063cb0/attachment.htm


More information about the CMake mailing list