cmake:enhance the module for adding extra libraries

change the extra library from a file to an import target;
this will avoid differences in the handling of static libraries
between different versions of cmake and different platforms.

after unifying as a target, extra libraries can be
handled as the same as other compiled libraries

Signed-off-by: xuxin19 <xuxin19@xiaomi.com>
This commit is contained in:
xuxin19 2024-08-09 16:45:11 +08:00 committed by Xiang Xiao
parent 1603e40607
commit ab488800bb
3 changed files with 25 additions and 37 deletions

View File

@ -640,14 +640,15 @@ if(CONFIG_BUILD_FLAT)
get_property(nuttx_apps_libs GLOBAL PROPERTY NUTTX_APPS_LIBRARIES)
endif()
set(nuttx_libs ${nuttx_kernel_libs} ${nuttx_system_libs} ${nuttx_apps_libs})
set(nuttx_libs ${nuttx_kernel_libs} ${nuttx_system_libs} ${nuttx_apps_libs}
${nuttx_extra_libs})
if(NOT CONFIG_ARCH_SIM)
# TODO: nostart/nodefault not applicable to nuttx toolchain
target_link_libraries(
nuttx PRIVATE ${NUTTX_EXTRA_FLAGS} -T${ldscript} -Wl,--start-group
${nuttx_libs} ${nuttx_extra_libs} -Wl,--end-group)
${nuttx_libs} -Wl,--end-group)
# generate binary outputs in different formats (.bin, .hex, etc)
nuttx_generate_outputs(nuttx)
@ -680,8 +681,8 @@ elseif(WIN32)
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/nuttx_all.lib
COMMAND ${CMAKE_AR} /OUT:${CMAKE_BINARY_DIR}/nuttx_all.lib
${nuttx_libs_paths} ${nuttx_extra_libs}
DEPENDS ${nuttx_libs} ${nuttx_extra_libs}
${nuttx_libs_paths}
DEPENDS ${nuttx_libs}
VERBATIM)
add_custom_target(nuttx_all-lib DEPENDS ${CMAKE_BINARY_DIR}/nuttx_all.lib)
add_dependencies(nuttx nuttx_all-lib)
@ -713,9 +714,9 @@ else()
COMMAND
${CMAKE_LINKER} ARGS -r $<$<BOOL:${CONFIG_SIM_M32}>:-m32>
$<TARGET_OBJECTS:sim_head> $<$<NOT:$<BOOL:${APPLE}>>:-Wl,--start-group>
${nuttx_libs_paths} ${nuttx_extra_libs}
$<$<NOT:$<BOOL:${APPLE}>>:-Wl,--end-group> -o nuttx.rel
DEPENDS ${nuttx_libs} ${nuttx_extra_libs} sim_head
${nuttx_libs_paths} $<$<NOT:$<BOOL:${APPLE}>>:-Wl,--end-group> -o
nuttx.rel
DEPENDS ${nuttx_libs} sim_head
COMMAND_EXPAND_LISTS)
else()
add_custom_command(
@ -723,10 +724,10 @@ else()
COMMAND
${CMAKE_C_COMPILER} ARGS -r $<$<BOOL:${CONFIG_SIM_M32}>:-m32>
$<TARGET_OBJECTS:sim_head> $<$<NOT:$<BOOL:${APPLE}>>:-Wl,--start-group>
${nuttx_libs_paths} ${nuttx_extra_libs}
$<$<NOT:$<BOOL:${APPLE}>>:-Wl,--end-group> -o nuttx.rel
${nuttx_libs_paths} $<$<NOT:$<BOOL:${APPLE}>>:-Wl,--end-group> -o
nuttx.rel
COMMAND ${CMAKE_OBJCOPY} --redefine-syms=nuttx-names.dat nuttx.rel
DEPENDS ${nuttx_libs} ${nuttx_extra_libs} sim_head sim_redefine_symbols
DEPENDS ${nuttx_libs} sim_head sim_redefine_symbols
COMMAND_EXPAND_LISTS)
endif()
add_custom_target(nuttx-rel DEPENDS nuttx.rel

View File

@ -87,12 +87,7 @@ if(NOT EXISTS ${extra_library} AND CONFIG_ARCH_TOOLCHAIN_CLANG)
OUTPUT_VARIABLE extra_library)
endif()
if(CMAKE_HOST_SYSTEM_NAME MATCHES "MSYS|CYGWIN|Windows")
cmake_path(GET extra_library FILENAME extra_filename_library)
list(APPEND EXTRA_LIB -l:${extra_filename_library})
else()
list(APPEND EXTRA_LIB ${extra_library})
endif()
list(APPEND EXTRA_LIB ${extra_library})
if(NOT CONFIG_LIBM)
execute_process(
@ -101,12 +96,7 @@ if(NOT CONFIG_LIBM)
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE extra_library)
if(CMAKE_HOST_SYSTEM_NAME MATCHES "MSYS|CYGWIN|Windows")
cmake_path(GET extra_library FILENAME extra_filename_library)
list(APPEND EXTRA_LIB -l:${extra_filename_library})
else()
list(APPEND EXTRA_LIB ${extra_library})
endif()
endif()
if(CONFIG_LIBSUPCXX)
@ -115,12 +105,7 @@ if(CONFIG_LIBSUPCXX)
--print-file-name=libsupc++.a
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE extra_library)
if(CMAKE_HOST_SYSTEM_NAME MATCHES "MSYS|CYGWIN|Windows")
cmake_path(GET extra_library FILENAME extra_filename_library)
list(APPEND EXTRA_LIB -l:${extra_filename_library})
else()
list(APPEND EXTRA_LIB ${extra_library})
endif()
endif()
if(CONFIG_ARCH_COVERAGE)
@ -129,12 +114,7 @@ if(CONFIG_ARCH_COVERAGE)
--print-file-name=libgcov.a
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE extra_library)
if(CMAKE_HOST_SYSTEM_NAME MATCHES "MSYS|CYGWIN|Windows")
cmake_path(GET extra_library FILENAME extra_filename_library)
list(APPEND EXTRA_LIB -l:${extra_filename_library})
else()
list(APPEND EXTRA_LIB ${extra_library})
endif()
endif()
nuttx_add_extra_library(${EXTRA_LIB})

View File

@ -191,11 +191,18 @@ endfunction()
#
# nuttx_add_extra_library
#
# Add extra library to extra attribute
# Add extra library to extra attribute, extra library will be treated as an
# import target and have a complete full path this will be used to avoid adding
# the -l prefix to the link target between different cmake versions and
# platformss
#
function(nuttx_add_extra_library)
foreach(target ${ARGN})
set_property(GLOBAL APPEND PROPERTY NUTTX_EXTRA_LIBRARIES ${target})
foreach(extra_lib ${ARGN})
# define the target name of the extra library
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" extra_target "${extra_lib}")
# set the absolute path of the library for the import target
nuttx_library_import(${extra_target} ${extra_lib})
set_property(GLOBAL APPEND PROPERTY NUTTX_EXTRA_LIBRARIES ${extra_target})
endforeach()
endfunction()