From ccb3b5002d74ff1551e35009e476f8f657ac3f50 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Thu, 29 Dec 2022 14:16:28 +1100 Subject: [PATCH] Improvements to external component/module support. Based on feedback from @tomsci - thanks! --- components/modules/project_include.cmake | 22 ++++++++++++ extcomp-template/CMakeLists.txt | 43 +++++++----------------- extcomp-template/Kconfig | 21 +++++------- 3 files changed, 43 insertions(+), 43 deletions(-) create mode 100644 components/modules/project_include.cmake diff --git a/components/modules/project_include.cmake b/components/modules/project_include.cmake new file mode 100644 index 00000000..46c87de8 --- /dev/null +++ b/components/modules/project_include.cmake @@ -0,0 +1,22 @@ +# Helper functions for external module registration. +# - extmod_register_conditional() for modules with a Kconfig option +# - extmod_register_unconditional() for always-enabled modules + +function(extmod_register_conditional confname) + if (${CONFIG_NODEMCU_CMODULE_${confname}}) + # If the module is enabled in menuconfig, add the linker option + # "-u _module_selected1" to make the linker include this + # module. See components/core/include/module.h for further details + # on how this works. + message("Including external module ${confname}") + target_link_libraries(${COMPONENT_LIB} "-u ${confname}_module_selected1") + endif() +endfunction() + +function(extmod_register_unconditional confname) + message("Including external module ${confname}") + # The module macros rely on the presence of a CONFIG_NODEMCU_CMODULE_XXX + # def, so we have to add it explicitly as it won't be coming from Kconfig + target_compile_options(${COMPONENT_LIB} PRIVATE "-DCONFIG_NODEMCU_CMODULE_${confname}") + target_link_libraries(${COMPONENT_LIB} "-u ${confname}_module_selected1") +endfunction() diff --git a/extcomp-template/CMakeLists.txt b/extcomp-template/CMakeLists.txt index 118e730f..846e58fb 100644 --- a/extcomp-template/CMakeLists.txt +++ b/extcomp-template/CMakeLists.txt @@ -1,37 +1,20 @@ -# Modify this list as necessary to include all your source files. -set(extmod_srcs - "mymod.c" -) - -# If necessary, add items to the PRIV_REQUIRES list below +# Update source files and includes as necessary idf_component_register( - SRCS ${extmod_srcs} - INCLUDE_DIRS "." "${CMAKE_CURRENT_BINARY_DIR}" + SRCS + "mymod.c" + PRIV_INCLUDE_DIRS + "${CMAKE_CURRENT_BINARY_DIR}" PRIV_REQUIRES "base_nodemcu" "lua" "platform" ) -# The remainder is boiler-plate glue to get the linker to actually include -# the modules enabled in Kconfig. No user-serviceable parts inside. - -# Match up all the extmod source files with their corresponding Kconfig -# option in the form NODEMCU_CMODULE_ and if enabled, add a -# "-u _module_selected1" option to force the linker to include -# the module. See components/core/include/module.h for further details on -# how this works. -set(extmods_enabled) -foreach(extmod_src ${extmod_srcs}) - string(REPLACE ".c" "" module_name ${extmod_src}) - string(TOUPPER ${module_name} module_ucase) - set(mod_opt "CONFIG_NODEMCU_CMODULE_${module_ucase}") - if (${${mod_opt}}) - list(APPEND extmods_enabled ${module_ucase}) - endif() -endforeach() -message("Including the following modules: ${extmods_enabled}") - -foreach(mod ${extmods_enabled}) - target_link_libraries(${COMPONENT_LIB} "-u ${mod}_module_selected1") -endforeach() +# To register the module with NodeMCU, use one of the below functions. +# The name given MUST match the first argument to the NODEMCU_MODULE() in +# the module's C file. +# +# For modules with a Kconfig option, use: +extmod_register_conditional(MYMOD) +# ...and for modules without a Kconfig option, instead use: +#extmod_register_unconditional(MYMOD) diff --git a/extcomp-template/Kconfig b/extcomp-template/Kconfig index 87bdda3a..7415c4a7 100644 --- a/extcomp-template/Kconfig +++ b/extcomp-template/Kconfig @@ -1,13 +1,8 @@ -menu "External modules" - - config NODEMCU_CMODULE_MYMOD - bool "Mymod module" - default "y" - help - Includes the mymod module. This module is only an example for - showing how to use external modules. Note that the config option - name has to be prefixed with NODEMCU_CMODULE_ and the suffix - has to match the first argument in the NODEMCU_MODULE() macro - in the .c file. - -endmenu +config NODEMCU_CMODULE_MYMOD + bool "External NodeMCU module: mymod" + default "y" + help + Includes the mymod module. This module is only an example for + showing how to use external modules. Note that the config option + name has to be prefixed with NODEMCU_CMODULE_ and the module + registered with extmod_register_conditional(MYMOD) in CMakeLists.txt