1# SPDX-License-Identifier: Apache-2.0 2# 3# Copyright (c) 2022, Nordic Semiconductor ASA 4# Copyright (c) 2023, Intel Corporation 5 6# FindGnuLd module for locating LLVM lld linker. 7# 8# The module defines the following variables: 9# 10# 'LLVMLLD_LINKER' 11# Path to LLVM lld linker 12# Set to 'LLVMLLD_LINKER-NOTFOUND' if ld was not found. 13# 14# 'LlvmLld_FOUND', 'LLVMLLD_FOUND' 15# True if LLVM lld was found. 16# 17# 'LLVMLLD_VERSION_STRING' 18# The version of LLVM lld. 19 20include(FindPackageHandleStandardArgs) 21 22# See if the compiler has a preferred linker 23execute_process(COMMAND ${CMAKE_C_COMPILER} --print-prog-name=ld.lld 24 OUTPUT_VARIABLE LLVMLLD_LINKER 25 OUTPUT_STRIP_TRAILING_WHITESPACE) 26 27if(NOT EXISTS "${LLVMLLD_LINKER}") 28 # Need to clear it or else find_program() won't replace the value. 29 set(LLVMLLD_LINKER) 30 31 if(DEFINED TOOLCHAIN_HOME) 32 # Search for linker under TOOLCHAIN_HOME if it is defined 33 # to limit which linker to use, or else we would be using 34 # host tools. 35 set(LLD_SEARCH_PATH PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) 36 endif() 37 38 # Note that, although there is lld, it cannot be used directly 39 # as it would complain about it not being a generic linker, 40 # and asks you to use ld.lld instead. So do not search for lld. 41 find_program(LLVMLLD_LINKER ld.lld ${LLD_SEARCH_PATH}) 42endif() 43 44if(LLVMLLD_LINKER) 45 # Parse the 'ld.lld --version' output to find the installed version. 46 execute_process( 47 COMMAND 48 ${LLVMLLD_LINKER} --version 49 OUTPUT_VARIABLE llvmlld_version_output 50 ERROR_VARIABLE llvmlld_error_output 51 RESULT_VARIABLE llvmlld_status 52 ) 53 54 set(LLVMLLD_VERSION_STRING) 55 if(${llvmlld_status} EQUAL 0) 56 # Extract GNU ld version. Different distros have their 57 # own version scheme so we need to account for that. 58 # Examples: 59 # - "GNU ld (GNU Binutils for Ubuntu) 2.34" 60 # - "GNU ld (Zephyr SDK 0.15.2) 2.38" 61 # - "GNU ld (Gentoo 2.39 p5) 2.39.0" 62 string(REGEX MATCH 63 "LLD ([0-9]+[.][0-9]+[.]?[0-9]*).*" 64 out_var ${llvmlld_version_output}) 65 set(LLVMLLD_VERSION_STRING ${CMAKE_MATCH_1}) 66 endif() 67endif() 68 69find_package_handle_standard_args(LlvmLld 70 REQUIRED_VARS LLVMLLD_LINKER 71 VERSION_VAR LLVMLLD_VERSION_STRING 72) 73