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

Christian Convey christian.convey at gmail.com
Sat Jul 28 11:10:57 EDT 2007


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


More information about the CMake mailing list