lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | # File containing various utilities |
| 2 | |
| 3 | # Converts a CMake list to a string containing elements separated by spaces |
| 4 | function(TO_LIST_SPACES _LIST_NAME OUTPUT_VAR) |
| 5 | set(NEW_LIST_SPACE) |
| 6 | foreach(ITEM ${${_LIST_NAME}}) |
| 7 | set(NEW_LIST_SPACE "${NEW_LIST_SPACE} ${ITEM}") |
| 8 | endforeach() |
| 9 | string(STRIP ${NEW_LIST_SPACE} NEW_LIST_SPACE) |
| 10 | set(${OUTPUT_VAR} "${NEW_LIST_SPACE}" PARENT_SCOPE) |
| 11 | endfunction() |
| 12 | |
| 13 | # Appends a lis of item to a string which is a space-separated list, if they don't already exist. |
| 14 | function(LIST_SPACES_APPEND_ONCE LIST_NAME) |
| 15 | string(REPLACE " " ";" _LIST ${${LIST_NAME}}) |
| 16 | list(APPEND _LIST ${ARGN}) |
| 17 | list(REMOVE_DUPLICATES _LIST) |
| 18 | to_list_spaces(_LIST NEW_LIST_SPACE) |
| 19 | set(${LIST_NAME} "${NEW_LIST_SPACE}" PARENT_SCOPE) |
| 20 | endfunction() |
| 21 | |
| 22 | # Convinience function that does the same as LIST(FIND ...) but with a TRUE/FALSE return value. |
| 23 | # Ex: IN_STR_LIST(MY_LIST "Searched item" WAS_FOUND) |
| 24 | function(IN_STR_LIST LIST_NAME ITEM_SEARCHED RETVAL) |
| 25 | list(FIND ${LIST_NAME} ${ITEM_SEARCHED} FIND_POS) |
| 26 | if(${FIND_POS} EQUAL -1) |
| 27 | set(${RETVAL} FALSE PARENT_SCOPE) |
| 28 | else() |
| 29 | set(${RETVAL} TRUE PARENT_SCOPE) |
| 30 | endif() |
| 31 | endfunction() |
| 32 | |
| 33 | # Returns a list of arguments that evaluate to true |
| 34 | function(collect_true output_var output_count_var) |
| 35 | set(${output_var}) |
| 36 | foreach(option_var IN LISTS ARGN) |
| 37 | if(${option_var}) |
| 38 | list(APPEND ${output_var} ${option_var}) |
| 39 | endif() |
| 40 | endforeach() |
| 41 | set(${output_var} ${${output_var}} PARENT_SCOPE) |
| 42 | list(LENGTH ${output_var} ${output_count_var}) |
| 43 | set(${output_count_var} ${${output_count_var}} PARENT_SCOPE) |
| 44 | endfunction() |