Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 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 | |
| 7 | include(CheckCXXCompilerFlag) |
| 8 | |
| 9 | unset(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 |
| 13 | macro(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}") |
Eric Fiselier | 66fd431 | 2018-10-01 01:00:11 +0000 | [diff] [blame^] | 19 | string(REPLACE ":" "_COLON_" strippedStr "${strippedStr}") |
Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 20 | string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}") |
| 21 | string(REPLACE "+" "X" strippedStr "${strippedStr}") |
| 22 | string(TOUPPER "${strippedStr}" ${output}) |
| 23 | endmacro() |
| 24 | |
| 25 | # Remove a list of flags from all CMake variables that affect compile flags. |
| 26 | # This can be used to remove unwanted flags specified on the command line |
| 27 | # or added in other parts of LLVM's cmake configuration. |
| 28 | macro(remove_flags) |
| 29 | foreach(var ${ARGN}) |
Eric Fiselier | 1e6fdc0 | 2017-01-14 06:06:47 +0000 | [diff] [blame] | 30 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") |
Eric Fiselier | 641e79e | 2017-01-14 10:22:21 +0000 | [diff] [blame] | 31 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}") |
| 32 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") |
| 33 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") |
Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 34 | string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 35 | string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
| 36 | string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") |
| 37 | string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") |
| 38 | string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}") |
| 39 | remove_definitions(${var}) |
| 40 | endforeach() |
| 41 | endmacro(remove_flags) |
| 42 | |
Eric Fiselier | 6065f05 | 2016-05-10 16:17:43 +0000 | [diff] [blame] | 43 | macro(check_flag_supported flag) |
| 44 | mangle_name("${flag}" flagname) |
| 45 | check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") |
| 46 | endmacro() |
| 47 | |
Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 48 | # Add a macro definition if condition is true. |
| 49 | macro(define_if condition def) |
| 50 | if (${condition}) |
| 51 | add_definitions(${def}) |
| 52 | endif() |
| 53 | endmacro() |
| 54 | |
| 55 | # Add a macro definition if condition is not true. |
| 56 | macro(define_if_not condition def) |
| 57 | if (NOT ${condition}) |
| 58 | add_definitions(${def}) |
| 59 | endif() |
| 60 | endmacro() |
| 61 | |
Eric Fiselier | 1a1c74b | 2015-10-14 00:22:05 +0000 | [diff] [blame] | 62 | # Add a macro definition to the __config_site file if the specified condition |
| 63 | # is 'true'. Note that '-D${def}' is not added. Instead it is expected that |
| 64 | # the build include the '__config_site' header. |
Eric Fiselier | 5cf9a82 | 2015-10-13 22:12:02 +0000 | [diff] [blame] | 65 | macro(config_define_if condition def) |
| 66 | if (${condition}) |
| 67 | set(${def} ON) |
Eric Fiselier | 5cf9a82 | 2015-10-13 22:12:02 +0000 | [diff] [blame] | 68 | set(LIBCXX_NEEDS_SITE_CONFIG ON) |
| 69 | endif() |
| 70 | endmacro() |
| 71 | |
| 72 | macro(config_define_if_not condition def) |
| 73 | if (NOT ${condition}) |
| 74 | set(${def} ON) |
Eric Fiselier | 5cf9a82 | 2015-10-13 22:12:02 +0000 | [diff] [blame] | 75 | set(LIBCXX_NEEDS_SITE_CONFIG ON) |
| 76 | endif() |
| 77 | endmacro() |
| 78 | |
Evgeniy Stepanov | da2ff7e | 2015-10-13 23:48:28 +0000 | [diff] [blame] | 79 | macro(config_define value def) |
| 80 | set(${def} ${value}) |
Evgeniy Stepanov | da2ff7e | 2015-10-13 23:48:28 +0000 | [diff] [blame] | 81 | set(LIBCXX_NEEDS_SITE_CONFIG ON) |
| 82 | endmacro() |
| 83 | |
Eric Fiselier | 96addd3 | 2016-06-02 01:10:08 +0000 | [diff] [blame] | 84 | # Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', |
| 85 | # 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'. |
| 86 | macro(add_target_flags) |
| 87 | foreach(value ${ARGN}) |
| 88 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}") |
| 89 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}") |
| 90 | list(APPEND LIBCXX_COMPILE_FLAGS ${value}) |
| 91 | list(APPEND LIBCXX_LINK_FLAGS ${value}) |
| 92 | endforeach() |
| 93 | endmacro() |
| 94 | |
| 95 | # If the specified 'condition' is true then add a list of flags to |
| 96 | # all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXX_COMPILE_FLAGS' |
| 97 | # and 'LIBCXX_LINK_FLAGS'. |
| 98 | macro(add_target_flags_if condition) |
| 99 | if (${condition}) |
| 100 | add_target_flags(${ARGN}) |
| 101 | endif() |
| 102 | endmacro() |
| 103 | |
Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 104 | # Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and |
| 105 | # 'LIBCXX_LINK_FLAGS'. |
| 106 | macro(add_flags) |
| 107 | foreach(value ${ARGN}) |
| 108 | list(APPEND LIBCXX_COMPILE_FLAGS ${value}) |
| 109 | list(APPEND LIBCXX_LINK_FLAGS ${value}) |
| 110 | endforeach() |
| 111 | endmacro() |
| 112 | |
| 113 | # If the specified 'condition' is true then add a list of flags to both |
| 114 | # 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'. |
| 115 | macro(add_flags_if condition) |
| 116 | if (${condition}) |
| 117 | add_flags(${ARGN}) |
| 118 | endif() |
| 119 | endmacro() |
| 120 | |
| 121 | # Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS |
| 122 | # if that flag is supported by the current compiler. |
| 123 | macro(add_flags_if_supported) |
| 124 | foreach(flag ${ARGN}) |
| 125 | mangle_name("${flag}" flagname) |
| 126 | check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") |
| 127 | add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) |
| 128 | endforeach() |
| 129 | endmacro() |
| 130 | |
| 131 | # Add a list of flags to 'LIBCXX_COMPILE_FLAGS'. |
| 132 | macro(add_compile_flags) |
| 133 | foreach(f ${ARGN}) |
| 134 | list(APPEND LIBCXX_COMPILE_FLAGS ${f}) |
| 135 | endforeach() |
| 136 | endmacro() |
| 137 | |
| 138 | # If 'condition' is true then add the specified list of flags to |
| 139 | # 'LIBCXX_COMPILE_FLAGS' |
| 140 | macro(add_compile_flags_if condition) |
| 141 | if (${condition}) |
| 142 | add_compile_flags(${ARGN}) |
| 143 | endif() |
| 144 | endmacro() |
| 145 | |
| 146 | # For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the |
| 147 | # flag is supported by the C++ compiler. |
| 148 | macro(add_compile_flags_if_supported) |
| 149 | foreach(flag ${ARGN}) |
| 150 | mangle_name("${flag}" flagname) |
Eric Fiselier | 6cc285b | 2015-07-31 21:09:38 +0000 | [diff] [blame] | 151 | check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") |
Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 152 | add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) |
| 153 | endforeach() |
| 154 | endmacro() |
| 155 | |
| 156 | # Add a list of flags to 'LIBCXX_LINK_FLAGS'. |
| 157 | macro(add_link_flags) |
| 158 | foreach(f ${ARGN}) |
| 159 | list(APPEND LIBCXX_LINK_FLAGS ${f}) |
| 160 | endforeach() |
| 161 | endmacro() |
| 162 | |
| 163 | # If 'condition' is true then add the specified list of flags to |
| 164 | # 'LIBCXX_LINK_FLAGS' |
| 165 | macro(add_link_flags_if condition) |
| 166 | if (${condition}) |
| 167 | add_link_flags(${ARGN}) |
| 168 | endif() |
| 169 | endmacro() |
| 170 | |
| 171 | # For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the |
| 172 | # flag is supported by the C++ compiler. |
| 173 | macro(add_link_flags_if_supported) |
| 174 | foreach(flag ${ARGN}) |
| 175 | mangle_name("${flag}" flagname) |
| 176 | check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") |
| 177 | add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) |
| 178 | endforeach() |
| 179 | endmacro() |
| 180 | |
| 181 | # Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'. |
| 182 | macro(add_library_flags) |
| 183 | foreach(lib ${ARGN}) |
| 184 | list(APPEND LIBCXX_LIBRARIES ${lib}) |
| 185 | endforeach() |
| 186 | endmacro() |
| 187 | |
| 188 | # if 'condition' is true then add the specified list of libraries and flags |
| 189 | # to 'LIBCXX_LIBRARIES'. |
| 190 | macro(add_library_flags_if condition) |
| 191 | if(${condition}) |
| 192 | add_library_flags(${ARGN}) |
| 193 | endif() |
| 194 | endmacro() |
| 195 | |
Eric Fiselier | cdb497b | 2016-10-09 21:34:03 +0000 | [diff] [blame] | 196 | # Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'. |
| 197 | macro(add_interface_library) |
| 198 | foreach(lib ${ARGN}) |
| 199 | list(APPEND LIBCXX_LIBRARIES ${lib}) |
| 200 | list(APPEND LIBCXX_INTERFACE_LIBRARIES ${lib}) |
| 201 | endforeach() |
| 202 | endmacro() |
| 203 | |
Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 204 | # Turn a comma separated CMake list into a space separated string. |
| 205 | macro(split_list listname) |
| 206 | string(REPLACE ";" " " ${listname} "${${listname}}") |
| 207 | endmacro() |