Use pybind11 to make a minimal C++ Python extension.
[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 set(CMAKE_CXX_STANDARD 14)
59 set(CMAKE_CXX_STANDARD_REQUIRED ON)
60
61 # Only interpret if() arguments as variables or keywords when unquoted.
62 cmake_policy(SET CMP0054 NEW)
63 # honor the language standard settings for try_compile()
64 cmake_policy(SET CMP0067 NEW)
65
66 # TODO: Account for externally-provided pybind headers where recommended by packaging system.
67 # Reference https://redmine.gromacs.org/issues/2896
68 add_subdirectory(external/pybind)
69
70 set(GMXAPI_PYTHON_EXTENSION_SOURCES
71     gmxapi/module.cpp
72     )
73
74 pybind11_add_module(_gmxapi
75                     ${GMXAPI_PYTHON_EXTENSION_SOURCES}
76                     )
77
78 target_include_directories(_gmxapi PRIVATE
79                            ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi
80                            ${CMAKE_CURRENT_BINARY_DIR}/gmxapi
81                            )
82
83 install(TARGETS _gmxapi LIBRARY DESTINATION gmxapi)
84
85 if(NOT SKBUILD)
86     # TODO: Determine packaging and installation cases and implementation.
87     # Reference https://redmine.gromacs.org/issues/2896 for additional discussion.
88     message(WARNING "CMake should be run by scikit-build through setup.py for proper Python packaging.")
89     # Try to do something sensible. Collect Python sources as setup.py would.
90     file(GLOB_RECURSE _py_sources CONFIGURE_DEPENDS $CMAKE_CURRENT_SOURCE_DIR/gmxapi/*.py)
91     install(FILES ${_py_sources} DESTINATION gmxapi)
92 endif()