components.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Given a list of components in 'component_paths', filter only paths to the components
  2. # mentioned in 'components' and return as a list in 'result_paths'
  3. function(components_get_paths component_paths components result_paths)
  4. set(result "")
  5. foreach(path ${component_paths})
  6. get_filename_component(name "${path}" NAME)
  7. if("${name}" IN_LIST components)
  8. list(APPEND result "${name}")
  9. endif()
  10. endforeach()
  11. set("${result_path}" "${result}" PARENT_SCOPE)
  12. endfunction()
  13. # Add a component to the build, using the COMPONENT variables defined
  14. # in the parent
  15. #
  16. function(register_component)
  17. get_filename_component(component_dir ${CMAKE_CURRENT_LIST_FILE} DIRECTORY)
  18. set(component ${COMPONENT_NAME})
  19. spaces2list(COMPONENT_SRCDIRS)
  20. spaces2list(COMPONENT_ADD_INCLUDEDIRS)
  21. spaces2list(COMPONENT_SRCEXCLUDE)
  22. if(COMPONENT_SRCDIRS)
  23. # Warn user if both COMPONENT_SRCDIRS and COMPONENT_SRCS are set
  24. if(COMPONENT_SRCS)
  25. message(WARNING "COMPONENT_SRCDIRS and COMPONENT_SRCS are both set, COMPONENT_SRCS will be ignored")
  26. endif()
  27. set(COMPONENT_SRCS "")
  28. foreach(dir ${COMPONENT_SRCDIRS})
  29. get_filename_component(abs_dir ${dir} ABSOLUTE BASE_DIR ${component_dir})
  30. if(NOT IS_DIRECTORY ${abs_dir})
  31. message(FATAL_ERROR "${CMAKE_CURRENT_LIST_FILE}: COMPONENT_SRCDIRS entry '${dir}' does not exist")
  32. endif()
  33. file(GLOB matches "${abs_dir}/*.c" "${abs_dir}/*.cpp" "${abs_dir}/*.S")
  34. if(matches)
  35. list(SORT matches)
  36. set(COMPONENT_SRCS "${COMPONENT_SRCS};${matches}")
  37. else()
  38. message(FATAL_ERROR "${CMAKE_CURRENT_LIST_FILE}: COMPONENT_SRCDIRS entry '${dir}' has no source files")
  39. endif()
  40. endforeach()
  41. endif()
  42. # Remove COMPONENT_SRCEXCLUDE matches
  43. foreach(exclude ${COMPONENT_SRCEXCLUDE})
  44. get_filename_component(exclude "${exclude}" ABSOLUTE ${component_dir})
  45. foreach(src ${COMPONENT_SRCS})
  46. get_filename_component(abs_src "${src}" ABSOLUTE ${component_dir})
  47. if("${exclude}" STREQUAL "${abs_src}") # compare as canonical paths
  48. list(REMOVE_ITEM COMPONENT_SRCS "${src}")
  49. endif()
  50. endforeach()
  51. endforeach()
  52. # add as a PUBLIC library (if there are source files) or INTERFACE (if header only)
  53. if(COMPONENT_SRCS OR embed_binaries)
  54. add_library(${component} STATIC ${COMPONENT_SRCS})
  55. set(include_type PUBLIC)
  56. else()
  57. add_library(${component} INTERFACE) # header-only component
  58. set(include_type INTERFACE)
  59. endif()
  60. # binaries to embed directly in library
  61. spaces2list(COMPONENT_EMBED_FILES)
  62. spaces2list(COMPONENT_EMBED_TXTFILES)
  63. foreach(embed_data ${COMPONENT_EMBED_FILES} ${COMPONENT_EMBED_TXTFILES})
  64. if(embed_data IN_LIST COMPONENT_EMBED_TXTFILES)
  65. set(embed_type "TEXT")
  66. else()
  67. set(embed_type "BINARY")
  68. endif()
  69. target_add_binary_data("${component}" "${embed_data}" "${embed_type}")
  70. endforeach()
  71. # add component public includes
  72. foreach(include_dir ${COMPONENT_ADD_INCLUDEDIRS})
  73. get_filename_component(abs_dir ${include_dir} ABSOLUTE BASE_DIR ${component_dir})
  74. if(NOT IS_DIRECTORY ${abs_dir})
  75. message(FATAL_ERROR "${CMAKE_CURRENT_LIST_FILE}: "
  76. "COMPONENT_ADD_INCLUDEDIRS entry '${include_dir}' not found")
  77. endif()
  78. target_include_directories(${component} ${include_type} ${abs_dir})
  79. endforeach()
  80. # add component private includes
  81. foreach(include_dir ${COMPONENT_PRIV_INCLUDEDIRS})
  82. if(${include_type} STREQUAL INTERFACE)
  83. message(FATAL_ERROR "${CMAKE_CURRENT_LIST_FILE} "
  84. "sets no component source files but sets COMPONENT_PRIV_INCLUDEDIRS")
  85. endif()
  86. get_filename_component(abs_dir ${include_dir} ABSOLUTE BASE_DIR ${component_dir})
  87. if(NOT IS_DIRECTORY ${abs_dir})
  88. message(FATAL_ERROR "${CMAKE_CURRENT_LIST_FILE}: "
  89. "COMPONENT_PRIV_INCLUDEDIRS entry '${include_dir}' does not exist")
  90. endif()
  91. target_include_directories(${component} PRIVATE ${abs_dir})
  92. endforeach()
  93. if(component IN_LIST BUILD_TEST_COMPONENTS)
  94. target_link_libraries(${component} "-L${CMAKE_CURRENT_BINARY_DIR}")
  95. target_link_libraries(${component} "-Wl,--whole-archive -l${component} -Wl,--no-whole-archive")
  96. endif()
  97. endfunction()
  98. function(register_config_only_component)
  99. get_filename_component(component_dir ${CMAKE_CURRENT_LIST_FILE} DIRECTORY)
  100. get_filename_component(component ${component_dir} NAME)
  101. # No-op for now...
  102. endfunction()
  103. function(add_component_dependencies target dep dep_type)
  104. get_target_property(target_type ${target} TYPE)
  105. get_target_property(target_imported ${target} IMPORTED)
  106. if(${target_type} STREQUAL STATIC_LIBRARY OR ${target_type} STREQUAL EXECUTABLE)
  107. if(TARGET ${dep})
  108. # Add all compile options exported by dep into target
  109. target_include_directories(${target} ${dep_type}
  110. $<TARGET_PROPERTY:${dep},INTERFACE_INCLUDE_DIRECTORIES>)
  111. target_compile_definitions(${target} ${dep_type}
  112. $<TARGET_PROPERTY:${dep},INTERFACE_COMPILE_DEFINITIONS>)
  113. target_compile_options(${target} ${dep_type}
  114. $<TARGET_PROPERTY:${dep},INTERFACE_COMPILE_OPTIONS>)
  115. endif()
  116. endif()
  117. endfunction()
  118. function(components_finish_registration)
  119. # have the executable target depend on all components in the build
  120. set_target_properties(${CMAKE_PROJECT_NAME}.elf PROPERTIES INTERFACE_COMPONENT_REQUIRES "${BUILD_COMPONENTS}")
  121. spaces2list(COMPONENT_REQUIRES_COMMON)
  122. # each component should see the include directories of its requirements
  123. #
  124. # (we can't do this until all components are registered and targets exist in cmake, as we have
  125. # a circular requirements graph...)
  126. foreach(a ${BUILD_COMPONENTS})
  127. if(TARGET ${a})
  128. get_component_requirements("${a}" a_deps a_priv_deps)
  129. list(APPEND a_priv_deps ${COMPONENT_REQUIRES_COMMON})
  130. foreach(b ${a_deps})
  131. add_component_dependencies(${a} ${b} PUBLIC)
  132. endforeach()
  133. foreach(b ${a_priv_deps})
  134. add_component_dependencies(${a} ${b} PRIVATE)
  135. endforeach()
  136. get_target_property(a_type ${a} TYPE)
  137. if(${a_type} MATCHES .+_LIBRARY)
  138. list(APPEND COMPONENT_LIBRARIES ${a})
  139. endif()
  140. endif()
  141. endforeach()
  142. # Add each component library's link-time dependencies (which are otherwise ignored) to the executable
  143. # LINK_DEPENDS in order to trigger a re-link when needed (on Ninja/Makefile generators at least).
  144. # (maybe this should probably be something CMake does, but it doesn't do it...)
  145. foreach(component ${BUILD_COMPONENTS})
  146. if(TARGET ${component})
  147. get_target_property(imported ${component} IMPORTED)
  148. get_target_property(type ${component} TYPE)
  149. if(NOT imported)
  150. if(${type} STREQUAL STATIC_LIBRARY OR ${type} STREQUAL EXECUTABLE)
  151. get_target_property(link_depends "${component}" LINK_DEPENDS)
  152. if(link_depends)
  153. set_property(TARGET ${CMAKE_PROJECT_NAME}.elf APPEND PROPERTY LINK_DEPENDS "${link_depends}")
  154. endif()
  155. endif()
  156. endif()
  157. endif()
  158. endforeach()
  159. target_link_libraries(${CMAKE_PROJECT_NAME}.elf ${COMPONENT_LIBRARIES})
  160. message(STATUS "Component libraries: ${COMPONENT_LIBRARIES}")
  161. endfunction()