cmake(enhance):enhance NuttX cmake target dependencies and link_library modules

Enhance CMake's add_dependencies for Nuttx so that
different targets can call dependencies without errors when they are not traversed.

In addition, since we do not call link_library directly,
we increment nuttx_link_library to inherit the PUBLIC property

Signed-off-by: xuxin19 <xuxin19@xiaomi.com>
This commit is contained in:
xuxin19 2024-11-21 15:44:22 +08:00 committed by Alan C. Assis
parent a1cbed020a
commit cfe78ad74e
5 changed files with 134 additions and 13 deletions

View file

@ -365,6 +365,10 @@ nuttx_export_kconfig(${CMAKE_BINARY_DIR}/.config)
include(nuttx_generate_headers) include(nuttx_generate_headers)
include(nuttx_generate_outputs) include(nuttx_generate_outputs)
include(nuttx_add_library) include(nuttx_add_library)
# add NuttX CMake extenstion after nuttx_add_library
include(nuttx_extensions)
include(nuttx_add_application) include(nuttx_add_application)
include(nuttx_add_romfs) include(nuttx_add_romfs)
include(nuttx_add_symtab) include(nuttx_add_symtab)
@ -378,9 +382,6 @@ include(menuconfig)
include(ExternalProject) include(ExternalProject)
include(FetchContent) include(FetchContent)
# add NuttX CMake extenstion at last
include(nuttx_extensions)
set(FETCHCONTENT_QUIET OFF) set(FETCHCONTENT_QUIET OFF)
# Board common directory ##################################################### # Board common directory #####################################################
@ -582,6 +583,9 @@ if(NOT CONFIG_BUILD_KERNEL)
endif() endif()
# after we traverse all build directories unify all target dependencies
process_all_target_dependencies()
# Link step ################################################################## # Link step ##################################################################
# Get linker script to use # Get linker script to use

View file

@ -154,6 +154,24 @@ function(nuttx_add_application)
set(TARGET "apps_${NAME}") set(TARGET "apps_${NAME}")
add_library(${TARGET} ${SRCS}) add_library(${TARGET} ${SRCS})
# Set apps global compile options & definitions hold by
# nuttx_apps_interface
target_compile_options(
${TARGET}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_apps_interface,APPS_COMPILE_OPTIONS>>
)
target_compile_definitions(
${TARGET}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_apps_interface,APPS_COMPILE_DEFINITIONS>>
)
target_include_directories(
${TARGET}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_apps_interface,APPS_INCLUDE_DIRECTORIES>>
)
nuttx_add_library_internal(${TARGET}) nuttx_add_library_internal(${TARGET})
# add to list of application libraries # add to list of application libraries

View file

@ -20,6 +20,9 @@
# #
# ############################################################################## # ##############################################################################
# global dependency maintenance target
add_custom_target(nuttx_dep_map)
include(nuttx_parse_function_args) include(nuttx_parse_function_args)
# ~~~ # ~~~
@ -55,16 +58,47 @@ function(nuttx_add_dependencies)
ARGN ARGN
${ARGN}) ${ARGN})
get_target_property(NO_COMPILABLE_TARGET ${TARGET} NO_COMPILABLE_TARGET) set_property(
if(NO_COMPILABLE_TARGET) TARGET nuttx_dep_map
return() APPEND
endif() PROPERTY ALL_PROCESS_TARGET ${TARGET})
foreach(dep ${DEPENDS}) foreach(dep ${DEPENDS})
# add dependencies set_property(
add_dependencies(${TARGET} ${dep}) TARGET nuttx_dep_map
# add export include directory APPEND
target_include_directories(${TARGET} PROPERTY ${TARGET} ${dep})
PRIVATE ${NUTTX_APPS_BINDIR}/include/${dep}) endforeach()
endfunction()
# After collecting all dependency mappings, process all targets uniformly
function(process_all_target_dependencies)
# get all target need add deps
get_property(
all_process_target
TARGET nuttx_dep_map
PROPERTY ALL_PROCESS_TARGET)
foreach(target ${all_process_target})
if(TARGET ${target})
get_target_property(NO_COMPILABLE_TARGET ${target} NO_COMPILABLE_TARGET)
if(NOT NO_COMPILABLE_TARGET)
# treat `target` as mapping key
get_property(
all_deps
TARGET nuttx_dep_map
PROPERTY ${target})
foreach(dep ${all_deps})
if(TARGET ${dep})
# ensure build time order
add_dependencies(${target} ${dep})
# inherit public properties
nuttx_link_libraries(${target} ${dep})
# compatible with previous export headers
target_include_directories(
${target} PRIVATE ${NUTTX_APPS_BINDIR}/include/${dep})
endif()
endforeach()
endif()
endif()
endforeach() endforeach()
endfunction() endfunction()

View file

@ -183,7 +183,22 @@ function(nuttx_add_library target)
add_library(${target} ${ARGN}) add_library(${target} ${ARGN})
add_dependencies(${target} apps_context) add_dependencies(${target} apps_context)
set_property(GLOBAL APPEND PROPERTY NUTTX_SYSTEM_LIBRARIES ${target}) set_property(GLOBAL APPEND PROPERTY NUTTX_SYSTEM_LIBRARIES ${target})
# Set apps global compile options & definitions hold by nuttx_apps_interface
target_compile_options(
${target}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_apps_interface,APPS_COMPILE_OPTIONS>>
)
target_compile_definitions(
${target}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_apps_interface,APPS_COMPILE_DEFINITIONS>>
)
target_include_directories(
${target}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:nuttx_apps_interface,APPS_INCLUDE_DIRECTORIES>>
)
nuttx_add_library_internal(${target}) nuttx_add_library_internal(${target})
endfunction() endfunction()

View file

@ -24,6 +24,10 @@
include(nuttx_parse_function_args) include(nuttx_parse_function_args)
# "nuttx_apps_interface" is a source-less target that encapsulates all the apps
# compiler options and include path needed by all apps libraries.
add_custom_target(nuttx_apps_interface)
# Macro: nuttx_library # Macro: nuttx_library
# #
# Creates a library target with the given name and mode. If MODE is "KERNEL", it # Creates a library target with the given name and mode. If MODE is "KERNEL", it
@ -222,3 +226,49 @@ function(nuttx_compile_options_ifndef cond)
nuttx_compile_options(${ARGN}) nuttx_compile_options(${ARGN})
endif() endif()
endfunction() endfunction()
# the visible scope is all the APPS include search path
function(nuttx_include_directories_for_all_apps)
set_property(
TARGET nuttx_apps_interface
APPEND
PROPERTY APPS_INCLUDE_DIRECTORIES ${ARGN})
endfunction()
# all apps compile_options
function(nuttx_compile_options_for_all_apps)
set_property(
TARGET nuttx_apps_interface
APPEND
PROPERTY APPS_COMPILE_OPTIONS ${ARGN})
endfunction()
# all apps compile_definitions
function(nuttx_compile_definitions_for_all_apps)
set_property(
TARGET nuttx_apps_interface
APPEND
PROPERTY APPS_COMPILE_DEFINITIONS ${ARGN})
endfunction()
# since we dont call `target_link_libraries` directly, we only inherit their
# compilation configuration
function(nuttx_link_libraries)
set(TARGET ${ARGV0})
if(TARGET ${TARGET})
foreach(dep ${ARGN})
target_compile_options(
${TARGET}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:${dep},INTERFACE_COMPILE_OPTIONS>>)
target_compile_definitions(
${TARGET}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:${dep},INTERFACE_COMPILE_DEFINITIONS>>)
target_include_directories(
${TARGET}
PRIVATE
$<GENEX_EVAL:$<TARGET_PROPERTY:${dep},INTERFACE_INCLUDE_DIRECTORIES>>)
endforeach()
endif()
endfunction()