Eliminate equality query dependence on quantifiers engine (#5831)
[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-2020 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 # Helper to print the configuration of a 2-valued or 3-valued option 'var'
128 # with prefix 'str'.
129 macro(print_config str var)
130 if(${var} STREQUAL "ON")
131 set(OPT_VAL_STR "on")
132 else()
133 set(OPT_VAL_STR "off")
134 endif()
135 message("${str} ${OPT_VAL_STR}")
136 endmacro()
137
138
139 # Collect all source files that are required to build libcvc4 in LIBCVC4_SRCS
140 # or LIBCVC4_GEN_SRCS. If GENERATED is the first argument the sources are
141 # added to LIBCVC4_GEN_SRCS. All sources are prepended with the absolute
142 # current path path. CMAKE_CURRENT_BINARY_DIR is prepended
143 # to generated source files.
144 macro(libcvc4_add_sources)
145 set(_sources ${ARGV})
146
147 # Check if the first argument is GENERATED.
148 list(GET _sources 0 _generated)
149 if(${_generated} STREQUAL "GENERATED")
150 list(REMOVE_AT _sources 0)
151 set(_cur_path ${CMAKE_CURRENT_BINARY_DIR})
152 set(_append_to LIBCVC4_GEN_SRCS)
153 else()
154 set(_cur_path ${CMAKE_CURRENT_SOURCE_DIR})
155 set(_append_to LIBCVC4_SRCS)
156 endif()
157
158 # Prepend source files with current path.
159 foreach(_src ${_sources})
160 list(APPEND ${_append_to} "${_cur_path}/${_src}")
161 endforeach()
162
163 file(RELATIVE_PATH
164 _rel_path "${PROJECT_SOURCE_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}")
165
166 # Make changes to list ${_append_to} visible to the parent scope if not
167 # called from src/.
168 # Note: ${_append_to} refers to the variable name whereas ${${_append_to}}
169 # refers to the contents of the variable.
170 if(_rel_path)
171 set(${_append_to} ${${_append_to}} PARENT_SCOPE)
172 endif()
173 endmacro()