Best/Shortest way to join a list in CMake

前端 未结 4 482
温柔的废话
温柔的废话 2021-02-04 06:07

What is the best way to join a list in CMake into a string?

By joining I mean convert SET(somelist \"a\" \"b\" \"c\\;c\") to \"a:b:c;c\" where the glue string (\":\") i

4条回答
  •  囚心锁ツ
    2021-02-04 06:18

    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
    

提交回复
热议问题