cpp-utilities/cmake/modules/ListToString.cmake

35 lines
1.2 KiB
CMake
Raw Normal View History

2020-01-10 17:58:41 +01:00
cmake_minimum_required(VERSION 3.3.0 FATAL_ERROR)
2018-07-27 16:30:11 +02:00
# prevent multiple inclusion
2019-02-06 17:30:52 +01:00
if (DEFINED LIST_TO_STRING_LOADED)
2018-07-27 16:30:11 +02:00
return()
2019-02-06 17:30:52 +01:00
endif ()
2018-07-27 16:30:11 +02:00
set(LIST_TO_STRING_LOADED ON)
2017-02-11 02:30:46 +01:00
2019-11-30 17:58:01 +01:00
function (list_to_string separator prefix suffix input_list output_string_var)
2018-07-27 16:30:11 +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
2019-02-06 17:30:52 +01:00
if (list_length LESS 2)
2018-07-27 16:30:11 +02:00
set(res "${prefix}${input_list}${suffix}")
2019-02-06 17:30:52 +01:00
else ()
2018-07-27 16:30:11 +02:00
math(EXPR last_element_index "${list_length} - 1")
2019-02-06 17:30:52 +01:00
foreach (index RANGE ${last_element_index})
2018-07-27 16:30:11 +02:00
# get current item_value
list(GET input_list ${index} item_value)
2019-02-06 17:30:52 +01:00
if (NOT item_value STREQUAL "")
2018-07-27 16:30:11 +02:00
# .. and append non-empty value to output string
set(res "${res}${prefix}${item_value}${suffix}")
# append separator if current element is NOT the last one.
2019-02-06 17:30:52 +01:00
if (NOT index EQUAL last_element_index)
2018-07-27 16:30:11 +02:00
set(res "${res}${separator}")
2019-02-06 17:30:52 +01:00
endif ()
endif ()
endforeach ()
endif ()
2019-11-30 17:58:01 +01:00
set(${output_string_var}
"${res}"
PARENT_SCOPE)
2019-02-06 17:30:52 +01:00
endfunction ()