[CMake] Concatenating lists back to string

Petr Kmoch petr.kmoch at gmail.com
Thu Oct 23 10:05:12 EDT 2014


Hi Domen.

This is what helps me reason about it:

A string with a ';' in it is a list.
An unqouted ';' separates arguments to CMake commands.
string(REPLACE ...) simply concatenates all of its 'input' parameters.

So, when you expand ${list_1} in the last line, it will simply replace in
the concatenation of its elements - so there's no semicolon in them, of
course. What you want to do is treat the entire list_1 as a single string:

string(REPLACE ";" "." str_2 "${list_1}")

This should do what you expect.

Petr

On Thu, Oct 23, 2014 at 3:49 PM, Domen Vrankar <domen.vrankar at gmail.com>
wrote:

> Hi,
>
> I am trying to convert string to list and back with string(REPLACE) and it
> doesn't work as expected (tested with cmake 2.8.12 on Ubuntu 14.04 and main
> branch in git on Ubuntu 14.10).
>
> cmake_minimum_required( VERSION 2.8.12 )
> project(test_list)
>
> set(str_1 "abc.def.ghi")
> message("str_1: ${str_1}")
> string(REPLACE "." ";" list_1 ${str_1})
> message("list_1: ${list_1}")
> string(REPLACE ";" "." str_2 ${list_1})
> message("str_2: ${str_2}")
>
>
> When I run cmake I get the following output:
> str_1: abc.def.ghi
> list_1: abc;def;ghi
> str_2: abcdefghi
>
> what I expected was that str_2 would be the same as str_1.
>
> In case that semicolons are not used everything works as expected:
>
> cmake_minimum_required( VERSION 2.8.12 )
> project(test_list)
>
> set(str_1 "abc.def.ghi")
> message("str_1: ${str_1}")
> string(REPLACE "." "'" list_1 ${str_1})
> message("list_1: ${list_1}")
> string(REPLACE "'" "." str_2 ${list_1})
> message("str_2: ${str_2}")
>
> Output:
> str_1: abc.def.ghi
> list_1: abc'def'ghi
> str_2: abc.def.ghi
>
> Is this a bug or is there a reason that ; is treated differently since it
> represents lists?
> Is there any other simpler option than to run foreach on the list and
> convert it back to a string?
>
> Thanks,
> Domen
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20141023/109ea362/attachment-0001.html>


More information about the CMake mailing list