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 7include(CheckCCompilerFlag) 8include(CheckCXXCompilerFlag) 9 10unset(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 14macro(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}) 23endmacro() 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. 28macro(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() 41endmacro(remove_flags) 42 43macro(check_flag_supported flag) 44 mangle_name("${flag}" flagname) 45 check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG") 46endmacro() 47 48macro(append_flags DEST) 49 foreach(value ${ARGN}) 50 list(APPEND ${DEST} ${value}) 51 list(APPEND ${DEST} ${value}) 52 endforeach() 53endmacro() 54 55# If the specified 'condition' is true then append the specified list of flags to DEST 56macro(append_flags_if condition DEST) 57 if (${condition}) 58 list(APPEND ${DEST} ${ARGN}) 59 endif() 60endmacro() 61 62# Add each flag in the list specified by DEST if that flag is supported by the current compiler. 63macro(append_flags_if_supported DEST) 64 foreach(flag ${ARGN}) 65 mangle_name("${flag}" flagname) 66 check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG") 67 append_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${DEST} ${flag}) 68 endforeach() 69endmacro() 70 71# Add a macro definition if condition is true. 72macro(define_if condition def) 73 if (${condition}) 74 add_definitions(${def}) 75 endif() 76endmacro() 77 78# Add a macro definition if condition is not true. 79macro(define_if_not condition def) 80 if (NOT ${condition}) 81 add_definitions(${def}) 82 endif() 83endmacro() 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. 88macro(config_define_if condition def) 89 if (${condition}) 90 set(${def} ON) 91 set(LIBUNWIND_NEEDS_SITE_CONFIG ON) 92 endif() 93endmacro() 94 95macro(config_define_if_not condition def) 96 if (NOT ${condition}) 97 set(${def} ON) 98 set(LIBUNWIND_NEEDS_SITE_CONFIG ON) 99 endif() 100endmacro() 101 102macro(config_define value def) 103 set(${def} ${value}) 104 set(LIBUNWIND_NEEDS_SITE_CONFIG ON) 105endmacro() 106 107# Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 108# 'LIBUNWIND_COMPILE_FLAGS' and 'LIBUNWIND_LINK_FLAGS'. 109macro(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() 116endmacro() 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'. 121macro(add_target_flags_if condition) 122 if (${condition}) 123 add_target_flags(${ARGN}) 124 endif() 125endmacro() 126 127# Add a specified list of flags to both 'LIBUNWIND_COMPILE_FLAGS' and 128# 'LIBUNWIND_LINK_FLAGS'. 129macro(add_flags) 130 foreach(value ${ARGN}) 131 list(APPEND LIBUNWIND_COMPILE_FLAGS ${value}) 132 list(APPEND LIBUNWIND_LINK_FLAGS ${value}) 133 endforeach() 134endmacro() 135 136# If the specified 'condition' is true then add a list of flags to both 137# 'LIBUNWIND_COMPILE_FLAGS' and 'LIBUNWIND_LINK_FLAGS'. 138macro(add_flags_if condition) 139 if (${condition}) 140 add_flags(${ARGN}) 141 endif() 142endmacro() 143 144# Add each flag in the list to LIBUNWIND_COMPILE_FLAGS and LIBUNWIND_LINK_FLAGS 145# if that flag is supported by the current compiler. 146macro(add_flags_if_supported) 147 foreach(flag ${ARGN}) 148 mangle_name("${flag}" flagname) 149 check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG") 150 add_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag}) 151 endforeach() 152endmacro() 153 154# Add a list of flags to 'LIBUNWIND_COMPILE_FLAGS'. 155macro(add_compile_flags) 156 foreach(f ${ARGN}) 157 list(APPEND LIBUNWIND_COMPILE_FLAGS ${f}) 158 endforeach() 159endmacro() 160 161# If 'condition' is true then add the specified list of flags to 162# 'LIBUNWIND_COMPILE_FLAGS' 163macro(add_compile_flags_if condition) 164 if (${condition}) 165 add_compile_flags(${ARGN}) 166 endif() 167endmacro() 168 169# For each specified flag, add that flag to 'LIBUNWIND_COMPILE_FLAGS' if the 170# flag is supported by the C++ compiler. 171macro(add_compile_flags_if_supported) 172 foreach(flag ${ARGN}) 173 mangle_name("${flag}" flagname) 174 check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG") 175 add_compile_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag}) 176 endforeach() 177endmacro() 178 179# Add a list of flags to 'LIBUNWIND_C_FLAGS'. 180macro(add_c_flags) 181 foreach(f ${ARGN}) 182 list(APPEND LIBUNWIND_C_FLAGS ${f}) 183 endforeach() 184endmacro() 185 186# If 'condition' is true then add the specified list of flags to 187# 'LIBUNWIND_C_FLAGS' 188macro(add_c_flags_if condition) 189 if (${condition}) 190 add_c_flags(${ARGN}) 191 endif() 192endmacro() 193 194# For each specified flag, add that flag to 'LIBUNWIND_C_FLAGS' if the 195# flag is supported by the C compiler. 196macro(add_c_compile_flags_if_supported) 197 foreach(flag ${ARGN}) 198 mangle_name("${flag}" flagname) 199 check_c_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG") 200 add_c_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag}) 201 endforeach() 202endmacro() 203 204# Add a list of flags to 'LIBUNWIND_CXX_FLAGS'. 205macro(add_cxx_flags) 206 foreach(f ${ARGN}) 207 list(APPEND LIBUNWIND_CXX_FLAGS ${f}) 208 endforeach() 209endmacro() 210 211# If 'condition' is true then add the specified list of flags to 212# 'LIBUNWIND_CXX_FLAGS' 213macro(add_cxx_flags_if condition) 214 if (${condition}) 215 add_cxx_flags(${ARGN}) 216 endif() 217endmacro() 218 219# For each specified flag, add that flag to 'LIBUNWIND_CXX_FLAGS' if the 220# flag is supported by the C compiler. 221macro(add_cxx_compile_flags_if_supported) 222 foreach(flag ${ARGN}) 223 mangle_name("${flag}" flagname) 224 check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG") 225 add_cxx_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag}) 226 endforeach() 227endmacro() 228 229# Add a list of flags to 'LIBUNWIND_LINK_FLAGS'. 230macro(add_link_flags) 231 foreach(f ${ARGN}) 232 list(APPEND LIBUNWIND_LINK_FLAGS ${f}) 233 endforeach() 234endmacro() 235 236# If 'condition' is true then add the specified list of flags to 237# 'LIBUNWIND_LINK_FLAGS' 238macro(add_link_flags_if condition) 239 if (${condition}) 240 add_link_flags(${ARGN}) 241 endif() 242endmacro() 243 244# For each specified flag, add that flag to 'LIBUNWIND_LINK_FLAGS' if the 245# flag is supported by the C++ compiler. 246macro(add_link_flags_if_supported) 247 foreach(flag ${ARGN}) 248 mangle_name("${flag}" flagname) 249 check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG") 250 add_link_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag}) 251 endforeach() 252endmacro() 253 254# Add a list of libraries or link flags to 'LIBUNWIND_LIBRARIES'. 255macro(add_library_flags) 256 foreach(lib ${ARGN}) 257 list(APPEND LIBUNWIND_LIBRARIES ${lib}) 258 endforeach() 259endmacro() 260 261# if 'condition' is true then add the specified list of libraries and flags 262# to 'LIBUNWIND_LIBRARIES'. 263macro(add_library_flags_if condition) 264 if(${condition}) 265 add_library_flags(${ARGN}) 266 endif() 267endmacro() 268 269# Turn a comma separated CMake list into a space separated string. 270macro(split_list listname) 271 string(REPLACE ";" " " ${listname} "${${listname}}") 272endmacro() 273