Add GROMACS library dependency to gmxapi package.
[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 find_package(gmxapi 0.0.8 REQUIRED
67              HINTS "$ENV{GROMACS_DIR}"
68              )
69 if(gmxapi_FOUND)
70     set(_suffix "")
71     # GROMACS master branch and development branches may have divergent
72     # pre-release APIs. This check allows us to distinguish them and behave
73     # differently if needed. github.com/kassonlab/gromacs-gmxapi devel branch
74     # sets gmxapi_EXPERIMENTAL=TRUE. Upstream GROMACS master branch does not.
75     # Ref: https://github.com/kassonlab/gmxapi/issues/166
76     if(gmxapi_EXPERIMENTAL)
77         set(_suffix " (unofficial)")
78     endif()
79     message(STATUS "Found gmxapi version ${gmxapi_VERSION}${_suffix}")
80 endif()
81
82 # TODO: Account for externally-provided pybind headers where recommended by packaging system.
83 # Reference https://redmine.gromacs.org/issues/2896
84 add_subdirectory(external/pybind)
85
86 set(GMXAPI_PYTHON_EXTENSION_SOURCES
87     gmxapi/module.cpp
88     )
89
90 pybind11_add_module(_gmxapi
91                     ${GMXAPI_PYTHON_EXTENSION_SOURCES}
92                     )
93
94 target_include_directories(_gmxapi PRIVATE
95                            ${CMAKE_CURRENT_SOURCE_DIR}/gmxapi
96                            ${CMAKE_CURRENT_BINARY_DIR}/gmxapi
97                            )
98
99 # RPATH management: make sure build artifacts can find GROMACS library.
100 set_target_properties(_gmxapi PROPERTIES SKIP_BUILD_RPATH FALSE)
101 set_target_properties(_gmxapi PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
102 set_target_properties(_gmxapi PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
103
104 target_link_libraries(_gmxapi PRIVATE Gromacs::gmxapi)
105
106 install(TARGETS _gmxapi LIBRARY DESTINATION gmxapi)
107
108 if(NOT SKBUILD)
109     # TODO: Determine packaging and installation cases and implementation.
110     # Reference https://redmine.gromacs.org/issues/2896 for additional discussion.
111     message(WARNING "CMake should be run by scikit-build through setup.py for proper Python packaging.")
112     # Try to do something sensible. Collect Python sources as setup.py would.
113     file(GLOB_RECURSE _py_sources CONFIGURE_DEPENDS $CMAKE_CURRENT_SOURCE_DIR/gmxapi/*.py)
114     install(FILES ${_py_sources} DESTINATION gmxapi)
115 endif()