Improvements to external component/module support.

Based on feedback from @tomsci - thanks!
This commit is contained in:
Johny Mattsson 2022-12-29 14:16:28 +11:00
parent 4c510a5a69
commit ccb3b5002d
3 changed files with 43 additions and 43 deletions

View File

@ -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 <confname>_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()

View File

@ -1,37 +1,20 @@
# Modify this list as necessary to include all your source files. # Update source files and includes as necessary
set(extmod_srcs
"mymod.c"
)
# If necessary, add items to the PRIV_REQUIRES list below
idf_component_register( idf_component_register(
SRCS ${extmod_srcs} SRCS
INCLUDE_DIRS "." "${CMAKE_CURRENT_BINARY_DIR}" "mymod.c"
PRIV_INCLUDE_DIRS
"${CMAKE_CURRENT_BINARY_DIR}"
PRIV_REQUIRES PRIV_REQUIRES
"base_nodemcu" "base_nodemcu"
"lua" "lua"
"platform" "platform"
) )
# The remainder is boiler-plate glue to get the linker to actually include # To register the module with NodeMCU, use one of the below functions.
# the modules enabled in Kconfig. No user-serviceable parts inside. # The name given MUST match the first argument to the NODEMCU_MODULE() in
# the module's C file.
# Match up all the extmod source files with their corresponding Kconfig #
# option in the form NODEMCU_CMODULE_<modname> and if enabled, add a # For modules with a Kconfig option, use:
# "-u <modname>_module_selected1" option to force the linker to include extmod_register_conditional(MYMOD)
# the module. See components/core/include/module.h for further details on # ...and for modules without a Kconfig option, instead use:
# how this works. #extmod_register_unconditional(MYMOD)
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()

View File

@ -1,13 +1,8 @@
menu "External modules" config NODEMCU_CMODULE_MYMOD
bool "External NodeMCU module: mymod"
config NODEMCU_CMODULE_MYMOD default "y"
bool "Mymod module" help
default "y" Includes the mymod module. This module is only an example for
help showing how to use external modules. Note that the config option
Includes the mymod module. This module is only an example for name has to be prefixed with NODEMCU_CMODULE_ and the module
showing how to use external modules. Note that the config option registered with extmod_register_conditional(MYMOD) in CMakeLists.txt
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