|
| 1 | +#[=============================================================================[ |
| 2 | +# PHP/Optimization |
| 3 | +
|
| 4 | +Enable interprocedural optimization (IPO/LTO) on all targets, if supported. |
| 5 | +
|
| 6 | +This adds linker flag `-flto` if it is supported by the compiler to run standard |
| 7 | +link-time optimizer. |
| 8 | +
|
| 9 | +It can be also controlled more granular with the |
| 10 | +`CMAKE_INTERPROCEDURAL_OPTIMIZATION_<CONFIG>` variables based on the build type. |
| 11 | +
|
| 12 | +This module also checks whether IPO/LTO can be enabled based on the PHP |
| 13 | +configuration (due to global register variables) and compiler/platform. |
| 14 | +
|
| 15 | +https://cmake.org/cmake/help/latest/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html |
| 16 | +#]=============================================================================] |
| 17 | + |
| 18 | +include_guard(GLOBAL) |
| 19 | + |
| 20 | +# TODO: Recheck Clang errors as OBJECT libraries doesn't seem to work. |
| 21 | +# TODO: Recheck and add PHP_LTO option to docs. |
| 22 | + |
| 23 | +option(PHP_LTO "Build PHP with link time optimization (LTO) if supported") |
| 24 | +mark_as_advanced(PHP_LTO) |
| 25 | + |
| 26 | +block(PROPAGATE CMAKE_INTERPROCEDURAL_OPTIMIZATION) |
| 27 | + set(enableIpo ${PHP_LTO}) |
| 28 | + set(reason "") |
| 29 | + |
| 30 | + if(NOT PHP_LTO) |
| 31 | + set(enableIpo FALSE) |
| 32 | + # Disabled. |
| 33 | + elseif( |
| 34 | + CMAKE_C_COMPILER_ID STREQUAL "GNU" |
| 35 | + AND ( |
| 36 | + NOT DEFINED ZEND_GLOBAL_REGISTER_VARIABLES |
| 37 | + OR ZEND_GLOBAL_REGISTER_VARIABLES |
| 38 | + ) |
| 39 | + ) |
| 40 | + # Zend/zend_execute.c uses global register variables and IPO is for now |
| 41 | + # disabled when using GNU C compiler due to a bug: |
| 42 | + # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68384 |
| 43 | + set(reason "GCC global register variables") |
| 44 | + set(enableIpo FALSE) |
| 45 | + elseif(CMAKE_C_COMPILER_ID STREQUAL "AppleClang") |
| 46 | + # See: https://gitlab.kitware.com/cmake/cmake/-/issues/25202 |
| 47 | + set(reason "AppleClang") |
| 48 | + set(enableIpo FALSE) |
| 49 | + elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_SIZEOF_VOID_P EQUAL 4) |
| 50 | + # On 32-bit Linux machine, this produced undefined references linker errors. |
| 51 | + set(reason "Clang on 32-bit") |
| 52 | + set(enableIpo FALSE) |
| 53 | + endif() |
| 54 | + |
| 55 | + if(enableIpo) |
| 56 | + include(CheckIPOSupported) |
| 57 | + check_ipo_supported(RESULT supported OUTPUT reason) |
| 58 | + if(NOT supported) |
| 59 | + set(enableIpo FALSE) |
| 60 | + if(NOT reason) |
| 61 | + set(reason "unsupported") |
| 62 | + endif() |
| 63 | + endif() |
| 64 | + endif() |
| 65 | + |
| 66 | + if(enableIpo) |
| 67 | + message(STATUS "Interprocedural optimization (IPO/LTO) enabled") |
| 68 | + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) |
| 69 | + else() |
| 70 | + if(reason) |
| 71 | + set(reason " (${reason})") |
| 72 | + endif() |
| 73 | + message(STATUS "Interprocedural optimization (IPO/LTO) disabled${reason}") |
| 74 | + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE) |
| 75 | + endif() |
| 76 | +endblock() |
0 commit comments