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 a specified list of flags to both 'LIBCXXABI_COMPILE_FLAGS' and 127# 'LIBCXXABI_LINK_FLAGS'. 128macro(add_flags) 129 foreach(value ${ARGN}) 130 list(APPEND LIBCXXABI_COMPILE_FLAGS ${value}) 131 list(APPEND LIBCXXABI_LINK_FLAGS ${value}) 132 endforeach() 133endmacro() 134 135# If the specified 'condition' is true then add a list of flags to both 136# 'LIBCXXABI_COMPILE_FLAGS' and 'LIBCXXABI_LINK_FLAGS'. 137macro(add_flags_if condition) 138 if (${condition}) 139 add_flags(${ARGN}) 140 endif() 141endmacro() 142 143# Add each flag in the list to LIBCXXABI_COMPILE_FLAGS and LIBCXXABI_LINK_FLAGS 144# if that flag is supported by the current compiler. 145macro(add_flags_if_supported) 146 foreach(flag ${ARGN}) 147 mangle_name("${flag}" flagname) 148 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 149 add_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 150 endforeach() 151endmacro() 152 153# Add a list of flags to 'LIBCXXABI_COMPILE_FLAGS'. 154macro(add_compile_flags) 155 foreach(f ${ARGN}) 156 list(APPEND LIBCXXABI_COMPILE_FLAGS ${f}) 157 endforeach() 158endmacro() 159 160# If 'condition' is true then add the specified list of flags to 161# 'LIBCXXABI_COMPILE_FLAGS' 162macro(add_compile_flags_if condition) 163 if (${condition}) 164 add_compile_flags(${ARGN}) 165 endif() 166endmacro() 167 168# For each specified flag, add that flag to 'LIBCXXABI_COMPILE_FLAGS' if the 169# flag is supported by the C++ compiler. 170macro(add_compile_flags_if_supported) 171 foreach(flag ${ARGN}) 172 mangle_name("${flag}" flagname) 173 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 174 add_compile_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 175 endforeach() 176endmacro() 177 178# For each specified flag, add that flag to 'LIBCXXABI_COMPILE_FLAGS' if the 179# flag is supported by the C compiler. 180macro(add_c_compile_flags_if_supported) 181 foreach(flag ${ARGN}) 182 mangle_name("${flag}" flagname) 183 check_c_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 184 add_compile_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 185 endforeach() 186endmacro() 187 188# Add a list of flags to 'LIBCXXABI_LINK_FLAGS'. 189macro(add_link_flags) 190 foreach(f ${ARGN}) 191 list(APPEND LIBCXXABI_LINK_FLAGS ${f}) 192 endforeach() 193endmacro() 194 195# If 'condition' is true then add the specified list of flags to 196# 'LIBCXXABI_LINK_FLAGS' 197macro(add_link_flags_if condition) 198 if (${condition}) 199 add_link_flags(${ARGN}) 200 endif() 201endmacro() 202 203# For each specified flag, add that flag to 'LIBCXXABI_LINK_FLAGS' if the 204# flag is supported by the C++ compiler. 205macro(add_link_flags_if_supported) 206 foreach(flag ${ARGN}) 207 mangle_name("${flag}" flagname) 208 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG") 209 add_link_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag}) 210 endforeach() 211endmacro() 212 213# Add a list of libraries or link flags to 'LIBCXXABI_LIBRARIES'. 214macro(add_library_flags) 215 foreach(lib ${ARGN}) 216 list(APPEND LIBCXXABI_LIBRARIES ${lib}) 217 endforeach() 218endmacro() 219 220# if 'condition' is true then add the specified list of libraries and flags 221# to 'LIBCXXABI_LIBRARIES'. 222macro(add_library_flags_if condition) 223 if(${condition}) 224 add_library_flags(${ARGN}) 225 endif() 226endmacro() 227 228# Turn a comma separated CMake list into a space separated string. 229macro(split_list listname) 230 string(REPLACE ";" " " ${listname} "${${listname}}") 231endmacro() 232