Improve LTO detection code
authorTomi Valkeinen <tomi.valkeinen@ti.com>
Wed, 28 Sep 2016 07:42:03 +0000 (10:42 +0300)
committerTomi Valkeinen <tomi.valkeinen@ti.com>
Thu, 29 Sep 2016 08:21:16 +0000 (11:21 +0300)
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>
CMakeLists.txt
LTO.cmake [new file with mode: 0644]

index 9214bfdf12e243ba2c616b7436f6ecb5a22113cf..630685287dab154eaf1879762588fa3bd102200e 100644 (file)
@@ -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 (file)
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()