Skip to content

CMake: Expose Python LTO Control #980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Other

- ADIOS2: require version 2.7.0+ #927
- pybind11: require version 2.6.2+ #977
- CMake: Expose Python LTO Control #980


0.13.4
Expand Down
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,25 @@ if(openPMD_HAVE_PYTHON)
src/binding/python/UnitDimension.cpp
)
target_link_libraries(openPMD.py PRIVATE openPMD)
target_link_libraries(openPMD.py PRIVATE pybind11::module pybind11::lto pybind11::windows_extras)
target_link_libraries(openPMD.py PRIVATE pybind11::module pybind11::windows_extras)

# 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()
message(STATUS "Python LTO/IPO: ${_USE_PY_LTO}")
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.18)
set_target_properties(openPMD.py PROPERTIES
INTERPROCEDURAL_OPTIMIZATION ${_USE_PY_LTO})
else()
if(_USE_PY_LTO)
target_link_libraries(openPMD.py PRIVATE pybind11::lto)
endif()
endif()
unset(_USE_PY_LTO)

pybind11_extension(openPMD.py)
pybind11_strip(openPMD.py)
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ python3 -m pip install -U cmake
openPMD_USE_MPI=ON python3 -m pip install openpmd-api --no-binary openpmd-api
```

For some exotic architectures and compilers, you might need to disable a compiler feature called [link-time/interprocedural optimization](https://en.wikipedia.org/wiki/Interprocedural_optimization) if you encounter linking problems:
```bash
export CMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF
# optional: --user
python3 -m pip install openpmd-api --no-binary openpmd-api
```

### From Source

[![Source Use Case](https://img.shields.io/badge/use_case-development-brightgreen)](https://cmake.org)
Expand Down
8 changes: 8 additions & 0 deletions docs/source/install/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ If MPI-support shall be enabled, we always have to recompile:
# optional: --user
openPMD_USE_MPI=ON python3 -m pip install openpmd-api --no-binary openpmd-api

For some exotic architectures and compilers, you might need to disable a compiler feature called `link-time/interprocedural optimization <https://en.wikipedia.org/wiki/Interprocedural_optimization>`_ if you encounter linking problems:

.. code-block:: bash

export CMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF
# optional: --user
python3 -m pip install openpmd-api --no-binary openpmd-api

.. _install-cmake:

.. only:: html
Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def build_extension(self, ext):
# Windows: has no RPath concept, all `.dll`s must be in %PATH%
# or same dir as calling executable
]
if CMAKE_INTERPROCEDURAL_OPTIMIZATION is not None:
cmake_args.append('-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=' +
CMAKE_INTERPROCEDURAL_OPTIMIZATION)
if sys.platform == "darwin":
cmake_args.append('-DCMAKE_INSTALL_RPATH=@loader_path')
else:
Expand Down Expand Up @@ -133,6 +136,8 @@ def build_extension(self, ext):
BUILD_TESTING)
BUILD_EXAMPLES = os.environ.get('openPMD_BUILD_EXAMPLES',
BUILD_EXAMPLES)
CMAKE_INTERPROCEDURAL_OPTIMIZATION = os.environ.get(
'CMAKE_INTERPROCEDURAL_OPTIMIZATION', None)

# https://cmake.org/cmake/help/v3.0/command/if.html
if openPMD_USE_MPI.upper() in ['1', 'ON', 'TRUE', 'YES']:
Expand Down