]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
CMake: Add support for CMAKE_LTO option.
authorTobias Hieta <tobias@plex.tv>
Thu, 9 Jan 2020 11:10:55 +0000 (12:10 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 13 Jan 2020 22:14:49 +0000 (23:14 +0100)
This enables Link Time Optimization. LTO is a proven technique for
optimizing across compilation units.

Closes #4799

CMakeLists.txt
lib/CMakeLists.txt
src/CMakeLists.txt

index 3ef8d0e4898a7d143d952819aae7adf54886be26..a8be8a332fb62dd6eab554ba4fe4cbbd0d4e5360 100644 (file)
@@ -80,6 +80,7 @@ if(WIN32)
   option(CURL_STATIC_CRT "Set to ON to build libcurl with static CRT on Windows (/MT)." OFF)
   option(ENABLE_INET_PTON "Set to OFF to prevent usage of inet_pton when building against modern SDKs while still requiring compatibility with older Windows versions, such as Windows XP, Windows Server 2003 etc." ON)
 endif()
+option(CURL_LTO "Turn on compiler Link Time Optimizations" OFF)
 
 cmake_dependent_option(ENABLE_THREADED_RESOLVER "Set to ON to enable threaded DNS lookup"
         ON "NOT ENABLE_ARES"
@@ -1165,6 +1166,23 @@ if(CURL_WERROR)
   endif()
 endif()
 
+if(CURL_LTO)
+  if(CMAKE_VERSION VERSION_LESS 3.9)
+    message(FATAL_ERROR "Requested LTO but your cmake version ${CMAKE_VERSION} is to old. You need at least 3.9")
+  endif()
+
+  cmake_policy(SET CMP0069 NEW)
+
+  include(CheckIPOSupported)
+  check_ipo_supported(RESULT CURL_HAS_LTO OUTPUT CURL_LTO_ERROR LANGUAGES C)
+  if(CURL_HAS_LTO)
+    message(STATUS "LTO supported and enabled")
+  else()
+    message(FATAL_ERROR "LTO was requested - but compiler doesn't support it\n${CURL_LTO_ERROR}")
+  endif()
+endif()
+
+
 # Ugly (but functional) way to include "Makefile.inc" by transforming it (= regenerate it).
 function(transform_makefile_inc INPUT_FILE OUTPUT_FILE)
   file(READ ${INPUT_FILE} MAKEFILE_INC_TEXT)
index a9c90b66507b14b1a271506cfa923570874b80ff..e73efb90aff99168f06224f7fc60b77cf75ef272 100644 (file)
@@ -96,6 +96,12 @@ endif()
 set_target_properties(${LIB_NAME} PROPERTIES PREFIX "")
 set_target_properties(${LIB_NAME} PROPERTIES IMPORT_PREFIX "")
 
+if(CURL_HAS_LTO)
+  set_target_properties(${LIB_NAME} PROPERTIES
+    INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
+    INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
+endif()
+
 if(WIN32)
   if(BUILD_SHARED_LIBS)
     # Add "_imp" as a suffix before the extension to avoid conflicting with the statically linked "libcurl.lib"
index 63e2b943d449207a471fa2099b27727d0d6df0ab..054541e40bd7ea08c39cb03ede129447f330dfe9 100644 (file)
@@ -46,6 +46,12 @@ add_executable(
   ${CURL_FILES}
   )
 
+if(CURL_HAS_LTO)
+  set_target_properties(${EXE_NAME} PROPERTIES
+    INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
+    INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
+endif()
+
 source_group("curlX source files" FILES ${CURLX_CFILES})
 source_group("curl source files" FILES ${CURL_CFILES})
 source_group("curl header files" FILES ${CURL_HFILES})