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