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}") |
| 19 | string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}") |
| 20 | string(REPLACE "+" "X" strippedStr "${strippedStr}") |
| 21 | string(TOUPPER "${strippedStr}" ${output}) |
| 22 | endmacro() |
| 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. |
| 27 | macro(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() |
| 36 | endmacro(remove_flags) |
| 37 | |
| 38 | # Add a macro definition if condition is true. |
| 39 | macro(define_if condition def) |
| 40 | if (${condition}) |
| 41 | add_definitions(${def}) |
| 42 | endif() |
| 43 | endmacro() |
| 44 | |
| 45 | # Add a macro definition if condition is not true. |
| 46 | macro(define_if_not condition def) |
| 47 | if (NOT ${condition}) |
| 48 | add_definitions(${def}) |
| 49 | endif() |
| 50 | endmacro() |
| 51 | |
Eric Fiselier | 1a1c74b | 2015-10-14 00:22:05 +0000 | [diff] [blame^] | 52 | # Add a macro definition to the __config_site file if the specified condition |
| 53 | # is 'true'. Note that '-D${def}' is not added. Instead it is expected that |
| 54 | # the build include the '__config_site' header. |
Eric Fiselier | 5cf9a82 | 2015-10-13 22:12:02 +0000 | [diff] [blame] | 55 | macro(config_define_if condition def) |
| 56 | if (${condition}) |
| 57 | set(${def} ON) |
Eric Fiselier | 5cf9a82 | 2015-10-13 22:12:02 +0000 | [diff] [blame] | 58 | set(LIBCXX_NEEDS_SITE_CONFIG ON) |
| 59 | endif() |
| 60 | endmacro() |
| 61 | |
| 62 | macro(config_define_if_not condition def) |
| 63 | if (NOT ${condition}) |
| 64 | set(${def} ON) |
Eric Fiselier | 5cf9a82 | 2015-10-13 22:12:02 +0000 | [diff] [blame] | 65 | set(LIBCXX_NEEDS_SITE_CONFIG ON) |
| 66 | endif() |
| 67 | endmacro() |
| 68 | |
Evgeniy Stepanov | da2ff7e | 2015-10-13 23:48:28 +0000 | [diff] [blame] | 69 | macro(config_define value def) |
| 70 | set(${def} ${value}) |
Evgeniy Stepanov | da2ff7e | 2015-10-13 23:48:28 +0000 | [diff] [blame] | 71 | set(LIBCXX_NEEDS_SITE_CONFIG ON) |
| 72 | endmacro() |
| 73 | |
Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 74 | # Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and |
| 75 | # 'LIBCXX_LINK_FLAGS'. |
| 76 | macro(add_flags) |
| 77 | foreach(value ${ARGN}) |
| 78 | list(APPEND LIBCXX_COMPILE_FLAGS ${value}) |
| 79 | list(APPEND LIBCXX_LINK_FLAGS ${value}) |
| 80 | endforeach() |
| 81 | endmacro() |
| 82 | |
| 83 | # If the specified 'condition' is true then add a list of flags to both |
| 84 | # 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'. |
| 85 | macro(add_flags_if condition) |
| 86 | if (${condition}) |
| 87 | add_flags(${ARGN}) |
| 88 | endif() |
| 89 | endmacro() |
| 90 | |
| 91 | # Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS |
| 92 | # if that flag is supported by the current compiler. |
| 93 | macro(add_flags_if_supported) |
| 94 | foreach(flag ${ARGN}) |
| 95 | mangle_name("${flag}" flagname) |
| 96 | check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") |
| 97 | add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) |
| 98 | endforeach() |
| 99 | endmacro() |
| 100 | |
| 101 | # Add a list of flags to 'LIBCXX_COMPILE_FLAGS'. |
| 102 | macro(add_compile_flags) |
| 103 | foreach(f ${ARGN}) |
| 104 | list(APPEND LIBCXX_COMPILE_FLAGS ${f}) |
| 105 | endforeach() |
| 106 | endmacro() |
| 107 | |
| 108 | # If 'condition' is true then add the specified list of flags to |
| 109 | # 'LIBCXX_COMPILE_FLAGS' |
| 110 | macro(add_compile_flags_if condition) |
| 111 | if (${condition}) |
| 112 | add_compile_flags(${ARGN}) |
| 113 | endif() |
| 114 | endmacro() |
| 115 | |
| 116 | # For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the |
| 117 | # flag is supported by the C++ compiler. |
| 118 | macro(add_compile_flags_if_supported) |
| 119 | foreach(flag ${ARGN}) |
| 120 | mangle_name("${flag}" flagname) |
Eric Fiselier | 6cc285b | 2015-07-31 21:09:38 +0000 | [diff] [blame] | 121 | check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") |
Eric Fiselier | 156a46d | 2015-07-30 22:30:34 +0000 | [diff] [blame] | 122 | add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) |
| 123 | endforeach() |
| 124 | endmacro() |
| 125 | |
| 126 | # Add a list of flags to 'LIBCXX_LINK_FLAGS'. |
| 127 | macro(add_link_flags) |
| 128 | foreach(f ${ARGN}) |
| 129 | list(APPEND LIBCXX_LINK_FLAGS ${f}) |
| 130 | endforeach() |
| 131 | endmacro() |
| 132 | |
| 133 | # If 'condition' is true then add the specified list of flags to |
| 134 | # 'LIBCXX_LINK_FLAGS' |
| 135 | macro(add_link_flags_if condition) |
| 136 | if (${condition}) |
| 137 | add_link_flags(${ARGN}) |
| 138 | endif() |
| 139 | endmacro() |
| 140 | |
| 141 | # For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the |
| 142 | # flag is supported by the C++ compiler. |
| 143 | macro(add_link_flags_if_supported) |
| 144 | foreach(flag ${ARGN}) |
| 145 | mangle_name("${flag}" flagname) |
| 146 | check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") |
| 147 | add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) |
| 148 | endforeach() |
| 149 | endmacro() |
| 150 | |
| 151 | # Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'. |
| 152 | macro(add_library_flags) |
| 153 | foreach(lib ${ARGN}) |
| 154 | list(APPEND LIBCXX_LIBRARIES ${lib}) |
| 155 | endforeach() |
| 156 | endmacro() |
| 157 | |
| 158 | # if 'condition' is true then add the specified list of libraries and flags |
| 159 | # to 'LIBCXX_LIBRARIES'. |
| 160 | macro(add_library_flags_if condition) |
| 161 | if(${condition}) |
| 162 | add_library_flags(${ARGN}) |
| 163 | endif() |
| 164 | endmacro() |
| 165 | |
| 166 | # Turn a comma separated CMake list into a space separated string. |
| 167 | macro(split_list listname) |
| 168 | string(REPLACE ";" " " ${listname} "${${listname}}") |
| 169 | endmacro() |