mcux-sdk icon indicating copy to clipboard operation
mcux-sdk copied to clipboard

Enhancement: Multi target CMake project support

Open imi415 opened this issue 2 years ago • 3 comments

Currently, MCUXpresso SDK only supports single target in top level CMakeLists.txt, since the components and drivers uses the following method adding source files to the build:

target_sources(${MCUX_SDK_PROJECT_NAME} PRIVATE
    #...something belongs to the component...
)

target_include_directories(${MCUX_SDK_PROJECT_NAME} PUBLIC
   #...some directories needs to be searched...
)

This method requires there could only be one single target (namely ${MCUX_SDK_PROJECT_NAME}), adding another target to the same project (with use cases below) will not work without touching the SDK content or adding them manually.

Typical use cases:

  • Linking to somewhere else by changing linker script: SRAM, FlexSPI, SDRAM..etc, you name it.
  • Different build configurations, compiler definitions

Possible solution: It would be nice if components are re-organized as static library targets, and built as dependencies of the main executable target... maybe not the best solution, and may requires a lot of effort...

imi415 avatar Apr 12 '22 13:04 imi415

Thanks for reporting the issue. Will need some time for investigation, the team is quite busy so the reply will be delayed. Appreciate for your patience.

mcuxsusan avatar Apr 20 '22 07:04 mcuxsusan

A short term solution to this would be integrate #30 as in this case those .cmake files will be included again, with MCUX_SDK_PROJECT_NAME (hopefully) being set to the new project target.

The long-term solution is to use (alias) targets to object libraries. drivers/common/driver_common.cmake might look like this:

#Description: Common Driver; user_visible: True
message("driver_common component is included.")

add_library(driver_common OBJECT
    ${CMAKE_CURRENT_LIST_DIR}/fsl_common.c
    ${CMAKE_CURRENT_LIST_DIR}/fsl_common_arm.c
)

target_include_directories(driver_common PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/.
)

target_link_libraries(driver_common
    core::driver::reset
    core::devices::CMSIS
)
add_library(core::driver::common ALIAS driver_common)

This is then referenced using

target_link_libraries(${MCUX_SDK_PROJECT_NAME} PRIVATE core::driver::common)

The benefit of aliased targets using :: is that they must be CMake targets. These cannot be fullfilled by some (stray) files. Refer to https://cmake.org/cmake/help/latest/command/target_link_libraries.html

This would look much more like "Modern CMake" where everything is passed using targets and properties and not using variables.

tq-steina avatar Apr 20 '23 13:04 tq-steina

+1 We need multi-target CMake support for dual-core NXP MCUs.

Badokas avatar Apr 13 '24 17:04 Badokas