diff --git a/Makefile b/Makefile index 28b51698c25..571def9a33d 100644 --- a/Makefile +++ b/Makefile @@ -625,6 +625,9 @@ validate_module_configs: clean: @[ ! -d "$(SRC_DIR)/build" ] || find "$(SRC_DIR)/build" -mindepth 1 -maxdepth 1 -type d -exec sh -c "echo {}; cmake --build {} -- clean || rm -rf {}" \; # use generated build system to clean, wipe build directory if it fails @git submodule foreach git clean -dX --force # some submodules generate build artifacts in source + @# Remove orphaned autogenerated Kconfig left by app dirs deleted in a NuttX apps upgrade + @# (untracked, so they survive 'git clean -dX' and break 'make olddefconfig'). + @cmake -P "$(SRC_DIR)/platforms/nuttx/NuttX/tools/prune_stale_apps_kconfig.cmake" submodulesclean: @git submodule foreach --quiet --recursive git clean -ff -x -d diff --git a/platforms/nuttx/NuttX/tools/prune_stale_apps_kconfig.cmake b/platforms/nuttx/NuttX/tools/prune_stale_apps_kconfig.cmake new file mode 100644 index 00000000000..7a65639fb66 --- /dev/null +++ b/platforms/nuttx/NuttX/tools/prune_stale_apps_kconfig.cmake @@ -0,0 +1,96 @@ +############################################################################ +# +# prune_stale_apps_kconfig.cmake +# +# When the NuttX "apps" submodule is upgraded (e.g. a kernel version bump or +# switching branches followed by `git submodule update`), git updates tracked +# files only. If the new revision *removes* an application directory, the +# autogenerated `Kconfig` that a previous build left inside it is untracked and +# therefore survives `git clean -dX` (and a plain checkout). That orphaned +# directory keeps getting picked up by apps/tools/mkkconfig.sh (which globs +# `*/Kconfig`), and its stale `Kconfig` still `source`s files that no longer +# exist, breaking `make olddefconfig`: +# +# olddefconfig: .../apps/gpsutils/Kconfig:9: '.../gpsutils/minmea/Kconfig' +# not found ... +# +# This helper removes exactly those stale leftovers: *untracked* `Kconfig` +# files under the apps tree that `source` a path which no longer exists. It +# never touches tracked files and never touches the nuttx tree, whose stale +# artifacts are all gitignored and already removed by `git clean -dX`. +# +# Usage: cmake -P prune_stale_apps_kconfig.cmake +# +############################################################################ + +get_filename_component(APPS_DIR "${CMAKE_CURRENT_LIST_DIR}/../apps" ABSOLUTE) + +if(NOT IS_DIRECTORY "${APPS_DIR}") + return() +endif() + +find_program(GIT_EXECUTABLE git) +if(NOT GIT_EXECUTABLE) + return() +endif() + +# Only operate inside a real git working tree (skip tarball exports). +execute_process( + COMMAND ${GIT_EXECUTABLE} -C "${APPS_DIR}" rev-parse --is-inside-work-tree + RESULT_VARIABLE _in_worktree + OUTPUT_QUIET ERROR_QUIET +) +if(NOT _in_worktree EQUAL 0) + return() +endif() + +# Iterate until stable: removing a child Kconfig can make its parent stale too. +set(_changed TRUE) +while(_changed) + set(_changed FALSE) + + # Matches files named exactly "Kconfig" at every depth, including the root. + file(GLOB_RECURSE _kconfigs "${APPS_DIR}/Kconfig") + + foreach(_kf ${_kconfigs}) + if(NOT EXISTS "${_kf}") + continue() + endif() + + # Never remove tracked Kconfig files - only build-generated orphans. + execute_process( + COMMAND ${GIT_EXECUTABLE} -C "${APPS_DIR}" ls-files --error-unmatch "${_kf}" + RESULT_VARIABLE _tracked + OUTPUT_QUIET ERROR_QUIET + ) + if(_tracked EQUAL 0) + continue() + endif() + + file(READ "${_kf}" _content) + string(REGEX MATCHALL "source[ \t]+\"[^\"]+\"" _sources "${_content}") + + set(_stale FALSE) + foreach(_entry ${_sources}) + string(REGEX REPLACE ".*\"([^\"]+)\".*" "\\1" _path "${_entry}") + if(NOT IS_ABSOLUTE "${_path}") + set(_path "${APPS_DIR}/${_path}") + endif() + if(NOT EXISTS "${_path}") + set(_stale TRUE) + break() + endif() + endforeach() + + if(_stale) + message(STATUS "pruning stale apps Kconfig: ${_kf}") + file(REMOVE "${_kf}") + set(_changed TRUE) + endif() + endforeach() +endwhile() + +# Note: a now-empty orphan directory (e.g. apps/gpsutils/) is harmless - git +# does not track empty directories and mkkconfig.sh only globs `*/Kconfig`, so +# an empty directory is never sourced. It is left in place to keep this script +# free of recursive directory deletion.