cmake: Move helper functions to cmake/Helpers.cmake.
[cvc5.git] / cmake / Helpers.cmake
1 include(CheckCCompilerFlag)
2 include(CheckCXXCompilerFlag)
3
4 macro(add_c_flag flag)
5 if(CMAKE_C_FLAGS)
6 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
7 else()
8 set(CMAKE_C_FLAGS "${flag}")
9 endif()
10 message(STATUS "Configuring with C flag '${flag}'")
11 endmacro()
12
13 macro(add_cxx_flag flag)
14 if(CMAKE_CXX_FLAGS)
15 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
16 else()
17 set(CMAKE_CXX_FLAGS "${flag}")
18 endif()
19 message(STATUS "Configuring with CXX flag '${flag}'")
20 endmacro()
21
22 macro(add_c_cxx_flag flag)
23 add_c_flag(${flag})
24 add_cxx_flag(${flag})
25 endmacro()
26
27 macro(add_check_c_flag flag)
28 string(REGEX REPLACE "[-=]" "_" flagname ${flag})
29 check_c_compiler_flag("${flag}" HAVE_FLAG${flagname})
30 if(HAVE_FLAG${flagname})
31 add_c_flag(${flag})
32 endif()
33 endmacro()
34
35 macro(add_check_cxx_flag flag)
36 string(REGEX REPLACE "[-=]" "_" flagname ${flag})
37 check_cxx_compiler_flag("${flag}" HAVE_FLAG${flagname})
38 if(HAVE_FLAG${flagname})
39 add_cxx_flag(${flag})
40 endif()
41 endmacro()
42
43 macro(add_check_c_cxx_flag flag)
44 add_check_c_flag(${flag})
45 add_check_cxx_flag(${flag})
46 endmacro()
47
48 macro(add_required_cxx_flag flag)
49 string(REGEX REPLACE "[-=]" "_" flagnamename ${flag})
50 check_cxx_compiler_flag("${flag}" HAVE_FLAG${flagname})
51 if (NOT HAVE_FLAG${flagname})
52 message(FATAL_ERROR "Required compiler flag ${flag} not supported")
53 endif()
54 add_cxx_flag(${flag})
55 endmacro()
56
57 macro(add_required_c_flag flag)
58 string(REGEX REPLACE "[-=]" "_" flagname ${flag})
59 check_c_compiler_flag("${flag}" HAVE_FLAG${flagname})
60 if (NOT HAVE_FLAG${flagname})
61 message(FATAL_ERROR "Required compiler flag ${flag} not supported")
62 endif()
63 add_c_flag(${flag})
64 endmacro()
65
66 macro(add_required_c_cxx_flag flag)
67 add_required_c_flag(${flag})
68 add_required_cxx_flag(${flag})
69 endmacro()
70
71 # CVC4 Boolean options are three-valued to detect if an option was set by the
72 # user. The available values are: IGNORE (default), ON, OFF
73 # Default options do not override options that were set by the user, i.e.,
74 # cvc4_set_option only sets an option if it's value is still IGNORE.
75 # This e.g., allows the user to disable proofs for debug builds (where proofs
76 # are enabled by default).
77 macro(cvc4_option var description)
78 set(${var} IGNORE CACHE STRING "${description}")
79 # Provide drop down menu options in cmake-gui
80 set_property(CACHE ${var} PROPERTY STRINGS IGNORE ON OFF)
81 endmacro()
82
83 # Only set option if the user did not set an option.
84 macro(cvc4_set_option var value)
85 if(${var} STREQUAL "IGNORE")
86 set(${var} ${value})
87 endif()
88 endmacro()
89
90 # Prepend 'prepand_value' to each element of the list 'in_list'. The result
91 # is stored in 'out_list'.
92 function(list_prepend in_list prepand_value out_list)
93 foreach(_elem ${${in_list}})
94 list(APPEND ${out_list} "${prepand_value}${_elem}")
95 endforeach()
96 set(${out_list} ${${out_list}} PARENT_SCOPE)
97 endfunction()
98
99 # Helper to print the configuration of a 2-valued or 3-valued option 'var'
100 # with prefix 'str'.
101 macro(print_config str var)
102 if(${var} STREQUAL "ON")
103 set(OPT_VAL_STR "on")
104 elseif(${var} STREQUAL "OFF")
105 set(OPT_VAL_STR "off")
106 elseif(${var} STREQUAL "IGNORE")
107 set(OPT_VAL_STR "default")
108 endif()
109 message("${str} ${OPT_VAL_STR}")
110 endmacro()
111
112
113 #-----------------------------------------------------------------------------#
114 # libcvc4 helper macros
115
116 # Collect all libraries that must be linked against libcvc4. These will be
117 # actually linked in src/CMakeLists.txt with target_link_libaries(...).
118 macro(libcvc4_link_libraries library)
119 set(LIBCVC4_LIBRARIES ${LIBCVC4_LIBRARIES} ${library})
120 endmacro()
121
122 # Collect all include directories that are required for libcvc4. These will be
123 # actually included in src/CMakeLists.txt with target_include_directories(...).
124 macro(libcvc4_include_directories dirs)
125 set(LIBCVC4_INCLUDES ${LIBCVC4_INCLUDES} ${dirs})
126 endmacro()
127
128 # Collect all source files that are required to build libcvc4 in LIBCVC4_SRCS
129 # or LIBCVC4_GEN_SRCS. If GENERATED is the first argument the sources are
130 # added to LIBCVC4_GEN_SRCS. All sources are prepended with the absolute
131 # current path path. CMAKE_CURRENT_BINARY_DIR is prepended
132 # to generated source files.
133 macro(libcvc4_add_sources)
134 set(_sources ${ARGV})
135
136 # Check if the first argument is GENERATED.
137 list(GET _sources 0 _generated)
138 if(${_generated} STREQUAL "GENERATED")
139 list(REMOVE_AT _sources 0)
140 set(_cur_path ${CMAKE_CURRENT_BINARY_DIR})
141 set(_append_to LIBCVC4_GEN_SRCS)
142 else()
143 set(_cur_path ${CMAKE_CURRENT_SOURCE_DIR})
144 set(_append_to LIBCVC4_SRCS)
145 endif()
146
147 # Prepend source files with current path.
148 foreach(_src ${_sources})
149 list(APPEND ${_append_to} "${_cur_path}/${_src}")
150 endforeach()
151
152 file(RELATIVE_PATH
153 _rel_path "${PROJECT_SOURCE_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}")
154
155 # Make changes to list ${_append_to} visible to the parent scope if not
156 # called from src/.
157 # Note: ${_append_to} refers to the variable name whereas ${${_append_to}}
158 # refers to the contents of the variable.
159 if(_rel_path)
160 set(${_append_to} ${${_append_to}} PARENT_SCOPE)
161 endif()
162 endmacro()