Update instructions in containers.rst
[gromacs.git] / cmake / gmxOptionUtilities.cmake
blobc893a4f02a731dd413e30cd5b8687a49d459bb46
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2013,2014,2015,2017,2020, 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.
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.
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.
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.
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.
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.
35 # Helper functions for managing more complex options
38 # Creates a string cache variable with multiple choices
40 # Usage:
41 #   gmx_option_multichoice(VAR "Description" DEFAULT_VALUE
42 #                          Value1 Value2 ... ValueN)
43 # Output:
44 #   VAR is set in the cache and in the caller's scope. The caller can assume
45 #   that it is always one of the provided values, converted to uppercase.
47 # Main benefit is that the list of allowed values only needs to be provided
48 # once, and gets used in multiple contexts:
49 #   1. It is automatically added to the description.
50 #   2. It is set as the STRINGS property of the created cache variable for use
51 #      with CMake GUIs.
52 #   3. The user-provided value is checked against the list, and a fatal error
53 #      is produced if the value is not known.  The caller does not need to
54 #      produce good error messages in cases where it may be necessary to check
55 #      for the validity again.
56 # As a special case, any "[built-in]" string in the allowed values is ignored
57 # when checking the user-provided value, but is added to all user-visible
58 # messages.
60 # It appears that ccmake does not use the STRINGS property, but perhaps some
61 # day...
63 function(GMX_OPTION_MULTICHOICE NAME DESCRIPTION DEFAULT)
64     # Some processing of the input values
65     string(REPLACE ";" ", " _allowed_comma_separated "${ARGN}")
66     set(_description "${DESCRIPTION}. Pick one of: ${_allowed_comma_separated}")
67     string(REPLACE "[built-in]" "" _allowed "${ARGN}")
69     # Set the cache properties
70     set(${NAME} ${DEFAULT} CACHE STRING "${_description}")
71     set_property(CACHE ${NAME} PROPERTY STRINGS ${_allowed})
73     # Check that the value is one of the allowed
74     set(_org_value "${${NAME}}")
75     string(TOUPPER "${${NAME}}" ${NAME})
76     string(TOUPPER "${_allowed}" _allowed_as_upper)
77     list(FIND _allowed_as_upper "${${NAME}}" _found_index)
78     if (_found_index EQUAL -1)
79         message(FATAL_ERROR "Invalid value for ${NAME}: ${_org_value}.  "
80                             "Pick one of: ${_allowed_comma_separated}")
81     endif()
82     # Always provide the upper-case value to the caller
83     set(${NAME} "${${NAME}}" PARENT_SCOPE)
84 endfunction()
86 # Convenience function for reporting a fatal error for an invalid input value
87 function(GMX_INVALID_OPTION_VALUE NAME)
88     message(FATAL_ERROR "Invalid value for ${NAME}: ${${NAME}}")
89 endfunction()
91 # Hides or shows a cache value based on conditions
93 # Usage:
94 #   gmx_add_cache_dependency(VAR TYPE CONDITIONS VALUE)
95 # where
96 #   VAR        is a name of a cached variable
97 #   TYPE       is the type of VAR
98 #   CONDITIONS is a list of conditional expressions (see below)
99 #   VALUE      is a value that is set to VAR if CONDITIONS is not satisfied
101 # Evaluates each condition in CONDITIONS, and if any of them is false,
102 # VAR is marked internal (hiding it from the user) and its value is set to
103 # VALUE.  The previous user-set value of VAR is still remembered in the cache,
104 # and used when CONDITIONS become true again.
106 # The conditions is a semicolon-separated list of conditions as specified for
107 # CMake if() statements, such as "GMX_FFT_LIBRARY STREQUAL FFTW3",
108 # "NOT GMX_MPI" or "GMX_MPI;NOT GMX_DOUBLE".  Note that quotes within the
109 # expressions don't work for some reason (even if escaped).
111 # The logic is adapted from cmake_dependent_option().
113 function(GMX_ADD_CACHE_DEPENDENCY NAME TYPE CONDITIONS FORCED_VALUE)
114     set(_available TRUE)
115     foreach(_cond ${CONDITIONS})
116         string(REGEX REPLACE " +" ";" _cond_as_list ${_cond})
117         if (${_cond_as_list})
118         else()
119             set(_available FALSE)
120         endif()
121     endforeach()
122     if (_available)
123         set_property(CACHE ${NAME} PROPERTY TYPE ${TYPE})
124     else()
125         set(${NAME} "${FORCED_VALUE}" PARENT_SCOPE)
126         set_property(CACHE ${NAME} PROPERTY TYPE INTERNAL)
127     endif()
128 endfunction()
130 # Works like cmake_dependent_option(), but allows for an arbitrary cache value
131 # instead of only an ON/OFF option
133 # Usage:
134 #   gmx_dependent_cache_variable(VAR "Description" TYPE DEFAULT CONDITIONS)
136 # Creates a cache variable VAR with the given description, type and default
137 # value.  If any of the conditions listed in CONDITIONS is not true, then
138 # the cache variable is marked internal (hiding it from the user) and the
139 # value of VAR is set to DEFAULT.  The previous user-set value of VAR is still
140 # remembered in the cache, and used when CONDITIONS become true again.
141 # Any further changes to the variable can be done with simple set()
142 # (or set_property(CACHE VAR PROPERTY VALUE ...) if the cache needs to be
143 # modified).
145 # See gmx_add_cache_dependency() on how to specify the conditions.
147 macro(GMX_DEPENDENT_CACHE_VARIABLE NAME DESCRIPTION TYPE DEFAULT CONDITIONS)
148     set(${NAME} "${DEFAULT}" CACHE ${TYPE} "${DESCRIPTION}")
149     gmx_add_cache_dependency(${NAME} ${TYPE} "${CONDITIONS}" "${DEFAULT}")
150 endmacro()
152 # Works like cmake_dependent_option(), but reuses the code from
153 # gmx_dependent_cache_variable() to make sure both behave the same way.
154 macro(GMX_DEPENDENT_OPTION NAME DESCRIPTION DEFAULT CONDITIONS)
155     gmx_dependent_cache_variable(${NAME} "${DESCRIPTION}" BOOL "${DEFAULT}" "${CONDITIONS}")
156 endmacro()
158 # Sets a boolean variable based on conditions
160 # Usage:
161 #   gmx_set_boolean(VAR CONDITIONS)
163 # Sets VAR to ON if all conditions listed in CONDITIONS are true, otherwise
164 # VAR is set OFF.
166 # See gmx_add_cache_dependency() on how to specify the conditions.
168 function (GMX_SET_BOOLEAN NAME CONDITIONS)
169     set(${NAME} ON)
170     foreach(_cond ${CONDITIONS})
171         string(REGEX REPLACE " +" ";" _cond_as_list ${_cond})
172         if (${_cond_as_list})
173         else()
174             set(${NAME} OFF)
175         endif()
176     endforeach()
177     set(${NAME} ${${NAME}} PARENT_SCOPE)
178 endfunction()
180 # Checks if one or more variables have changed since last call to this function
182 # Usage:
183 #   gmx_check_if_changed(RESULT VAR1 VAR2 ... VARN)
185 # Sets RESULT to true if any of the given variables VAR1 ... VARN has
186 # changed since the last call to this function for that variable.
187 # Changes are tracked also across CMake runs.
188 function(GMX_CHECK_IF_CHANGED RESULT)
189     set(_result FALSE)
190     foreach (_var ${ARGN})
191         if (NOT "${${_var}}" STREQUAL "${${_var}_PREVIOUS_VALUE}")
192             set(_result TRUE)
193         endif()
194         set(${_var}_PREVIOUS_VALUE "${${_var}}" CACHE INTERNAL
195             "Previous value of ${_var} for change tracking")
196     endforeach()
197     set(${RESULT} ${_result} PARENT_SCOPE)
198 endfunction()