From feaf0ff5761b8f4deb1b8e93d4e760975ac28739 Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Wed, 7 Jun 2023 10:28:22 +0200 Subject: [PATCH] cmake: Treat native libraries differently where needed When building a native library there is a few options we cannot pass to the compiler and linker, including instructing them to genrate non PIE code (as it is still to early to say that), or garbage collect unused sections (as we are not yet doing the final linking). We also need to provide different link options when building the elf for the DTS gen_handles parsing (as that script requires a "final" executable elf) than when we build the native library itself. Signed-off-by: Alberto Escolar Piedras --- CMakeLists.txt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d354d008179..390d12fbd90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -351,7 +351,7 @@ if(NOT CONFIG_NATIVE_BUILD) toolchain_ld_baremetal() endif() -if(CONFIG_CPP AND NOT CONFIG_MINIMAL_LIBCPP) +if(CONFIG_CPP AND NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY) # @Intent: Set linker specific flags for C++ toolchain_ld_cpp() endif() @@ -394,7 +394,12 @@ zephyr_compile_options($<$:$:$>) zephyr_compile_options($<$:$>) -zephyr_link_libraries($) + +# In case of CONFIG_NATIVE_LIBRARY we also don't want position independent code, +# but we cannot tell that to the linker yet as we are first only doing a +# relocatable link into a static library +zephyr_link_libraries_ifndef(CONFIG_NATIVE_LIBRARY + $) # Allow the user to inject options when calling cmake, e.g. # 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..' @@ -1209,6 +1214,8 @@ if(CONFIG_USERSPACE OR CONFIG_DEVICE_DEPS) LIBRARIES_POST_SCRIPT "" DEPENDENCIES ${CODE_RELOCATION_DEP} ) + target_link_libraries_ifdef(CONFIG_NATIVE_LIBRARY ${ZEPHYR_LINK_STAGE_EXECUTABLE} + $) target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE} BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map ) @@ -1426,6 +1433,8 @@ toolchain_ld_link_elf( LINKER_SCRIPT ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD} DEPENDENCIES ${CODE_RELOCATION_DEP} ) +target_link_libraries_ifdef(CONFIG_NATIVE_LIBRARY ${ZEPHYR_LINK_STAGE_EXECUTABLE} + $) target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE} BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map )