linker: use find_package() to find LLVM lld

This introduces a new cmake module FindLlvmLld.cmake to do
the work to discover LLVM lld linker.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2023-02-24 16:39:41 -08:00 committed by Anas Nashif
commit 41f015b39b
2 changed files with 62 additions and 1 deletions

View file

@ -1,7 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
set_property(TARGET linker PROPERTY devices_start_symbol "__device_start")
find_program(CMAKE_LINKER ld.lld )
find_package(LlvmLld REQUIRED)
set(CMAKE_LINKER ${LLVMLLD_LINKER})
set_ifndef(LINKERFLAGPREFIX -Wl)

View file

@ -0,0 +1,60 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022, Nordic Semiconductor ASA
# Copyright (c) 2023, Intel Corporation
# FindGnuLd module for locating LLVM lld linker.
#
# The module defines the following variables:
#
# 'LLVMLLD_LINKER'
# Path to LLVM lld linker
# Set to 'LLVMLLD_LINKER-NOTFOUND' if ld was not found.
#
# 'LlvmLld_FOUND', 'LLVMLLD_FOUND'
# True if LLVM lld was found.
#
# 'LLVMLLD_VERSION_STRING'
# The version of LLVM lld.
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(LLD_SEARCH_PATH PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
endif()
# Note that, although there is lld, it cannot be used directly
# as it would complain about it not being a generic linker,
# and asks you to use ld.lld instead. So do not search for lld.
find_program(LLVMLLD_LINKER ld.lld ${LLD_SEARCH_PATH})
if(LLVMLLD_LINKER)
# Parse the 'ld.lld --version' output to find the installed version.
execute_process(
COMMAND
${LLVMLLD_LINKER} --version
OUTPUT_VARIABLE llvmlld_version_output
ERROR_VARIABLE llvmlld_error_output
RESULT_VARIABLE llvmlld_status
)
set(LLVMLLD_VERSION_STRING)
if(${llvmlld_status} EQUAL 0)
# Extract GNU ld version. Different distros have their
# own version scheme so we need to account for that.
# Examples:
# - "GNU ld (GNU Binutils for Ubuntu) 2.34"
# - "GNU ld (Zephyr SDK 0.15.2) 2.38"
# - "GNU ld (Gentoo 2.39 p5) 2.39.0"
string(REGEX MATCH
"LLD ([0-9]+[.][0-9]+[.]?[0-9]*).*"
out_var ${llvmlld_version_output})
set(LLVMLLD_VERSION_STRING ${CMAKE_MATCH_1})
endif()
endif()
find_package_handle_standard_args(LlvmLld
REQUIRED_VARS LLVMLLD_LINKER
VERSION_VAR LLVMLLD_VERSION_STRING
)