summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomi Valkeinen <tomi.valkeinen@ti.com>2016-09-28 10:42:03 +0300
committerTomi Valkeinen <tomi.valkeinen@ti.com>2016-09-29 11:21:16 +0300
commit8170f7a37747c3a414b8ad45bb587adcdac6e0da (patch)
tree5e75a6728184d79e4146f875a2e747f252e2708e
parent07c6c86874fef1e4aeccd37dc62cc009ba7212b8 (diff)
Improve LTO detection code
Split LTO detection into separate func and file, and create a cached LTO_WORKS variable. Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ti.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
-rw-r--r--CMakeLists.txt37
-rw-r--r--LTO.cmake32
2 files changed, 41 insertions, 28 deletions
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()