Skip to content

Commit d1b3646

Browse files
committed
Implemented basic LTO/IPO callables
Currently only utilizing the IPO implementation of CMake, which itself only seems to work with LLVM's thin LTO and MSVC's /GL
1 parent 1f822d1 commit d1b3646

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ This is a collection of quite useful scripts that expand the possibilities for b
5353
- [Formatting `formatting.cmake`](#formatting-formattingcmake)
5454
- [clang-format](#clang-format)
5555
- [cmake-format](#cmake-format)
56+
- [Link Time Optimization / Interprocedural Optimization `link-time-optimization.cmake`](#link-time-optimization--interprocedural-optimization-link-time-optimizationcmake)
57+
- [Optional Arguments](#optional-arguments-4)
58+
- [REQUIRED](#required)
5659

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

@@ -349,3 +352,12 @@ file(GLOB_RECURSE CMAKE_FILES
349352
350353
cmake_format(TARGET_NAME ${CMAKE_FILES})
351354
```
355+
356+
## Link Time Optimization / Interprocedural Optimization [`link-time-optimization.cmake`](link-time-optimization.cmake)
357+
358+
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.
359+
360+
### Optional Arguments
361+
362+
#### REQUIRED
363+
If this is passed in, CMake configuration will fail with an error if LTO/IPO is not supported

link-time-optimization.cmake

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#
2+
# Copyright (C) 2021 by George Cave - gcave@stablecoder.ca
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
# use this file except in compliance with the License. You may obtain a copy of
6+
# the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations under
14+
# the License.
15+
16+
include(CheckIPOSupported)
17+
18+
# Checks for, and enables IPO/LTO for all following targets
19+
# ~~~
20+
# Optional:
21+
# REQUIRED - If this is passed in, CMake configuration will fail with an error if LTO/IPO is not supported
22+
# ~~~
23+
macro(link_time_optimization)
24+
# Argument parsing
25+
set(options REQUIRED)
26+
set(single_value_keywords)
27+
set(multi_value_keywords)
28+
cmake_parse_arguments(
29+
link_time_optimization "${options}" "${single_value_keywords}"
30+
"${multi_value_keywords}" ${ARGN})
31+
32+
check_ipo_supported(RESULT result OUTPUT output)
33+
if(result)
34+
# It's available, set it for all following items
35+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
36+
else()
37+
if(link_time_optimization_REQUIRED)
38+
message(
39+
FATAL_ERROR
40+
"Link Time Optimization not supported, but listed as REQUIRED: ${output}"
41+
)
42+
else()
43+
message(WARNING "Link Time Optimization not supported: ${output}")
44+
endif()
45+
endif()
46+
endmacro()
47+
48+
# Checks for, and enables IPO/LTO for the specified target
49+
# ~~~
50+
# Required:
51+
# TARGET_NAME - Name of the target to generate code coverage for
52+
# Optional:
53+
# REQUIRED - If this is passed in, CMake configuration will fail with an error if LTO/IPO is not supported
54+
# ~~~
55+
function(target_link_time_optimization TARGET_NAME)
56+
# Argument parsing
57+
set(options REQUIRED)
58+
set(single_value_keywords)
59+
set(multi_value_keywords)
60+
cmake_parse_arguments(
61+
target_link_time_optimization "${options}" "${single_value_keywords}"
62+
"${multi_value_keywords}" ${ARGN})
63+
64+
check_ipo_supported(RESULT result OUTPUT output)
65+
if(result)
66+
# It's available, set it for all following items
67+
set_property(TARGET ${TARGET_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION
68+
TRUE)
69+
else()
70+
if(target_link_time_optimization_REQUIRED)
71+
message(
72+
FATAL_ERROR
73+
"Link Time Optimization not supported, but listed as REQUIRED for the ${TARGET_NAME} target: ${output}"
74+
)
75+
else()
76+
message(WARNING "Link Time Optimization not supported: ${output}")
77+
endif()
78+
endif()
79+
endfunction()

0 commit comments

Comments
 (0)