[CMake] cmake - escape/delay variable expansion

Michael Hertling mhertling at online.de
Wed Jun 2 22:24:04 EDT 2010


On 06/02/2010 10:24 PM, Doug Reiland wrote:
> Is there anyway to do in a generic fashion?
> For example, I don't know what all the variables are.
> I can be certain that they are valid, for example build-dir is set in scope.
> However, the string can have several "variables" in it.

The following function EXPAND() replaces all occurrences of "${...}" in
INPUT with the current value of the appropriate variable and stores the
result in OUTPUT:

FUNCTION(EXPAND OUTPUT INPUT)
    STRING(REGEX MATCH "\\\${[^}]*}" m "${INPUT}")
    WHILE(m)
        STRING(REGEX REPLACE "\\\${(.*)}" "\\1" v "${m}")
        STRING(REPLACE "\${${v}}" "${${v}}" INPUT "${INPUT}")
        STRING(REGEX MATCH "\\\${[^}]*}" m "${INPUT}")
    ENDWHILE()
    SET("${OUTPUT}" "${INPUT}" PARENT_SCOPE)
ENDFUNCTION()

So, e.g., set(foo \${build-dir}/foo.c) and expand(foo ${foo})
with build-dir=/tmp/src yield foo=/tmp/src/foo.c as desired.

Regards,

Michael

> On Wed, Jun 2, 2010 at 3:06 PM, Michael Hertling <mhertling at online.de> wrote:
>> On 06/02/2010 08:37 PM, Doug Reiland wrote:
>>> Sorry for another newbie question.
>>>
>>> Say, to setup a variable as follow so ${build-dir} doesn't get expanded
>>> set(foo \${build-dir}/foo.c)
>>>
>>> How would a expand foo so ${build-dir} gets expanded?
>>
>> STRING(REPLACE "\${build-dir}" "xyz" foo "${foo}")
>>
>> Regards,
>>
>> Michael


More information about the CMake mailing list