| 1 |
cmake_minimum_required(VERSION 3.17.0 FATAL_ERROR) |
| 2 |
|
| 3 |
# prevent multiple inclusion |
| 4 |
if (DEFINED LIST_TO_STRING_LOADED) |
| 5 |
return() |
| 6 |
endif () |
| 7 |
set(LIST_TO_STRING_LOADED ON) |
| 8 |
|
| 9 |
function (list_to_string separator prefix suffix input_list output_string_var) |
| 10 |
set(res "") |
| 11 |
# get list length |
| 12 |
list(LENGTH input_list list_length) |
| 13 |
# if the list has 0 or 1 element, there is no need to loop over |
| 14 |
if (list_length LESS 2) |
| 15 |
set(res "${prefix}${input_list}${suffix}") |
| 16 |
else () |
| 17 |
math(EXPR last_element_index "${list_length} - 1") |
| 18 |
foreach (index RANGE ${last_element_index}) |
| 19 |
# get current item_value |
| 20 |
list(GET input_list ${index} item_value) |
| 21 |
if (NOT item_value STREQUAL "") |
| 22 |
# .. and append non-empty value to output string |
| 23 |
set(res "${res}${prefix}${item_value}${suffix}") |
| 24 |
# append separator if current element is NOT the last one. |
| 25 |
if (NOT index EQUAL last_element_index) |
| 26 |
set(res "${res}${separator}") |
| 27 |
endif () |
| 28 |
endif () |
| 29 |
endforeach () |
| 30 |
endif () |
| 31 |
set(${output_string_var} |
| 32 |
"${res}" |
| 33 |
PARENT_SCOPE) |
| 34 |
endfunction () |