Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 1 | # HandleLibcxxFlags - A set of macros used to setup the flags used to compile |
| 2 | # and link libc++abi. These macros add flags to the following CMake variables. |
| 3 | # - LIBUNWIND_COMPILE_FLAGS: flags used to compile libunwind |
| 4 | # - LIBUNWIND_LINK_FLAGS: flags used to link libunwind |
| 5 | # - LIBUNWIND_LIBRARIES: libraries to link libunwind to. |
| 6 | |
| 7 | include(CheckCCompilerFlag) |
| 8 | include(CheckCXXCompilerFlag) |
| 9 | |
| 10 | unset(add_flag_if_supported) |
| 11 | |
| 12 | # Mangle the name of a compiler flag into a valid CMake identifier. |
| 13 | # Ex: --std=c++11 -> STD_EQ_CXX11 |
| 14 | macro(mangle_name str output) |
| 15 | string(STRIP "${str}" strippedStr) |
| 16 | string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}") |
| 17 | string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}") |
| 18 | string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}") |
| 19 | string(REPLACE "-" "_" strippedStr "${strippedStr}") |
| 20 | string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}") |
| 21 | string(REPLACE "+" "X" strippedStr "${strippedStr}") |
| 22 | string(TOUPPER "${strippedStr}" ${output}) |
| 23 | endmacro() |
| 24 | |
| 25 | # Remove a list of flags from all CMake variables that affect compile flags. |
| 26 | # This can be used to remove unwanted flags specified on the command line |
| 27 | # or added in other parts of LLVM's cmake configuration. |
| 28 | macro(remove_flags) |
| 29 | foreach(var ${ARGN}) |
| 30 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") |
| 31 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}") |
| 32 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") |
| 33 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") |
| 34 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 35 | string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
| 36 | string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") |
| 37 | string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") |
| 38 | string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}") |
| 39 | remove_definitions(${var}) |
| 40 | endforeach() |
| 41 | endmacro(remove_flags) |
| 42 | |
| 43 | macro(check_flag_supported flag) |
| 44 | mangle_name("${flag}" flagname) |
Petr Hosek | 705543f | 2022-03-10 11:47:09 +0200 | [diff] [blame] | 45 | check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG") |
Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 46 | endmacro() |
| 47 | |
| 48 | macro(append_flags DEST) |
| 49 | foreach(value ${ARGN}) |
| 50 | list(APPEND ${DEST} ${value}) |
| 51 | list(APPEND ${DEST} ${value}) |
| 52 | endforeach() |
| 53 | endmacro() |
| 54 | |
| 55 | # If the specified 'condition' is true then append the specified list of flags to DEST |
| 56 | macro(append_flags_if condition DEST) |
| 57 | if (${condition}) |
| 58 | list(APPEND ${DEST} ${ARGN}) |
| 59 | endif() |
| 60 | endmacro() |
| 61 | |
| 62 | # Add each flag in the list specified by DEST if that flag is supported by the current compiler. |
| 63 | macro(append_flags_if_supported DEST) |
| 64 | foreach(flag ${ARGN}) |
| 65 | mangle_name("${flag}" flagname) |
Petr Hosek | 705543f | 2022-03-10 11:47:09 +0200 | [diff] [blame] | 66 | check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG") |
| 67 | append_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${DEST} ${flag}) |
Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 68 | endforeach() |
| 69 | endmacro() |
| 70 | |
| 71 | # Add a macro definition if condition is true. |
| 72 | macro(define_if condition def) |
| 73 | if (${condition}) |
| 74 | add_definitions(${def}) |
| 75 | endif() |
| 76 | endmacro() |
| 77 | |
| 78 | # Add a macro definition if condition is not true. |
| 79 | macro(define_if_not condition def) |
| 80 | if (NOT ${condition}) |
| 81 | add_definitions(${def}) |
| 82 | endif() |
| 83 | endmacro() |
| 84 | |
| 85 | # Add a macro definition to the __config_site file if the specified condition |
| 86 | # is 'true'. Note that '-D${def}' is not added. Instead it is expected that |
| 87 | # the build include the '__config_site' header. |
| 88 | macro(config_define_if condition def) |
| 89 | if (${condition}) |
| 90 | set(${def} ON) |
| 91 | set(LIBUNWIND_NEEDS_SITE_CONFIG ON) |
| 92 | endif() |
| 93 | endmacro() |
| 94 | |
| 95 | macro(config_define_if_not condition def) |
| 96 | if (NOT ${condition}) |
| 97 | set(${def} ON) |
| 98 | set(LIBUNWIND_NEEDS_SITE_CONFIG ON) |
| 99 | endif() |
| 100 | endmacro() |
| 101 | |
| 102 | macro(config_define value def) |
| 103 | set(${def} ${value}) |
| 104 | set(LIBUNWIND_NEEDS_SITE_CONFIG ON) |
| 105 | endmacro() |
| 106 | |
| 107 | # Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', |
| 108 | # 'LIBUNWIND_COMPILE_FLAGS' and 'LIBUNWIND_LINK_FLAGS'. |
| 109 | macro(add_target_flags) |
| 110 | foreach(value ${ARGN}) |
| 111 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}") |
| 112 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}") |
| 113 | list(APPEND LIBUNWIND_COMPILE_FLAGS ${value}) |
| 114 | list(APPEND LIBUNWIND_LINK_FLAGS ${value}) |
| 115 | endforeach() |
| 116 | endmacro() |
| 117 | |
| 118 | # If the specified 'condition' is true then add a list of flags to |
| 119 | # all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBUNWIND_COMPILE_FLAGS' |
| 120 | # and 'LIBUNWIND_LINK_FLAGS'. |
| 121 | macro(add_target_flags_if condition) |
| 122 | if (${condition}) |
| 123 | add_target_flags(${ARGN}) |
| 124 | endif() |
| 125 | endmacro() |
| 126 | |
Louis Dionne | 37be990 | 2021-07-15 13:02:43 -0400 | [diff] [blame] | 127 | # Add all the flags supported by the compiler to all of |
| 128 | # 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBUNWIND_COMPILE_FLAGS' |
| 129 | # and 'LIBUNWIND_LINK_FLAGS'. |
| 130 | macro(add_target_flags_if_supported) |
| 131 | foreach(flag ${ARGN}) |
| 132 | mangle_name("${flag}" flagname) |
Petr Hosek | 705543f | 2022-03-10 11:47:09 +0200 | [diff] [blame] | 133 | check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG") |
| 134 | add_target_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag}) |
Louis Dionne | 37be990 | 2021-07-15 13:02:43 -0400 | [diff] [blame] | 135 | endforeach() |
| 136 | endmacro() |
| 137 | |
Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 138 | # Add a specified list of flags to both 'LIBUNWIND_COMPILE_FLAGS' and |
| 139 | # 'LIBUNWIND_LINK_FLAGS'. |
| 140 | macro(add_flags) |
| 141 | foreach(value ${ARGN}) |
| 142 | list(APPEND LIBUNWIND_COMPILE_FLAGS ${value}) |
| 143 | list(APPEND LIBUNWIND_LINK_FLAGS ${value}) |
| 144 | endforeach() |
| 145 | endmacro() |
| 146 | |
| 147 | # If the specified 'condition' is true then add a list of flags to both |
| 148 | # 'LIBUNWIND_COMPILE_FLAGS' and 'LIBUNWIND_LINK_FLAGS'. |
| 149 | macro(add_flags_if condition) |
| 150 | if (${condition}) |
| 151 | add_flags(${ARGN}) |
| 152 | endif() |
| 153 | endmacro() |
| 154 | |
| 155 | # Add each flag in the list to LIBUNWIND_COMPILE_FLAGS and LIBUNWIND_LINK_FLAGS |
| 156 | # if that flag is supported by the current compiler. |
| 157 | macro(add_flags_if_supported) |
| 158 | foreach(flag ${ARGN}) |
| 159 | mangle_name("${flag}" flagname) |
Petr Hosek | 705543f | 2022-03-10 11:47:09 +0200 | [diff] [blame] | 160 | check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG") |
| 161 | add_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag}) |
Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 162 | endforeach() |
| 163 | endmacro() |
| 164 | |
| 165 | # Add a list of flags to 'LIBUNWIND_COMPILE_FLAGS'. |
| 166 | macro(add_compile_flags) |
| 167 | foreach(f ${ARGN}) |
| 168 | list(APPEND LIBUNWIND_COMPILE_FLAGS ${f}) |
| 169 | endforeach() |
| 170 | endmacro() |
| 171 | |
| 172 | # If 'condition' is true then add the specified list of flags to |
| 173 | # 'LIBUNWIND_COMPILE_FLAGS' |
| 174 | macro(add_compile_flags_if condition) |
| 175 | if (${condition}) |
| 176 | add_compile_flags(${ARGN}) |
| 177 | endif() |
| 178 | endmacro() |
| 179 | |
| 180 | # For each specified flag, add that flag to 'LIBUNWIND_COMPILE_FLAGS' if the |
| 181 | # flag is supported by the C++ compiler. |
| 182 | macro(add_compile_flags_if_supported) |
| 183 | foreach(flag ${ARGN}) |
| 184 | mangle_name("${flag}" flagname) |
Petr Hosek | 705543f | 2022-03-10 11:47:09 +0200 | [diff] [blame] | 185 | check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG") |
| 186 | add_compile_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag}) |
Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 187 | endforeach() |
| 188 | endmacro() |
| 189 | |
| 190 | # Add a list of flags to 'LIBUNWIND_C_FLAGS'. |
| 191 | macro(add_c_flags) |
| 192 | foreach(f ${ARGN}) |
| 193 | list(APPEND LIBUNWIND_C_FLAGS ${f}) |
| 194 | endforeach() |
| 195 | endmacro() |
| 196 | |
| 197 | # If 'condition' is true then add the specified list of flags to |
| 198 | # 'LIBUNWIND_C_FLAGS' |
| 199 | macro(add_c_flags_if condition) |
| 200 | if (${condition}) |
| 201 | add_c_flags(${ARGN}) |
| 202 | endif() |
| 203 | endmacro() |
| 204 | |
| 205 | # For each specified flag, add that flag to 'LIBUNWIND_C_FLAGS' if the |
| 206 | # flag is supported by the C compiler. |
| 207 | macro(add_c_compile_flags_if_supported) |
| 208 | foreach(flag ${ARGN}) |
| 209 | mangle_name("${flag}" flagname) |
Petr Hosek | 705543f | 2022-03-10 11:47:09 +0200 | [diff] [blame] | 210 | check_c_compiler_flag("${flag}" "C_SUPPORTS_${flagname}_FLAG") |
| 211 | add_c_flags_if(C_SUPPORTS_${flagname}_FLAG ${flag}) |
Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 212 | endforeach() |
| 213 | endmacro() |
| 214 | |
| 215 | # Add a list of flags to 'LIBUNWIND_CXX_FLAGS'. |
| 216 | macro(add_cxx_flags) |
| 217 | foreach(f ${ARGN}) |
| 218 | list(APPEND LIBUNWIND_CXX_FLAGS ${f}) |
| 219 | endforeach() |
| 220 | endmacro() |
| 221 | |
| 222 | # If 'condition' is true then add the specified list of flags to |
| 223 | # 'LIBUNWIND_CXX_FLAGS' |
| 224 | macro(add_cxx_flags_if condition) |
| 225 | if (${condition}) |
| 226 | add_cxx_flags(${ARGN}) |
| 227 | endif() |
| 228 | endmacro() |
| 229 | |
| 230 | # For each specified flag, add that flag to 'LIBUNWIND_CXX_FLAGS' if the |
| 231 | # flag is supported by the C compiler. |
| 232 | macro(add_cxx_compile_flags_if_supported) |
| 233 | foreach(flag ${ARGN}) |
| 234 | mangle_name("${flag}" flagname) |
Petr Hosek | 705543f | 2022-03-10 11:47:09 +0200 | [diff] [blame] | 235 | check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG") |
| 236 | add_cxx_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag}) |
Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 237 | endforeach() |
| 238 | endmacro() |
| 239 | |
| 240 | # Add a list of flags to 'LIBUNWIND_LINK_FLAGS'. |
| 241 | macro(add_link_flags) |
| 242 | foreach(f ${ARGN}) |
| 243 | list(APPEND LIBUNWIND_LINK_FLAGS ${f}) |
| 244 | endforeach() |
| 245 | endmacro() |
| 246 | |
| 247 | # If 'condition' is true then add the specified list of flags to |
| 248 | # 'LIBUNWIND_LINK_FLAGS' |
| 249 | macro(add_link_flags_if condition) |
| 250 | if (${condition}) |
| 251 | add_link_flags(${ARGN}) |
| 252 | endif() |
| 253 | endmacro() |
| 254 | |
| 255 | # For each specified flag, add that flag to 'LIBUNWIND_LINK_FLAGS' if the |
| 256 | # flag is supported by the C++ compiler. |
| 257 | macro(add_link_flags_if_supported) |
| 258 | foreach(flag ${ARGN}) |
| 259 | mangle_name("${flag}" flagname) |
Petr Hosek | 705543f | 2022-03-10 11:47:09 +0200 | [diff] [blame] | 260 | check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG") |
| 261 | add_link_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag}) |
Petr Hosek | cfb6df8 | 2019-10-11 22:22:29 +0000 | [diff] [blame] | 262 | endforeach() |
| 263 | endmacro() |
| 264 | |
| 265 | # Add a list of libraries or link flags to 'LIBUNWIND_LIBRARIES'. |
| 266 | macro(add_library_flags) |
| 267 | foreach(lib ${ARGN}) |
| 268 | list(APPEND LIBUNWIND_LIBRARIES ${lib}) |
| 269 | endforeach() |
| 270 | endmacro() |
| 271 | |
| 272 | # if 'condition' is true then add the specified list of libraries and flags |
| 273 | # to 'LIBUNWIND_LIBRARIES'. |
| 274 | macro(add_library_flags_if condition) |
| 275 | if(${condition}) |
| 276 | add_library_flags(${ARGN}) |
| 277 | endif() |
| 278 | endmacro() |
| 279 | |
| 280 | # Turn a comma separated CMake list into a space separated string. |
| 281 | macro(split_list listname) |
| 282 | string(REPLACE ";" " " ${listname} "${${listname}}") |
| 283 | endmacro() |
Louis Dionne | 44c86bb | 2022-05-11 11:06:28 -0400 | [diff] [blame] | 284 | |
| 285 | # For each specified flag, add that compile flag to the provided target. |
| 286 | # The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE. |
| 287 | function(target_add_compile_flags_if_supported target visibility) |
| 288 | foreach(flag ${ARGN}) |
| 289 | mangle_name("${flag}" flagname) |
| 290 | check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG") |
| 291 | if (CXX_SUPPORTS_${flagname}_FLAG) |
| 292 | target_compile_options(${target} ${visibility} ${flag}) |
| 293 | endif() |
| 294 | endforeach() |
| 295 | endfunction() |