Best/Shortest way to join a list in CMake

ID : 274385

viewed : 24

Tags : listjoincmakelist





Top 5 Answer for Best/Shortest way to join a list in CMake

vote vote

95

Usually this task is solved with simple string REPLACE command. You can find a number of such replaces in scripts coming with cmake. But if you really need to care about semicolons inside you values, then you can use the following code:

function(JOIN VALUES GLUE OUTPUT)   string (REGEX REPLACE "([^\\]|^);" "\\1${GLUE}" _TMP_STR "${VALUES}")   string (REGEX REPLACE "[\\](.)" "\\1" _TMP_STR "${_TMP_STR}") #fixes escaping   set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE) endfunction()  SET( letters "" "\;a" b c "d\;d" ) JOIN("${letters}" ":" output) MESSAGE("${output}") # :;a:b:c:d;d 
vote vote

75

You can use the string REPLACE function:

function(JOIN VALUES GLUE OUTPUT)   string (REPLACE ";" "${GLUE}" _TMP_STR "${VALUES}")   set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE) endfunction()  #USAGE: SET(somelist a b c) JOIN("${somelist}" ":" output) MESSAGE("${output}") # will output "a:b:c" 
vote vote

61

The answers above are ok, when you do not use semicolons or when you can (and want to) escape them. I prefer not to escape semicolons, so I wrote the following function:

function(JOIN OUTPUT GLUE)     set(_TMP_RESULT "")     set(_GLUE "") # effective glue is empty at the beginning     foreach(arg ${ARGN})         set(_TMP_RESULT "${_TMP_RESULT}${_GLUE}${arg}")         set(_GLUE "${GLUE}")     endforeach()     set(${OUTPUT} "${_TMP_RESULT}" PARENT_SCOPE) endfunction() 

The advantage is that the list can be empty and it doesn't have to be in a variable (but may be written in the place of invocation).

The usage is:

set(SOME_LIST a b c d) join(RESULT "/" ${SOME_LIST}) message(STATUS "RESULT='${RESULT}'") # outputs RESULT='a/b/c/d' # or join(RESULT " " e f g h) message(STATUS "RESULT='${RESULT}'") # outputs RESULT='e f g h' 
vote vote

54

Top 3 video Explaining Best/Shortest way to join a list in CMake







Related QUESTION?