Skip to content

Commit

Permalink
Implemented basic LTO/IPO callables
Browse files Browse the repository at this point in the history
Currently only utilizing the IPO implementation of CMake, which itself
only seems to work with LLVM's thin LTO and MSVC's /GL
  • Loading branch information
StableCoder committed Mar 28, 2021
1 parent 1f822d1 commit d1b3646
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ This is a collection of quite useful scripts that expand the possibilities for b
- [Formatting `formatting.cmake`](#formatting-formattingcmake)
- [clang-format](#clang-format)
- [cmake-format](#cmake-format)
- [Link Time Optimization / Interprocedural Optimization `link-time-optimization.cmake`](#link-time-optimization--interprocedural-optimization-link-time-optimizationcmake)
- [Optional Arguments](#optional-arguments-4)
- [REQUIRED](#required)

## C++ Standards [`c++-standards.cmake`](c++-standards.cmake)

Expand Down Expand Up @@ -349,3 +352,12 @@ file(GLOB_RECURSE CMAKE_FILES
cmake_format(TARGET_NAME ${CMAKE_FILES})
```

## Link Time Optimization / Interprocedural Optimization [`link-time-optimization.cmake`](link-time-optimization.cmake)

There are two callable objects here, `link_time_optimization` which applies LTO/IPO for all following targets, and `target_link_time_optimization` which applies it to a specified target.

### Optional Arguments

#### REQUIRED
If this is passed in, CMake configuration will fail with an error if LTO/IPO is not supported
79 changes: 79 additions & 0 deletions link-time-optimization.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#
# Copyright (C) 2021 by George Cave - [email protected]
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

include(CheckIPOSupported)

# Checks for, and enables IPO/LTO for all following targets
# ~~~
# Optional:
# REQUIRED - If this is passed in, CMake configuration will fail with an error if LTO/IPO is not supported
# ~~~
macro(link_time_optimization)
# Argument parsing
set(options REQUIRED)
set(single_value_keywords)
set(multi_value_keywords)
cmake_parse_arguments(
link_time_optimization "${options}" "${single_value_keywords}"
"${multi_value_keywords}" ${ARGN})

check_ipo_supported(RESULT result OUTPUT output)
if(result)
# It's available, set it for all following items
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
else()
if(link_time_optimization_REQUIRED)
message(
FATAL_ERROR
"Link Time Optimization not supported, but listed as REQUIRED: ${output}"
)
else()
message(WARNING "Link Time Optimization not supported: ${output}")
endif()
endif()
endmacro()

# Checks for, and enables IPO/LTO for the specified target
# ~~~
# Required:
# TARGET_NAME - Name of the target to generate code coverage for
# Optional:
# REQUIRED - If this is passed in, CMake configuration will fail with an error if LTO/IPO is not supported
# ~~~
function(target_link_time_optimization TARGET_NAME)
# Argument parsing
set(options REQUIRED)
set(single_value_keywords)
set(multi_value_keywords)
cmake_parse_arguments(
target_link_time_optimization "${options}" "${single_value_keywords}"
"${multi_value_keywords}" ${ARGN})

check_ipo_supported(RESULT result OUTPUT output)
if(result)
# It's available, set it for all following items
set_property(TARGET ${TARGET_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION
TRUE)
else()
if(target_link_time_optimization_REQUIRED)
message(
FATAL_ERROR
"Link Time Optimization not supported, but listed as REQUIRED for the ${TARGET_NAME} target: ${output}"
)
else()
message(WARNING "Link Time Optimization not supported: ${output}")
endif()
endif()
endfunction()

0 comments on commit d1b3646

Please sign in to comment.