Merge remote-tracking branch 'origin/release-2021' into merge-2021-into-master
[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 allows source distributions of the gmxapi Python package
36 # to rely on scikit-build for support of various Python packaging systems. The
37 # simplest use case is to allow the `setup.py` file to invoke skbuild to
38 # configure and run CMake. CMake could be invoked directly by the user or a
39 # parent package, but the Python distribution would not be packaged automatically.
40 # Reference https://gitlab.com/gromacs/gromacs/-/issues/2896 for additional discussion.
41 cmake_minimum_required(VERSION 3.16.3)
42
43 # This needs to be set before project() in order to pick up toolchain files
44 #list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake)
45
46 # OS X deployment target should be >=10.14 for modern C++ compatibility.
47 # Reference https://scikit-build.readthedocs.io/en/latest/generators.html#macosx
48 # and https://github.com/MacPython/wiki/wiki/Spinning-wheels
49 set(CMAKE_OSX_DEPLOYMENT_TARGET 10.14 CACHE STRING
50     "OS X deployment target below 10.14 does not use modern standard library")
51 set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING
52     "OS X should build Python package for 64-bit architecture"
53     FORCE)
54
55 # Note that this is the gmxapi._gmxapi Python bindings package version,
56 # not the C++ API version. It is not essential that it match the pure Python
57 # package version, but is likely to do so.
58 project(gmxapi VERSION 0.3.0)
59
60 # Check if Python package is being built directly or via add_subdirectory
61 set(GMXAPI_MASTER_PROJECT OFF)
62 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
63     set(GMXAPI_MASTER_PROJECT ON)
64 endif()
65
66 set(CMAKE_CXX_STANDARD 17)
67 set(CMAKE_CXX_STANDARD_REQUIRED ON)
68
69 # Only interpret if() arguments as variables or keywords when unquoted.
70 cmake_policy(SET CMP0054 NEW)
71 # honor the language standard settings for try_compile()
72 cmake_policy(SET CMP0067 NEW)
73 if(POLICY CMP0074) #3.12
74     # Allow gmxapi_ROOT hint.
75     cmake_policy(SET CMP0074 NEW)
76 endif()
77
78 if(GMXAPI_MASTER_PROJECT)
79     find_package(gmxapi 0.2 REQUIRED
80                  HINTS "$ENV{GROMACS_DIR}"
81                  )
82     if (gmxapi_VERSION VERSION_LESS 0.2.1)
83         message(WARNING "Your GROMACS installation does not support custom MD plugins. "
84                 "If you need this feature, please install GROMACS 2021.3 or higher.")
85     endif ()
86 endif()
87 if(gmxapi_FOUND)
88     set(_suffix "")
89     # GROMACS master branch and development branches may have divergent
90     # pre-release APIs. This check allows us to distinguish them and behave
91     # differently if needed. github.com/kassonlab/gromacs-gmxapi devel branch
92     # sets gmxapi_EXPERIMENTAL=TRUE. Upstream GROMACS master branch does not.
93     # Ref: https://github.com/kassonlab/gmxapi/issues/166
94     if(gmxapi_EXPERIMENTAL)
95         set(_suffix " (unofficial)")
96     endif()
97     message(STATUS "Found gmxapi version ${gmxapi_VERSION}${_suffix}")
98 endif()
99
100 # The Gromacs::gmxapi target could be imported from an existing installation or
101 # provided as an alias target within the GROMACS build tree.
102 if (NOT TARGET Gromacs::gmxapi)
103     message(FATAL_ERROR "Cannot build Python package without GROMACS gmxapi support.")
104 endif ()
105
106 # TODO: Provide user hints for mpi4py installation.
107 # Note that neither the Python package nor the Gromacs::gmxapi CMake target are
108 # built with MPI in any case, but they _should_ be built with a C++ compiler
109 # that is compatible with the available MPI compiler wrappers, and technically
110 # _that_ is what we want to help the user identify when installing mpi4py, even
111 # if libgromacs is not built with MPI support either.
112 # For convenience, it is fine if libgmxapi and _gmxapi are built with the mpi
113 # compiler wrapper.
114
115 option(GMXAPI_USE_BUNDLED_PYBIND
116        "Use pybind11 headers bundled with this repository. If OFF, CMake does `find_package(pybind11)`."
117        ON)
118 if(GMXAPI_USE_BUNDLED_PYBIND)
119     add_subdirectory(external/pybind)
120 else()
121     # Reference https://gitlab.com/gromacs/gromacs/-/issues/2896
122     find_package(pybind11 2.2 REQUIRED)
123 endif()
124
125 set(GMXAPI_PYTHON_EXTENSION_SOURCES
126     gmxapi/module.cpp
127     gmxapi/export_context.cpp
128     gmxapi/export_exceptions.cpp
129     gmxapi/export_system.cpp
130     gmxapi/export_tprfile.cpp
131     gmxapi/pycontext.cpp
132     gmxapi/pysystem.cpp
133     )
134
135 pybind11_add_module(_gmxapi
136                     ${GMXAPI_PYTHON_EXTENSION_SOURCES}
137                     )
138
139 if (gmxapi_VERSION VERSION_GREATER_EQUAL 0.2.1)
140     target_sources(_gmxapi PRIVATE gmxapi/launch_021.cpp)
141 else()
142     target_sources(_gmxapi PRIVATE gmxapi/launch_020.cpp)
143 endif()
144
145 target_include_directories(_gmxapi PRIVATE
146                            ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi
147                            ${CMAKE_CURRENT_BINARY_DIR}/gmxapi
148                            )
149
150 # RPATH management: make sure build artifacts can find GROMACS library.
151 set_target_properties(_gmxapi PROPERTIES SKIP_BUILD_RPATH FALSE)
152
153 # Python sources (*.py) will be packaged by scikit-build and setuptools.
154 # Note that library targets are built in CMAKE_LIBRARY_OUTPUT_DIRECTORY if not otherwise specified.
155 # This may be an unexpected location, whether inherited from the GROMACS build tree
156 # or the SKBUILD framework. Note, also, that when scikit-build is invoked with setup.py,
157 # the CMake build takes place in a subdirectory of ./_skbuild/.
158 set(GMXAPI_PYTHON_STAGING_DIR ${CMAKE_CURRENT_BINARY_DIR}/gmxapi_staging)
159 set_target_properties(_gmxapi PROPERTIES
160                       LIBRARY_OUTPUT_DIRECTORY ${GMXAPI_PYTHON_STAGING_DIR}/gmxapi)
161
162 if(GMXAPI_MASTER_PROJECT)
163     # TODO: This requirement is probably overly restrictive.
164     find_package(GROMACS 2021 REQUIRED
165                  NAMES gromacs gromacs_mpi
166                  HINTS "$ENV{GROMACS_DIR}"
167                  )
168 endif()
169
170 # Get details of GROMACS installation needed by the Python package at run time.
171
172 # Get the MPI capability.
173 get_target_property(_gmx_mpi Gromacs::gmxapi MPI)
174 if (${_gmx_mpi} STREQUAL "library")
175     set(_gmx_mpi_type "\"library\"")
176 elseif(${_gmx_mpi} STREQUAL "tmpi")
177     set(_gmx_mpi_type "\"tmpi\"")
178 elseif(${_gmx_mpi} STREQUAL "none")
179     set(_gmx_mpi_type "null")
180 else()
181     message(FATAL_ERROR "Unrecognized gmxapi MPI value: ${_gmx_mpi}")
182 endif ()
183 unset(_gmx_mpi)
184 # Get the path of the command line entry point and binary install directory.
185 if (NOT TARGET Gromacs::gmx)
186     message(FATAL_ERROR "GROMACS command line tool not found.")
187 endif ()
188 get_target_property(_gmx_executable_imported Gromacs::gmx IMPORTED)
189 if (_gmx_executable_imported)
190     get_target_property(_gmx_executable Gromacs::gmx LOCATION)
191     get_filename_component(_gmx_bindir ${_gmx_executable} DIRECTORY)
192     message(STATUS "Imported ${_gmx_bindir} executable.")
193     unset(_gmx_executable_imported)
194 else()
195     get_target_property(_gmx_bindir Gromacs::gmx RUNTIME_OUTPUT_DIRECTORY)
196     get_target_property(_gmx_executable Gromacs::gmx OUTPUT_NAME)
197     set(_gmx_executable "${_gmx_bindir}/${_gmx_executable}")
198     message(STATUS "Using ${_gmx_executable} from build tree.")
199 endif ()
200 if (NOT _gmx_bindir OR NOT _gmx_executable)
201     message(FATAL_ERROR "Could not get path for gmx wrapper binary.")
202 endif ()
203 configure_file(gmxapi/gmxconfig.json.in ${GMXAPI_PYTHON_STAGING_DIR}/gmxapi/gmxconfig.json)
204 unset(_gmx_executable)
205 unset(_gmx_bindir)
206 unset(_gmx_mpi_type)
207
208 # scikit-build sets SKBUILD when running Python packaging tools through setup.py
209 # (e.g. with pip)
210 if(SKBUILD)
211     # The Python module is being built for a GROMACS installation.
212     set_target_properties(_gmxapi PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
213     set_target_properties(_gmxapi PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
214     target_link_libraries(_gmxapi PRIVATE Gromacs::gmxapi)
215     # By default, scikit-build expects the library to be installed into a directory
216     # named for the Python package as in setup.py.
217     install(TARGETS _gmxapi LIBRARY DESTINATION gmxapi)
218     install(FILES ${GMXAPI_PYTHON_STAGING_DIR}/gmxapi/gmxconfig.json DESTINATION gmxapi)
219 else()
220     # The Python module is being built against GROMACS in its build tree.
221     # Note: we do not have plans to install the staged package when SKBUILD != TRUE
222     set_target_properties(_gmxapi PROPERTIES BUILD_WITH_INSTALL_RPATH FALSE)
223     target_link_libraries(_gmxapi PRIVATE Gromacs::gmxapi)
224
225     # TODO: Determine packaging and installation cases and implementation.
226     # Reference https://gitlab.com/gromacs/gromacs/-/issues/2896 for additional discussion.
227     # Currently, CMake should be run by scikit-build through setup.py for proper Python packaging.
228     # We don't want to install by default in the outer scope of the GROMACS
229     # CMake procedure because we could end up trying to install to a system directory
230     # the user did not intend, or a user might install a Python-installation-specific
231     # package into an overly generic GROMACS path.
232
233     # Instead, we should probably build a source package and alert the user of its location.
234     # We can use CMake to call the Python packaging tools to create an 'sdist'
235     # source distribution archive to be installed in the GROMACS installation
236     # destination. We can use the build directory as the working directory for
237     # easier clean-up, as well.
238     # TODO: (ref Issue #2896) Build and install 'sdist' with GROMACS.
239
240     # However, we can still produce an importable package for documentation builds and
241     # basic testing in ${CMAKE_CURRENT_BINARY_DIR}/gmxapi_staging
242     file(GLOB_RECURSE _py_sources
243          CONFIGURE_DEPENDS
244          ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi/*.py)
245     foreach(_package_file IN LISTS _py_sources)
246         get_filename_component(_absolute_dir ${_package_file} DIRECTORY)
247         file(RELATIVE_PATH _relative_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_absolute_dir})
248         file(COPY ${_package_file} DESTINATION ${GMXAPI_PYTHON_STAGING_DIR}/${_relative_dir})
249     endforeach()
250     file(COPY setup.py CMakeLists.txt DESTINATION ${GMXAPI_PYTHON_STAGING_DIR})
251     # Set CMake variable pybind11_DIR to ${CMAKE_CURRENT_SOURCE_DIR}/external/pybind/tools
252     # if re-invoking CMake (including via Python setuptools) for the files in gmxapi_staging.
253
254     # Unit test and build docs using PYTHONPATH=$CMAKE_CURRENT_BINARY_DIR/gmxapi_staging
255     set_target_properties(_gmxapi PROPERTIES staging_dir ${GMXAPI_PYTHON_STAGING_DIR})
256     # Note: Integration testing for multiple Python versions and/or CMake-driven
257     # sdist preparation could be performed with CMake custom_commands and custom_targets.
258 endif()
259
260 # When building as part of GROMACS umbrella project, add a testing target
261 # to the `check` target. Normal usage is to first install the Python package,
262 # then run `pytest` on the `tests` directory. Refer to gmxapi package documentation.
263 if(NOT GMXAPI_MASTER_PROJECT)
264         if (BUILD_TESTING)
265                 add_subdirectory(test)
266         endif()
267 endif()