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
+26 -7
View File
@@ -65,6 +65,8 @@ endif()
# CONFIG_<app> value)
# SRCS : source files
# 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
# contains differently named <app>_main() entries for different <app>. 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}
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_APP_LINK_OPTIONS>>
-T ${CMAKE_BINARY_DIR}/gnu-elf.ld
$<$<TARGET_EXISTS:STARTUP_OBJS>:$<TARGET_OBJECTS:STARTUP_OBJS>>
--start-group
$<$<BOOL:${CONFIG_BUILD_KERNEL}>:$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_LINK_LIBRARIES>>>
$<IF:$<STREQUAL:"${DYNLIB}","y">,$<TARGET_PROPERTY:nuttx_global,NUTTX_MOD_APP_LINK_OPTIONS>,$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_APP_LINK_OPTIONS>>
${LINK_FLAGS} -T ${CMAKE_BINARY_DIR}/gnu-elf.ld
$<$<AND:$<TARGET_EXISTS:STARTUP_OBJS>,$<STREQUAL:"${DYNLIB}","y">>:$<TARGET_OBJECTS:STARTUP_OBJS>>
$<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--start-group
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_LINK_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}
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
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_global,NUTTX_MOD_APP_COMPILE_OPTIONS>>
)
endif()
install(TARGETS ${TARGET})
set_property(
TARGET nuttx
+72
View File
@@ -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(