1 include(ExternalProject)
3 macro(kconfig_set_variables)
4 set(CONFIG_DIR ${CMAKE_BINARY_DIR}/config)
5 set_default(SDKCONFIG ${PROJECT_PATH}/sdkconfig)
6 set(SDKCONFIG_HEADER ${CONFIG_DIR}/sdkconfig.h)
7 set(SDKCONFIG_CMAKE ${CONFIG_DIR}/sdkconfig.cmake)
8 set(SDKCONFIG_JSON ${CONFIG_DIR}/sdkconfig.json)
10 set(ROOT_KCONFIG ${IDF_PATH}/Kconfig)
12 set_default(SDKCONFIG_DEFAULTS "${SDKCONFIG}.defaults")
14 # ensure all source files can include sdkconfig.h
15 include_directories("${CONFIG_DIR}")
19 # Prefer a prebuilt mconf-idf on Windows
20 find_program(WINPTY winpty)
21 find_program(MCONF mconf-idf)
23 # Fall back to the old binary which was called 'mconf' not 'mconf-idf'
25 find_program(MCONF mconf)
27 message(WARNING "Falling back to mconf binary '${MCONF}' not mconf-idf. "
28 "This is probably because an old version of IDF mconf is installed and this is fine. "
29 "However if there are config problems please check the Getting Started guide for your platform.")
34 find_program(NATIVE_GCC gcc)
37 "Windows requires a prebuilt mconf-idf for your platform "
38 "on the PATH, or an MSYS2 version of gcc on the PATH to build mconf-idf. "
39 "Consult the setup docs for ESP-IDF on Windows.")
42 set(MCONF "${WINPTY}" "${MCONF}")
47 # Use the existing Makefile to build mconf (out of tree) when needed
49 set(MCONF kconfig_bin/mconf-idf)
51 externalproject_add(mconf-idf
52 SOURCE_DIR ${IDF_PATH}/tools/kconfig
54 BINARY_DIR "kconfig_bin"
55 BUILD_COMMAND make -f ${IDF_PATH}/tools/kconfig/Makefile mconf-idf
56 BUILD_BYPRODUCTS ${MCONF}
61 file(GLOB mconf_srcfiles ${IDF_PATH}/tools/kconfig/*.c)
62 externalproject_add_stepdependencies(mconf-idf build
64 ${IDF_PATH}/tools/kconfig/Makefile
65 ${CMAKE_CURRENT_LIST_FILE})
68 set(menuconfig_depends DEPENDS mconf-idf)
72 # Find all Kconfig files for all components
73 function(kconfig_process_config)
74 file(MAKE_DIRECTORY "${CONFIG_DIR}")
76 set(kconfigs_projbuild)
78 # Find Kconfig and Kconfig.projbuild for each component as applicable
79 # if any of these change, cmake should rerun
80 foreach(dir ${BUILD_COMPONENT_PATHS} "${CMAKE_SOURCE_DIR}/main")
81 file(GLOB kconfig "${dir}/Kconfig")
83 set(kconfigs "${kconfigs} ${kconfig}")
84 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${kconfig})
86 file(GLOB kconfig ${dir}/Kconfig.projbuild)
88 set(kconfigs_projbuild "${kconfigs_projbuild} ${kconfig}")
89 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${kconfig})
93 if(EXISTS ${SDKCONFIG_DEFAULTS})
94 set(defaults_arg --defaults "${SDKCONFIG_DEFAULTS}")
97 # Set these in the parent scope, so that they can be written to project_description.json
98 set(kconfigs "${kconfigs}")
99 set(COMPONENT_KCONFIGS "${kconfigs}" PARENT_SCOPE)
100 set(COMPONENT_KCONFIGS_PROJBUILD "${kconfigs_projbuild}" PARENT_SCOPE)
102 set(confgen_basecommand
103 ${PYTHON} ${IDF_PATH}/tools/kconfig_new/confgen.py
104 --kconfig ${ROOT_KCONFIG}
105 --config ${SDKCONFIG}
107 --create-config-if-missing
108 --env "COMPONENT_KCONFIGS=${kconfigs}"
109 --env "COMPONENT_KCONFIGS_PROJBUILD=${kconfigs_projbuild}"
112 # Generate the menuconfig target (uses C-based mconf-idf tool, either prebuilt or via mconf-idf target above)
113 add_custom_target(menuconfig
114 ${menuconfig_depends}
115 # create any missing config file, with defaults if necessary
116 COMMAND ${confgen_basecommand} --output config ${SDKCONFIG}
117 COMMAND ${CMAKE_COMMAND} -E env
118 "COMPONENT_KCONFIGS=${kconfigs}"
119 "COMPONENT_KCONFIGS_PROJBUILD=${kconfigs_projbuild}"
121 "KCONFIG_CONFIG=${SDKCONFIG}"
122 ${MCONF} ${ROOT_KCONFIG}
126 # Generate configuration output via confgen.py
127 # makes sdkconfig.h and skdconfig.cmake
129 # This happens during the cmake run not during the build
130 execute_process(COMMAND ${confgen_basecommand}
131 --output header ${SDKCONFIG_HEADER}
132 --output cmake ${SDKCONFIG_CMAKE}
133 --output json ${SDKCONFIG_JSON}
134 RESULT_VARIABLE config_result)
136 message(FATAL_ERROR "Failed to run confgen.py (${confgen_basecommand}). Error ${config_result}")
139 # When sdkconfig file changes in the future, trigger a cmake run
140 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${SDKCONFIG}")
142 # Ditto if either of the generated files are missing/modified (this is a bit irritating as it means
143 # you can't edit these manually without them being regenerated, but I don't know of a better way...)
144 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${SDKCONFIG_HEADER}")
145 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${SDKCONFIG_CMAKE}")
147 # Or if the config generation tool changes
148 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${IDF_PATH}/tools/kconfig_new/confgen.py")
149 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${IDF_PATH}/tools/kconfig_new/kconfiglib.py")
151 set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
152 ADDITIONAL_MAKE_CLEAN_FILES
153 "${SDKCONFIG_HEADER}" "${SDKCONFIG_CMAKE}")