kconfig.cmake 10 KB

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