Best/Shortest way to join a list in CMake

前端 未结 4 492
温柔的废话
温柔的废话 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:31

    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'
    

提交回复
热议问题