From 07d6f352a2b3bfdc8de0fb7f0fc58346502e0ee1 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Fri, 14 May 2021 13:59:15 -0700 Subject: [PATCH 1/2] Docs: CMake LTO honor global flag In this example, the IPO/LTO flags should not be added if the general property is already set for all targets via `CMAKE_INTERPROCEDURAL_OPTIMIZATION`. Also, if `CMAKE_INTERPROCEDURAL_OPTIMIZATION` is set to `OFF`, LTO flags should also not be added. This is sometimes needed when working around compiler bugs with IPO/LTO, e.g. on ppc64le with some toolchains. --- docs/compiling.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/compiling.rst b/docs/compiling.rst index 3a8a270d58..b8bb65ce7a 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -500,7 +500,10 @@ You can use these targets to build complex applications. For example, the add_library(example MODULE main.cpp) - target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras) + target_link_libraries(example PRIVATE pybind11::module pybind11::windows_extras) + if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION) + target_link_libraries(example PRIVATE pybind11::lto) + endif() pybind11_extension(example) pybind11_strip(example) From 9851db19adf654cdd206e356b00b81b5766eecdb Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Fri, 14 May 2021 14:23:37 -0700 Subject: [PATCH 2/2] Docs: Update Guidance for LTO & CMake Quirks --- docs/compiling.rst | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/compiling.rst b/docs/compiling.rst index b8bb65ce7a..9278e6b729 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -500,10 +500,7 @@ You can use these targets to build complex applications. For example, the add_library(example MODULE main.cpp) - target_link_libraries(example PRIVATE pybind11::module pybind11::windows_extras) - if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION) - target_link_libraries(example PRIVATE pybind11::lto) - endif() + target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras) pybind11_extension(example) pybind11_strip(example) @@ -511,6 +508,26 @@ You can use these targets to build complex applications. For example, the set_target_properties(example PROPERTIES CXX_VISIBILITY_PRESET "hidden" CUDA_VISIBILITY_PRESET "hidden") +Since prior to CMake 3.18 the ``INTERPROCEDURAL_OPTIMIZATION`` property exists but is not working reliably, a manual user section around linking the legacy ``pybind11::lto`` target should look like this: + +.. code-block:: cmake + + # LTO/IPO: CMake target properties work well for 3.18+ and are buggy before + set(_USE_PY_LTO ON) # default shall be ON + if(DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION) # overwrite default if defined + if(NOT CMAKE_INTERPROCEDURAL_OPTIMIZATION) + set(_USE_PY_LTO OFF) + endif() + endif() + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.18) + set_target_properties(example PROPERTIES + INTERPROCEDURAL_OPTIMIZATION ${_USE_PY_LTO}) + else() + if(_USE_PY_LTO) + target_link_libraries(example PRIVATE pybind11::lto) + endif() + endif() + Instead of setting properties, you can set ``CMAKE_*`` variables to initialize these correctly. .. warning::