From 9c8960cf5ffe63ab5a48595d65bed44bad5ba474 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Sat, 30 Nov 2019 23:01:38 -0500 Subject: [PATCH] Use lld with clang, gold with gcc, LTO only on release builds --- CMakeLists.txt | 64 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5dbaf06dd..204ed1b69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,24 +156,60 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") link_directories(/usr/local/lib) endif() -if(("${CMAKE_BUILD_TYPE}" STREQUAL "Release" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") - AND "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") - option(USE_LD_GOLD "Link with GNU Gold and enable LTO" ON) +if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + option(USE_LD_LLD "Link with LLD" ON) + elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + option(USE_LD_GOLD "Link with GNU Gold" ON) + endif() + + include(CheckIPOSupported) + check_ipo_supported(RESULT LTO_SUPPORTED) + if(LTO_SUPPORTED AND ("${CMAKE_BUILD_TYPE}" STREQUAL "Release" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")) + option(USE_LTO "Enable LTO" ON) + else() + option(USE_LTO "Enable LTO" OFF) + endif() else() - option(USE_LD_GOLD "Link with GNU Gold and enable LTO" OFF) + option(USE_LD_LLD "Link with LLD" OFF) + option(USE_LD_GOLD "Link with GNU Gold" OFF) + option(USE_LTO "Enable LTO" OFF) endif() -if(USE_LD_GOLD AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")) - execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION) - if("${LD_VERSION}" MATCHES "GNU gold") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags") - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags") - add_compile_options(-flto=thin) - add_link_options(-flto=thin) - message(STATUS "GNU gold linker enabled.") +if(USE_LD_LLD) + execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=lld -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION) + if("${LD_VERSION}" MATCHES "LLD") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld") + if(USE_LTO) + add_compile_options(-flto=thin) + add_link_options(-flto=thin) + message(STATUS "LLD linker enabled with LTO.") else() - message(WARNING "GNU gold linker isn't available, using the default system linker.") - set(USE_LD_GOLD OFF) + message(STATUS "LLD linker enabled.") endif() + set(USE_LD_GOLD OFF) + else() + message(WARNING "LLD linker isn't available, using the default system linker.") + set(USE_LD_LLD OFF) + endif() +endif() +if(USE_LD_GOLD) + execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION) + if("${LD_VERSION}" MATCHES "GNU gold") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags") + if(USE_LTO) + add_compile_options(-flto=thin) + add_link_options(-flto=thin) + message(STATUS "GNU gold linker enabled with LTO.") + else() + message(STATUS "GNU gold linker enabled.") + endif() + set(USE_LD_LLD OFF) + else() + message(WARNING "GNU gold linker isn't available, using the default system linker.") + set(USE_LD_GOLD OFF) + endif() endif() # Add discord-rpc here