pico-sdk
pico-sdk copied to clipboard
Title: pico_sdk_import.cmake triggers infinite recursion if pico_sdk_init() is called within it
Description: While working on a project targeting the RP2350 with Pico SDK v2.0.0, I encountered a critical issue stemming from an internal modification to pico_sdk_import.cmake. I had added the following lines at the bottom of pico_sdk_import.cmake to automatically invoke SDK initialization: pico_sdk_init() message(STATUS "✅ pico_sdk_init() was explicitly invoked from pico_sdk_import.cmake")
However, this causes infinite recursion in the CMake configuration step, resulting in: CMake Error at GNU-ASM.cmake:2 (include): Maximum recursion depth of 1000 exceeded
Upon investigation, the root cause appears to be that pico_sdk_init() calls add_subdirectory(${PICO_SDK_PATH} pico-sdk), which in turn triggers pico-sdk/CMakeLists.txt, which contains: include(pico_sdk_import.cmake)
This leads to repeated inclusion of the SDK, creating an infinite loop. Why this matters: Although calling pico_sdk_init() from within the import script might seem convenient, this pattern violates the SDK's internal assumptions and breaks builds in a non-obvious way. Even advanced users may be tripped up due to the misleading logs—especially since the recursion depth limit is only hit after many steps. Suggestion: Either:
- Consider explicitly documenting that pico_sdk_init() should only be called from the user's CMakeLists.txt, never from pico_sdk_import.cmake.
- Or defensively guard against recursion via a variable check (if(NOT PICO_SDK_ALREADY_INITIALIZED) etc). Environment:
- Pico SDK version: v2.0.0
- Platform: RP2350 (PICO_PLATFORM=rp2350-arm-s)
- CMake version: 3.28.3
- Host: WSL2 on Ubuntu
- GCC: arm-none-eabi-gcc 13.2.1 Thanks for all your work on the SDK. Let me know if you’d like a minimal reproduction repo—I’m happy to provide one. Let me know if you'd like a more technical tone, or if you'd prefer to submit this as a question instead of a bug report. I'm here to polish it however you need.