1 cmake_minimum_required(VERSION 3.4.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 VERSION 0.0.7)
6 # This project requires a GROMACS supporting gmxapi 0.0.7 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.
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`.
14 set(CMAKE_CXX_STANDARD 14)
15 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
17 # CMake modules are in a subdirectory to keep this file cleaner
18 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
20 # Assuming GROMACS is in our path or that we have set either the gmxapi_DIR or GROMACS_DIR environment variables,
21 # this will find the CMake configuration for the GROMACS libraries we need and define the CMake library objects
25 PATHS "$ENV{GROMACS_DIR}"
29 message("gmxapi found")
31 message("Found gmxapi version ${gmxapi_VERSION_MAJOR}.${gmxapi_VERSION_MINOR}.${gmxapi_VERSION_PATCH}")
34 ######################################################
35 # The following is boiler-plate recommended by GROMACS
36 ######################################################
37 # In principle, this could be deduced from GROMACS_IS_DOUBLE returned by
38 # find_package(GROMACS) based on the suffix alone, but it is clearer that the
39 # user explicitly sets what they want to get, and then need to provide a suffix
41 option(GMX_DOUBLE "Use double precision" OFF)
42 set(GMX_SUFFIX "" CACHE STRING "Suffix for the GROMACS installation to use (empty for default)")
44 # This does not allow for a non-suffixed double-precision libgromacs, but
45 # that should be rare enough for demonstration purposes.
46 if (GMX_DOUBLE AND NOT GMX_SUFFIX)
47 set(GROMACS_SUFFIX "_d")
49 set(GROMACS_SUFFIX ${GMX_SUFFIX})
52 find_package(GROMACS REQUIRED)
53 gromacs_check_double(GMX_DOUBLE)
54 gromacs_check_compiler(CXX)
55 include_directories(${GROMACS_INCLUDE_DIRS})
56 add_definitions(${GROMACS_DEFINITIONS})
58 # Use static linking on MSVC
59 if (CMAKE_GENERATOR MATCHES "Visual Studio")
60 string(REPLACE /MD /MT CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
61 set(CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE} CACHE STRING "" FORCE)
62 string(REPLACE /MD /MT CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
63 set(CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG} CACHE STRING "" FORCE)
65 ########################################################
67 # Stuff for our plugin:
69 # If the user is not in a virtual environment and is not a privileged user and has not specified an install location
70 # for the Python module (GMXPLUGIN_INSTALL_PATH), this option causes the automatic install location to query the user
71 # site-packages directory instead of using the default site-packages directory for the interpreter.
72 option(GMXPLUGIN_USER_INSTALL
73 "Override the default site-packages directory with the user-specific Python packages directory. \
74 (Do not use with virtual environments.) \
75 Has no effect if GMXPLUGIN_INSTALL_PATH is defined or cached. \
76 Use -UGMXPLUGIN_INSTALL_PATH to force recalculation."
79 # Since a user may have multiple virtual environments with different Python interpreters, it is generally confusing to
80 # have a package for a virtual environment installed in the user's default user site-packages directory. If virtual
81 # 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
82 # not use any Python virtual environments, we recommend you _do_ perform "user" installs exclusively. Overall, we
83 # we recommend you use Python virtual environments and activate one before performing a regular (non-"user") install.
85 unset(PYTHONINTERP_FOUND)
86 unset(PYTHONLIBS_FOUND)
87 find_package(PythonInterp)
88 if (PYTHONINTERP_FOUND)
89 message(STATUS "Found Python interpreter: ${PYTHON_EXECUTABLE}")
90 add_subdirectory(src/external/pybind11)
92 if (GMXPLUGIN_USER_INSTALL)
93 execute_process(COMMAND ${PYTHON_EXECUTABLE} "-m" "site" "--user-site"
94 OUTPUT_VARIABLE GMXPLUGIN_DEFAULT_SITE_PACKAGES
95 OUTPUT_STRIP_TRAILING_WHITESPACE)
96 message(STATUS "Python user site-packages directory is ${GMXPLUGIN_DEFAULT_SITE_PACKAGES}")
98 execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
99 "import sys; import os; \
100 print(os.path.abspath(os.path.join(sys.prefix, \
102 'python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}', \
104 OUTPUT_VARIABLE GMXPLUGIN_DEFAULT_SITE_PACKAGES
105 OUTPUT_STRIP_TRAILING_WHITESPACE)
106 message(STATUS "Python site-packages directory is ${GMXPLUGIN_DEFAULT_SITE_PACKAGES}")
110 "Found Python interpreter ${PYTHON_EXECUTABLE} but this Python installation does not have developer tools."
111 "Set PYTHON_EXECUTABLE to the Python interpreter that was installed with a working Python.h header file.")
114 message(FATAL "Could not find Python interpreter. Set CMake flag -DPYTHON_EXECUTABLE=/path/to/python to hint.")
117 # At some point this may be part of a CMake package with several components for which a single CMAKE_INSTALL_PREFIX does
118 # not make sense, so let's manage the install path separately.
119 set(GMXPLUGIN_INSTALL_PATH ${GMXPLUGIN_DEFAULT_SITE_PACKAGES} CACHE PATH
120 "Path to Python module install location (site-packages). For an automatically determined install location based on \
121 the Python installation, leave undefined or explicitly undefined with -UGMXPLUGIN_INSTALL_PATH and, optionally, set \
122 GMXPLUGIN_USER_INSTALL on or off to specify the installation's site-packages directory or the 'user' site-packages \
125 message(STATUS "Python module will be installed to GMXPLUGIN_INSTALL_PATH cache value ${GMXPLUGIN_INSTALL_PATH}")
128 # Set up testing options.
131 # Either use the git subtree in tests/googletest or download a fresh copy of
132 # googletest master branch.
133 option(DOWNLOAD_GOOGLETEST OFF "Download the latest master branch of googletest.")
134 mark_as_advanced(DOWNLOAD_GOOGLETEST)
136 # Prevent overriding the parent project's compiler/linker
137 # settings on Windows
138 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
141 # Now move on to building the custom code.
142 add_subdirectory(src)
144 # Set up documentation build targets (work in progress).
145 add_subdirectory(docs)
148 # Process CMake configuration for Python and C++ tests.
150 add_subdirectory(tests)