7da67d4403e7a0284ecf4b837baa8ec8d27f8777
[alexxy/gromacs.git] / cmake / gmxOptionUtilities.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2013,2014, 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 # Helper functions for managing more complex options
36 #
37
38 # Creates a string cache variable with multiple choices
39 #
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.
46 #
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.
59 #
60 # It appears that ccmake does not use the STRINGS property, but perhaps some
61 # day...
62 #
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}")
68
69     # Set the cache properties
70     set(${NAME} ${DEFAULT} CACHE STRING ${_description})
71     set_property(CACHE ${NAME} PROPERTY STRINGS ${_allowed})
72
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()
85
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()
90
91 # Hides or shows a cache value based on conditions
92 #
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
100 #
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.
105 #
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).
110 #
111 # The logic is adapted from cmake_dependent_option().
112 #
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()
129
130 # Works like cmake_dependent_option(), but allows for an arbitrary cache value
131 # instead of only an ON/OFF option
132 #
133 # Usage:
134 #   gmx_dependent_cache_variable(VAR "Description" TYPE DEFAULT CONDITIONS)
135 #
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).
144 #
145 # See gmx_add_cache_dependency() on how to specify the conditions.
146 #
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()
151
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()
157
158 # Checks if one or more cache variables have changed
159 #
160 # Usage:
161 #   gmx_check_if_changed(RESULT VAR1 VAR2 ... VARN)
162 #
163 # Sets RESULT to true if any of the given cache variables VAR1 ... VARN has
164 # changes since the last call to this function for that variable.
165 # Changes are tracked also across CMake runs.
166 function(GMX_CHECK_IF_CHANGED RESULT)
167     set(_result FALSE)
168     foreach (_var ${ARGN})
169         if (NOT "${_var}" STREQUAL "${_var}_PREVIOUS_VALUE")
170             set(_result TRUE)
171         endif()
172         set(${_var}_PREVIOUS_VALUE "${${_var}}" CACHE INTERNAL
173             "Previous value of ${_var} for change tracking")
174     endforeach()
175     set(${RESULT} ${_result} PARENT_SCOPE)
176 endfunction()