From cfe78ad74e39e9a3e5ba8c0a6b30372051813d29 Mon Sep 17 00:00:00 2001 From: xuxin19 Date: Thu, 21 Nov 2024 15:44:22 +0800 Subject: [PATCH] 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 --- CMakeLists.txt | 10 ++++-- cmake/nuttx_add_application.cmake | 18 +++++++++++ cmake/nuttx_add_dependencies.cmake | 52 ++++++++++++++++++++++++------ cmake/nuttx_add_library.cmake | 17 +++++++++- cmake/nuttx_extensions.cmake | 50 ++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ff7f55fcd90..804093cadde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -365,6 +365,10 @@ nuttx_export_kconfig(${CMAKE_BINARY_DIR}/.config) include(nuttx_generate_headers) include(nuttx_generate_outputs) include(nuttx_add_library) + +# add NuttX CMake extenstion after nuttx_add_library +include(nuttx_extensions) + include(nuttx_add_application) include(nuttx_add_romfs) include(nuttx_add_symtab) @@ -378,9 +382,6 @@ include(menuconfig) include(ExternalProject) include(FetchContent) -# add NuttX CMake extenstion at last -include(nuttx_extensions) - set(FETCHCONTENT_QUIET OFF) # Board common directory ##################################################### @@ -582,6 +583,9 @@ if(NOT CONFIG_BUILD_KERNEL) endif() +# after we traverse all build directories unify all target dependencies +process_all_target_dependencies() + # Link step ################################################################## # Get linker script to use diff --git a/cmake/nuttx_add_application.cmake b/cmake/nuttx_add_application.cmake index f2eaddb1aa7..1c8259e3a76 100644 --- a/cmake/nuttx_add_application.cmake +++ b/cmake/nuttx_add_application.cmake @@ -154,6 +154,24 @@ function(nuttx_add_application) set(TARGET "apps_${NAME}") add_library(${TARGET} ${SRCS}) + # Set apps global compile options & definitions hold by + # nuttx_apps_interface + target_compile_options( + ${TARGET} + PRIVATE + $> + ) + target_compile_definitions( + ${TARGET} + PRIVATE + $> + ) + target_include_directories( + ${TARGET} + PRIVATE + $> + ) + nuttx_add_library_internal(${TARGET}) # add to list of application libraries diff --git a/cmake/nuttx_add_dependencies.cmake b/cmake/nuttx_add_dependencies.cmake index 46aeea9a5fb..c1291729c20 100644 --- a/cmake/nuttx_add_dependencies.cmake +++ b/cmake/nuttx_add_dependencies.cmake @@ -20,6 +20,9 @@ # # ############################################################################## +# global dependency maintenance target +add_custom_target(nuttx_dep_map) + include(nuttx_parse_function_args) # ~~~ @@ -55,16 +58,47 @@ function(nuttx_add_dependencies) ARGN ${ARGN}) - get_target_property(NO_COMPILABLE_TARGET ${TARGET} NO_COMPILABLE_TARGET) - if(NO_COMPILABLE_TARGET) - return() - endif() + set_property( + TARGET nuttx_dep_map + APPEND + PROPERTY ALL_PROCESS_TARGET ${TARGET}) foreach(dep ${DEPENDS}) - # add dependencies - add_dependencies(${TARGET} ${dep}) - # add export include directory - target_include_directories(${TARGET} - PRIVATE ${NUTTX_APPS_BINDIR}/include/${dep}) + set_property( + TARGET nuttx_dep_map + APPEND + PROPERTY ${TARGET} ${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() endfunction() diff --git a/cmake/nuttx_add_library.cmake b/cmake/nuttx_add_library.cmake index fc97281d9ff..380696af0ad 100644 --- a/cmake/nuttx_add_library.cmake +++ b/cmake/nuttx_add_library.cmake @@ -183,7 +183,22 @@ function(nuttx_add_library target) add_library(${target} ${ARGN}) add_dependencies(${target} apps_context) 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 + $> + ) + target_compile_definitions( + ${target} + PRIVATE + $> + ) + target_include_directories( + ${target} + PRIVATE + $> + ) nuttx_add_library_internal(${target}) endfunction() diff --git a/cmake/nuttx_extensions.cmake b/cmake/nuttx_extensions.cmake index 09251b50617..aa2232e16d4 100644 --- a/cmake/nuttx_extensions.cmake +++ b/cmake/nuttx_extensions.cmake @@ -24,6 +24,10 @@ 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 # # 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}) endif() 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 + $>) + target_compile_definitions( + ${TARGET} + PRIVATE + $>) + target_include_directories( + ${TARGET} + PRIVATE + $>) + endforeach() + endif() +endfunction()