summaryrefslogtreecommitdiff
path: root/Modules/Compiler/GNU.cmake
diff options
context:
space:
mode:
authorRuslan Baratov <ruslan_baratov@yahoo.com>2017-03-28 14:47:42 +0800
committerBrad King <brad.king@kitware.com>2017-03-30 14:56:52 -0400
commitdfa8263f4be4f1413b73c81649fdc4567a71e56a (patch)
tree23e540d2dcb39793a0f9ef503e392fb854beb985 /Modules/Compiler/GNU.cmake
parent1588a577d16cfb1a689a444b1db1df3ccff2cc3d (diff)
downloadcmake-dfa8263f4be4f1413b73c81649fdc4567a71e56a.tar.gz
Implement interprocedural optimization for GNU compilers
Honor the `INTERPROCEDURAL_OPTIMIZATION` target property for GNU compilers by activating their link-time-optimization (LTO) flags.
Diffstat (limited to 'Modules/Compiler/GNU.cmake')
-rw-r--r--Modules/Compiler/GNU.cmake39
1 files changed, 39 insertions, 0 deletions
diff --git a/Modules/Compiler/GNU.cmake b/Modules/Compiler/GNU.cmake
index b67002c4bd..4b1c598465 100644
--- a/Modules/Compiler/GNU.cmake
+++ b/Modules/Compiler/GNU.cmake
@@ -45,4 +45,43 @@ macro(__compiler_gnu lang)
if(NOT APPLE OR NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4) # work around #4462
set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
endif()
+
+ set(_CMAKE_IPO_SUPPORTED_BY_CMAKE YES)
+ set(_CMAKE_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO)
+
+ # '-flto' introduced since GCC 4.5:
+ # * https://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Option-Summary.html (no)
+ # * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Option-Summary.html (yes)
+ if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.5)
+ set(_CMAKE_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
+ set(__lto_flags -flto)
+
+ if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 4.7)
+ # '-ffat-lto-objects' introduced since GCC 4.7:
+ # * https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Option-Summary.html (no)
+ # * https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Option-Summary.html (yes)
+ list(APPEND __lto_flags -fno-fat-lto-objects)
+ endif()
+
+ set(CMAKE_${lang}_COMPILE_OPTIONS_IPO ${__lto_flags})
+
+ # Need to use version of 'ar'/'ranlib' with plugin support.
+ # Quote from [documentation][1]:
+ #
+ # To create static libraries suitable for LTO,
+ # use gcc-ar and gcc-ranlib instead of ar and ranlib
+ #
+ # [1]: https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gcc/Optimize-Options.html
+ set(CMAKE_${lang}_ARCHIVE_CREATE_IPO
+ "${CMAKE_GCC_AR} cr <TARGET> <LINK_FLAGS> <OBJECTS>"
+ )
+
+ set(CMAKE_${lang}_ARCHIVE_APPEND_IPO
+ "${CMAKE_GCC_AR} r <TARGET> <LINK_FLAGS> <OBJECTS>"
+ )
+
+ set(CMAKE_${lang}_ARCHIVE_FINISH_IPO
+ "${CMAKE_GCC_RANLIB} <TARGET>"
+ )
+ endif()
endmacro()