Utilities.cmake 1.5 KB

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