Skip to content

Commit

Permalink
Merge pull request #2989 from MRtrix3/mrtrix_use_lld_option
Browse files Browse the repository at this point in the history
Disable the use of LLD by default
  • Loading branch information
daljit46 authored Sep 12, 2024
2 parents 45e3d96 + 2f27ac6 commit 39d61f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ option(MRTRIX_STRIP_CONDA "Strip ananconda/mininconda from PATH to avoid conflic
option(MRTRIX_USE_PCH "Use precompiled headers" ON)
option(MRTRIX_PYTHON_SOFTLINK "Build directory softlink to Python source code rather than copying" ON)
option(MRTRIX_BUILD_NON_CORE_STATIC "Build MRtrix's non-core code as a static library" OFF)
option(MRTRIX_USE_LLD "Use lld as the linker" OFF)

set(MRTRIX_DEPENDENCIES_DIR "" CACHE PATH
"An optional local directory containing all thirdparty dependencies:\n \
Expand Down
44 changes: 26 additions & 18 deletions cmake/LinkerSetup.cmake
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# On Linux we want to use the LLVM linker if available as it is faster than the default GNU linker.
# The LLVM linker is faster than the default GNU linker.
# Unfortunately, lld is not able to perform LTO when compiling with GCC.
if(MRTRIX_USE_LLD)
include(CheckCXXCompilerFlag)

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_INTERPROCEDURAL_OPTIMIZATION)
set(GCC_LTO TRUE)
else()
set(GCC_LTO FALSE)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_INTERPROCEDURAL_OPTIMIZATION)
set(GCC_LTO TRUE)
message(WARNING "LLD cannot be used with GCC when LTO is enabled. Please another compiler or disable LTO.")
else()
set(GCC_LTO FALSE)
endif()

if(UNIX AND NOT APPLE AND NOT GCC_LTO)
find_program(LLVM_LINKER NAMES "ld.lld")
if(NOT GCC_LTO)
set(LINKER_FLAG "-fuse-ld=lld")
check_cxx_compiler_flag(${LINKER_FLAG} CXX_SUPPORTS_LLVM_LINKER)

if(LLVM_LINKER)
message(STATUS "Using LLVM linker: ${LLVM_LINKER}")
if(CXX_SUPPORTS_LLVM_LINKER)
find_program(LLVM_LINKER NAMES lld ld.lld)
message(STATUS "Using LLVM linker: ${LLVM_LINKER}")

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.29)
set(CMAKE_LINKER_TYPE "LLD" CACHE STRING "Linker type")
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fuse-ld=lld")
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.29)
set(CMAKE_LINKER_TYPE "LLD" CACHE STRING "Linker type")
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fuse-ld=lld")
endif()
else()
message(WARNING "Compiler does not support LLVM linker. Using default linker.")
endif()
endif()

endif()
endif()

0 comments on commit 39d61f8

Please sign in to comment.