component.cmake 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #
  2. # Internal function for retrieving component properties from a component target.
  3. #
  4. function(__component_get_property var component_target property)
  5. get_property(val TARGET ${component_target} PROPERTY ${property})
  6. set(${var} "${val}" PARENT_SCOPE)
  7. endfunction()
  8. #
  9. # Internal function for setting component properties on a component target. As with build properties,
  10. # set properties are also keeped track of.
  11. #
  12. function(__component_set_property component_target property val)
  13. cmake_parse_arguments(_ "APPEND" "" "" ${ARGN})
  14. if(__APPEND)
  15. set_property(TARGET ${component_target} APPEND PROPERTY ${property} "${val}")
  16. else()
  17. set_property(TARGET ${component_target} PROPERTY ${property} "${val}")
  18. endif()
  19. # Keep track of set component properties
  20. __component_get_property(properties ${component_target} __COMPONENT_PROPERTIES)
  21. if(NOT property IN_LIST properties)
  22. __component_set_property(${component_target} __COMPONENT_PROPERTIES ${property} APPEND)
  23. endif()
  24. endfunction()
  25. #
  26. # Given a component name or alias, get the corresponding component target.
  27. #
  28. function(__component_get_target var name_or_alias)
  29. # Look at previously resolved names or aliases
  30. idf_build_get_property(component_names_resolved __COMPONENT_NAMES_RESOLVED)
  31. list(FIND component_names_resolved ${name_or_alias} result)
  32. if(NOT result EQUAL -1)
  33. # If it has been resolved before, return that value. The index is the same
  34. # as in __COMPONENT_NAMES_RESOLVED as these are parallel lists.
  35. idf_build_get_property(component_targets_resolved __COMPONENT_TARGETS_RESOLVED)
  36. list(GET component_targets_resolved ${result} target)
  37. set(${var} ${target} PARENT_SCOPE)
  38. return()
  39. endif()
  40. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  41. # Assume first that the paramters is an alias.
  42. string(REPLACE "::" "_" name_or_alias "${name_or_alias}")
  43. set(component_target ___${name_or_alias})
  44. if(component_target IN_LIST component_targets)
  45. set(${var} ${component_target} PARENT_SCOPE)
  46. set(target ${component_target})
  47. else() # assumption is wrong, try to look for it manually
  48. unset(target)
  49. foreach(component_target ${component_targets})
  50. __component_get_property(_component_name ${component_target} COMPONENT_NAME)
  51. if(name_or_alias STREQUAL _component_name)
  52. set(target ${component_target})
  53. break()
  54. endif()
  55. endforeach()
  56. set(${var} ${target} PARENT_SCOPE)
  57. endif()
  58. # Save the resolved name or alias
  59. if(target)
  60. idf_build_set_property(__COMPONENT_NAMES_RESOLVED ${name_or_alias} APPEND)
  61. idf_build_set_property(__COMPONENT_TARGETS_RESOLVED ${target} APPEND)
  62. endif()
  63. endfunction()
  64. #
  65. # Called during component registration, sets basic properties of the current component.
  66. #
  67. macro(__component_set_properties)
  68. __component_get_property(type ${component_target} COMPONENT_TYPE)
  69. # Fill in the rest of component property
  70. __component_set_property(${component_target} SRCS "${sources}")
  71. __component_set_property(${component_target} INCLUDE_DIRS "${__INCLUDE_DIRS}")
  72. if(type STREQUAL LIBRARY)
  73. __component_set_property(${component_target} PRIV_INCLUDE_DIRS "${__PRIV_INCLUDE_DIRS}")
  74. endif()
  75. __component_set_property(${component_target} LDFRAGMENTS "${__LDFRAGMENTS}")
  76. __component_set_property(${component_target} EMBED_FILES "${__EMBED_FILES}")
  77. __component_set_property(${component_target} EMBED_TXTFILES "${__EMBED_TXTFILES}")
  78. __component_set_property(${component_target} REQUIRED_IDF_TARGETS "${__REQUIRED_IDF_TARGETS}")
  79. endmacro()
  80. #
  81. # Perform a quick check if given component dir satisfies basic requirements.
  82. #
  83. function(__component_dir_quick_check var component_dir)
  84. set(res 1)
  85. get_filename_component(abs_dir ${component_dir} ABSOLUTE)
  86. get_filename_component(base_dir ${abs_dir} NAME)
  87. string(SUBSTRING "${base_dir}" 0 1 first_char)
  88. # Check the component directory contains a CMakeLists.txt file
  89. # - warn and skip anything which isn't valid looking (probably cruft)
  90. if(NOT first_char STREQUAL ".")
  91. if(NOT EXISTS "${abs_dir}/CMakeLists.txt")
  92. message(STATUS "Component directory ${abs_dir} does not contain a CMakeLists.txt file. "
  93. "No component will be added")
  94. set(res 0)
  95. endif()
  96. else()
  97. set(res 0) # quietly ignore dot-folders
  98. endif()
  99. set(${var} ${res} PARENT_SCOPE)
  100. endfunction()
  101. #
  102. # Write a CMake file containing all component and their properties. This is possible because each component
  103. # keeps a list of all its properties.
  104. #
  105. function(__component_write_properties output_file)
  106. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  107. foreach(component_target ${component_targets})
  108. __component_get_property(component_properties ${component_target} __COMPONENT_PROPERTIES)
  109. foreach(property ${component_properties})
  110. __component_get_property(val ${component_target} ${property})
  111. set(component_properties_text
  112. "${component_properties_text}\nset(__component_${component_target}_${property} \"${val}\")")
  113. endforeach()
  114. file(WRITE ${output_file} "${component_properties_text}")
  115. endforeach()
  116. endfunction()
  117. #
  118. # Add a component to process in the build. The components are keeped tracked of in property
  119. # __COMPONENT_TARGETS in component target form.
  120. #
  121. function(__component_add component_dir prefix)
  122. # For each component, two entities are created: a component target and a component library. The
  123. # component library is created during component registration (the actual static/interface library).
  124. # On the other hand, component targets are created early in the build
  125. # (during adding component as this function suggests).
  126. # This is so that we still have a target to attach properties to up until the component registration.
  127. # Plus, interface libraries have limitations on the types of properties that can be set on them,
  128. # so later in the build, these component targets actually contain the properties meant for the
  129. # corresponding component library.
  130. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  131. get_filename_component(abs_dir ${component_dir} ABSOLUTE)
  132. get_filename_component(base_dir ${abs_dir} NAME)
  133. if(NOT EXISTS "${abs_dir}/CMakeLists.txt")
  134. message(FATAL_ERROR "Directory '${component_dir}' does not contain a component.")
  135. endif()
  136. set(component_name ${base_dir})
  137. # The component target has three underscores as a prefix. The corresponding component library
  138. # only has two.
  139. set(component_target ___${prefix}_${component_name})
  140. # If a component of the same name has not been added before If it has been added
  141. # before just override the properties. As a side effect, components added later
  142. # 'override' components added earlier.
  143. if(NOT component_target IN_LIST component_targets)
  144. if(NOT TARGET ${component_target})
  145. add_library(${component_target} STATIC IMPORTED)
  146. endif()
  147. idf_build_set_property(__COMPONENT_TARGETS ${component_target} APPEND)
  148. else()
  149. __component_get_property(dir ${component_target} COMPONENT_DIR)
  150. __component_set_property(${component_target} COMPONENT_OVERRIDEN_DIR ${dir})
  151. endif()
  152. set(component_lib __${prefix}_${component_name})
  153. set(component_dir ${abs_dir})
  154. set(component_alias ${prefix}::${component_name}) # The 'alias' of the component library,
  155. # used to refer to the component outside
  156. # the build system. Users can use this name
  157. # to resolve ambiguity with component names
  158. # and to link IDF components to external targets.
  159. # Set the basic properties of the component
  160. __component_set_property(${component_target} COMPONENT_LIB ${component_lib})
  161. __component_set_property(${component_target} COMPONENT_NAME ${component_name})
  162. __component_set_property(${component_target} COMPONENT_DIR ${component_dir})
  163. __component_set_property(${component_target} COMPONENT_ALIAS ${component_alias})
  164. __component_set_property(${component_target} __PREFIX ${prefix})
  165. # Set Kconfig related properties on the component
  166. __kconfig_component_init(${component_target})
  167. endfunction()
  168. #
  169. # Given a component directory, get the requirements by expanding it early. The expansion is performed
  170. # using a separate CMake script (the expansion is performed in a separate instance of CMake in scripting mode).
  171. #
  172. function(__component_get_requirements)
  173. idf_build_get_property(idf_path IDF_PATH)
  174. idf_build_get_property(build_dir BUILD_DIR)
  175. set(build_properties_file ${build_dir}/build_properties.temp.cmake)
  176. set(component_properties_file ${build_dir}/component_properties.temp.cmake)
  177. set(component_requires_file ${build_dir}/component_requires.temp.cmake)
  178. __build_write_properties(${build_properties_file})
  179. __component_write_properties(${component_properties_file})
  180. execute_process(COMMAND "${CMAKE_COMMAND}"
  181. -D "ESP_PLATFORM=1"
  182. -D "BUILD_PROPERTIES_FILE=${build_properties_file}"
  183. -D "COMPONENT_PROPERTIES_FILE=${component_properties_file}"
  184. -D "COMPONENT_REQUIRES_FILE=${component_requires_file}"
  185. -P "${idf_path}/tools/cmake/scripts/component_get_requirements.cmake"
  186. RESULT_VARIABLE result
  187. ERROR_VARIABLE error)
  188. if(NOT result EQUAL 0)
  189. message(FATAL_ERROR "${error}")
  190. endif()
  191. idf_build_get_property(idf_component_manager IDF_COMPONENT_MANAGER)
  192. if(idf_component_manager AND idf_component_manager EQUAL "1")
  193. # Call for component manager once again to inject dependencies
  194. idf_build_get_property(python PYTHON)
  195. execute_process(COMMAND ${python}
  196. "-m"
  197. "idf_component_manager.prepare_components"
  198. "--project_dir=${project_dir}"
  199. "inject_requirements"
  200. "--idf_path=${idf_path}"
  201. "--build_dir=${build_dir}"
  202. "--component_requires_file=${component_requires_file}"
  203. RESULT_VARIABLE result
  204. ERROR_VARIABLE error)
  205. if(NOT result EQUAL 0)
  206. message(FATAL_ERROR "${error}")
  207. endif()
  208. endif()
  209. include(${component_requires_file})
  210. file(REMOVE ${build_properties_file})
  211. file(REMOVE ${component_properties_file})
  212. file(REMOVE ${component_requires_file})
  213. endfunction()
  214. # __component_add_sources, __component_check_target, __component_add_include_dirs
  215. #
  216. # Utility macros for component registration. Adds source files and checks target requirements,
  217. # and adds include directories respectively.
  218. macro(__component_add_sources sources)
  219. set(sources "")
  220. if(__SRCS)
  221. if(__SRC_DIRS)
  222. message(WARNING "SRCS and SRC_DIRS are both specified; ignoring SRC_DIRS.")
  223. endif()
  224. foreach(src ${__SRCS})
  225. get_filename_component(src "${src}" ABSOLUTE BASE_DIR ${COMPONENT_DIR})
  226. list(APPEND sources ${src})
  227. endforeach()
  228. else()
  229. if(__SRC_DIRS)
  230. foreach(dir ${__SRC_DIRS})
  231. get_filename_component(abs_dir ${dir} ABSOLUTE BASE_DIR ${COMPONENT_DIR})
  232. if(NOT IS_DIRECTORY ${abs_dir})
  233. message(FATAL_ERROR "SRC_DIRS entry '${dir}' does not exist.")
  234. endif()
  235. file(GLOB dir_sources "${abs_dir}/*.c" "${abs_dir}/*.cpp" "${abs_dir}/*.S")
  236. list(SORT dir_sources)
  237. if(dir_sources)
  238. foreach(src ${dir_sources})
  239. get_filename_component(src "${src}" ABSOLUTE BASE_DIR ${COMPONENT_DIR})
  240. list(APPEND sources "${src}")
  241. endforeach()
  242. else()
  243. message(WARNING "No source files found for SRC_DIRS entry '${dir}'.")
  244. endif()
  245. endforeach()
  246. endif()
  247. if(__EXCLUDE_SRCS)
  248. foreach(src ${__EXCLUDE_SRCS})
  249. get_filename_component(src "${src}" ABSOLUTE)
  250. list(REMOVE_ITEM sources "${src}")
  251. endforeach()
  252. endif()
  253. endif()
  254. list(REMOVE_DUPLICATES sources)
  255. endmacro()
  256. macro(__component_add_include_dirs lib dirs type)
  257. foreach(dir ${dirs})
  258. get_filename_component(_dir ${dir} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_LIST_DIR})
  259. if(NOT IS_DIRECTORY ${_dir})
  260. message(FATAL_ERROR "Include directory '${_dir}' is not a directory.")
  261. endif()
  262. target_include_directories(${lib} ${type} ${_dir})
  263. endforeach()
  264. endmacro()
  265. macro(__component_check_target)
  266. if(__REQUIRED_IDF_TARGETS)
  267. idf_build_get_property(idf_target IDF_TARGET)
  268. if(NOT idf_target IN_LIST __REQUIRED_IDF_TARGETS)
  269. message(FATAL_ERROR "Component ${COMPONENT_NAME} only supports targets: ${__REQUIRED_IDF_TARGETS}")
  270. endif()
  271. endif()
  272. endmacro()
  273. # __component_set_dependencies, __component_set_all_dependencies
  274. #
  275. # Links public and private requirements for the currently processed component
  276. macro(__component_set_dependencies reqs type)
  277. foreach(req ${reqs})
  278. if(req IN_LIST build_component_targets)
  279. __component_get_property(req_lib ${req} COMPONENT_LIB)
  280. if("${type}" STREQUAL "PRIVATE")
  281. set_property(TARGET ${component_lib} APPEND PROPERTY LINK_LIBRARIES ${req_lib})
  282. set_property(TARGET ${component_lib} APPEND PROPERTY INTERFACE_LINK_LIBRARIES $<LINK_ONLY:${req_lib}>)
  283. elseif("${type}" STREQUAL "PUBLIC")
  284. set_property(TARGET ${component_lib} APPEND PROPERTY LINK_LIBRARIES ${req_lib})
  285. set_property(TARGET ${component_lib} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${req_lib})
  286. else() # INTERFACE
  287. set_property(TARGET ${component_lib} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${req_lib})
  288. endif()
  289. endif()
  290. endforeach()
  291. endmacro()
  292. macro(__component_set_all_dependencies)
  293. __component_get_property(type ${component_target} COMPONENT_TYPE)
  294. idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
  295. if(NOT type STREQUAL CONFIG_ONLY)
  296. __component_get_property(reqs ${component_target} __REQUIRES)
  297. __component_set_dependencies("${reqs}" PUBLIC)
  298. __component_get_property(priv_reqs ${component_target} __PRIV_REQUIRES)
  299. __component_set_dependencies("${priv_reqs}" PRIVATE)
  300. else()
  301. __component_get_property(reqs ${component_target} __REQUIRES)
  302. __component_set_dependencies("${reqs}" INTERFACE)
  303. endif()
  304. endmacro()
  305. # idf_component_get_property
  306. #
  307. # @brief Retrieve the value of the specified component property
  308. #
  309. # @param[out] var the variable to store the value of the property in
  310. # @param[in] component the component name or alias to get the value of the property of
  311. # @param[in] property the property to get the value of
  312. #
  313. # @param[in, optional] GENERATOR_EXPRESSION (option) retrieve the generator expression for the property
  314. # instead of actual value
  315. function(idf_component_get_property var component property)
  316. cmake_parse_arguments(_ "GENERATOR_EXPRESSION" "" "" ${ARGN})
  317. __component_get_target(component_target ${component})
  318. if(__GENERATOR_EXPRESSION)
  319. set(val "$<TARGET_PROPERTY:${component_target},${property}>")
  320. else()
  321. __component_get_property(val ${component_target} ${property})
  322. endif()
  323. set(${var} "${val}" PARENT_SCOPE)
  324. endfunction()
  325. # idf_component_set_property
  326. #
  327. # @brief Set the value of the specified component property related. The property is
  328. # also added to the internal list of component properties if it isn't there already.
  329. #
  330. # @param[in] component component name or alias of the component to set the property of
  331. # @param[in] property the property to set the value of
  332. # @param[out] value value of the property to set to
  333. #
  334. # @param[in, optional] APPEND (option) append the value to the current value of the
  335. # property instead of replacing it
  336. function(idf_component_set_property component property val)
  337. cmake_parse_arguments(_ "APPEND" "" "" ${ARGN})
  338. __component_get_target(component_target ${component})
  339. if(__APPEND)
  340. __component_set_property(${component_target} ${property} "${val}" APPEND)
  341. else()
  342. __component_set_property(${component_target} ${property} "${val}")
  343. endif()
  344. endfunction()
  345. # idf_component_register
  346. #
  347. # @brief Register a component to the build, creating component library targets etc.
  348. #
  349. # @param[in, optional] SRCS (multivalue) list of source files for the component
  350. # @param[in, optional] SRC_DIRS (multivalue) list of source directories to look for source files
  351. # in (.c, .cpp. .S); ignored when SRCS is specified.
  352. # @param[in, optional] EXCLUDE_SRCS (multivalue) used to exclude source files for the specified
  353. # SRC_DIRS
  354. # @param[in, optional] INCLUDE_DIRS (multivalue) public include directories for the created component library
  355. # @param[in, optional] PRIV_INCLUDE_DIRS (multivalue) private include directories for the created component library
  356. # @param[in, optional] LDFRAGMENTS (multivalue) linker script fragments for the component
  357. # @param[in, optional] REQUIRES (multivalue) publicly required components in terms of usage requirements
  358. # @param[in, optional] PRIV_REQUIRES (multivalue) privately required components in terms of usage requirements
  359. # or components only needed for functions/values defined in its project_include.cmake
  360. # @param[in, optional] REQUIRED_IDF_TARGETS (multivalue) the list of IDF build targets that the component only supports
  361. # @param[in, optional] EMBED_FILES (multivalue) list of binary files to embed with the component
  362. # @param[in, optional] EMBED_TXTFILES (multivalue) list of text files to embed with the component
  363. # @param[in, optional] KCONFIG (single value) override the default Kconfig
  364. # @param[in, optional] KCONFIG_PROJBUILD (single value) override the default Kconfig
  365. function(idf_component_register)
  366. set(options)
  367. set(single_value KCONFIG KCONFIG_PROJBUILD)
  368. set(multi_value SRCS SRC_DIRS EXCLUDE_SRCS
  369. INCLUDE_DIRS PRIV_INCLUDE_DIRS LDFRAGMENTS REQUIRES
  370. PRIV_REQUIRES REQUIRED_IDF_TARGETS EMBED_FILES EMBED_TXTFILES)
  371. cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" ${ARGN})
  372. if(NOT __idf_component_context)
  373. message(FATAL_ERROR "Called idf_component_register from a non-component directory.")
  374. endif()
  375. __component_check_target()
  376. __component_add_sources(sources)
  377. # Add component manifest to the list of dependencies
  378. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${COMPONENT_DIR}/idf_component.yml")
  379. # Create the final target for the component. This target is the target that is
  380. # visible outside the build system.
  381. __component_get_target(component_target ${COMPONENT_ALIAS})
  382. __component_get_property(component_lib ${component_target} COMPONENT_LIB)
  383. # Use generator expression so that users can append/override flags even after call to
  384. # idf_build_process
  385. idf_build_get_property(include_directories INCLUDE_DIRECTORIES GENERATOR_EXPRESSION)
  386. idf_build_get_property(compile_options COMPILE_OPTIONS GENERATOR_EXPRESSION)
  387. idf_build_get_property(c_compile_options C_COMPILE_OPTIONS GENERATOR_EXPRESSION)
  388. idf_build_get_property(cxx_compile_options CXX_COMPILE_OPTIONS GENERATOR_EXPRESSION)
  389. idf_build_get_property(common_reqs ___COMPONENT_REQUIRES_COMMON)
  390. include_directories("${include_directories}")
  391. add_compile_options("${compile_options}")
  392. add_c_compile_options("${c_compile_options}")
  393. add_cxx_compile_options("${cxx_compile_options}")
  394. # Unfortunately add_definitions() does not support generator expressions. A new command
  395. # add_compile_definition() does but is only available on CMake 3.12 or newer. This uses
  396. # add_compile_options(), which can add any option as the workaround.
  397. #
  398. # TODO: Use add_compile_definitions() once minimum supported version is 3.12 or newer.
  399. idf_build_get_property(compile_definitions COMPILE_DEFINITIONS GENERATOR_EXPRESSION)
  400. add_compile_options("${compile_definitions}")
  401. if(common_reqs) # check whether common_reqs exists, this may be the case in minimalistic host unit test builds
  402. list(REMOVE_ITEM common_reqs ${component_lib})
  403. endif()
  404. link_libraries(${common_reqs})
  405. idf_build_get_property(config_dir CONFIG_DIR)
  406. # The contents of 'sources' is from the __component_add_sources call
  407. if(sources OR __EMBED_FILES OR __EMBED_TXTFILES)
  408. add_library(${component_lib} STATIC ${sources})
  409. __component_set_property(${component_target} COMPONENT_TYPE LIBRARY)
  410. __component_add_include_dirs(${component_lib} "${__INCLUDE_DIRS}" PUBLIC)
  411. __component_add_include_dirs(${component_lib} "${__PRIV_INCLUDE_DIRS}" PRIVATE)
  412. __component_add_include_dirs(${component_lib} "${config_dir}" PUBLIC)
  413. set_target_properties(${component_lib} PROPERTIES OUTPUT_NAME ${COMPONENT_NAME} LINKER_LANGUAGE C)
  414. __ldgen_add_component(${component_lib})
  415. else()
  416. add_library(${component_lib} INTERFACE)
  417. __component_set_property(${component_target} COMPONENT_TYPE CONFIG_ONLY)
  418. __component_add_include_dirs(${component_lib} "${__INCLUDE_DIRS}" INTERFACE)
  419. __component_add_include_dirs(${component_lib} "${config_dir}" INTERFACE)
  420. endif()
  421. # Alias the static/interface library created for linking to external targets.
  422. # The alias is the <prefix>::<component name> name.
  423. __component_get_property(component_alias ${component_target} COMPONENT_ALIAS)
  424. add_library(${component_alias} ALIAS ${component_lib})
  425. # Perform other component processing, such as embedding binaries and processing linker
  426. # script fragments
  427. foreach(file ${__EMBED_FILES})
  428. target_add_binary_data(${component_lib} "${file}" "BINARY")
  429. endforeach()
  430. foreach(file ${__EMBED_TXTFILES})
  431. target_add_binary_data(${component_lib} "${file}" "TEXT")
  432. endforeach()
  433. if(__LDFRAGMENTS)
  434. __ldgen_add_fragment_files("${__LDFRAGMENTS}")
  435. endif()
  436. # Set dependencies
  437. __component_set_all_dependencies()
  438. # Make the COMPONENT_LIB variable available in the component CMakeLists.txt
  439. set(COMPONENT_LIB ${component_lib} PARENT_SCOPE)
  440. # COMPONENT_TARGET is deprecated but is made available with same function
  441. # as COMPONENT_LIB for compatibility.
  442. set(COMPONENT_TARGET ${component_lib} PARENT_SCOPE)
  443. __component_set_properties()
  444. endfunction()
  445. # idf_component_mock
  446. #
  447. # @brief Create mock component with CMock and register it to IDF build system.
  448. #
  449. # @param[in, optional] INCLUDE_DIRS (multivalue) list include directories which belong to the header files
  450. # provided in MOCK_HEADER_FILES. If any other include directories are necessary, they need
  451. # to be passed here, too.
  452. # @param[in, optional] MOCK_HEADER_FILES (multivalue) list of header files from which the mocks shall be generated.
  453. # @param[in, optional] REQUIRES (multivalue) any other components required by the mock component.
  454. #
  455. function(idf_component_mock)
  456. set(options)
  457. set(single_value)
  458. set(multi_value MOCK_HEADER_FILES INCLUDE_DIRS REQUIRES)
  459. cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}" ${ARGN})
  460. list(APPEND __REQUIRES "cmock")
  461. set(MOCK_GENERATED_HEADERS "")
  462. set(MOCK_GENERATED_SRCS "")
  463. set(MOCK_FILES "")
  464. set(IDF_PATH $ENV{IDF_PATH})
  465. set(CMOCK_DIR "${IDF_PATH}/components/cmock/CMock")
  466. set(MOCK_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  467. list(APPEND __INCLUDE_DIRS "${MOCK_GEN_DIR}/mocks")
  468. foreach(header_file ${__MOCK_HEADER_FILES})
  469. get_filename_component(file_without_dir ${header_file} NAME_WE)
  470. list(APPEND MOCK_GENERATED_HEADERS "${MOCK_GEN_DIR}/mocks/Mock${file_without_dir}.h")
  471. list(APPEND MOCK_GENERATED_SRCS "${MOCK_GEN_DIR}/mocks/Mock${file_without_dir}.c")
  472. endforeach()
  473. file(MAKE_DIRECTORY "${MOCK_GEN_DIR}/mocks")
  474. idf_component_register(SRCS "${MOCK_GENERATED_SRCS}"
  475. INCLUDE_DIRS ${__INCLUDE_DIRS}
  476. REQUIRES ${__REQUIRES})
  477. add_custom_command(
  478. OUTPUT ruby_found SYMBOLIC
  479. COMMAND "ruby" "-v"
  480. COMMENT "Try to find ruby. If this fails, you need to install ruby"
  481. )
  482. # This command builds the mocks.
  483. # First, environment variable UNITY_DIR is set. This is necessary to prevent unity from looking in its own submodule
  484. # which doesn't work in our CI yet...
  485. # The rest is a straight forward call to cmock.rb, consult cmock's documentation for more information.
  486. add_custom_command(
  487. OUTPUT ${MOCK_GENERATED_SRCS} ${MOCK_GENERATED_HEADERS}
  488. DEPENDS ruby_found
  489. COMMAND ${CMAKE_COMMAND} -E env "UNITY_DIR=${IDF_PATH}/components/unity/unity"
  490. ruby
  491. ${CMOCK_DIR}/lib/cmock.rb
  492. -o${CMAKE_CURRENT_SOURCE_DIR}/mock/mock_config.yaml
  493. ${__MOCK_HEADER_FILES}
  494. )
  495. endfunction()
  496. #
  497. # Deprecated functions
  498. #
  499. # register_component
  500. #
  501. # Compatibility function for registering 3.xx style components.
  502. macro(register_component)
  503. spaces2list(COMPONENT_SRCS)
  504. spaces2list(COMPONENT_SRCDIRS)
  505. spaces2list(COMPONENT_ADD_INCLUDEDIRS)
  506. spaces2list(COMPONENT_PRIV_INCLUDEDIRS)
  507. spaces2list(COMPONENT_REQUIRES)
  508. spaces2list(COMPONENT_PRIV_REQUIRES)
  509. spaces2list(COMPONENT_ADD_LDFRAGMENTS)
  510. spaces2list(COMPONENT_EMBED_FILES)
  511. spaces2list(COMPONENT_EMBED_TXTFILES)
  512. spaces2list(COMPONENT_SRCEXCLUDE)
  513. idf_component_register(SRCS "${COMPONENT_SRCS}"
  514. SRC_DIRS "${COMPONENT_SRCDIRS}"
  515. INCLUDE_DIRS "${COMPONENT_ADD_INCLUDEDIRS}"
  516. PRIV_INCLUDE_DIRS "${COMPONENT_PRIV_INCLUDEDIRS}"
  517. REQUIRES "${COMPONENT_REQUIRES}"
  518. PRIV_REQUIRES "${COMPONENT_PRIV_REQUIRES}"
  519. LDFRAGMENTS "${COMPONENT_ADD_LDFRAGMENTS}"
  520. EMBED_FILES "${COMPONENT_EMBED_FILES}"
  521. EMBED_TXTFILES "${COMPONENT_EMBED_TXTFILES}"
  522. EXCLUDE_SRCS "${COMPONENT_SRCEXCLUDE}")
  523. endmacro()
  524. # require_idf_targets
  525. #
  526. # Compatibility function for requiring IDF build targets for 3.xx style components.
  527. function(require_idf_targets)
  528. set(__REQUIRED_IDF_TARGETS "${ARGN}")
  529. __component_check_target()
  530. endfunction()
  531. # register_config_only_component
  532. #
  533. # Compatibility function for registering 3.xx style config components.
  534. macro(register_config_only_component)
  535. register_component()
  536. endmacro()
  537. # component_compile_options
  538. #
  539. # Wrapper around target_compile_options that passes the component name
  540. function(component_compile_options)
  541. target_compile_options(${COMPONENT_LIB} PRIVATE ${ARGV})
  542. endfunction()
  543. # component_compile_definitions
  544. #
  545. # Wrapper around target_compile_definitions that passes the component name
  546. function(component_compile_definitions)
  547. target_compile_definitions(${COMPONENT_LIB} PRIVATE ${ARGV})
  548. endfunction()