@@ -122,59 +122,77 @@ function(nuttx_generate_kconfig)
122122 REQUIRED
123123 ARGN
124124 ${ARGN} )
125+
126+ # Exit early if: - The current directory does NOT contain a CMakeLists.txt
127+ # (not an app dir), OR - The output Kconfig already exists in the apps binary
128+ # directory
125129 if (NOT EXISTS "${CMAKE_CURRENT_LIST_DIR} /CMakeLists.txt"
126130 OR EXISTS "${NUTTX_APPS_BINDIR} /Kconfig" )
127131 return ()
128132 endif ()
129133
134+ # Determine output Kconfig file path
130135 set (KCONFIG_OUTPUT_FILE)
131136 if (MENUDESC)
132137 string (REPLACE "/" "_" KCONFIG_PREFIX ${CMAKE_CURRENT_LIST_DIR} )
133138 if (WIN32 )
139+ # On Windows, also replace drive letter separators like "C:".
134140 string (REPLACE ":" "_" KCONFIG_PREFIX ${KCONFIG_PREFIX} )
135141 endif ()
142+
143+ # Output Kconfig file path: <apps_bindir>/<prefix>_Kconfig
136144 string (APPEND KCONFIG_OUTPUT_FILE ${NUTTX_APPS_BINDIR} "/"
137145 ${KCONFIG_PREFIX} "_Kconfig" )
146+
147+ # Start a menu block
138148 file (WRITE ${KCONFIG_OUTPUT_FILE} "menu \" ${MENUDESC} \"\n " )
139149 else ()
150+ # Without MENUDESC, generate the root Kconfig file.
140151 set (KCONFIG_OUTPUT_FILE ${NUTTX_APPS_BINDIR} /Kconfig)
141152 endif ()
142153
143- file (
144- GLOB SUB_CMAKESCRIPTS
145- LIST_DIRECTORIES false
146- ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR} /*/CMakeLists.txt)
154+ # Collect valid directories that contain Kconfig or CMakeLists
155+ set (DIR_LIST)
156+ foreach (external_dir ${EXTERNAL_DIRECTORIES} )
157+ get_filename_component (external_reldir "${external_dir} " REALPATH)
158+ if (IS_DIRECTORY ${external_reldir} )
159+ list (APPEND DIR_LIST "${external_reldir} " )
160+ endif ()
161+ endforeach ()
162+
163+ # Get all entries directly under the current app directory
164+ file (GLOB FIRST_LEVEL_DIRS "${CMAKE_CURRENT_SOURCE_DIR} /*" )
147165
148- if ( NOT MENUDESC)
149- set (EXTERNAL_CMAKESCRIPTS )
150- foreach (external_dir ${EXTERNAL_DIRECTORIES} )
151- if (EXISTS ${external_dir } /CMakeLists.txt)
152- list (APPEND EXTERNAL_CMAKESCRIPTS "${external_dir} /CMakeLists.txt " )
166+ # Add directories that contain either CMakeLists.txt or Kconfig
167+ foreach (dir IN LISTS FIRST_LEVEL_DIRS )
168+ if ( IS_DIRECTORY " ${dir} " )
169+ if (EXISTS " ${dir } /CMakeLists.txt" OR EXISTS " ${dir} /Kconfig" )
170+ list (APPEND DIR_LIST "${dir} " )
153171 endif ()
154- endforeach ()
155- endif ()
172+ endif ()
173+ endforeach ()
156174
157- # we need to recursively generate the Kconfig menus of multi-level
158- # directories.
159- #
160- # when generating a Kconfig file for the current directory, it should include
161- # and invoke all the Kconfig files gathered from its subdirectories.
162- foreach (SUB_CMAKESCRIPT ${SUB_CMAKESCRIPTS} ${EXTERNAL_CMAKESCRIPTS} )
163- string (REPLACE "CMakeLists.txt" "Kconfig" SUB_KCONFIG ${SUB_CMAKESCRIPT} )
175+ # Generate "source" entries for each discovered Kconfig
176+ foreach (dir ${DIR_LIST} )
177+ set (SUB_KCONFIG "${dir} /Kconfig" )
164178 string (REPLACE "/" "_" MENUCONFIG ${SUB_KCONFIG} )
165179 if (WIN32 )
166180 string (REPLACE ":" "_" MENUCONFIG ${MENUCONFIG} )
167181 endif ()
168- # check whether the subdirectory will include a generated Kconfig file.
182+
183+ # Source Kconfig from the corresponding apps binary directory if present
169184 if (EXISTS ${NUTTX_APPS_BINDIR} /${MENUCONFIG} )
170185 file (APPEND ${KCONFIG_OUTPUT_FILE}
171186 "source \" ${NUTTX_APPS_BINDIR} /${MENUCONFIG} \"\n " )
172187 endif ()
188+
189+ # Source Kconfig from the directory if present
173190 if (EXISTS ${SUB_KCONFIG} )
174191 file (APPEND ${KCONFIG_OUTPUT_FILE} "source \" ${SUB_KCONFIG} \"\n " )
175192 endif ()
176193 endforeach ()
177194
195+ # Close the menu block if MENUDESC was used
178196 if (MENUDESC)
179197 file (APPEND ${KCONFIG_OUTPUT_FILE} "endmenu # ${MENUDESC} \n " )
180198 endif ()
0 commit comments