Incorporate sample_restraint repository.
[alexxy/gromacs.git] / python_packaging / sample_restraint / CMakeLists.txt
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)
5
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.
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 14)
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 # 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
22 # Gromacs::gmxapi
23 find_package(gmxapi
24              0.0.7 REQUIRED CONFIG
25              PATHS "$ENV{GROMACS_DIR}"
26              )
27
28 if(gmxapi_FOUND)
29     message("gmxapi found")
30 endif()
31 message("Found gmxapi version ${gmxapi_VERSION_MAJOR}.${gmxapi_VERSION_MINOR}.${gmxapi_VERSION_PATCH}")
32
33
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
40 # to match.
41 option(GMX_DOUBLE "Use double precision" OFF)
42 set(GMX_SUFFIX "" CACHE STRING "Suffix for the GROMACS installation to use (empty for default)")
43
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")
48 else()
49     set(GROMACS_SUFFIX ${GMX_SUFFIX})
50 endif()
51
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})
57
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)
64 endif()
65 ########################################################
66
67 # Stuff for our plugin:
68 #
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."
77        OFF)
78
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.
84
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)
91     if (PYTHON_LIBRARIES)
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}")
97         else()
98             execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
99                                 "import sys; import os; \
100                                 print(os.path.abspath(os.path.join(sys.prefix, \
101                                     'lib', \
102                                     'python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}', \
103                                     'site-packages')))"
104                             OUTPUT_VARIABLE GMXPLUGIN_DEFAULT_SITE_PACKAGES
105                             OUTPUT_STRIP_TRAILING_WHITESPACE)
106             message(STATUS "Python site-packages directory is ${GMXPLUGIN_DEFAULT_SITE_PACKAGES}")
107         endif()
108     else()
109         message(FATAL_ERROR
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.")
112     endif()
113 else()
114     message(FATAL "Could not find Python interpreter. Set CMake flag -DPYTHON_EXECUTABLE=/path/to/python to hint.")
115 endif()
116
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 \
123     directory.")
124
125 message(STATUS "Python module will be installed to GMXPLUGIN_INSTALL_PATH cache value ${GMXPLUGIN_INSTALL_PATH}")
126
127
128 # Set up testing options.
129 include(CTest)
130
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)
135
136 # Prevent overriding the parent project's compiler/linker
137 # settings on Windows
138 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
139
140
141 # Now move on to building the custom code.
142 add_subdirectory(src)
143
144 # Set up documentation build targets (work in progress).
145 add_subdirectory(docs)
146
147
148 # Process CMake configuration for Python and C++ tests.
149 if(BUILD_TESTING)
150     add_subdirectory(tests)
151 endif()