cmake: Improve nuttx_generate_kconfig()
Build Documentation / build-html (push) Has been cancelled

Enhance the nuttx_generate_kconfig() implementation to ensure that
both subdirectories and external directories containing a CMakeLists.txt
or a Kconfig file are properly included during Kconfig generation.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
This commit is contained in:
wangchengdong
2025-12-01 20:09:22 +08:00
committed by Xiang Xiao
parent 6f4d610636
commit a2773f267e
+37 -19
View File
@@ -122,59 +122,77 @@ function(nuttx_generate_kconfig)
REQUIRED REQUIRED
ARGN ARGN
${ARGN}) ${ARGN})
# Exit early if: - The current directory does NOT contain a CMakeLists.txt
# (not an app dir), OR - The output Kconfig already exists in the apps binary
# directory
if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt" if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt"
OR EXISTS "${NUTTX_APPS_BINDIR}/Kconfig") OR EXISTS "${NUTTX_APPS_BINDIR}/Kconfig")
return() return()
endif() endif()
# Determine output Kconfig file path
set(KCONFIG_OUTPUT_FILE) set(KCONFIG_OUTPUT_FILE)
if(MENUDESC) if(MENUDESC)
string(REPLACE "/" "_" KCONFIG_PREFIX ${CMAKE_CURRENT_LIST_DIR}) string(REPLACE "/" "_" KCONFIG_PREFIX ${CMAKE_CURRENT_LIST_DIR})
if(WIN32) if(WIN32)
# On Windows, also replace drive letter separators like "C:".
string(REPLACE ":" "_" KCONFIG_PREFIX ${KCONFIG_PREFIX}) string(REPLACE ":" "_" KCONFIG_PREFIX ${KCONFIG_PREFIX})
endif() endif()
# Output Kconfig file path: <apps_bindir>/<prefix>_Kconfig
string(APPEND KCONFIG_OUTPUT_FILE ${NUTTX_APPS_BINDIR} "/" string(APPEND KCONFIG_OUTPUT_FILE ${NUTTX_APPS_BINDIR} "/"
${KCONFIG_PREFIX} "_Kconfig") ${KCONFIG_PREFIX} "_Kconfig")
# Start a menu block
file(WRITE ${KCONFIG_OUTPUT_FILE} "menu \"${MENUDESC}\"\n") file(WRITE ${KCONFIG_OUTPUT_FILE} "menu \"${MENUDESC}\"\n")
else() else()
# Without MENUDESC, generate the root Kconfig file.
set(KCONFIG_OUTPUT_FILE ${NUTTX_APPS_BINDIR}/Kconfig) set(KCONFIG_OUTPUT_FILE ${NUTTX_APPS_BINDIR}/Kconfig)
endif() endif()
file( # Collect valid directories that contain Kconfig or CMakeLists
GLOB SUB_CMAKESCRIPTS set(DIR_LIST)
LIST_DIRECTORIES false foreach(external_dir ${EXTERNAL_DIRECTORIES})
${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/*/CMakeLists.txt) get_filename_component(external_reldir "${external_dir}" REALPATH)
if(IS_DIRECTORY ${external_reldir})
list(APPEND DIR_LIST "${external_reldir}")
endif()
endforeach()
if(NOT MENUDESC) # Get all entries directly under the current app directory
set(EXTERNAL_CMAKESCRIPTS) file(GLOB FIRST_LEVEL_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/*")
foreach(external_dir ${EXTERNAL_DIRECTORIES})
if(EXISTS ${external_dir}/CMakeLists.txt) # Add directories that contain either CMakeLists.txt or Kconfig
list(APPEND EXTERNAL_CMAKESCRIPTS "${external_dir}/CMakeLists.txt") foreach(dir IN LISTS FIRST_LEVEL_DIRS)
if(IS_DIRECTORY "${dir}")
if(EXISTS "${dir}/CMakeLists.txt" OR EXISTS "${dir}/Kconfig")
list(APPEND DIR_LIST "${dir}")
endif() endif()
endforeach() endif()
endif() endforeach()
# we need to recursively generate the Kconfig menus of multi-level # Generate "source" entries for each discovered Kconfig
# directories. foreach(dir ${DIR_LIST})
# set(SUB_KCONFIG "${dir}/Kconfig")
# when generating a Kconfig file for the current directory, it should include
# and invoke all the Kconfig files gathered from its subdirectories.
foreach(SUB_CMAKESCRIPT ${SUB_CMAKESCRIPTS} ${EXTERNAL_CMAKESCRIPTS})
string(REPLACE "CMakeLists.txt" "Kconfig" SUB_KCONFIG ${SUB_CMAKESCRIPT})
string(REPLACE "/" "_" MENUCONFIG ${SUB_KCONFIG}) string(REPLACE "/" "_" MENUCONFIG ${SUB_KCONFIG})
if(WIN32) if(WIN32)
string(REPLACE ":" "_" MENUCONFIG ${MENUCONFIG}) string(REPLACE ":" "_" MENUCONFIG ${MENUCONFIG})
endif() endif()
# check whether the subdirectory will include a generated Kconfig file.
# Source Kconfig from the corresponding apps binary directory if present
if(EXISTS ${NUTTX_APPS_BINDIR}/${MENUCONFIG}) if(EXISTS ${NUTTX_APPS_BINDIR}/${MENUCONFIG})
file(APPEND ${KCONFIG_OUTPUT_FILE} file(APPEND ${KCONFIG_OUTPUT_FILE}
"source \"${NUTTX_APPS_BINDIR}/${MENUCONFIG}\"\n") "source \"${NUTTX_APPS_BINDIR}/${MENUCONFIG}\"\n")
endif() endif()
# Source Kconfig from the directory if present
if(EXISTS ${SUB_KCONFIG}) if(EXISTS ${SUB_KCONFIG})
file(APPEND ${KCONFIG_OUTPUT_FILE} "source \"${SUB_KCONFIG}\"\n") file(APPEND ${KCONFIG_OUTPUT_FILE} "source \"${SUB_KCONFIG}\"\n")
endif() endif()
endforeach() endforeach()
# Close the menu block if MENUDESC was used
if(MENUDESC) if(MENUDESC)
file(APPEND ${KCONFIG_OUTPUT_FILE} "endmenu # ${MENUDESC}\n") file(APPEND ${KCONFIG_OUTPUT_FILE} "endmenu # ${MENUDESC}\n")
endif() endif()