C++ code and Python bindings for TPR parameter read/write.
[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, 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://redmine.gromacs.org/issues/2896 for additional discussion.
41 cmake_minimum_required(VERSION 3.9.6)
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.9 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.9 CACHE STRING
50     "OS X deployment target below 10.9 does not use modern standard library"
51     FORCE)
52 set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING
53     "OS X should build Python package for 64-bit architecture"
54     FORCE)
55
56 project(gmxapi VERSION 0.1.0)
57
58 # Check if Python package is being built directly or via add_subdirectory
59 set(GMXAPI_MASTER_PROJECT OFF)
60 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
61     set(GMXAPI_MASTER_PROJECT ON)
62 endif()
63
64 set(CMAKE_CXX_STANDARD 14)
65 set(CMAKE_CXX_STANDARD_REQUIRED ON)
66
67 # Only interpret if() arguments as variables or keywords when unquoted.
68 cmake_policy(SET CMP0054 NEW)
69 # honor the language standard settings for try_compile()
70 cmake_policy(SET CMP0067 NEW)
71
72 if(GMXAPI_MASTER_PROJECT)
73     find_package(gmxapi 0.0.8 REQUIRED
74                  HINTS "$ENV{GROMACS_DIR}"
75                  )
76 endif()
77 if(gmxapi_FOUND)
78     set(_suffix "")
79     # GROMACS master branch and development branches may have divergent
80     # pre-release APIs. This check allows us to distinguish them and behave
81     # differently if needed. github.com/kassonlab/gromacs-gmxapi devel branch
82     # sets gmxapi_EXPERIMENTAL=TRUE. Upstream GROMACS master branch does not.
83     # Ref: https://github.com/kassonlab/gmxapi/issues/166
84     if(gmxapi_EXPERIMENTAL)
85         set(_suffix " (unofficial)")
86     endif()
87     message(STATUS "Found gmxapi version ${gmxapi_VERSION}${_suffix}")
88 endif()
89
90 option(GMXAPI_USE_BUNDLED_PYBIND
91        "Use pybind11 headers bundled with this repository. If OFF, CMake does `find_package(pybind11)`."
92        ON)
93 if(GMXAPI_USE_BUNDLED_PYBIND)
94     add_subdirectory(external/pybind)
95 else()
96     # Reference https://redmine.gromacs.org/issues/2896
97     find_package(pybind11 2.2 REQUIRED)
98 endif()
99
100 set(GMXAPI_PYTHON_EXTENSION_SOURCES
101     gmxapi/module.cpp
102     gmxapi/export_context.cpp
103     gmxapi/export_exceptions.cpp
104     gmxapi/export_system.cpp
105     gmxapi/export_tprfile.cpp
106     gmxapi/pycontext.cpp
107     gmxapi/pysystem.cpp
108     gmxapi/tprfile.cpp
109     )
110
111 pybind11_add_module(_gmxapi
112                     ${GMXAPI_PYTHON_EXTENSION_SOURCES}
113                     )
114
115 target_include_directories(_gmxapi PRIVATE
116                            ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi
117                            ${CMAKE_CURRENT_BINARY_DIR}/gmxapi
118                            )
119
120 # RPATH management: make sure build artifacts can find GROMACS library.
121 set_target_properties(_gmxapi PROPERTIES SKIP_BUILD_RPATH FALSE)
122
123 # Python sources (*.py) will be packaged by scikit-build and setuptools.
124 # Note that library targets are built in CMAKE_LIBRARY_OUTPUT_DIRECTORY if not otherwise specified.
125 # This may be an unexpected location, whether inherited from the GROMACS build tree
126 # or the SKBUILD framework. Note, also, that when scikit-build is invoked with setup.py,
127 # the CMake build takes place in a subdirectory of ./_skbuild/.
128 set(GMXAPI_PYTHON_STAGING_DIR ${CMAKE_CURRENT_BINARY_DIR}/gmxapi_staging)
129 set_target_properties(_gmxapi PROPERTIES
130                       LIBRARY_OUTPUT_DIRECTORY ${GMXAPI_PYTHON_STAGING_DIR}/gmxapi)
131
132 # scikit-build sets SKBUILD when running Python packaging tools through setup.py
133 # (e.g. with pip)
134 if(SKBUILD)
135     # The Python module is being built for a GROMACS installation.
136     set_target_properties(_gmxapi PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
137     set_target_properties(_gmxapi PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
138     target_link_libraries(_gmxapi PRIVATE Gromacs::gmxapi)
139     # By default, scikit-build expects the library to be installed into a directory
140     # named for the Python package as in setup.py.
141     install(TARGETS _gmxapi LIBRARY DESTINATION gmxapi)
142 else()
143     # The Python module is being built against GROMACS in its build tree.
144     # Note: we do not have plans to install the staged package when SKBUILD != TRUE
145     set_target_properties(_gmxapi PROPERTIES BUILD_WITH_INSTALL_RPATH FALSE)
146     target_link_libraries(_gmxapi PRIVATE Gromacs::gmxapi)
147
148     # TODO: Determine packaging and installation cases and implementation.
149     # Reference https://redmine.gromacs.org/issues/2896 for additional discussion.
150     # Currently, CMake should be run by scikit-build through setup.py for proper Python packaging.
151     # We don't want to install by default in the outer scope of the GROMACS
152     # CMake procedure because we could end up trying to install to a system directory
153     # the user did not intend, or a user might install a Python-installation-specific
154     # package into an overly generic GROMACS path.
155
156     # Instead, we should probably build a source package and alert the user of its location.
157     # We can use CMake to call the Python packaging tools to create an 'sdist'
158     # source distribution archive to be installed in the GROMACS installation
159     # destination. We can use the build directory as the working directory for
160     # easier clean-up, as well.
161     # TODO: (ref issue #2896) Build and install 'sdist' with GROMACS.
162
163     # However, we can still produce an importable package for documentation builds and
164     # basic testing in ${CMAKE_CURRENT_BINARY_DIR}/gmxapi_staging
165     if(CMAKE_VERSION VERSION_LESS 3.12)
166         file(GLOB_RECURSE _py_sources
167              ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi/*.py)
168     else()
169         # CONFIGURE_DEPENDS appears in CMake 3.12 and can help to more robustly detect
170         # the need to update anything depending on the staged package.
171         file(GLOB_RECURSE _py_sources
172              CONFIGURE_DEPENDS
173              ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi/*.py)
174     endif()
175     foreach(_package_file IN LISTS _py_sources)
176         get_filename_component(_absolute_dir ${_package_file} DIRECTORY)
177         file(RELATIVE_PATH _relative_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_absolute_dir})
178         file(COPY ${_package_file} DESTINATION gmxapi_staging/${_relative_dir})
179     endforeach()
180     file(COPY setup.py CMakeLists.txt DESTINATION gmxapi_staging)
181     # Set CMake variable pybind11_DIR to ${CMAKE_CURRENT_SOURCE_DIR}/external/pybind/tools
182     # if re-invoking CMake (including via Python setuptools) for the files in gmxapi_staging.
183
184     # Unit test and build docs using PYTHONPATH=$CMAKE_CURRENT_BINARY_DIR/gmxapi_staging/gmxapi
185
186     # Note: Integration testing for multiple Python versions and/or CMake-driven
187     # sdist preparation could be performed with CMake custom_commands and custom_targets.
188 endif()
189
190 # When building as part of GROMACS umbrella project, add a testing target
191 # to the `check` target. Normal usage is to first install the Python package,
192 # then run `pytest` on the `tests` directory. Refer to gmxapi package documentation.
193 if(NOT GMXAPI_MASTER_PROJECT)
194     add_subdirectory(test)
195 endif()