Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Output a list with delimiters

Tags:

cmake

By default, CMake outputs lists without delimiters, eg

set(my_list a b c d)
message(${my_list})

Outputs

abcd

How can you (easily) make CMake output something like what is actually stored?

a;b;c;d

(A typical use case is for outputting a list of search paths)

like image 263
Zero Avatar asked Jul 16 '13 00:07

Zero


2 Answers

Enclose the dereferenced variable in quotes.

set(my_list a b c d) message("${my_list}") 

Outputs

a;b;c;d 
like image 187
Zero Avatar answered Oct 08 '22 00:10

Zero


You could write a function to join the items of a list together with a delimiter, and then print that out instead. For example, such a function:

function (ListToString result delim)     list(GET ARGV 2 temp)     math(EXPR N "${ARGC}-1")     foreach(IDX RANGE 3 ${N})         list(GET ARGV ${IDX} STR)         set(temp "${temp}${delim}${STR}")     endforeach()     set(${result} "${temp}" PARENT_SCOPE) endfunction(ListToString) 

Then, you could use it like so:

set(my_list a b c d) ListToString(str ", " ${my_list}) message(STATUS "${str}") 

Which outputs:

a, b, c, d 
like image 44
MuertoExcobito Avatar answered Oct 07 '22 23:10

MuertoExcobito