kconfig.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. include(ExternalProject)
  2. function(__kconfig_init)
  3. if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "FreeBSD")
  4. set(MAKE_COMMMAND "gmake")
  5. else()
  6. set(MAKE_COMMMAND "make")
  7. endif()
  8. idf_build_get_property(idf_path IDF_PATH)
  9. if(CMAKE_HOST_WIN32 AND DEFINED ENV{MSYSTEM})
  10. # Prefer a prebuilt mconf-idf on Windows
  11. find_program(WINPTY winpty)
  12. unset(MCONF CACHE) # needed when MSYS and CMD is intermixed (cache could contain an incompatible path)
  13. find_program(MCONF mconf-idf)
  14. # Fall back to the old binary which was called 'mconf' not 'mconf-idf'
  15. if(NOT MCONF)
  16. find_program(MCONF mconf)
  17. if(MCONF)
  18. message(WARNING "Falling back to mconf binary '${MCONF}' not mconf-idf. "
  19. "This is probably because an old version of IDF mconf is installed and this is fine. "
  20. "However if there are config problems please check the Getting Started guide for your platform.")
  21. endif()
  22. endif()
  23. if(NOT MCONF)
  24. find_program(NATIVE_GCC gcc)
  25. if(NOT NATIVE_GCC)
  26. message(FATAL_ERROR
  27. "Windows requires an MSYS2 version of gcc on the PATH to build mconf-idf. "
  28. "Consult the setup docs for ESP-IDF on Windows.")
  29. else()
  30. # Use the existing Makefile to build mconf (out of tree) when needed
  31. #
  32. set(MCONF ${CMAKE_BINARY_DIR}/kconfig_bin/mconf-idf)
  33. set(src_path ${idf_path}/tools/kconfig)
  34. # note: we preemptively remove any build files from the src dir
  35. # as we're building out of tree, but don't want build system to
  36. # #include any from there that were previously build with/for make
  37. externalproject_add(mconf-idf
  38. SOURCE_DIR ${src_path}
  39. CONFIGURE_COMMAND ""
  40. BINARY_DIR "${CMAKE_BINARY_DIR}/kconfig_bin"
  41. BUILD_COMMAND rm -f ${src_path}/zconf.lex.c ${src_path}/zconf.hash.c
  42. COMMAND ${MAKE_COMMMAND} -f ${src_path}/Makefile mconf-idf
  43. BUILD_BYPRODUCTS ${MCONF}
  44. INSTALL_COMMAND ""
  45. EXCLUDE_FROM_ALL 1
  46. )
  47. file(GLOB mconf_srcfiles ${src_path}/*.c)
  48. list(REMOVE_ITEM mconf_srcfiles "${src_path}/zconf.lex.c" "${src_path}/zconf.hash.c")
  49. externalproject_add_stepdependencies(mconf-idf build
  50. ${mconf_srcfiles}
  51. ${src_path}/Makefile
  52. ${CMAKE_CURRENT_LIST_FILE})
  53. unset(mconf_srcfiles)
  54. unset(src_path)
  55. set(menuconfig_depends DEPENDS mconf-idf)
  56. endif()
  57. else()
  58. execute_process(COMMAND "${MCONF}" -v
  59. RESULT_VARIABLE mconf_res
  60. OUTPUT_VARIABLE mconf_out
  61. ERROR_VARIABLE mconf_err)
  62. if(${mconf_res})
  63. message(WARNING "Failed to detect version of mconf-idf. Return code was ${mconf_res}.")
  64. else()
  65. string(STRIP "${mconf_out}" mconf_out)
  66. set(mconf_expected_ver "mconf-v4.6.0.0-idf-20190628-win32")
  67. if(NOT ${mconf_out} STREQUAL "mconf-idf version ${mconf_expected_ver}")
  68. message(WARNING "Unexpected ${mconf_out}. Expected ${mconf_expected_ver}. "
  69. "Please check the ESP-IDF Getting Started guide for version "
  70. "${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}.${IDF_VERSION_PATCH} "
  71. "to correct this issue")
  72. else()
  73. message(STATUS "${mconf_out}") # prints: mconf-idf version ....
  74. endif()
  75. endif()
  76. if(WINPTY)
  77. set(MCONF "\"${WINPTY}\" \"${MCONF}\"")
  78. endif()
  79. endif()
  80. idf_build_set_property(__MCONF ${MCONF})
  81. idf_build_set_property(__MENUCONFIG_DEPENDS "${menuconfig_depends}")
  82. endif()
  83. idf_build_get_property(idf_path IDF_PATH)
  84. idf_build_set_property(__ROOT_KCONFIG ${idf_path}/Kconfig)
  85. idf_build_set_property(__ROOT_SDKCONFIG_RENAME ${idf_path}/sdkconfig.rename)
  86. idf_build_set_property(__OUTPUT_SDKCONFIG 1)
  87. endfunction()
  88. #
  89. # Initialize Kconfig-related properties for components.
  90. # This function assumes that all basic properties of the components have been
  91. # set prior to calling it.
  92. #
  93. function(__kconfig_component_init component_target)
  94. __component_get_property(component_dir ${component_target} COMPONENT_DIR)
  95. file(GLOB kconfig "${component_dir}/Kconfig")
  96. __component_set_property(${component_target} KCONFIG "${kconfig}")
  97. file(GLOB kconfig "${component_dir}/Kconfig.projbuild")
  98. __component_set_property(${component_target} KCONFIG_PROJBUILD "${kconfig}")
  99. file(GLOB sdkconfig_rename "${component_dir}/sdkconfig.rename")
  100. __component_set_property(${component_target} SDKCONFIG_RENAME "${sdkconfig_rename}")
  101. endfunction()
  102. #
  103. # Generate the config files and create config related targets and configure
  104. # dependencies.
  105. #
  106. function(__kconfig_generate_config sdkconfig sdkconfig_defaults)
  107. # List all Kconfig and Kconfig.projbuild in known components
  108. idf_build_get_property(component_targets __COMPONENT_TARGETS)
  109. idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
  110. foreach(component_target ${component_targets})
  111. if(component_target IN_LIST build_component_targets)
  112. __component_get_property(kconfig ${component_target} KCONFIG)
  113. __component_get_property(kconfig_projbuild ${component_target} KCONFIG_PROJBUILD)
  114. __component_get_property(sdkconfig_rename ${component_target} SDKCONFIG_RENAME)
  115. if(kconfig)
  116. list(APPEND kconfigs ${kconfig})
  117. endif()
  118. if(kconfig_projbuild)
  119. list(APPEND kconfig_projbuilds ${kconfig_projbuild})
  120. endif()
  121. if(sdkconfig_rename)
  122. list(APPEND sdkconfig_renames ${sdkconfig_rename})
  123. endif()
  124. endif()
  125. endforeach()
  126. # Store the list version of kconfigs and kconfig_projbuilds
  127. idf_build_set_property(KCONFIGS "${kconfigs}")
  128. idf_build_set_property(KCONFIG_PROJBUILDS "${kconfig_projbuilds}")
  129. idf_build_get_property(idf_target IDF_TARGET)
  130. idf_build_get_property(idf_path IDF_PATH)
  131. string(REPLACE ";" " " kconfigs "${kconfigs}")
  132. string(REPLACE ";" " " kconfig_projbuilds "${kconfig_projbuilds}")
  133. string(REPLACE ";" " " sdkconfig_renames "${sdkconfig_renames}")
  134. # These are the paths for files which will contain the generated "source" lines for COMPONENT_KCONFIGS and
  135. # COMPONENT_KCONFIGS_PROJBUILD
  136. set(kconfigs_projbuild_path "${CMAKE_CURRENT_BINARY_DIR}/kconfigs_projbuild.in")
  137. set(kconfigs_path "${CMAKE_CURRENT_BINARY_DIR}/kconfigs.in")
  138. # Place config-related environment arguments into config.env file
  139. # to work around command line length limits for execute_process
  140. # on Windows & CMake < 3.11
  141. set(config_env_path "${CMAKE_CURRENT_BINARY_DIR}/config.env")
  142. configure_file("${idf_path}/tools/kconfig_new/config.env.in" ${config_env_path})
  143. idf_build_set_property(CONFIG_ENV_PATH ${config_env_path})
  144. if(sdkconfig_defaults)
  145. foreach(sdkconfig_default ${sdkconfig_defaults})
  146. list(APPEND defaults_arg --defaults "${sdkconfig_default}")
  147. endforeach()
  148. endif()
  149. if(sdkconfig_defaults)
  150. foreach(sdkconfig_default ${sdkconfig_defaults})
  151. if(EXISTS "${sdkconfig_default}.${idf_target}")
  152. list(APPEND defaults_arg --defaults "${sdkconfig_default}.${idf_target}")
  153. endif()
  154. endforeach()
  155. endif()
  156. idf_build_get_property(root_kconfig __ROOT_KCONFIG)
  157. idf_build_get_property(root_sdkconfig_rename __ROOT_SDKCONFIG_RENAME)
  158. idf_build_get_property(python PYTHON)
  159. set(prepare_kconfig_files_command
  160. ${python} ${idf_path}/tools/kconfig_new/prepare_kconfig_files.py
  161. --env-file ${config_env_path})
  162. set(confgen_basecommand
  163. ${python} ${idf_path}/tools/kconfig_new/confgen.py
  164. --kconfig ${root_kconfig}
  165. --sdkconfig-rename ${root_sdkconfig_rename}
  166. --config ${sdkconfig}
  167. ${defaults_arg}
  168. --env-file ${config_env_path})
  169. idf_build_get_property(build_dir BUILD_DIR)
  170. set(config_dir ${build_dir}/config)
  171. file(MAKE_DIRECTORY "${config_dir}")
  172. message(STATUS "Project sdkconfig file ${sdkconfig}")
  173. # Generate the config outputs
  174. set(sdkconfig_cmake ${config_dir}/sdkconfig.cmake)
  175. set(sdkconfig_header ${config_dir}/sdkconfig.h)
  176. set(sdkconfig_json ${config_dir}/sdkconfig.json)
  177. set(sdkconfig_json_menus ${config_dir}/kconfig_menus.json)
  178. idf_build_get_property(output_sdkconfig __OUTPUT_SDKCONFIG)
  179. if(output_sdkconfig)
  180. execute_process(
  181. COMMAND ${prepare_kconfig_files_command})
  182. execute_process(
  183. COMMAND ${confgen_basecommand}
  184. --output header ${sdkconfig_header}
  185. --output cmake ${sdkconfig_cmake}
  186. --output json ${sdkconfig_json}
  187. --output json_menus ${sdkconfig_json_menus}
  188. --output config ${sdkconfig}
  189. RESULT_VARIABLE config_result)
  190. else()
  191. execute_process(
  192. COMMAND ${prepare_kconfig_files_command})
  193. execute_process(
  194. COMMAND ${confgen_basecommand}
  195. --output header ${sdkconfig_header}
  196. --output cmake ${sdkconfig_cmake}
  197. --output json ${sdkconfig_json}
  198. --output json_menus ${sdkconfig_json_menus}
  199. RESULT_VARIABLE config_result)
  200. endif()
  201. if(config_result)
  202. message(FATAL_ERROR "Failed to run confgen.py (${confgen_basecommand}). Error ${config_result}")
  203. endif()
  204. # Add the generated config header to build specifications.
  205. idf_build_set_property(INCLUDE_DIRECTORIES ${config_dir} APPEND)
  206. # When sdkconfig file changes in the future, trigger a cmake run
  207. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${sdkconfig}")
  208. # Ditto if either of the generated files are missing/modified (this is a bit irritating as it means
  209. # you can't edit these manually without them being regenerated, but I don't know of a better way...)
  210. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${sdkconfig_header}")
  211. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${sdkconfig_cmake}")
  212. # Or if the config generation tool changes
  213. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${idf_path}/tools/kconfig_new/confgen.py")
  214. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${idf_path}/tools/kconfig_new/kconfiglib.py")
  215. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  216. ADDITIONAL_MAKE_CLEAN_FILES "${sdkconfig_header}" "${sdkconfig_cmake}")
  217. idf_build_set_property(SDKCONFIG_HEADER ${sdkconfig_header})
  218. idf_build_set_property(SDKCONFIG_JSON ${sdkconfig_json})
  219. idf_build_set_property(SDKCONFIG_CMAKE ${sdkconfig_cmake})
  220. idf_build_set_property(SDKCONFIG_JSON_MENUS ${sdkconfig_json_menus})
  221. idf_build_set_property(CONFIG_DIR ${config_dir})
  222. if(CMAKE_HOST_WIN32 AND DEFINED ENV{MSYSTEM})
  223. idf_build_get_property(menuconfig_depends __MENUCONFIG_DEPENDS)
  224. idf_build_get_property(mconf __MCONF)
  225. set(MENUCONFIG_CMD ${mconf})
  226. else()
  227. set(MENUCONFIG_CMD ${python} ${idf_path}/tools/kconfig_new/menuconfig.py)
  228. set(TERM_CHECK_CMD ${python} ${idf_path}/tools/check_term.py)
  229. endif()
  230. # Generate the menuconfig target
  231. add_custom_target(menuconfig
  232. ${menuconfig_depends}
  233. # create any missing config file, with defaults if necessary
  234. COMMAND ${prepare_kconfig_files_command}
  235. COMMAND ${confgen_basecommand}
  236. --env "IDF_TARGET=${idf_target}"
  237. --dont-write-deprecated
  238. --output config ${sdkconfig}
  239. COMMAND ${TERM_CHECK_CMD}
  240. COMMAND ${CMAKE_COMMAND} -E env
  241. "COMPONENT_KCONFIGS_SOURCE_FILE=${kconfigs_path}"
  242. "COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE=${kconfigs_projbuild_path}"
  243. "IDF_CMAKE=y"
  244. "KCONFIG_CONFIG=${sdkconfig}"
  245. "IDF_TARGET=${idf_target}"
  246. ${MENUCONFIG_CMD} ${root_kconfig}
  247. # VERBATIM cannot be used here because it cannot handle ${mconf}="winpty mconf-idf" and the escaping must be
  248. # done manually
  249. USES_TERMINAL
  250. # additional run of confgen esures that the deprecated options will be inserted into sdkconfig (for backward
  251. # compatibility)
  252. COMMAND ${confgen_basecommand} --env "IDF_TARGET=${idf_target}" --output config ${sdkconfig}
  253. )
  254. # Custom target to run confserver.py from the build tool
  255. add_custom_target(confserver
  256. COMMAND ${prepare_kconfig_files_command}
  257. COMMAND ${PYTHON} ${IDF_PATH}/tools/kconfig_new/confserver.py
  258. --env-file ${config_env_path}
  259. --kconfig ${IDF_PATH}/Kconfig
  260. --sdkconfig-rename ${root_sdkconfig_rename}
  261. --config ${sdkconfig}
  262. VERBATIM
  263. USES_TERMINAL)
  264. endfunction()