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 52 # Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and 53 # 'LIBCXX_LINK_FLAGS'. 54 macro(add_flags) 55 foreach(value ${ARGN}) 56 list(APPEND LIBCXX_COMPILE_FLAGS ${value}) 57 list(APPEND LIBCXX_LINK_FLAGS ${value}) 58 endforeach() 59 endmacro() 60 61 # If the specified 'condition' is true then add a list of flags to both 62 # 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'. 63 macro(add_flags_if condition) 64 if (${condition}) 65 add_flags(${ARGN}) 66 endif() 67 endmacro() 68 69 # Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS 70 # if that flag is supported by the current compiler. 71 macro(add_flags_if_supported) 72 foreach(flag ${ARGN}) 73 mangle_name("${flag}" flagname) 74 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") 75 add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) 76 endforeach() 77 endmacro() 78 79 # Add a list of flags to 'LIBCXX_COMPILE_FLAGS'. 80 macro(add_compile_flags) 81 foreach(f ${ARGN}) 82 list(APPEND LIBCXX_COMPILE_FLAGS ${f}) 83 endforeach() 84 endmacro() 85 86 # If 'condition' is true then add the specified list of flags to 87 # 'LIBCXX_COMPILE_FLAGS' 88 macro(add_compile_flags_if condition) 89 if (${condition}) 90 add_compile_flags(${ARGN}) 91 endif() 92 endmacro() 93 94 # For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the 95 # flag is supported by the C++ compiler. 96 macro(add_compile_flags_if_supported) 97 foreach(flag ${ARGN}) 98 mangle_name("${flag}" flagname) 99 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") 100 add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) 101 endforeach() 102 endmacro() 103 104 # Add a list of flags to 'LIBCXX_LINK_FLAGS'. 105 macro(add_link_flags) 106 foreach(f ${ARGN}) 107 list(APPEND LIBCXX_LINK_FLAGS ${f}) 108 endforeach() 109 endmacro() 110 111 # If 'condition' is true then add the specified list of flags to 112 # 'LIBCXX_LINK_FLAGS' 113 macro(add_link_flags_if condition) 114 if (${condition}) 115 add_link_flags(${ARGN}) 116 endif() 117 endmacro() 118 119 # For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the 120 # flag is supported by the C++ compiler. 121 macro(add_link_flags_if_supported) 122 foreach(flag ${ARGN}) 123 mangle_name("${flag}" flagname) 124 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG") 125 add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag}) 126 endforeach() 127 endmacro() 128 129 # Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'. 130 macro(add_library_flags) 131 foreach(lib ${ARGN}) 132 list(APPEND LIBCXX_LIBRARIES ${lib}) 133 endforeach() 134 endmacro() 135 136 # if 'condition' is true then add the specified list of libraries and flags 137 # to 'LIBCXX_LIBRARIES'. 138 macro(add_library_flags_if condition) 139 if(${condition}) 140 add_library_flags(${ARGN}) 141 endif() 142 endmacro() 143 144 # Turn a comma separated CMake list into a space separated string. 145 macro(split_list listname) 146 string(REPLACE ";" " " ${listname} "${${listname}}") 147 endmacro() 148