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