Use lld with clang, gold with gcc, LTO only on release builds

This commit is contained in:
Luke Street 2019-11-30 23:01:38 -05:00
parent 697a100bca
commit 9c8960cf5f
1 changed files with 50 additions and 14 deletions

View File

@ -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