diff --git a/arch/arm/src/cmake/elf.cmake b/arch/arm/src/cmake/elf.cmake index c199c9a6fc2..57deeba4de3 100644 --- a/arch/arm/src/cmake/elf.cmake +++ b/arch/arm/src/cmake/elf.cmake @@ -22,6 +22,8 @@ nuttx_elf_compile_options(-fvisibility=hidden -mlong-calls) +nuttx_mod_compile_options(-fvisibility=hidden -mlong-calls) + nuttx_elf_compile_options_ifdef(CONFIG_UNWINDER_ARM -fno-unwind-tables -fno-asynchronous-unwind-tables) @@ -32,6 +34,8 @@ nuttx_elf_link_options_ifdef( nuttx_elf_link_options_ifdef(CONFIG_BINFMT_ELF_RELOCATABLE -r) +nuttx_mod_link_options(-r) + nuttx_elf_link_options_ifdef(CONFIG_BUILD_KERNEL -Bstatic) nuttx_elf_link_options_ifdef(CONFIG_DEBUG_OPT_UNUSED_SECTIONS --gc-sections) diff --git a/arch/arm64/src/cmake/elf.cmake b/arch/arm64/src/cmake/elf.cmake index d0ce0a6db85..2e54432f5dc 100644 --- a/arch/arm64/src/cmake/elf.cmake +++ b/arch/arm64/src/cmake/elf.cmake @@ -22,8 +22,12 @@ nuttx_elf_compile_options(-fvisibility=hidden) +nuttx_mod_compile_options(-fvisibility=hidden) + nuttx_elf_link_options_ifdef(CONFIG_BINFMT_ELF_RELOCATABLE -r) +nuttx_mod_link_options(-r) + nuttx_elf_link_options_ifdef(CONFIG_BUILD_KERNEL -Bstatic) nuttx_elf_link_options_ifdef(CONFIG_DEBUG_OPT_UNUSED_SECTIONS --gc-sections) diff --git a/arch/x86_64/src/cmake/elf.cmake b/arch/x86_64/src/cmake/elf.cmake index a5bcaa5e9f5..302688f9e19 100644 --- a/arch/x86_64/src/cmake/elf.cmake +++ b/arch/x86_64/src/cmake/elf.cmake @@ -22,8 +22,12 @@ nuttx_elf_compile_options(-fvisibility=hidden -fno-pic -mcmodel=large) +nuttx_mod_compile_options(-fvisibility=hidden) + nuttx_elf_link_options(-r) +nuttx_mod_link_options(-r) + nuttx_elf_link_options_ifdef(CONFIG_DEBUG_OPT_UNUSED_SECTIONS --gc-sections) nuttx_elf_link_options(-e __start) diff --git a/cmake/nuttx_add_application.cmake b/cmake/nuttx_add_application.cmake index 1fd342bbfa0..058b00bf2b0 100644 --- a/cmake/nuttx_add_application.cmake +++ b/cmake/nuttx_add_application.cmake @@ -65,6 +65,8 @@ endif() # CONFIG_ value) # SRCS : source files # NO_MAIN_ALIAS : do not add a main=_main alias(*) +# DYNLIB : if "y", build as dynamic loadable library +# LINK_FLAGS : link flags only for elf or loadable link # # (*) This is only really needed in convoluted cases where a single .c file # contains differently named _main() entries for different . This @@ -92,8 +94,10 @@ function(nuttx_add_application) PRIORITY STACKSIZE MODULE + DYNLIB MULTI_VALUE COMPILE_FLAGS + LINK_FLAGS INCLUDE_DIRECTORIES SRCS DEPENDS @@ -114,7 +118,8 @@ function(nuttx_add_application) if(SRCS_EXIST) if(MODULE AND ("${MODULE}" STREQUAL "m") - OR CONFIG_BUILD_KERNEL) + OR CONFIG_BUILD_KERNEL + OR "${DYNLIB}" STREQUAL "y") # create as standalone executable (loadable application or "module") set(TARGET "${NAME}") @@ -125,18 +130,24 @@ function(nuttx_add_application) set(TARGET "ELF_${TARGET}") add_library(${TARGET} ${SRCS}) add_dependencies(${TARGET} apps_post) + if(NOT "${CMAKE_LD}" MATCHES "gcc$") + set(USE_LINKER True) + endif() add_custom_command( TARGET ${TARGET} POST_BUILD COMMAND ${CMAKE_LD} - $> - -T ${CMAKE_BINARY_DIR}/gnu-elf.ld - $<$:$> - --start-group - $<$:$>> + $,$,$> + ${LINK_FLAGS} -T ${CMAKE_BINARY_DIR}/gnu-elf.ld + $<$,$>:$> + $<$>:-Wl,>--start-group + $> $> - $ --end-group -o + $<$>:-Wl,>--whole-archive + $ + $<$>:-Wl,>--no-whole-archive + $<$>:-Wl,>--end-group -o ${CMAKE_BINARY_DIR}/bin_debug/${ELF_NAME} COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/bin_debug/${ELF_NAME} @@ -170,6 +181,14 @@ function(nuttx_add_application) ) endif() + if("${DYNLIB}" STREQUAL "y") + target_compile_options( + ${TARGET} + PRIVATE + $> + ) + endif() + install(TARGETS ${TARGET}) set_property( TARGET nuttx diff --git a/cmake/nuttx_extensions.cmake b/cmake/nuttx_extensions.cmake index 9b6807a56a9..4bb8314fd88 100644 --- a/cmake/nuttx_extensions.cmake +++ b/cmake/nuttx_extensions.cmake @@ -329,6 +329,78 @@ function(nuttx_elf_link_options_ifndef cond) endif() endfunction() +# Function: nuttx_mod_compile_options +# +# Adds compile options to mod targets +# +# Usage: nuttx_mod_compile_options("-O2" "-Wall") +function(nuttx_mod_compile_options) + set_property( + TARGET nuttx_global + APPEND + PROPERTY NUTTX_MOD_APP_COMPILE_OPTIONS ${ARGN}) +endfunction() + +# Function: nuttx_mod_compile_options_ifdef +# +# Conditionally adds compile options to the mod target if the given condition is +# true. +# +# Usage: nuttx_mod_compile_options_ifdef(MY_CONDITION "-O2" "-Wall") +function(nuttx_mod_compile_options_ifdef cond) + if(${cond}) + nuttx_mod_compile_options(${ARGN}) + endif() +endfunction() + +# Function: nuttx_mod_compile_options_ifndef +# +# Conditionally adds compile options to the mod target if the given condition is +# false. +# +# Usage: nuttx_mod_compile_options_ifndef(MY_CONDITION "-O2" "-Wall") +function(nuttx_mod_compile_options_ifndef cond) + if(NOT ${cond}) + nuttx_mod_compile_options(${ARGN}) + endif() +endfunction() + +# Function: nuttx_mod_link_options +# +# Adds link options to mod targets +# +# Usage: nuttx_mod_link_options("-r") +function(nuttx_mod_link_options) + set_property( + TARGET nuttx_global + APPEND + PROPERTY NUTTX_MOD_APP_LINK_OPTIONS ${ARGN}) +endfunction() + +# Function: nuttx_mod_link_options_ifdef +# +# Conditionally adds link options to the mod target if the given condition is +# true. +# +# Usage: nuttx_mod_link_options_ifdef(MY_CONDITION "-r") +function(nuttx_mod_link_options_ifdef cond) + if(${cond}) + nuttx_mod_link_options(${ARGN}) + endif() +endfunction() + +# Function: nuttx_mod_link_options_ifndef +# +# Conditionally adds link options to the mod target if the given condition is +# false. +# +# Usage: nuttx_mod_link_options_ifndef(MY_CONDITION "-r") +function(nuttx_mod_link_options_ifndef cond) + if(NOT ${cond}) + nuttx_mod_link_options(${ARGN}) + endif() +endfunction() + # the visible scope is all the APPS include search path function(nuttx_include_directories_for_all_apps) set_property(