]> granicus.if.org Git - esp-idf/blob - tools/cmake/kconfig.cmake
cmake: Fix COMPONENT_SRCEXCLUDE functionality
[esp-idf] / tools / cmake / kconfig.cmake
1 include(ExternalProject)
2
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)
9
10     set(ROOT_KCONFIG ${IDF_PATH}/Kconfig)
11
12     set_default(SDKCONFIG_DEFAULTS "${SDKCONFIG}.defaults")
13
14     # ensure all source files can include sdkconfig.h
15     include_directories("${CONFIG_DIR}")
16 endmacro()
17
18 if(CMAKE_HOST_WIN32)
19     # Prefer a prebuilt mconf-idf on Windows
20     find_program(WINPTY winpty)
21     find_program(MCONF mconf-idf)
22
23     # Fall back to the old binary which was called 'mconf' not 'mconf-idf'
24     if(NOT MCONF)
25         find_program(MCONF mconf)
26         if(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.")
30         endif()
31     endif()
32
33     if(NOT MCONF)
34         find_program(NATIVE_GCC gcc)
35         if(NOT NATIVE_GCC)
36             message(FATAL_ERROR
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.")
40         endif()
41     elseif(WINPTY)
42         set(MCONF "${WINPTY}" "${MCONF}")
43     endif()
44 endif()
45
46 if(NOT MCONF)
47     # Use the existing Makefile to build mconf (out of tree) when needed
48     #
49     set(MCONF kconfig_bin/mconf-idf)
50
51     externalproject_add(mconf-idf
52         SOURCE_DIR ${IDF_PATH}/tools/kconfig
53         CONFIGURE_COMMAND ""
54         BINARY_DIR "kconfig_bin"
55         BUILD_COMMAND make -f ${IDF_PATH}/tools/kconfig/Makefile mconf-idf
56         BUILD_BYPRODUCTS ${MCONF}
57         INSTALL_COMMAND ""
58         EXCLUDE_FROM_ALL 1
59         )
60
61     file(GLOB mconf_srcfiles ${IDF_PATH}/tools/kconfig/*.c)
62     externalproject_add_stepdependencies(mconf-idf build
63         ${mconf_srcfiles}
64         ${IDF_PATH}/tools/kconfig/Makefile
65         ${CMAKE_CURRENT_LIST_FILE})
66     unset(mconf_srcfiles)
67
68     set(menuconfig_depends DEPENDS mconf-idf)
69
70 endif()
71
72 # Find all Kconfig files for all components
73 function(kconfig_process_config)
74     file(MAKE_DIRECTORY "${CONFIG_DIR}")
75     set(kconfigs)
76     set(kconfigs_projbuild)
77
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")
82         if(kconfig)
83             set(kconfigs "${kconfigs} ${kconfig}")
84             set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${kconfig})
85         endif()
86         file(GLOB kconfig ${dir}/Kconfig.projbuild)
87         if(kconfig)
88             set(kconfigs_projbuild "${kconfigs_projbuild} ${kconfig}")
89             set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${kconfig})
90         endif()
91     endforeach()
92
93     if(EXISTS ${SDKCONFIG_DEFAULTS})
94         set(defaults_arg --defaults "${SDKCONFIG_DEFAULTS}")
95     endif()
96
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)
101
102     set(confgen_basecommand
103         ${PYTHON} ${IDF_PATH}/tools/kconfig_new/confgen.py
104         --kconfig ${ROOT_KCONFIG}
105         --config ${SDKCONFIG}
106         ${defaults_arg}
107         --create-config-if-missing
108         --env "COMPONENT_KCONFIGS=${kconfigs}"
109         --env "COMPONENT_KCONFIGS_PROJBUILD=${kconfigs_projbuild}"
110         --env "IDF_CMAKE=y")
111
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}"
120         "IDF_CMAKE=y"
121         "KCONFIG_CONFIG=${SDKCONFIG}"
122         ${MCONF} ${ROOT_KCONFIG}
123         VERBATIM
124         USES_TERMINAL)
125
126     # Generate configuration output via confgen.py
127     # makes sdkconfig.h and skdconfig.cmake
128     #
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)
135     if(config_result)
136         message(FATAL_ERROR "Failed to run confgen.py (${confgen_basecommand}). Error ${config_result}")
137     endif()
138
139     # When sdkconfig file changes in the future, trigger a cmake run
140     set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${SDKCONFIG}")
141
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}")
146
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")
150
151     set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
152         ADDITIONAL_MAKE_CLEAN_FILES
153         "${SDKCONFIG_HEADER}" "${SDKCONFIG_CMAKE}")
154
155 endfunction()