Docs: Update Guidance for LTO & CMake Quirks

This commit is contained in:
Axel Huebl 2021-05-14 14:23:37 -07:00
parent 07d6f352a2
commit 9851db19ad
No known key found for this signature in database
GPG Key ID: 472D2C364E21D94B
1 changed files with 21 additions and 4 deletions

View File

@ -500,10 +500,7 @@ You can use these targets to build complex applications. For example, the
add_library(example MODULE main.cpp) add_library(example MODULE main.cpp)
target_link_libraries(example PRIVATE pybind11::module pybind11::windows_extras) target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras)
if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION)
target_link_libraries(example PRIVATE pybind11::lto)
endif()
pybind11_extension(example) pybind11_extension(example)
pybind11_strip(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" set_target_properties(example PROPERTIES CXX_VISIBILITY_PRESET "hidden"
CUDA_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. Instead of setting properties, you can set ``CMAKE_*`` variables to initialize these correctly.
.. warning:: .. warning::