component_utils.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. function(debug message)
  2. if(DEBUG)
  3. message(STATUS "${message}")
  4. endif()
  5. endfunction()
  6. # Given a component name (find_name) and a list of component paths (component_paths),
  7. # return the path to the component in 'variable'
  8. #
  9. # Fatal error is printed if the component is not found.
  10. function(find_component_path find_name components component_paths variable)
  11. list(FIND components ${find_name} idx)
  12. if(NOT idx EQUAL -1)
  13. list(GET component_paths ${idx} path)
  14. set("${variable}" "${path}" PARENT_SCOPE)
  15. return()
  16. else()
  17. endif()
  18. # TODO: find a way to print the dependency chain that lead to this not-found component
  19. message(WARNING "Required component ${find_name} is not found in any of the provided COMPONENT_DIRS")
  20. endfunction()
  21. # components_find_all: Search 'component_dirs' for components and return them
  22. # as a list of names in 'component_names' and a list of full paths in
  23. # 'component_paths'
  24. #
  25. # component_paths contains only unique component names. Directories
  26. # earlier in the component_dirs list take precedence.
  27. function(components_find_all component_dirs component_paths component_names test_component_names)
  28. # component_dirs entries can be files or lists of files
  29. set(paths "")
  30. set(names "")
  31. set(test_names "")
  32. # start by expanding the component_dirs list with all subdirectories
  33. foreach(dir ${component_dirs})
  34. # Iterate any subdirectories for values
  35. file(GLOB subdirs LIST_DIRECTORIES true "${dir}/*")
  36. foreach(subdir ${subdirs})
  37. set(component_dirs "${component_dirs};${subdir}")
  38. endforeach()
  39. endforeach()
  40. # Look for a component in each component_dirs entry
  41. foreach(dir ${component_dirs})
  42. debug("Looking for CMakeLists.txt in ${dir}")
  43. file(GLOB component "${dir}/CMakeLists.txt")
  44. if(component)
  45. debug("CMakeLists.txt file ${component}")
  46. get_filename_component(component "${component}" DIRECTORY)
  47. get_filename_component(name "${component}" NAME)
  48. if(NOT name IN_LIST names)
  49. list(APPEND names "${name}")
  50. list(APPEND paths "${component}")
  51. # Look for test component directory
  52. file(GLOB test "${component}/test/CMakeLists.txt")
  53. if(test)
  54. list(APPEND test_names "${name}")
  55. endif()
  56. endif()
  57. else() # no CMakeLists.txt file
  58. # test for legacy component.mk and warn
  59. file(GLOB legacy_component "${dir}/component.mk")
  60. if(legacy_component)
  61. get_filename_component(legacy_component "${legacy_component}" DIRECTORY)
  62. message(WARNING "Component ${legacy_component} contains old-style component.mk but no CMakeLists.txt. "
  63. "Component will be skipped.")
  64. endif()
  65. endif()
  66. endforeach()
  67. set(${component_paths} ${paths} PARENT_SCOPE)
  68. set(${component_names} ${names} PARENT_SCOPE)
  69. set(${test_component_names} ${test_names} PARENT_SCOPE)
  70. endfunction()