linker: ld: see if compiler has a preferred linker

This asks the compiler if it has its own preference for ld.bfd.
This is useful for LLVM (when CONFIG_LLVM_USE_LD=y) so we know
which linker clang is using.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2023-02-24 17:11:18 -08:00 committed by Anas Nashif
parent a5cc22b2a9
commit 0e00c3da5c

View file

@ -20,16 +20,26 @@
# Note that this will use CROSS_COMPILE, if defined,
# as a prefix to the linker executable.
if(DEFINED TOOLCHAIN_HOME)
# Search for linker under TOOLCHAIN_HOME if it is defined
# to limit which linker to use, or else we would be using
# host tools.
set(LD_SEARCH_PATH PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
endif()
# See if the compiler has a preferred linker
execute_process(COMMAND ${CMAKE_C_COMPILER} --print-prog-name=ld.bfd
OUTPUT_VARIABLE GNULD_LINKER
OUTPUT_STRIP_TRAILING_WHITESPACE)
find_program(GNULD_LINKER ${CROSS_COMPILE}ld.bfd ${LD_SEARCH_PATH})
if(NOT GNULD_LINKER)
find_program(GNULD_LINKER ${CROSS_COMPILE}ld ${LD_SEARCH_PATH})
if(NOT EXISTS "${GNULD_LINKER}")
# Need to clear it or else find_program() won't replace the value.
set(GNULD_LINKER)
if(DEFINED TOOLCHAIN_HOME)
# Search for linker under TOOLCHAIN_HOME if it is defined
# to limit which linker to use, or else we would be using
# host tools.
set(LD_SEARCH_PATH PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
endif()
find_program(GNULD_LINKER ${CROSS_COMPILE}ld.bfd ${LD_SEARCH_PATH})
if(NOT GNULD_LINKER)
find_program(GNULD_LINKER ${CROSS_COMPILE}ld ${LD_SEARCH_PATH})
endif()
endif()
if(GNULD_LINKER)