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