kconfig.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. include(ExternalProject)
  2. function(__kconfig_init)
  3. idf_build_get_property(idf_path IDF_PATH)
  4. idf_build_set_property(__ROOT_KCONFIG ${idf_path}/Kconfig)
  5. idf_build_set_property(__ROOT_SDKCONFIG_RENAME ${idf_path}/sdkconfig.rename)
  6. idf_build_set_property(__OUTPUT_SDKCONFIG 1)
  7. endfunction()
  8. #
  9. # Initialize Kconfig-related properties for components.
  10. # This function assumes that all basic properties of the components have been
  11. # set prior to calling it.
  12. #
  13. function(__kconfig_component_init component_target)
  14. __component_get_property(component_dir ${component_target} COMPONENT_DIR)
  15. file(GLOB kconfig "${component_dir}/Kconfig")
  16. list(SORT kconfig)
  17. __component_set_property(${component_target} KCONFIG "${kconfig}")
  18. file(GLOB kconfig "${component_dir}/Kconfig.projbuild")
  19. list(SORT kconfig)
  20. __component_set_property(${component_target} KCONFIG_PROJBUILD "${kconfig}")
  21. file(GLOB sdkconfig_rename "${component_dir}/sdkconfig.rename")
  22. file(GLOB sdkconfig_rename_target "${component_dir}/sdkconfig.rename.${IDF_TARGET}")
  23. list(APPEND sdkconfig_rename ${sdkconfig_rename_target})
  24. list(SORT sdkconfig_rename)
  25. __component_set_property(${component_target} SDKCONFIG_RENAME "${sdkconfig_rename}")
  26. endfunction()
  27. #
  28. # Add bootloader components Kconfig and Kconfig.projbuild files to BOOTLOADER_KCONFIG
  29. # and BOOTLOADER_KCONFIGS_PROJ properties respectively.
  30. #
  31. function(__kconfig_bootloader_component_add component_dir)
  32. idf_build_get_property(bootloader_kconfigs BOOTLOADER_KCONFIGS)
  33. idf_build_get_property(bootloader_kconfigs_proj BOOTLOADER_KCONFIGS_PROJ)
  34. file(GLOB kconfig "${component_dir}/Kconfig")
  35. list(SORT kconfig)
  36. if(EXISTS "${kconfig}" AND NOT IS_DIRECTORY "${kconfig}")
  37. list(APPEND bootloader_kconfigs "${kconfig}")
  38. endif()
  39. file(GLOB kconfig "${component_dir}/Kconfig.projbuild")
  40. list(SORT kconfig)
  41. if(EXISTS "${kconfig}" AND NOT IS_DIRECTORY "${kconfig}")
  42. list(APPEND bootloader_kconfigs_proj "${kconfig}")
  43. endif()
  44. idf_build_set_property(BOOTLOADER_KCONFIGS "${bootloader_kconfigs}")
  45. idf_build_set_property(BOOTLOADER_KCONFIGS_PROJ "${bootloader_kconfigs_proj}")
  46. endfunction()
  47. #
  48. # Generate the config files and create config related targets and configure
  49. # dependencies.
  50. #
  51. function(__kconfig_generate_config sdkconfig sdkconfig_defaults)
  52. # List all Kconfig and Kconfig.projbuild in known components
  53. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  54. idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
  55. foreach(component_target ${component_targets})
  56. if(component_target IN_LIST build_component_targets)
  57. __component_get_property(kconfig ${component_target} KCONFIG)
  58. __component_get_property(kconfig_projbuild ${component_target} KCONFIG_PROJBUILD)
  59. __component_get_property(sdkconfig_rename ${component_target} SDKCONFIG_RENAME)
  60. if(kconfig)
  61. list(APPEND kconfigs ${kconfig})
  62. endif()
  63. if(kconfig_projbuild)
  64. list(APPEND kconfig_projbuilds ${kconfig_projbuild})
  65. endif()
  66. if(sdkconfig_rename)
  67. list(APPEND sdkconfig_renames ${sdkconfig_rename})
  68. endif()
  69. endif()
  70. endforeach()
  71. # Take into account bootloader components configuration files
  72. idf_build_get_property(bootloader_kconfigs BOOTLOADER_KCONFIGS)
  73. idf_build_get_property(bootloader_kconfigs_proj BOOTLOADER_KCONFIGS_PROJ)
  74. if(bootloader_kconfigs)
  75. list(APPEND kconfigs "${bootloader_kconfigs}")
  76. endif()
  77. if(bootloader_kconfigs_proj)
  78. list(APPEND kconfig_projbuilds "${bootloader_kconfigs_proj}")
  79. endif()
  80. # Store the list version of kconfigs and kconfig_projbuilds
  81. idf_build_set_property(KCONFIGS "${kconfigs}")
  82. idf_build_set_property(KCONFIG_PROJBUILDS "${kconfig_projbuilds}")
  83. idf_build_get_property(idf_target IDF_TARGET)
  84. idf_build_get_property(idf_path IDF_PATH)
  85. idf_build_get_property(idf_env_fpga __IDF_ENV_FPGA)
  86. # These are the paths for files which will contain the generated "source" lines for COMPONENT_KCONFIGS and
  87. # COMPONENT_KCONFIGS_PROJBUILD
  88. set(kconfigs_projbuild_path "${CMAKE_CURRENT_BINARY_DIR}/kconfigs_projbuild.in")
  89. set(kconfigs_path "${CMAKE_CURRENT_BINARY_DIR}/kconfigs.in")
  90. # Place config-related environment arguments into config.env file
  91. # to work around command line length limits for execute_process
  92. # on Windows & CMake < 3.11
  93. set(config_env_path "${CMAKE_CURRENT_BINARY_DIR}/config.env")
  94. configure_file("${idf_path}/tools/kconfig_new/config.env.in" ${config_env_path})
  95. idf_build_set_property(CONFIG_ENV_PATH ${config_env_path})
  96. if(sdkconfig_defaults)
  97. foreach(sdkconfig_default ${sdkconfig_defaults})
  98. list(APPEND defaults_arg --defaults "${sdkconfig_default}")
  99. if(EXISTS "${sdkconfig_default}.${idf_target}")
  100. list(APPEND defaults_arg --defaults "${sdkconfig_default}.${idf_target}")
  101. endif()
  102. endforeach()
  103. endif()
  104. idf_build_get_property(root_kconfig __ROOT_KCONFIG)
  105. idf_build_get_property(root_sdkconfig_rename __ROOT_SDKCONFIG_RENAME)
  106. idf_build_get_property(python PYTHON)
  107. set(prepare_kconfig_files_command
  108. ${python} ${idf_path}/tools/kconfig_new/prepare_kconfig_files.py
  109. --list-separator=semicolon
  110. --env-file ${config_env_path})
  111. set(confgen_basecommand
  112. ${python} ${idf_path}/tools/kconfig_new/confgen.py
  113. --list-separator=semicolon
  114. --kconfig ${root_kconfig}
  115. --sdkconfig-rename ${root_sdkconfig_rename}
  116. --config ${sdkconfig}
  117. ${defaults_arg}
  118. --env-file ${config_env_path})
  119. idf_build_get_property(build_dir BUILD_DIR)
  120. set(config_dir ${build_dir}/config)
  121. file(MAKE_DIRECTORY "${config_dir}")
  122. message(STATUS "Project sdkconfig file ${sdkconfig}")
  123. # Generate the config outputs
  124. set(sdkconfig_cmake ${config_dir}/sdkconfig.cmake)
  125. set(sdkconfig_header ${config_dir}/sdkconfig.h)
  126. set(sdkconfig_json ${config_dir}/sdkconfig.json)
  127. set(sdkconfig_json_menus ${config_dir}/kconfig_menus.json)
  128. idf_build_get_property(output_sdkconfig __OUTPUT_SDKCONFIG)
  129. if(output_sdkconfig)
  130. execute_process(
  131. COMMAND ${prepare_kconfig_files_command})
  132. execute_process(
  133. COMMAND ${confgen_basecommand}
  134. --output header ${sdkconfig_header}
  135. --output cmake ${sdkconfig_cmake}
  136. --output json ${sdkconfig_json}
  137. --output json_menus ${sdkconfig_json_menus}
  138. --output config ${sdkconfig}
  139. RESULT_VARIABLE config_result)
  140. else()
  141. execute_process(
  142. COMMAND ${prepare_kconfig_files_command})
  143. execute_process(
  144. COMMAND ${confgen_basecommand}
  145. --output header ${sdkconfig_header}
  146. --output cmake ${sdkconfig_cmake}
  147. --output json ${sdkconfig_json}
  148. --output json_menus ${sdkconfig_json_menus}
  149. RESULT_VARIABLE config_result)
  150. endif()
  151. if(config_result)
  152. message(FATAL_ERROR "Failed to run confgen.py (${confgen_basecommand}). Error ${config_result}")
  153. endif()
  154. # Add the generated config header to build specifications.
  155. idf_build_set_property(INCLUDE_DIRECTORIES ${config_dir} APPEND)
  156. # When sdkconfig file changes in the future, trigger a cmake run
  157. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${sdkconfig}")
  158. # Ditto if either of the generated files are missing/modified (this is a bit irritating as it means
  159. # you can't edit these manually without them being regenerated, but I don't know of a better way...)
  160. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${sdkconfig_header}")
  161. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${sdkconfig_cmake}")
  162. # Or if the config generation tool changes
  163. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${idf_path}/tools/kconfig_new/confgen.py")
  164. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  165. ADDITIONAL_MAKE_CLEAN_FILES "${sdkconfig_header}" "${sdkconfig_cmake}")
  166. idf_build_set_property(SDKCONFIG_HEADER ${sdkconfig_header})
  167. idf_build_set_property(SDKCONFIG_JSON ${sdkconfig_json})
  168. idf_build_set_property(SDKCONFIG_CMAKE ${sdkconfig_cmake})
  169. idf_build_set_property(SDKCONFIG_JSON_MENUS ${sdkconfig_json_menus})
  170. idf_build_set_property(CONFIG_DIR ${config_dir})
  171. set(MENUCONFIG_CMD ${python} -m menuconfig)
  172. set(TERM_CHECK_CMD ${python} ${idf_path}/tools/check_term.py)
  173. # Generate the menuconfig target
  174. add_custom_target(menuconfig
  175. ${menuconfig_depends}
  176. # create any missing config file, with defaults if necessary
  177. COMMAND ${prepare_kconfig_files_command}
  178. COMMAND ${confgen_basecommand}
  179. --env "IDF_TARGET=${idf_target}"
  180. --env "IDF_ENV_FPGA=${idf_env_fpga}"
  181. --dont-write-deprecated
  182. --output config ${sdkconfig}
  183. COMMAND ${TERM_CHECK_CMD}
  184. COMMAND ${CMAKE_COMMAND} -E env
  185. "COMPONENT_KCONFIGS_SOURCE_FILE=${kconfigs_path}"
  186. "COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE=${kconfigs_projbuild_path}"
  187. "KCONFIG_CONFIG=${sdkconfig}"
  188. "IDF_TARGET=${idf_target}"
  189. "IDF_ENV_FPGA=${idf_env_fpga}"
  190. ${MENUCONFIG_CMD} ${root_kconfig}
  191. USES_TERMINAL
  192. # additional run of confgen esures that the deprecated options will be inserted into sdkconfig (for backward
  193. # compatibility)
  194. COMMAND ${confgen_basecommand}
  195. --env "IDF_TARGET=${idf_target}"
  196. --env "IDF_ENV_FPGA=${idf_env_fpga}"
  197. --output config ${sdkconfig}
  198. )
  199. # Custom target to run confserver.py from the build tool
  200. add_custom_target(confserver
  201. COMMAND ${prepare_kconfig_files_command}
  202. COMMAND ${PYTHON} ${IDF_PATH}/tools/kconfig_new/confserver.py
  203. --env-file ${config_env_path}
  204. --kconfig ${IDF_PATH}/Kconfig
  205. --sdkconfig-rename ${root_sdkconfig_rename}
  206. --config ${sdkconfig}
  207. VERBATIM
  208. USES_TERMINAL)
  209. add_custom_target(save-defconfig
  210. COMMAND ${prepare_kconfig_files_command}
  211. COMMAND ${confgen_basecommand}
  212. --dont-write-deprecated
  213. --output savedefconfig ${CMAKE_SOURCE_DIR}/sdkconfig.defaults
  214. USES_TERMINAL
  215. )
  216. endfunction()