Skip to content

Clear the cached target source lists before each configure - #3540

Open
dkovyazin wants to merge 1 commit into
nanoframework:mainfrom
ledtrees:prep/cmake-clear-cached-lists
Open

dkovyazin wants to merge 1 commit into
nanoframework:mainfrom
ledtrees:prep/cmake-clear-cached-lists

Conversation

@dkovyazin

Copy link
Copy Markdown
Contributor

Description

  • New nf_clear_cached_target_lists() macro in CMake/binutils.common.cmake, which unsets the 37 cache variables that hold the per-target source and include lists.
  • It is called once in the top level CMakeLists.txt, right before the RTOS dispatch, so it runs ahead of every add_subdirectory() that fills those lists.

Motivation and Context

The lists are built with list(APPEND ...) and then stored in the cache:

list(APPEND TARGET_ESP32_IDF_COMMON_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/targetHAL.c)
...
set(TARGET_ESP32_IDF_COMMON_SOURCES ${TARGET_ESP32_IDF_COMMON_SOURCES} CACHE INTERNAL "make global")

list(APPEND) starts from the value already in the cache, so on every reconfigure of an existing build directory the list gains another full copy of itself, and — the part that actually breaks builds — a source file removed from a CMakeLists stays in the cache forever:

CMake Error at CMake/binutils.ESP32.cmake:310 (target_sources):
  Cannot find source file:
    .../targets/ESP32/_common/targetHAL_StorageOperation.cpp

Deleting that one cache entry, or the whole build directory, is the only way out today. CI never sees it because it always configures a fresh build directory; it hits developers doing incremental builds across any commit that removes a source file.

The clear is done centrally rather than per file because several trees fill one list from more than one CMakeLists — TARGET_THREADX_COMMON_SOURCES is contributed to by targets/ThreadX/_common, targets/ThreadX/ST/_common, targets/ThreadX/ST/_common/CubeMX and the board directory — so a reset at the top of each writing file would drop the earlier contributions. One reset in the parent scope, before the subdirectories are added, is correct for every tree and covers ChibiOS, ESP32, FreeRTOS/NXP, ThreadX and TI_SimpleLink alike.

TARGET_BASE_LOCATION and the other cached scalars are left alone: they are written with plain set(), not appended to, so they were never affected.

How Has This Been Tested?

On hardware I only have ESP32 parts, so the ESP32 tree is what was exercised end to end. Everything below is on ESP32_S3_QUAD, IDF 5.5.4, configured through the preset.

The bug, reproduced on a real build directory. With the call commented out, a single reconfigure of an already configured directory:

TARGET_ESP32_IDF_COMMON_SOURCES:  32 entries, 15 duplicated   (16 after the first configure)
TARGET_ESP32_IDF_NANOCLR_SOURCES: 26 entries, 12 duplicated   (13)
TARGET_ESP32_IDF_NETWORK_SOURCES:  8 entries,  3 duplicated   (4)

With the fix. The same reconfigure keeps all six ESP32 lists at their correct length with no duplicates, and a directory already poisoned by the numbers above recovers to 16/13/4 on the next configure — no need to wipe it.

The failure from the issue. Removed targetHAL_Time.cpp from targets/ESP32/_common/CMakeLists.txt and reconfigured the existing build directory: the entry is gone from TARGET_ESP32_IDF_COMMON_SOURCES (16 → 15). Putting the line back and reconfiguring again gives a list byte for byte identical to the first clean configure.

For the other RTOS trees I read through the append chains rather than building them — the reset sits in the only scope that runs before all of them, which is what makes the multi-contributor case above safe. As you said on the issue, the pipeline can take those.

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

The per-target source and include lists are built with list(APPEND) on cache
variables, so on a reconfigure of an existing build directory the append starts
from what the previous run left in the cache: every list gains another copy of
itself, and a source file removed from a CMakeLists stays in the build until
that cache entry is deleted by hand.

The lists are cleared once in the top level CMakeLists, before the target
subdirectories that fill them are added, so the trees where several
CMakeLists contribute to the same list keep working.
@coderabbitai

coderabbitai Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 234fdc92-1aa4-4f93-8235-368b9bf7a2c3

📥 Commits

Reviewing files that changed from the base of the PR and between f4d2726 and 1896ed0.

📒 Files selected for processing (2)
  • CMake/binutils.common.cmake
  • CMakeLists.txt

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed stale build configuration data persisting across reconfiguration, which could cause outdated source and include lists to be reused.

Walkthrough

The CMake build adds a macro that clears cached target source and include-directory lists. The top-level build script calls the macro during configuration before platform subdirectories repopulate those lists.

Changes

CMake cached target lists

Layer / File(s) Summary
Clear and invoke cached-list reset
CMake/binutils.common.cmake, CMakeLists.txt
The new nf_clear_cached_target_lists() macro unsets enumerated source and include-directory variables from the CMake cache. CMakeLists.txt calls it before later configuration steps.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: josesimoes

Merge Risk: ⚪ Minimal · up to 1896e

The configure clears cached target lists before platform directories repopulate them, addressing the reported reconfigure issue. No concrete remaining merge risk was established.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change and is concise. It exceeds the preferred 50-character length, but it remains descriptive and relevant.
Description check ✅ Passed The description clearly explains the cache-list duplication issue, the central reset macro, the affected build flow, and the testing performed.
Linked Issues check ✅ Passed Issue #1841 requires clearing cached per-target source and include lists before their CMakeLists repopulate them. The PR adds nf_clear_cached_target_lists() and unsets 37 relevant cache variables, i…
Out of Scope Changes check ✅ Passed The changes are limited to the cache-reset macro and its top-level call. Both changes directly support issue #1841 by resetting the lists before repopulation. No unrelated product or build behavior is…

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@josesimoes josesimoes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid solving this by maintaining a hard-coded list of cache variables.

This list is easy to get out of sync with the variables that are introduced by the target CMake files: a newly added source/include list may be omitted here, while a renamed or removed variable may leave stale cleanup code behind. In that case the original reconfigure bug would silently return for that target.

The underlying issue appears to be that transient, computed source/include lists are being stored in the CMake cache and then extended with list(APPEND) on subsequent configure runs. The cache persists by design, so this is not limited to unsupported local changes or an unusually dirty build directory. After all, reconfiguring an existing build tree after changing a source list is a normal CMake workflow.

The proposed likely fixes the current reported failure, but it adds a second manually maintained source-of-truth and leaves the underlying cache/state design unchanged.

Ideally we should be attaching sources and include directories directly to the relevant CMake target with target_sources() and target_include_directories(), but that requires a serious rewrite on the build system. Which is not ideal in the current time frame (develop branch in active development along with IFU).
I suggest we keep this in the to-do list and address it in a near future when that is more convinient.
For the time being, please be patient and midful of the issue on your local development work.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cached target source lists accumulate across reconfigures and keep removed files

3 participants