cpp-utilities/cmake/modules/ListToString.cmake

28 lines
1.2 KiB
CMake
Raw Normal View History

2016-06-09 22:57:51 +02:00
if(NOT DEFINED LIST_TO_STRING_EXISTS)
set(LIST_TO_STRING_EXISTS true)
function(list_to_string separator prefix suffix input_list output_string_var)
2016-06-09 22:57:51 +02:00
set(res "")
# get list length
list(LENGTH input_list list_length)
# if the list has 0 or 1 element, there is no need to loop over
if(list_length LESS 2)
2016-07-27 21:43:31 +02:00
set(res "${prefix}${input_list}${suffix}")
2016-06-09 22:57:51 +02:00
else()
math(EXPR last_element_index "${list_length} - 1")
foreach(index RANGE ${last_element_index})
# get current item_value
list(GET input_list ${index} item_value)
if(NOT item_value STREQUAL "")
# .. and append non-empty value to output string
set(res "${res}${prefix}${item_value}${suffix}")
2016-06-09 22:57:51 +02:00
# append separator if current element is NOT the last one.
if(NOT index EQUAL last_element_index)
set(res "${res}${separator}")
endif()
endif()
endforeach()
endif()
set(${output_string_var} "${res}" PARENT_SCOPE)
endfunction()
endif()