Skip to content

Commit

Permalink
Merge pull request #143 from xanderlent/cmake-lto
Browse files Browse the repository at this point in the history
Integrate with CMake's LTO support

Fixes #140
  • Loading branch information
PJK authored Apr 11, 2020
2 parents 5c73f08 + 4d860bc commit 1151d00
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ else()
set(CBOR_RESTRICT_SPECIFIER "restrict")

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -g -ggdb -DDEBUG=true")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -flto -Wall -DNDEBUG")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall -DNDEBUG")

if(SANITIZE)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} \
Expand All @@ -80,7 +80,6 @@ else()
endif()

set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto")


include(CheckTypeSize)
Expand Down Expand Up @@ -124,12 +123,37 @@ install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h DESTINATION include/cbo
# Make the header visible at compile time
include_directories(${PROJECT_BINARY_DIR})

# CMake >= 3.9.0 enables LTO for GCC and Clang with INTERPROCEDURAL_OPTIMIZATION
# Policy CMP0069 enables this behavior when we set the minimum CMake version < 3.9.0
# Checking for LTO support before setting INTERPROCEDURAL_OPTIMIZATION is mandatory with CMP0069 set to NEW.
set(use_lto FALSE)
if(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0")
cmake_policy(SET CMP0069 NEW)
# Require LTO support to build libcbor with newer CMake versions
include(CheckIPOSupported)
check_ipo_supported(RESULT use_lto)
endif(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0")
if(use_lto)
message(STATUS "LTO is enabled")
else()
message(STATUS "LTO is not enabled")
endif(use_lto)

subdirs(src)
if(use_lto)
set_property(DIRECTORY src PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif(use_lto)

if (WITH_TESTS)
subdirs(test)
if(use_lto)
set_property(DIRECTORY test PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif(use_lto)
endif (WITH_TESTS)

if (WITH_EXAMPLES)
subdirs(examples)
subdirs(examples)
if(use_lto)
set_property(DIRECTORY examples PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif(use_lto)
endif (WITH_EXAMPLES)

0 comments on commit 1151d00

Please sign in to comment.