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 <alberto.escolar.piedras@nordicsemi.no>
This commit is contained in:
Alberto Escolar Piedras 2023-06-07 10:28:22 +02:00 committed by Chris Friedt
commit feaf0ff576

View file

@ -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($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,
# @Intent: Do not make position independent code / executable
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
zephyr_link_libraries($<TARGET_PROPERTY:linker,no_position_independent>)
# 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
$<TARGET_PROPERTY:linker,no_position_independent>)
# 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_PROPERTY:linker,no_position_independent>)
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_PROPERTY:linker,partial_linking>)
target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
)