diff --git a/cmake/nuttx_add_application.cmake b/cmake/nuttx_add_application.cmake index 058b00bf2b0..d8f72d2ddfd 100644 --- a/cmake/nuttx_add_application.cmake +++ b/cmake/nuttx_add_application.cmake @@ -123,6 +123,15 @@ function(nuttx_add_application) # create as standalone executable (loadable application or "module") set(TARGET "${NAME}") + # determine the compiled elf mode + if(CONFIG_BUILD_KERNEL) + set(KERNEL_ELF_MODE True) # kernel elf will link all user libs + elseif("${MODULE}" STREQUAL "m") + set(LOADABLE_ELF_MODE True) # loadable elf only link extra libs + elseif("${DYNLIB}" STREQUAL "y") + set(DYNLIB_ELF_MODE True) # dynlib elf dont need start obj and other lib + endif() + # Use ELF capable toolchain, by building manually and overwriting the # non-elf output if(NOT CMAKE_C_ELF_COMPILER) @@ -137,12 +146,20 @@ function(nuttx_add_application) TARGET ${TARGET} POST_BUILD COMMAND - ${CMAKE_LD} - $,$,$> - ${LINK_FLAGS} -T ${CMAKE_BINARY_DIR}/gnu-elf.ld - $<$,$>:$> + # add default link option + ${CMAKE_LD} -T ${CMAKE_BINARY_DIR}/gnu-elf.ld + # add global MOD link option if dynlib link + $<$:$> + # add global ELF link option if m&kernel link + $<$,$>:$> + # add local link option last + ${LINK_FLAGS} + # link startup obj if m&kernel link + $<$,$>>:$> $<$>:-Wl,>--start-group - $> + # link user lib if kernel link + $<$:$>> + # always link extra libs $> $<$>:-Wl,>--whole-archive $ @@ -181,7 +198,7 @@ function(nuttx_add_application) ) endif() - if("${DYNLIB}" STREQUAL "y") + if(DYNLIB_ELF_MODE) target_compile_options( ${TARGET} PRIVATE