component_utils.cmake 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(FATAL_ERROR "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. if(EXISTS ${dir}/CMakeLists.txt)
  35. get_filename_component(_path "${dir}" ABSOLUTE)
  36. get_filename_component(_name "${_path}" NAME)
  37. if(NOT name IN_LIST names)
  38. list(APPEND _names "${_name}")
  39. list(APPEND _paths "${_path}")
  40. endif()
  41. if(EXISTS "${_path}/test/CMakeLists.txt")
  42. list(APPEND _test_names "${_name}")
  43. endif()
  44. else()
  45. if(EXISTS ${dir}/component.mk)
  46. get_filename_component(legacy_component "${legacy_component}" DIRECTORY)
  47. message(WARNING "Component ${legacy_component} contains old-style component.mk but no CMakeLists.txt. "
  48. "Component will be skipped.")
  49. else()
  50. if(NOT __recursing) # recurse only once
  51. file(GLOB subdirs LIST_DIRECTORIES true "${dir}/*")
  52. set(__recursing 1)
  53. components_find_all("${subdirs}" __paths __names __test_names)
  54. set(__recursing 0)
  55. if(__paths)
  56. list(APPEND _paths "${__paths}")
  57. list(APPEND _names "${__names}")
  58. list(APPEND _test_names "${__test_names}")
  59. endif()
  60. endif()
  61. endif()
  62. endif()
  63. endforeach()
  64. set(${test_component_names} "${_test_names}" PARENT_SCOPE)
  65. set(${component_paths} "${_paths}" PARENT_SCOPE)
  66. set(${component_names} "${_names}" PARENT_SCOPE)
  67. endfunction()