cmake(enhance):cmake support DYNLIB build

add `DYNLIB` option for nuttx_add_application for dynamic loadable lib

Signed-off-by: xuxin19 <xuxin19@xiaomi.com>
This commit is contained in:
xuxin19
2025-08-01 15:14:36 +08:00
committed by GUIDINGLI
parent a7c6f589f9
commit 00e79122af
5 changed files with 110 additions and 7 deletions
+4
View File
@@ -22,6 +22,8 @@
nuttx_elf_compile_options(-fvisibility=hidden -mlong-calls) 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 nuttx_elf_compile_options_ifdef(CONFIG_UNWINDER_ARM -fno-unwind-tables
-fno-asynchronous-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_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_BUILD_KERNEL -Bstatic)
nuttx_elf_link_options_ifdef(CONFIG_DEBUG_OPT_UNUSED_SECTIONS --gc-sections) nuttx_elf_link_options_ifdef(CONFIG_DEBUG_OPT_UNUSED_SECTIONS --gc-sections)
+4
View File
@@ -22,8 +22,12 @@
nuttx_elf_compile_options(-fvisibility=hidden) nuttx_elf_compile_options(-fvisibility=hidden)
nuttx_mod_compile_options(-fvisibility=hidden)
nuttx_elf_link_options_ifdef(CONFIG_BINFMT_ELF_RELOCATABLE -r) 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_BUILD_KERNEL -Bstatic)
nuttx_elf_link_options_ifdef(CONFIG_DEBUG_OPT_UNUSED_SECTIONS --gc-sections) nuttx_elf_link_options_ifdef(CONFIG_DEBUG_OPT_UNUSED_SECTIONS --gc-sections)
+4
View File
@@ -22,8 +22,12 @@
nuttx_elf_compile_options(-fvisibility=hidden -fno-pic -mcmodel=large) nuttx_elf_compile_options(-fvisibility=hidden -fno-pic -mcmodel=large)
nuttx_mod_compile_options(-fvisibility=hidden)
nuttx_elf_link_options(-r) 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_ifdef(CONFIG_DEBUG_OPT_UNUSED_SECTIONS --gc-sections)
nuttx_elf_link_options(-e __start) nuttx_elf_link_options(-e __start)
+26 -7
View File
@@ -65,6 +65,8 @@ endif()
# CONFIG_<app> value) # CONFIG_<app> value)
# SRCS : source files # SRCS : source files
# NO_MAIN_ALIAS : do not add a main=<app>_main alias(*) # NO_MAIN_ALIAS : do not add a main=<app>_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 # (*) This is only really needed in convoluted cases where a single .c file
# contains differently named <app>_main() entries for different <app>. This # contains differently named <app>_main() entries for different <app>. This
@@ -92,8 +94,10 @@ function(nuttx_add_application)
PRIORITY PRIORITY
STACKSIZE STACKSIZE
MODULE MODULE
DYNLIB
MULTI_VALUE MULTI_VALUE
COMPILE_FLAGS COMPILE_FLAGS
LINK_FLAGS
INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES
SRCS SRCS
DEPENDS DEPENDS
@@ -114,7 +118,8 @@ function(nuttx_add_application)
if(SRCS_EXIST) if(SRCS_EXIST)
if(MODULE if(MODULE
AND ("${MODULE}" STREQUAL "m") AND ("${MODULE}" STREQUAL "m")
OR CONFIG_BUILD_KERNEL) OR CONFIG_BUILD_KERNEL
OR "${DYNLIB}" STREQUAL "y")
# create as standalone executable (loadable application or "module") # create as standalone executable (loadable application or "module")
set(TARGET "${NAME}") set(TARGET "${NAME}")
@@ -125,18 +130,24 @@ function(nuttx_add_application)
set(TARGET "ELF_${TARGET}") set(TARGET "ELF_${TARGET}")
add_library(${TARGET} ${SRCS}) add_library(${TARGET} ${SRCS})
add_dependencies(${TARGET} apps_post) add_dependencies(${TARGET} apps_post)
if(NOT "${CMAKE_LD}" MATCHES "gcc$")
set(USE_LINKER True)
endif()
add_custom_command( add_custom_command(
TARGET ${TARGET} TARGET ${TARGET}
POST_BUILD POST_BUILD
COMMAND COMMAND
${CMAKE_LD} ${CMAKE_LD}
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_APP_LINK_OPTIONS>> $<IF:$<STREQUAL:"${DYNLIB}","y">,$<TARGET_PROPERTY:nuttx_global,NUTTX_MOD_APP_LINK_OPTIONS>,$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_APP_LINK_OPTIONS>>
-T ${CMAKE_BINARY_DIR}/gnu-elf.ld ${LINK_FLAGS} -T ${CMAKE_BINARY_DIR}/gnu-elf.ld
$<$<TARGET_EXISTS:STARTUP_OBJS>:$<TARGET_OBJECTS:STARTUP_OBJS>> $<$<AND:$<TARGET_EXISTS:STARTUP_OBJS>,$<STREQUAL:"${DYNLIB}","y">>:$<TARGET_OBJECTS:STARTUP_OBJS>>
--start-group $<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--start-group
$<$<BOOL:${CONFIG_BUILD_KERNEL}>:$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_LINK_LIBRARIES>>> $<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_LINK_LIBRARIES>>
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_LINK_EXTRA_LIBRARIES>> $<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_LINK_EXTRA_LIBRARIES>>
$<TARGET_FILE:${TARGET}> --end-group -o $<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--whole-archive
$<TARGET_FILE:${TARGET}>
$<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--no-whole-archive
$<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--end-group -o
${CMAKE_BINARY_DIR}/bin_debug/${ELF_NAME} ${CMAKE_BINARY_DIR}/bin_debug/${ELF_NAME}
COMMAND COMMAND
${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/bin_debug/${ELF_NAME} ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/bin_debug/${ELF_NAME}
@@ -170,6 +181,14 @@ function(nuttx_add_application)
) )
endif() endif()
if("${DYNLIB}" STREQUAL "y")
target_compile_options(
${TARGET}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_MOD_APP_COMPILE_OPTIONS>>
)
endif()
install(TARGETS ${TARGET}) install(TARGETS ${TARGET})
set_property( set_property(
TARGET nuttx TARGET nuttx
+72
View File
@@ -329,6 +329,78 @@ function(nuttx_elf_link_options_ifndef cond)
endif() endif()
endfunction() 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 # the visible scope is all the APPS include search path
function(nuttx_include_directories_for_all_apps) function(nuttx_include_directories_for_all_apps)
set_property( set_property(