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

David Cole david.cole at kitware.com
Sat Jul 28 07:37:51 EDT 2007


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


On 7/27/07, Christian Convey <christian.convey at gmail.com> wrote:
>
> Could someone please explain why this code works:
>
> SET(FOO 10)
> SET(FOONAME FOO)
> MESSAGE("FOO = ${${FOONAME}}")
>
> but this code does not?
>
> MACRO(BAR)
>    MESSAGE("ARGV0 = ${ARGV0}")
>    SET(ARGV0_NAME ARGV0)
>    MESSAGE("ARGV0 = ${${ARGV0_NAME}}")
> ENDMACRO(BAR)
>
> BAR(123)
>
> In the first block of code, "FOO = 10" is printed to the console when
> I run this through CMake.
>
> But in the second block of code (using the "BAR" macro), I get this
> output:
> ARGV0 = 123
> ARGV0 =
>
> So why can I dynamically construct the variable name in the first
> example ("FOO = 10"), but in the second example ("ARGV0 = ") ?
>
> 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/e24b0330/attachment.htm


More information about the CMake mailing list