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# - LIBCXXABI_COMPILE_FLAGS: flags used to compile libc++abi 4# - LIBCXXABI_LINK_FLAGS: flags used to link libc++abi 5# - LIBCXXABI_LIBRARIES: libraries to link libc++abi to. 6 7include(CheckCXXCompilerFlag) 8 9unset(add_flag_if_supported) 10 11# Mangle the name of a compiler flag into a valid CMake identifier. 12# Ex: --std=c++11 -> STD_EQ_CXX11 13macro(mangle_name str output) 14 string(STRIP "${str}" strippedStr) 15 string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}") 16 string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}") 17 string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}") 18 string(REPLACE "-" "_" strippedStr "${strippedStr}") 19 string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}") 20 string(REPLACE "+" "X" strippedStr "${strippedStr}") 21 string(TOUPPER "${strippedStr}" ${output}) 22endmacro() 23 24# Remove a list of flags from all CMake variables that affect compile flags. 25# This can be used to remove unwanted flags specified on the command line 26# or added in other parts of LLVM's cmake configuration. 27macro(remove_flags) 28 foreach(var ${ARGN}) 29 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") 30 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}") 31 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") 32 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") 33 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 34 string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") 35 string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") 36 string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") 37 string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}") 38 remove_definitions(${var}) 39 endforeach() 40endmacro(remove_flags) 41 42macro(check_flag_supported flag) 43 mangle_name("${flag}" flagname) 44 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 45endmacro() 46 47macro(append_flags DEST) 48 foreach(value ${ARGN}) 49 list(APPEND ${DEST} ${value}) 50 list(APPEND ${DEST} ${value}) 51 endforeach() 52endmacro() 53 54# If the specified 'condition' is true then append the specified list of flags to DEST 55macro(append_flags_if condition DEST) 56 if (${condition}) 57 list(APPEND ${DEST} ${ARGN}) 58 endif() 59endmacro() 60 61# Add each flag in the list specified by DEST if that flag is supported by the current compiler. 62macro(append_flags_if_supported DEST) 63 foreach(flag ${ARGN}) 64 mangle_name("${flag}" flagname) 65 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 66 append_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${DEST} ${flag}) 67 endforeach() 68endmacro() 69 70# Add a macro definition if condition is true. 71macro(define_if condition def) 72 if (${condition}) 73 add_definitions(${def}) 74 endif() 75endmacro() 76 77# Add a macro definition if condition is not true. 78macro(define_if_not condition def) 79 if (NOT ${condition}) 80 add_definitions(${def}) 81 endif() 82endmacro() 83 84# Add a macro definition to the __config_site file if the specified condition 85# is 'true'. Note that '-D${def}' is not added. Instead it is expected that 86# the build include the '__config_site' header. 87macro(config_define_if condition def) 88 if (${condition}) 89 set(${def} ON) 90 set(LIBCXXABI_NEEDS_SITE_CONFIG ON) 91 endif() 92endmacro() 93 94macro(config_define_if_not condition def) 95 if (NOT ${condition}) 96 set(${def} ON) 97 set(LIBCXXABI_NEEDS_SITE_CONFIG ON) 98 endif() 99endmacro() 100 101macro(config_define value def) 102 set(${def} ${value}) 103 set(LIBCXXABI_NEEDS_SITE_CONFIG ON) 104endmacro() 105 106# Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 107# 'LIBCXXABI_COMPILE_FLAGS' and 'LIBCXXABI_LINK_FLAGS'. 108macro(add_target_flags) 109 foreach(value ${ARGN}) 110 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}") 111 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}") 112 list(APPEND LIBCXXABI_COMPILE_FLAGS ${value}) 113 list(APPEND LIBCXXABI_LINK_FLAGS ${value}) 114 endforeach() 115endmacro() 116 117# If the specified 'condition' is true then add a list of flags to 118# all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXXABI_COMPILE_FLAGS' 119# and 'LIBCXXABI_LINK_FLAGS'. 120macro(add_target_flags_if condition) 121 if (${condition}) 122 add_target_flags(${ARGN}) 123 endif() 124endmacro() 125 126# Add all the flags supported by the compiler to all of 127# 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXXABI_COMPILE_FLAGS' 128# and 'LIBCXXABI_LINK_FLAGS'. 129macro(add_target_flags_if_supported) 130 foreach(flag ${ARGN}) 131 mangle_name("${flag}" flagname) 132 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 133 add_target_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 134 endforeach() 135endmacro() 136 137# Add a specified list of flags to both 'LIBCXXABI_COMPILE_FLAGS' and 138# 'LIBCXXABI_LINK_FLAGS'. 139macro(add_flags) 140 foreach(value ${ARGN}) 141 list(APPEND LIBCXXABI_COMPILE_FLAGS ${value}) 142 list(APPEND LIBCXXABI_LINK_FLAGS ${value}) 143 endforeach() 144endmacro() 145 146# If the specified 'condition' is true then add a list of flags to both 147# 'LIBCXXABI_COMPILE_FLAGS' and 'LIBCXXABI_LINK_FLAGS'. 148macro(add_flags_if condition) 149 if (${condition}) 150 add_flags(${ARGN}) 151 endif() 152endmacro() 153 154# Add each flag in the list to LIBCXXABI_COMPILE_FLAGS and LIBCXXABI_LINK_FLAGS 155# if that flag is supported by the current compiler. 156macro(add_flags_if_supported) 157 foreach(flag ${ARGN}) 158 mangle_name("${flag}" flagname) 159 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 160 add_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 161 endforeach() 162endmacro() 163 164# Add a list of flags to 'LIBCXXABI_COMPILE_FLAGS'. 165macro(add_compile_flags) 166 foreach(f ${ARGN}) 167 list(APPEND LIBCXXABI_COMPILE_FLAGS ${f}) 168 endforeach() 169endmacro() 170 171# If 'condition' is true then add the specified list of flags to 172# 'LIBCXXABI_COMPILE_FLAGS' 173macro(add_compile_flags_if condition) 174 if (${condition}) 175 add_compile_flags(${ARGN}) 176 endif() 177endmacro() 178 179# For each specified flag, add that flag to 'LIBCXXABI_COMPILE_FLAGS' if the 180# flag is supported by the C++ compiler. 181macro(add_compile_flags_if_supported) 182 foreach(flag ${ARGN}) 183 mangle_name("${flag}" flagname) 184 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 185 add_compile_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 186 endforeach() 187endmacro() 188 189# For each specified flag, add that flag to 'LIBCXXABI_COMPILE_FLAGS' if the 190# flag is supported by the C compiler. 191macro(add_c_compile_flags_if_supported) 192 foreach(flag ${ARGN}) 193 mangle_name("${flag}" flagname) 194 check_c_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 195 add_compile_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 196 endforeach() 197endmacro() 198 199# Add a list of flags to 'LIBCXXABI_LINK_FLAGS'. 200macro(add_link_flags) 201 foreach(f ${ARGN}) 202 list(APPEND LIBCXXABI_LINK_FLAGS ${f}) 203 endforeach() 204endmacro() 205 206# If 'condition' is true then add the specified list of flags to 207# 'LIBCXXABI_LINK_FLAGS' 208macro(add_link_flags_if condition) 209 if (${condition}) 210 add_link_flags(${ARGN}) 211 endif() 212endmacro() 213 214# For each specified flag, add that flag to 'LIBCXXABI_LINK_FLAGS' if the 215# flag is supported by the C++ compiler. 216macro(add_link_flags_if_supported) 217 foreach(flag ${ARGN}) 218 mangle_name("${flag}" flagname) 219 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 220 add_link_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 221 endforeach() 222endmacro() 223 224# Add a list of libraries or link flags to 'LIBCXXABI_LIBRARIES'. 225macro(add_library_flags) 226 foreach(lib ${ARGN}) 227 list(APPEND LIBCXXABI_LIBRARIES ${lib}) 228 endforeach() 229endmacro() 230 231# if 'condition' is true then add the specified list of libraries and flags 232# to 'LIBCXXABI_LIBRARIES'. 233macro(add_library_flags_if condition) 234 if(${condition}) 235 add_library_flags(${ARGN}) 236 endif() 237endmacro() 238 239# Turn a comma separated CMake list into a space separated string. 240macro(split_list listname) 241 string(REPLACE ";" " " ${listname} "${${listname}}") 242endmacro() 243