Add cardinality class definition (#6302)
[cvc5.git] / cmake / Helpers.cmake
1 #####################
2 ## Helpers.cmake
3 ## Top contributors (to current version):
4 ## Mathias Preiner, Aina Niemetz, Andres Noetzli
5 ## This file is part of the CVC4 project.
6 ## Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
7 ## in the top-level source directory and their institutional affiliations.
8 ## All rights reserved. See the file COPYING in the top-level source
9 ## directory for licensing information.
10 ##
11 include(CheckCCompilerFlag)
12 include(CheckCXXCompilerFlag)
13
14 if(NOT WIN32)
15 string(ASCII 27 Esc)
16 set(Yellow "${Esc}[33m")
17 set(ResetColor "${Esc}[m")
18 endif()
19
20 # Add a C flag to the global list of C flags.
21 macro(add_c_flag flag)
22 if(CMAKE_C_FLAGS)
23 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
24 else()
25 set(CMAKE_C_FLAGS "${flag}")
26 endif()
27 message(STATUS "Configuring with C flag '${flag}'")
28 endmacro()
29
30 # Add a CXX flag to the global list of CXX flags.
31 macro(add_cxx_flag flag)
32 if(CMAKE_CXX_FLAGS)
33 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
34 else()
35 set(CMAKE_CXX_FLAGS "${flag}")
36 endif()
37 message(STATUS "Configuring with CXX flag '${flag}'")
38 endmacro()
39
40 # Add a C and CXX flag to the global list of C/CXX flags.
41 macro(add_c_cxx_flag flag)
42 add_c_flag(${flag})
43 add_cxx_flag(${flag})
44 endmacro()
45
46 # Check if C flag is supported and add to global list of C flags.
47 macro(add_check_c_flag flag)
48 string(REGEX REPLACE "[-=]" "_" flagname ${flag})
49 check_c_compiler_flag("${flag}" HAVE_FLAG${flagname})
50 if(HAVE_FLAG${flagname})
51 add_c_flag(${flag})
52 endif()
53 endmacro()
54
55 # Check if CXX flag is supported and add to global list of CXX flags.
56 macro(add_check_cxx_flag flag)
57 string(REGEX REPLACE "[-=]" "_" flagname ${flag})
58 check_cxx_compiler_flag("${flag}" HAVE_FLAG${flagname})
59 if(HAVE_FLAG${flagname})
60 add_cxx_flag(${flag})
61 endif()
62 endmacro()
63
64 # Check if C/CXX flag is supported and add to global list of C/CXX flags.
65 macro(add_check_c_cxx_flag flag)
66 add_check_c_flag(${flag})
67 add_check_cxx_flag(${flag})
68 endmacro()
69
70 # Add required CXX flag. Configuration fails if the CXX flag is not supported
71 # by the compiler.
72 macro(add_required_cxx_flag flag)
73 string(REGEX REPLACE "[-=]" "_" flagnamename ${flag})
74 check_cxx_compiler_flag("${flag}" HAVE_FLAG${flagname})
75 if (NOT HAVE_FLAG${flagname})
76 message(FATAL_ERROR "Required compiler flag ${flag} not supported")
77 endif()
78 add_cxx_flag(${flag})
79 endmacro()
80
81 # Add required C flag. Configuration fails if the C flag is not supported by
82 # the compiler.
83 macro(add_required_c_flag flag)
84 string(REGEX REPLACE "[-=]" "_" flagname ${flag})
85 check_c_compiler_flag("${flag}" HAVE_FLAG${flagname})
86 if (NOT HAVE_FLAG${flagname})
87 message(FATAL_ERROR "Required compiler flag ${flag} not supported")
88 endif()
89 add_c_flag(${flag})
90 endmacro()
91
92 # Add requied C/CXX flag. Configuration fails if the C/CXX flag is not
93 # supported by the compiler.
94 macro(add_required_c_cxx_flag flag)
95 add_required_c_flag(${flag})
96 add_required_cxx_flag(${flag})
97 endmacro()
98
99 # CVC4 Boolean options are three-valued to detect if an option was set by the
100 # user. The available values are: IGNORE (default), ON, OFF
101 # Default options do not override options that were set by the user, i.e.,
102 # cvc4_set_option only sets an option if it's value is still IGNORE.
103 # This e.g., allows the user to disable proofs for debug builds (where proofs
104 # are enabled by default).
105 macro(cvc4_option var description)
106 set(${var} IGNORE CACHE STRING "${description}")
107 # Provide drop down menu options in cmake-gui
108 set_property(CACHE ${var} PROPERTY STRINGS IGNORE ON OFF)
109 endmacro()
110
111 # Only set option if the user did not set an option.
112 macro(cvc4_set_option var value)
113 if(${var} STREQUAL "IGNORE")
114 set(${var} ${value})
115 endif()
116 endmacro()
117
118 # Prepend 'prepand_value' to each element of the list 'in_list'. The result
119 # is stored in 'out_list'.
120 function(list_prepend in_list prepand_value out_list)
121 foreach(_elem ${${in_list}})
122 list(APPEND ${out_list} "${prepand_value}${_elem}")
123 endforeach()
124 set(${out_list} ${${out_list}} PARENT_SCOPE)
125 endfunction()
126
127 macro(print_info msg)
128 message("${Blue}${msg}${ResetColor}")
129 endmacro()
130
131 # Helper to print the configuration of a 2-valued or 3-valued option 'var'
132 # with prefix 'str'.
133 function(print_config str var)
134 if("${var}" STREQUAL "ON")
135 set(OPT_VAL_STR "on")
136 elseif("${var}" STREQUAL "OFF" OR "${var}" STREQUAL "IGNORE")
137 set(OPT_VAL_STR "off")
138 else()
139 set(OPT_VAL_STR "${var}")
140 endif()
141 message("${Blue}${str}: ${Green}${OPT_VAL_STR}${ResetColor}")
142 endfunction()
143
144
145 # Collect all source files that are required to build libcvc4 in LIBCVC4_SRCS
146 # or LIBCVC4_GEN_SRCS. If GENERATED is the first argument the sources are
147 # added to LIBCVC4_GEN_SRCS. All sources are prepended with the absolute
148 # current path path. CMAKE_CURRENT_BINARY_DIR is prepended
149 # to generated source files.
150 macro(libcvc4_add_sources)
151 set(_sources ${ARGV})
152
153 # Check if the first argument is GENERATED.
154 list(GET _sources 0 _generated)
155 if(${_generated} STREQUAL "GENERATED")
156 list(REMOVE_AT _sources 0)
157 set(_cur_path ${CMAKE_CURRENT_BINARY_DIR})
158 set(_append_to LIBCVC4_GEN_SRCS)
159 else()
160 set(_cur_path ${CMAKE_CURRENT_SOURCE_DIR})
161 set(_append_to LIBCVC4_SRCS)
162 endif()
163
164 # Prepend source files with current path.
165 foreach(_src ${_sources})
166 list(APPEND ${_append_to} "${_cur_path}/${_src}")
167 endforeach()
168
169 file(RELATIVE_PATH
170 _rel_path "${PROJECT_SOURCE_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}")
171
172 # Make changes to list ${_append_to} visible to the parent scope if not
173 # called from src/.
174 # Note: ${_append_to} refers to the variable name whereas ${${_append_to}}
175 # refers to the contents of the variable.
176 if(_rel_path)
177 set(${_append_to} ${${_append_to}} PARENT_SCOPE)
178 endif()
179 endmacro()
180
181 # Check if given Python module is installed and raises a FATAL_ERROR error
182 # if the module cannot be found.
183 function(check_python_module module)
184 execute_process(
185 COMMAND
186 ${PYTHON_EXECUTABLE} -c "import ${module}"
187 RESULT_VARIABLE
188 RET_MODULE_TEST
189 ERROR_QUIET
190 )
191
192 if(RET_MODULE_TEST)
193 message(FATAL_ERROR
194 "Could not find module ${module} for Python "
195 "version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}. "
196 "Make sure to install ${module} for this Python version "
197 "via \n`${PYTHON_EXECUTABLE} -m pip install ${module}'.\n"
198 "Note: You need to have pip installed for this Python version.")
199 endif()
200 endfunction()