|
| 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