Require pybind 2.6 from environment for gmxapi Python package extension module.
[alexxy/gromacs.git] / python_packaging / src / CMakeLists.txt
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 # This CMakeLists.txt is not intended to be used directly, but either through
36 # setup.py or as an inclusion of the full GROMACS project.
37 # See https://manual.gromacs.org/current/gmxapi/userguide/install.html for more.
38 cmake_minimum_required(VERSION 3.16.3)
39
40 # This needs to be set before project() in order to pick up toolchain files
41 #list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake)
42
43 # OS X deployment target should be >=10.14 for modern C++ compatibility.
44 # Reference https://scikit-build.readthedocs.io/en/latest/generators.html#macosx
45 # and https://github.com/MacPython/wiki/wiki/Spinning-wheels
46 set(CMAKE_OSX_DEPLOYMENT_TARGET 10.14 CACHE STRING
47     "OS X deployment target below 10.14 does not use modern standard library")
48 set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING
49     "OS X should build Python package for 64-bit architecture"
50     FORCE)
51
52 # Note that this is the gmxapi._gmxapi Python bindings package version,
53 # not the C++ API version. It is not essential that it match the pure Python
54 # package version, but is likely to do so.
55 project(gmxapi)
56
57 # Check if Python package is being built directly or via add_subdirectory
58 set(GMXAPI_MASTER_PROJECT OFF)
59 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
60     set(GMXAPI_MASTER_PROJECT ON)
61     if (NOT Python3_FIND_STRATEGY)
62         # If the user provides a hint for the Python installation with Python3_ROOT_DIR,
63         # prevent FindPython3 from overriding the choice with a newer Python version
64         # when CMP0094 is set to OLD.
65         set(Python3_FIND_STRATEGY LOCATION)
66     endif ()
67     if(NOT Python3_FIND_VIRTUALENV)
68         # We advocate using Python venvs to manage package availability, so by default
69         # we want to preferentially discover user-space software.
70         set(Python3_FIND_VIRTUALENV FIRST)
71     endif()
72 endif()
73
74 set(CMAKE_CXX_STANDARD 17)
75 set(CMAKE_CXX_STANDARD_REQUIRED ON)
76
77 # Only interpret if() arguments as variables or keywords when unquoted.
78 cmake_policy(SET CMP0054 NEW)
79 # honor the language standard settings for try_compile()
80 cmake_policy(SET CMP0067 NEW)
81 if(POLICY CMP0074) #3.12
82     # Allow gmxapi_ROOT hint.
83     cmake_policy(SET CMP0074 NEW)
84 endif()
85
86 find_package(Python3 3.7 COMPONENTS Interpreter Development)
87 find_package(pybind11 2.6 CONFIG)
88 # If we are not running through setup.py, we may need to look for the pybind11 headers.
89 if (NOT pybind11_FOUND)
90     execute_process(
91         COMMAND
92         "${Python3_EXECUTABLE}" -c
93         "import pybind11; print(pybind11.get_cmake_dir())"
94         OUTPUT_VARIABLE _tmp_dir
95         OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ECHO STDOUT)
96     list(APPEND CMAKE_PREFIX_PATH "${_tmp_dir}")
97     find_package(pybind11 2.6 CONFIG)
98 endif ()
99 if (NOT pybind11_FOUND)
100     message(FATAL_ERROR "Python package build dependencies not found with interpreter ${Python3_EXECUTABLE}. "
101             "See https://manual.gromacs.org/current/gmxapi/userguide/install.html")
102 endif ()
103
104 if(GMXAPI_MASTER_PROJECT)
105     find_package(gmxapi 0.2 REQUIRED
106                  HINTS "$ENV{GROMACS_DIR}"
107                  )
108     if (gmxapi_VERSION VERSION_LESS 0.2.1)
109         message(WARNING "Your GROMACS installation does not support custom MD plugins. "
110                 "If you need this feature, please install GROMACS 2021.3 or higher.")
111     endif ()
112 else()
113     # Building as part of the GROMACS master project. GROMACS CMake logic should
114     # not be processing this unless Python3 was appropriately detected.
115     if (NOT Python3_FOUND)
116         message(FATAL_ERROR "Error in CMake script. Please report GROMACS bug.")
117     endif ()
118
119     get_target_property(gmxapi_VERSION gmxapi VERSION)
120 endif()
121
122 if(gmxapi_FOUND)
123     set(_suffix "")
124     # GROMACS master branch and development branches may have divergent
125     # pre-release APIs. This check allows us to distinguish them and behave
126     # differently if needed. github.com/kassonlab/gromacs-gmxapi devel branch
127     # sets gmxapi_EXPERIMENTAL=TRUE. Upstream GROMACS master branch does not.
128     # Ref: https://github.com/kassonlab/gmxapi/issues/166
129     if(gmxapi_EXPERIMENTAL)
130         set(_suffix " (unofficial)")
131     endif()
132 endif()
133
134 message(STATUS "Configuring Python package for gmxapi version ${gmxapi_VERSION}${_suffix}")
135
136 # The Gromacs::gmxapi target could be imported from an existing installation or
137 # provided as an alias target within the GROMACS build tree.
138 if (NOT TARGET Gromacs::gmxapi)
139     message(FATAL_ERROR "Cannot build Python package without GROMACS gmxapi support.")
140 endif ()
141
142 # TODO(#3279): Provide user hints for mpi4py installation.
143 # Note that neither the Python package nor the Gromacs::gmxapi CMake target are
144 # built with MPI in any case, but they _should_ be built with a C++ compiler
145 # that is compatible with the available MPI compiler wrappers, and technically
146 # _that_ is what we want to help the user identify when installing mpi4py, even
147 # if libgromacs is not built with MPI support either.
148 # For convenience, it is fine if libgmxapi and _gmxapi are built with the mpi
149 # compiler wrapper.
150
151 pybind11_add_module(_gmxapi
152                     gmxapi/module.cpp
153                     gmxapi/export_context.cpp
154                     gmxapi/export_exceptions.cpp
155                     gmxapi/export_system.cpp
156                     gmxapi/export_tprfile.cpp
157                     gmxapi/pycontext.cpp
158                     gmxapi/pysystem.cpp
159                     )
160
161 if (gmxapi_VERSION VERSION_GREATER_EQUAL 0.2.1)
162     target_sources(_gmxapi PRIVATE gmxapi/launch_021.cpp)
163 else()
164     message(WARNING "Found an old gmxapi library version. Please consider updating your GROMACS installation.")
165     target_sources(_gmxapi PRIVATE gmxapi/launch_020.cpp)
166 endif()
167
168 target_include_directories(_gmxapi PRIVATE
169                            ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi
170                            ${CMAKE_CURRENT_BINARY_DIR}/gmxapi
171                            )
172
173 # RPATH management: make sure build artifacts can find GROMACS library.
174 set_target_properties(_gmxapi PROPERTIES SKIP_BUILD_RPATH FALSE)
175
176 if(GMXAPI_MASTER_PROJECT)
177     # TODO: This requirement is probably overly restrictive.
178     find_package(GROMACS 2021 REQUIRED
179                  NAMES gromacs gromacs_mpi
180                  HINTS "$ENV{GROMACS_DIR}"
181                  )
182 endif()
183
184 # Get details of GROMACS installation needed by the Python package at run time.
185
186 # Get the MPI capability.
187 get_target_property(_gmx_mpi Gromacs::gmxapi MPI)
188 if (${_gmx_mpi} STREQUAL "library")
189     set(_gmx_mpi_type "\"library\"")
190 elseif(${_gmx_mpi} STREQUAL "tmpi")
191     set(_gmx_mpi_type "\"tmpi\"")
192 elseif(${_gmx_mpi} STREQUAL "none")
193     set(_gmx_mpi_type "null")
194 else()
195     message(FATAL_ERROR "Unrecognized gmxapi MPI value: ${_gmx_mpi}")
196 endif ()
197 unset(_gmx_mpi)
198 # Get the path of the command line entry point and binary install directory.
199 if (NOT TARGET Gromacs::gmx)
200     message(FATAL_ERROR "GROMACS command line tool not found.")
201 endif ()
202 get_target_property(_gmx_executable_imported Gromacs::gmx IMPORTED)
203 if (_gmx_executable_imported)
204     get_target_property(_gmx_executable Gromacs::gmx LOCATION)
205     get_filename_component(_gmx_bindir ${_gmx_executable} DIRECTORY)
206     message(STATUS "Imported ${_gmx_bindir} executable.")
207     unset(_gmx_executable_imported)
208 else()
209     get_target_property(_gmx_bindir Gromacs::gmx RUNTIME_OUTPUT_DIRECTORY)
210     get_target_property(_gmx_executable Gromacs::gmx OUTPUT_NAME)
211     set(_gmx_executable "${_gmx_bindir}/${_gmx_executable}")
212     message(STATUS "Using ${_gmx_executable} from build tree.")
213 endif ()
214 if (NOT _gmx_bindir OR NOT _gmx_executable)
215     message(FATAL_ERROR "Could not get path for gmx wrapper binary.")
216 endif ()
217 configure_file(gmxapi/gmxconfig.json.in gmxapi/gmxconfig.json)
218 unset(_gmx_executable)
219 unset(_gmx_bindir)
220 unset(_gmx_mpi_type)
221
222 if (GMXAPI_MASTER_PROJECT)
223     set_target_properties(_gmxapi PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
224     set_target_properties(_gmxapi PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
225     target_link_libraries(_gmxapi PRIVATE Gromacs::gmxapi)
226     # The Python setup.py sets CMAKE_LIBRARY_OUTPUT_DIRECTORY and will be looking for generated files there.
227     file(COPY ${CMAKE_CURRENT_BINARY_DIR}/gmxapi/gmxconfig.json
228          DESTINATION ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
229 else()
230     # The rest of the logic in this conditional is to support the GMX_PYTHON_PACKAGE option
231     # for testing the gmxapi Python packages within a full GROMACS project build_command and.
232     # for building full GROMACS project documentation.
233
234     set(GMXAPI_PYTHON_STAGING_DIR ${CMAKE_CURRENT_BINARY_DIR}/gmxapi_staging)
235     # Instead, we should probably build a source package and alert the user of its location.
236     # We can use CMake to call the Python packaging tools to create an 'sdist'
237     # source distribution archive to be installed in the GROMACS installation
238     # destination. We can use the build directory as the working directory for
239     # easier clean-up, as well.
240     # TODO: (ref Issue #2896) Build and install 'sdist' with GROMACS.
241
242     # The Python module is being built against GROMACS in its build tree, so we will not install.
243     set_target_properties(_gmxapi PROPERTIES BUILD_WITH_INSTALL_RPATH FALSE)
244     target_link_libraries(_gmxapi PRIVATE Gromacs::gmxapi)
245     # However, we can still produce an importable package for documentation builds and
246     # basic testing in ${CMAKE_CURRENT_BINARY_DIR}/gmxapi_staging
247     set_target_properties(_gmxapi PROPERTIES
248                           LIBRARY_OUTPUT_DIRECTORY ${GMXAPI_PYTHON_STAGING_DIR}/gmxapi)
249     file(GLOB_RECURSE _py_sources
250          CONFIGURE_DEPENDS
251          ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi/*.py)
252     foreach(_package_file IN LISTS _py_sources)
253         get_filename_component(_absolute_dir ${_package_file} DIRECTORY)
254         file(RELATIVE_PATH _relative_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_absolute_dir})
255         file(COPY ${_package_file} DESTINATION ${GMXAPI_PYTHON_STAGING_DIR}/${_relative_dir})
256     endforeach()
257     file(COPY setup.py CMakeLists.txt DESTINATION ${GMXAPI_PYTHON_STAGING_DIR})
258     file(COPY ${CMAKE_CURRENT_BINARY_DIR}/gmxapi/gmxconfig.json DESTINATION ${GMXAPI_PYTHON_STAGING_DIR}/gmxapi)
259
260     # Unit test and build docs using PYTHONPATH=$CMAKE_CURRENT_BINARY_DIR/gmxapi_staging
261     set_target_properties(_gmxapi PROPERTIES staging_dir ${GMXAPI_PYTHON_STAGING_DIR})
262     # Note: Integration testing for multiple Python versions and/or CMake-driven
263     # sdist preparation could be performed with CMake custom_commands and custom_targets.
264 endif()
265
266 # When building as part of GROMACS umbrella project, add a testing target
267 # to the `check` target. Normal usage is to first install the Python package,
268 # then run `pytest` on the `tests` directory. Refer to gmxapi package documentation.
269 if(NOT GMXAPI_MASTER_PROJECT)
270         if (BUILD_TESTING)
271                 add_subdirectory(test)
272         endif()
273 endif()