diff --git a/tools/cmake/project.cmake b/tools/cmake/project.cmake index a8f3cea063..6ab459df84 100644 --- a/tools/cmake/project.cmake +++ b/tools/cmake/project.cmake @@ -785,21 +785,6 @@ macro(project project_name) target_link_libraries(${project_elf} PRIVATE "-Wl,--start-group") endif() - if(test_components) - target_link_libraries(${project_elf} PRIVATE "-Wl,--whole-archive") - foreach(test_component ${test_components}) - if(TARGET ${test_component}) - target_link_libraries(${project_elf} PRIVATE ${test_component}) - endif() - endforeach() - target_link_libraries(${project_elf} PRIVATE "-Wl,--no-whole-archive") - endif() - - idf_build_get_property(build_components BUILD_COMPONENT_ALIASES) - if(test_components) - list(REMOVE_ITEM build_components ${test_components}) - endif() - if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") # Compiling for the host, and the host is macOS, so the linker is Darwin LD. # Note, when adding support for Clang and LLD based toolchain this check will @@ -809,6 +794,29 @@ macro(project project_name) set(linker_type "GNU") endif() + if(test_components) + if(linker_type STREQUAL "GNU") + target_link_libraries(${project_elf} PRIVATE "-Wl,--whole-archive") + foreach(test_component ${test_components}) + if(TARGET ${test_component}) + target_link_libraries(${project_elf} PRIVATE ${test_component}) + endif() + endforeach() + target_link_libraries(${project_elf} PRIVATE "-Wl,--no-whole-archive") + elseif(linker_type STREQUAL "Darwin") + foreach(test_component ${test_components}) + if(TARGET ${test_component}) + target_link_libraries(${project_elf} PRIVATE "-Wl,-force_load" ${test_component}) + endif() + endforeach() + endif() + endif() + + idf_build_get_property(build_components BUILD_COMPONENT_ALIASES) + if(test_components) + list(REMOVE_ITEM build_components ${test_components}) + endif() + foreach(build_component ${build_components}) __component_get_target(build_component_target ${build_component}) __component_get_property(whole_archive ${build_component_target} WHOLE_ARCHIVE)