Skip to content

Commit baa8240

Browse files
karolzwolakigcbot
authored andcommitted
cmake: Add option USE_LINKER to specify linker and prefer lld by default
This change adds USE_LINKER option to cmake that allows for changing the linker that's going to be used. If not specified, it's gonna probe the system for a few linkers in some predetermined order and select the first one that's available. The preference looks like this right now: `[lld, ld]``, so if you have lld installed on your machine cmake is automatically going to use it to link for faster build times. Prior to this change, we just used GNU ld linker which is quite slow. Note that this option only targets linux systems.
1 parent 1ce1fc9 commit baa8240

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

IGC/CMakeLists.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,65 @@ target_compile_definitions(${IGC_BUILD__PROJ__igc_dll} PRIVATE VER_ORIGINALNAME=
17201720

17211721

17221722
if(UNIX)
1723+
# Allow user to pick a linker explicitly:
1724+
# cmake -DUSE_LINKER=lld ..
1725+
set(USE_LINKER "" CACHE STRING "Preferred linker (lld, wild, mold etc.)")
1726+
1727+
include(CheckLinkerFlag)
1728+
1729+
# Helper function: check whether a linker flag works
1730+
function(_try_linker LINKER_NAME RESULT_VAR)
1731+
if (LINKER_NAME STREQUAL "ld")
1732+
# ld is always available and `-fuse-ld=ld` isn't supported
1733+
set(${RESULT_VAR} TRUE PARENT_SCOPE)
1734+
return()
1735+
endif()
1736+
check_linker_flag(CXX "-fuse-ld=${LINKER_NAME};-Wl,--build-id=sha1,--undefined-version" ${RESULT_VAR})
1737+
endfunction()
1738+
1739+
# List of linkers to auto-detect when user didn't specify one
1740+
set(_default_linkers lld ld)
1741+
1742+
if (USE_LINKER STREQUAL "")
1743+
message(STATUS "No linker specified — probing system")
1744+
1745+
foreach(L IN LISTS _default_linkers)
1746+
_try_linker(${L} HAS_${L})
1747+
if (HAS_${L})
1748+
message(STATUS "Using detected linker: ${L}")
1749+
set(USE_LINKER "${L}" CACHE STRING "" FORCE)
1750+
break()
1751+
endif()
1752+
endforeach()
1753+
1754+
if (USE_LINKER STREQUAL "")
1755+
message(STATUS "No preferred linkers found — using compiler default")
1756+
endif()
1757+
1758+
else()
1759+
# User explicitly requested a linker — verify it
1760+
_try_linker(${USE_LINKER} HAS_${USE_LINKER})
1761+
if (NOT HAS_${USE_LINKER})
1762+
message(FATAL_ERROR
1763+
"Requested linker '${USE_LINKER}' is not usable on this system.")
1764+
endif()
1765+
message(STATUS "Using user-requested linker: ${USE_LINKER}")
1766+
endif()
1767+
1768+
if ((NOT USE_LINKER STREQUAL "") AND (NOT USE_LINKER STREQUAL "ld"))
1769+
igc_config_flag_apply_settings(
1770+
LinkerOptions
1771+
CMAKE_SHARED_LINKER_FLAGS
1772+
ALL_PATTERN ""
1773+
SET_RAW
1774+
-fuse-ld=${USE_LINKER}
1775+
# other linkers than `ld` seem to have behave slightly differently and require this option
1776+
-Wl,--undefined-version
1777+
# different linkers might use build id (lld might use 8 bytes which is too small), so make make it consistent
1778+
-Wl,--build-id=sha1
1779+
)
1780+
endif()
1781+
17231782

17241783
#Copying script to current directory to elevate permissions - replaces file if already exists
17251784
file(

0 commit comments

Comments
 (0)