1# HandleLibcxxFlags - A set of macros used to setup the flags used to compile 2# and link libc++. These macros add flags to the following CMake variables. 3# - LIBCXX_COMPILE_FLAGS: flags used to compile libc++ 4# - LIBCXX_LINK_FLAGS: flags used to link libc++ 5# - LIBCXX_LIBRARIES: libraries to link libc++ 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}" "LIBCXX_SUPPORTS_${flagname}_FLAG") 45endmacro() 46 47# Add a macro definition if condition is true. 48macro(define_if condition def) 49 if (${condition}) 50 add_definitions(${def}) 51 endif() 52endmacro() 53 54# Add a macro definition if condition is not true. 55macro(define_if_not condition def) 56 if (NOT ${condition}) 57 add_definitions(${def}) 58 endif() 59endmacro() 60 61# Add a macro definition to the __config_site file if the specified condition 62# is 'true'. Note that '-D${def}' is not added. Instead it is expected that 63# the build include the '__config_site' header. 64macro(config_define_if condition def) 65 if (${condition}) 66 set(${def} ON) 67 set(LIBCXX_NEEDS_SITE_CONFIG ON) 68 endif() 69endmacro() 70 71macro(config_define_if_not condition def) 72 if (NOT ${condition}) 73 set(${def} ON) 74 set(LIBCXX_NEEDS_SITE_CONFIG ON) 75 endif() 76endmacro() 77 78macro(config_define value def) 79 set(${def} ${value}) 80 set(LIBCXX_NEEDS_SITE_CONFIG ON) 81endmacro() 82 83# Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 84# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'. 85macro(add_target_flags) 86 foreach(value ${ARGN}) 87 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}") 88 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}") 89 list(APPEND LIBCXX_COMPILE_FLAGS ${value}) 90 list(APPEND LIBCXX_LINK_FLAGS ${value}) 91 endforeach() 92endmacro() 93 94# If the specified 'condition' is true then add a list of flags to 95# all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXX_COMPILE_FLAGS' 96# and 'LIBCXX_LINK_FLAGS'. 97macro(add_target_flags_if condition) 98 if (${condition}) 99 add_target_flags(${ARGN}) 100 endif() 101endmacro() 102 103# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and 104# 'LIBCXX_LINK_FLAGS'. 105macro(add_flags) 106 foreach(value ${ARGN}) 107 list(APPEND LIBCXX_COMPILE_FLAGS ${value}) 108 list(APPEND LIBCXX_LINK_FLAGS ${value}) 109 endforeach() 110endmacro() 111 112# If the specified 'condition' is true then add a list of flags to both 113# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'. 114macro(add_flags_if condition) 115 if (${condition}) 116 add_flags(${ARGN}) 117 endif() 118endmacro() 119 120# Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS 121# if that flag is supported by the current compiler. 122macro(add_flags_if_supported) 123 foreach(flag ${ARGN}) 124 mangle_name("${flag}" flagname) 125 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") 126 add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) 127 endforeach() 128endmacro() 129 130# Add a list of flags to 'LIBCXX_COMPILE_FLAGS'. 131macro(add_compile_flags) 132 foreach(f ${ARGN}) 133 list(APPEND LIBCXX_COMPILE_FLAGS ${f}) 134 endforeach() 135endmacro() 136 137# If 'condition' is true then add the specified list of flags to 138# 'LIBCXX_COMPILE_FLAGS' 139macro(add_compile_flags_if condition) 140 if (${condition}) 141 add_compile_flags(${ARGN}) 142 endif() 143endmacro() 144 145# For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the 146# flag is supported by the C++ compiler. 147macro(add_compile_flags_if_supported) 148 foreach(flag ${ARGN}) 149 mangle_name("${flag}" flagname) 150 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") 151 add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) 152 endforeach() 153endmacro() 154 155# Add a list of flags to 'LIBCXX_LINK_FLAGS'. 156macro(add_link_flags) 157 foreach(f ${ARGN}) 158 list(APPEND LIBCXX_LINK_FLAGS ${f}) 159 endforeach() 160endmacro() 161 162# If 'condition' is true then add the specified list of flags to 163# 'LIBCXX_LINK_FLAGS' 164macro(add_link_flags_if condition) 165 if (${condition}) 166 add_link_flags(${ARGN}) 167 endif() 168endmacro() 169 170# For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the 171# flag is supported by the C++ compiler. 172macro(add_link_flags_if_supported) 173 foreach(flag ${ARGN}) 174 mangle_name("${flag}" flagname) 175 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") 176 add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) 177 endforeach() 178endmacro() 179 180# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'. 181macro(add_library_flags) 182 foreach(lib ${ARGN}) 183 list(APPEND LIBCXX_LIBRARIES ${lib}) 184 endforeach() 185endmacro() 186 187# if 'condition' is true then add the specified list of libraries and flags 188# to 'LIBCXX_LIBRARIES'. 189macro(add_library_flags_if condition) 190 if(${condition}) 191 add_library_flags(${ARGN}) 192 endif() 193endmacro() 194 195# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'. 196macro(add_interface_library) 197 foreach(lib ${ARGN}) 198 list(APPEND LIBCXX_LIBRARIES ${lib}) 199 list(APPEND LIBCXX_INTERFACE_LIBRARIES ${lib}) 200 endforeach() 201endmacro() 202 203# Turn a comma separated CMake list into a space separated string. 204macro(split_list listname) 205 string(REPLACE ";" " " ${listname} "${${listname}}") 206endmacro() 207