I've written macros like this that work:<br><br>MACRO(abc var)<br> # Reset to empty:<br> SET(${var} "")<br><br> # Append each optional arg to ${var} if it matches "\\.h$"<br>
FOREACH(item ${ARGN})<br> IF("${item}" MATCHES "\\.h$")<br> SET(${var} ${${var}} "${item}")<br> ENDIF("${item}" MATCHES "\\.h$")<br>
ENDFOREACH(item)<br>
ENDMACRO(abc)<br>
<br># Call it like this:<br>abc(header_files a.cpp a.h b.cpp x.h)<br>MESSAGE("header_files='${header_files}'")<br><br># Or like this:<br>
SET(all_files2 "a.cpp;a.h;b.cpp;x.h")<br>abc(header_files2 ${all_files2})<br>
MESSAGE("all_files2='${all_files2}'")<br>
MESSAGE("header_files2='${header_files2}'")<br>
<br># Or like this:<br>
SET(all_files3 a.cpp a.h b.cpp x.h)<br>
abc(header_files3 ${all_files3})<br>
MESSAGE("all_files3='${all_files3}'")<br>
MESSAGE("header_files3='${header_files3}'")<br>
<br># Notes:<br>#<br># all_files2 and all_files3 are equivalent<br>#<br># If you call the macro abc with ${all_files2} it works like you expect, if you call it with "${all_files2}" (including<br># double quotes), then it appears as one optional argument inside the macro and you will get different results...
<br><br><br>HTH and good luck,<br>David<br><br><br><div><span class="gmail_quote">On 7/28/07, <b class="gmail_sendername">Christian Convey</b> <<a href="mailto:christian.convey@gmail.com">christian.convey@gmail.com</a>
> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">On 7/28/07, David Cole <<a href="mailto:david.cole@kitware.com">david.cole@kitware.com
</a>> wrote:<br>> Try this:<br>> MACRO(BAR)<br>> SET(bar_ARGV0 "${ARGV0}")<br>> MESSAGE("bar_ARGV0 = ${bar_ARGV0}")<br>> SET(ARGV0_NAME bar_ARGV0)<br>> MESSAGE("bar_ARGV0 = ${${bar_ARGV0_NAME}}")
<br>> ENDMACRO(BAR)<br>><br>> I'm not *the* expert to explain this behavior, but I can tell you this:<br>> When a macro is called, it is evaluated in its entirety in terms of variable<br>> substitutions. In other words, the text of the macro is scanned and variable
<br>> substitutions for ${ARG*} and any formally named macro parameters are done<br>> prior to executing the first line of the macro. The only "variables" that<br>> exist as you execute are those that existed prior to calling the macro plus
<br>> those that you explicitly set within the macro. The formal parameters and<br>> the ARG* values are not variables. If they were, you could not achieve<br>> macros calling macros without having local scope to macro variables.
<br>><br>> So, in your example, doing a SET as the first line with "${ARGV0}" as the<br>> value is substituted prior to running and it works as you expect. In your<br>> original example, ${ARGV0} only appears inside a MESSAGE command, and there
<br>> is no variable named that, so you cannot access it via double indirection<br>> with ARGV0 as a variable name,,,<br>><br>> This has the one nice effect that the macro parameters are hidden and so do<br>> not pollute the CMake variable space, but it does take some getting used
<br>> to... (as you were suggesting in your other thread -- it would be nice to<br>> have a mechanism to achieve variables local in scope to a macro) -- Perhaps<br>> you could see if it's already a feature request in CMake's bug tracker? If
<br>> not, feel free to add a feature request for macro local variables.<br>><br>><br>> HTH,<br>> David<br>><br><br>Thanks David, that's a big help.<br><br>Given what you said, do you know how I can achieve my broader goal?
<br><br>I have a macro like this:<br><br>MACRO( EXTRACT_HEADER_FILENAMES OUTPUT_VARNAME)<br><br>The idea is that you invoke it like this:<br> EXTRACT_HEADER_FILENAMES(Foo a.cpp;a.h;b.cpp;x.h)<br>So that afteward ${Foo} = "
a.h;x.h"<br><br>I *wanted* to use a FOREACH loop to iterate through ARGV2, ARGV3, ...,<br>to test each filename to see if it ends in ".h". I would append each<br>filename ending in ".h" to the ${OUTPUT_VARNAME} variable.
<br><br>Is there some way can iterate over that subset of ARGV like I want to?<br><br>Or is there some better way to extract the header filenames from a<br>variable that contains a list of filenames? (I had no luck with
<br>STRING(REGEX MATCH ALL ...) )<br><br>Thanks,<br>Christian<br>_______________________________________________<br>CMake mailing list<br><a href="mailto:CMake@cmake.org">CMake@cmake.org</a><br><a href="http://www.cmake.org/mailman/listinfo/cmake">
http://www.cmake.org/mailman/listinfo/cmake</a><br></blockquote></div><br>