From 8170f7a37747c3a414b8ad45bb587adcdac6e0da Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Wed, 28 Sep 2016 10:42:03 +0300 Subject: Improve LTO detection code Split LTO detection into separate func and file, and create a cached LTO_WORKS variable. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- CMakeLists.txt | 37 +++++++++---------------------------- LTO.cmake | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 LTO.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9214bfd..6306852 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 2.8) project(kms++) +include(LTO.cmake) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -35,34 +37,13 @@ set(CMAKE_POSITION_INDEPENDENT_CODE TRUE) # static link libc # set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") -if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG AND NOT DEFINED HAS_LTO_FLAG) - CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG) - - if (HAS_LTO_FLAG) - find_program(LTO_AR NAMES "${CMAKE_C_COMPILER}-ar" gcc-ar) - find_program(LTO_RANLIB NAMES "${CMAKE_C_COMPILER}-ranlib" gcc-ranlib) - - if (LTO_AR) - EXECUTE_PROCESS(COMMAND "${LTO_AR}" --version RESULT_VARIABLE ret OUTPUT_QUIET ERROR_QUIET) - if (ret) - unset(LTO_AR CACHE) - endif() - endif() - - if (LTO_RANLIB) - EXECUTE_PROCESS(COMMAND "${LTO_RANLIB}" --version RESULT_VARIABLE ret OUTPUT_QUIET ERROR_QUIET) - if (ret) - unset(LTO_RANLIB CACHE) - endif() - endif() - - if (LTO_AR AND LTO_RANLIB) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto") - set(CMAKE_AR "${LTO_AR}") - set(CMAKE_RANLIB "${LTO_RANLIB}") - else() - message(STATUS "gcc-ar or gcc-ranlib not found, disabling LTO") - endif() +check_lto() + +if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) + if (LTO_WORKS) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto") + set(CMAKE_AR "${LTO_AR}") + set(CMAKE_RANLIB "${LTO_RANLIB}") endif() endif() diff --git a/LTO.cmake b/LTO.cmake new file mode 100644 index 0000000..9a18d77 --- /dev/null +++ b/LTO.cmake @@ -0,0 +1,32 @@ +function(check_lto) + if (DEFINED LTO_WORKS) + return() + endif() + + set(LTO_WORKS FALSE CACHE INTERNAL "LTO works") + + CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG) + + if (NOT HAS_LTO_FLAG) + return() + endif() + + find_program(LTO_AR NAMES "${CMAKE_C_COMPILER}-ar" gcc-ar) + find_program(LTO_RANLIB NAMES "${CMAKE_C_COMPILER}-ranlib" gcc-ranlib) + + if (NOT LTO_AR OR NOT LTO_RANLIB) + return() + endif() + + EXECUTE_PROCESS(COMMAND "${LTO_AR}" --version RESULT_VARIABLE ret OUTPUT_QUIET ERROR_QUIET) + if (ret) + return() + endif() + + EXECUTE_PROCESS(COMMAND "${LTO_RANLIB}" --version RESULT_VARIABLE ret OUTPUT_QUIET ERROR_QUIET) + if (ret) + return() + endif() + + set(LTO_WORKS TRUE CACHE INTERNAL "LTO works") +endfunction() -- cgit v1.2.3