Require pybind 2.6 from environment for gmxapi Python package extension module.
[alexxy/gromacs.git] / python_packaging / sample_restraint / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.16.3)
2 # If you are using this repository as a template, you should probably change the
3 # project name and adopt your own versioning scheme.
4 project(sample_restraint)
5
6 # This project requires a GROMACS supporting gmxapi 0.0.8 or higher. It should
7 # be sufficient to source the GMXRC, but you can also set the GROMACS_DIR or
8 # gmxapi_DIR environment variable to help CMake find the GROMACS installation.
9
10 # Note that the code will need to be built separately for different versions of Python and for substantially different
11 # versions of GROMACS. If building from the command line, you can specify a Python executable with the PYTHON_EXECUTABLE
12 # variable. For instance, to make sure you are building for your default Python, cmake -DPYTHON_EXECUTABLE=`which python`.
13
14 set(CMAKE_CXX_STANDARD 17)
15 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
16
17 # CMake modules are in a subdirectory to keep this file cleaner
18 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
19
20 # Check if Python package is being built directly or via add_subdirectory.
21 # I.e. is this being built as a standalone project or as part of the GROMACS
22 # build tree (for testing)?
23 set(GMXAPI_EXTENSION_MASTER_PROJECT OFF)
24 if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
25     set(GMXAPI_EXTENSION_MASTER_PROJECT ON)
26 endif()
27
28 option(GMXAPI_EXTENSION_DOWNLOAD_PYBIND ON)
29 if(GMXAPI_EXTENSION_DOWNLOAD_PYBIND)
30     configure_file(CMakeLists.pybind.in pybind-download/CMakeLists.txt)
31     execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
32                     RESULT_VARIABLE result
33                     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pybind-download)
34     if(result)
35         message(FATAL_ERROR "CMake step for pybind download failed: ${result}")
36     endif()
37     execute_process(COMMAND ${CMAKE_COMMAND} --build .
38                     RESULT_VARIABLE result
39                     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pybind-download)
40     if(result)
41         message(FATAL_ERROR "Build step for pybind failed: ${result}")
42     endif()
43     add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/pybind-src)
44 else()
45     find_package(pybind11 2.6 REQUIRED)
46 endif()
47 # Assuming GROMACS is in our path or that we have set either the gmxapi_DIR or GROMACS_DIR environment variables,
48 # this will find the CMake configuration for the GROMACS libraries we need and define the CMake library objects
49 # Gromacs::gmxapi
50 if(GMXAPI_EXTENSION_MASTER_PROJECT)
51     find_package(gmxapi
52                  0.2.1 REQUIRED CONFIG
53                  PATHS "$ENV{GROMACS_DIR}"
54                  )
55
56     message(STATUS "Found gmxapi version ${gmxapi_VERSION_MAJOR}.${gmxapi_VERSION_MINOR}.${gmxapi_VERSION_PATCH}")
57 endif()
58
59 if(GMXAPI_EXTENSION_MASTER_PROJECT)
60     ######################################################
61     # The following is boiler-plate recommended by GROMACS
62     ######################################################
63     # In principle, this could be deduced from GROMACS_IS_DOUBLE returned by
64     # find_package(GROMACS) based on the suffix alone, but it is clearer that the
65     # user explicitly sets what they want to get, and then need to provide a suffix
66     # to match.
67     option(GMX_DOUBLE "Use double precision" OFF)
68     set(GMX_SUFFIX "" CACHE STRING "Suffix for the GROMACS installation to use (empty for default)")
69
70     # This does not allow for a non-suffixed double-precision libgromacs, but
71     # that should be rare enough for demonstration purposes.
72     if (GMX_DOUBLE AND NOT GMX_SUFFIX)
73         set(GROMACS_SUFFIX "_d")
74     else()
75         set(GROMACS_SUFFIX ${GMX_SUFFIX})
76     endif()
77
78     find_package(GROMACS REQUIRED)
79     gromacs_check_double(GMX_DOUBLE)
80     gromacs_check_compiler(CXX)
81     include_directories(${GROMACS_INCLUDE_DIRS})
82     add_definitions(${GROMACS_DEFINITIONS})
83
84     # Use static linking on MSVC
85     if (CMAKE_GENERATOR MATCHES "Visual Studio")
86         string(REPLACE /MD /MT CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
87         set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE} CACHE STRING "" FORCE)
88         string(REPLACE /MD /MT CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
89         set(CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG} CACHE STRING "" FORCE)
90     endif()
91
92     # Activate the CTest module for this directory.
93     include(CTest)
94 endif()
95
96 ########################################################
97
98 # Stuff for our plugin:
99 #
100 # If the user is not in a virtual environment and is not a privileged user and has not specified an install location
101 # for the Python module (GMXPLUGIN_INSTALL_PATH), this option causes the automatic install location to query the user
102 # site-packages directory instead of using the default site-packages directory for the interpreter.
103 option(GMXPLUGIN_USER_INSTALL
104        "Override the default site-packages directory with the user-specific Python packages directory. \
105        (Do not use with virtual environments.) \
106        Has no effect if GMXPLUGIN_INSTALL_PATH is defined or cached. \
107        Use -UGMXPLUGIN_INSTALL_PATH to force recalculation."
108        OFF)
109
110 # Since a user may have multiple virtual environments with different Python interpreters, it is generally confusing to
111 # have a package for a virtual environment installed in the user's default user site-packages directory. If virtual
112 # environments are in use at all, we recommend you do _not_ perform a "user" install in or out of a virtual env. If you do
113 # not use any Python virtual environments, we recommend you _do_ perform "user" installs exclusively. Overall, we
114 # we recommend you use Python virtual environments and activate one before performing a regular (non-"user") install.
115 if (PYTHON_EXECUTABLE)
116     message(STATUS "Found Python interpreter: ${PYTHON_EXECUTABLE}")
117     if (PYTHON_LIBRARIES OR PythonLibs_FOUND OR pybind11_FOUND)
118         if (GMXPLUGIN_USER_INSTALL)
119             execute_process(COMMAND ${PYTHON_EXECUTABLE} "-m" "site" "--user-site"
120                             OUTPUT_VARIABLE GMXPLUGIN_DEFAULT_SITE_PACKAGES
121                             OUTPUT_STRIP_TRAILING_WHITESPACE)
122             message(STATUS "Python user site-packages directory is ${GMXPLUGIN_DEFAULT_SITE_PACKAGES}")
123         else()
124             execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
125                                 "import sys; import os; \
126                                 print(os.path.abspath(os.path.join(sys.prefix, \
127                                     'lib', \
128                                     'python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}', \
129                                     'site-packages')))"
130                             OUTPUT_VARIABLE GMXPLUGIN_DEFAULT_SITE_PACKAGES
131                             OUTPUT_STRIP_TRAILING_WHITESPACE)
132             message(STATUS "Python site-packages directory is ${GMXPLUGIN_DEFAULT_SITE_PACKAGES}")
133         endif()
134     else()
135         message(FATAL_ERROR
136             "Found Python interpreter ${PYTHON_EXECUTABLE} but this Python installation does not have developer tools."
137             "Set PYTHON_EXECUTABLE to the Python interpreter that was installed with a working Python.h header file.")
138     endif()
139 else()
140     message(FATAL_ERROR "Could not find Python interpreter. Set CMake flag -DPYTHON_EXECUTABLE=/path/to/python to hint.")
141 endif()
142
143 # At some point this may be part of a CMake package with several components for which a single CMAKE_INSTALL_PREFIX does
144 # not make sense, so let's manage the install path separately.
145 set(GMXPLUGIN_INSTALL_PATH ${GMXPLUGIN_DEFAULT_SITE_PACKAGES} CACHE PATH
146     "Path to Python module install location (site-packages). For an automatically determined install location based on \
147     the Python installation, leave undefined or explicitly undefined with -UGMXPLUGIN_INSTALL_PATH and, optionally, set \
148     GMXPLUGIN_USER_INSTALL on or off to specify the installation's site-packages directory or the 'user' site-packages \
149     directory.")
150
151 if(GMXAPI_EXTENSION_MASTER_PROJECT)
152     message(STATUS "Python module will be installed to GMXPLUGIN_INSTALL_PATH cache value ${GMXPLUGIN_INSTALL_PATH}")
153 endif()
154
155 # TODO: (Issue #3027) Handle Googletest sources both for forked projects and
156 #  for GROMACS-project-internal testing.
157 # Projects based on this subtree should bundle googletest sources.
158 # The GROMACS project already bundles googletest sources for internal use, but
159 # they will only be available to this project with some additional management by
160 # the parent project CMake configuration.
161 option(DOWNLOAD_GOOGLETEST "Download the latest master branch of googletest." OFF)
162 mark_as_advanced(DOWNLOAD_GOOGLETEST)
163
164 # Prevent overriding the parent project's compiler/linker
165 # settings on Windows
166 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
167
168
169 # Now move on to building the custom code.
170 add_subdirectory(src)
171
172 # Set up documentation build targets (work in progress).
173 add_subdirectory(docs)
174
175 # Process CMake configuration for Python and C++ tests.
176 include(CTest)
177 if(BUILD_TESTING)
178     include(GoogleTest)
179     add_subdirectory(tests)
180 endif()