Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/cmake/configure-linker.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,35 @@ function(configure_linker)
set (LLD_LINKER_NAMES lld-link)
else()
set(LLD_LINKER_NAMES ld.lld ld64.lld)
set(MOLD_LINKER_NAMES mold sold)
set(MOLD_LINKER_NAMES /usr/libexec/mold/ld /usr/local/libexec/mold/ld mold sold)
find_program(MOLD_LINKER NAMES ${MOLD_LINKER_NAMES})
endif()

find_program(LLD_LINKER NAMES ${LLD_LINKER_NAMES})

if(MOLD_LINKER)
message(STATUS "configure_linker: `mold` found in environment, will use it as default.")
message(STATUS "configure_linker: `mold` (${MOLD_LINKER}) found in environment, will use it as default.")
set(CMAKE_LINKER_TYPE MOLD PARENT_SCOPE)
if(MOLD_LINKER MATCHES "libexec")
string(REPLACE "/ld" "" MOLD_LINKER_PATH ${MOLD_LINKER})
#
# The reason why we're setting this variables is that GCC < 12.1 does not
# support the "-fuse-ld=mold" parameter, which causes CMake configure to fail
# when configured with mold + < GCC 12.1 combo. There's a detailed discussion
# about this in CMake issue board: https://gitlab.kitware.com/cmake/cmake/-/issues/25748#note_1494202
# The gist of itis that CMake maintainers decided to not to cover this edge case
# so we have to intervene.
#
# CMake will override this with "-fuse-ld=mold" for clang and GCC >= 12.1
#
set(CMAKE_C_USING_LINKER_MOLD "-B${MOLD_LINKER_PATH}" PARENT_SCOPE)
set(CMAKE_CXX_USING_LINKER_MOLD "-B${MOLD_LINKER_PATH}" PARENT_SCOPE)
endif()
elseif(LLD_LINKER)
message(STATUS "configure_linker: `lld` found in environment, will use it as default.")
message(STATUS "configure_linker: `lld` (${LLD_LINKER}) found in environment, will use it as default.")
set(CMAKE_LINKER_TYPE LLD PARENT_SCOPE)
else()
message(STATUS "configure_linker: neither `mold` or `lld` is present in the environment, will use the toolchain default.")
message(STATUS "configure_linker: could use neither `mold` nor `lld`; will use the toolchain default.")
endif()
else()
message(STATUS "configure_linker: will use the user specified linker: `${CMAKE_LINKER_TYPE}`")
Expand Down
Loading