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