py/usermod: Route usermod sources through native port build.#18
Draft
andrewleech wants to merge 1 commit intoreview/usermod-native-source-registrationfrom
Draft
py/usermod: Route usermod sources through native port build.#18andrewleech wants to merge 1 commit intoreview/usermod-native-source-registrationfrom
andrewleech wants to merge 1 commit intoreview/usermod-native-source-registrationfrom
Conversation
User C module sources on cmake-based ports were compiled via INTERFACE library linking rather than through the native port build system. This meant usermod sources missed platform compile flags -- on ESP32, user C modules were compiled without optimisation flags. Add MICROPY_SOURCE_USERMOD to the native source registration in both esp32 (idf_component_register SRCS) and rp2 (target_sources). Clear INTERFACE_SOURCES from usermod targets after gathering them into the flat list to prevent double compilation. The existing target_link_libraries for the usermod INTERFACE library is kept for propagating non-source properties (compile definitions, compile options, link libraries). Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
User C module sources on cmake-based ports are compiled via INTERFACE library linking rather than through the port's native source registration. This means usermod sources miss platform compile flags that are applied at the directory or component level. On ESP32, user C modules are compiled without optimisation flags across all architectures because ESP-IDF applies flags via
add_compile_options()duringidf_component_register(), which doesn't propagate to INTERFACE library sources. On rp2 it works by accident becauseCMAKE_BUILD_TYPE=MinSizeRelsets flags via the truly globalCMAKE_C_FLAGS.This adds
MICROPY_SOURCE_USERMOD(the flat source list already gathered byusermod_gather_sources()) to each port's native source registration —idf_component_register(SRCS)on esp32 andtarget_sources()on rp2 — the same way every other source domain (py, extmod, shared, lib, drivers, port, board) already works.INTERFACE_SOURCESare cleared from usermod targets after gathering to prevent double compilation. The existingtarget_link_libraries(${MICROPY_TARGET} usermod)is kept for propagating non-source INTERFACE properties (compile definitions, compile options, link libraries).No user-facing API change. Existing
micropython.cmakemodule files work as-is.This is the root cause of several existing issues:
INTERFACE_SOURCESafter gathering into the flat list.target_compile_definitions(... INTERFACE ...)now reach usermod sources because they compile as part of the main target which links the INTERFACE library.CMAKE_BUILD_EARLY_EXPANSION, which is a separate mechanism from source compilation.Testing
Built ESP32_GENERIC (IDF v5.5.1) and RPI_PICO with
USER_C_MODULESpointing toexamples/usercmodule/micropython.cmake. Verified from build logs:-O2, matching core sources. Previously they had no optimisation flag.USER_C_MODULESalso succeeds (emptyMICROPY_SOURCE_USERMODis harmless).Not tested: zephyr port (doesn't currently support user C modules).
Trade-offs and Alternatives
usermod_gather_sources()is now a destructive operation — it clearsINTERFACE_SOURCESfrom each target as a side effect, making it non-idempotent. This is acceptable because it's called exactly once (line 63 ofpy/usermod.cmake) and the alternative of conditionally skipping the INTERFACE link or adding per-port source deduplication would be more invasive.An alternative would be to inject platform flags into the usermod INTERFACE library (e.g.
idf_build_get_property+target_compile_options(usermod INTERFACE ...)), but that's a workaround rather than fixing the architectural mismatch.Generative AI
I used generative AI tools when creating this PR, but a human has checked the code and is responsible for the description above.