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.
7 include(CheckCXXCompilerFlag)
9 unset(add_flag_if_supported)
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 ":" "_COLON_" strippedStr "${strippedStr}")
20 string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
21 string(REPLACE "+" "X" strippedStr "${strippedStr}")
22 string(TOUPPER "${strippedStr}" ${output})
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.
30 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
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}")
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})
41 endmacro(remove_flags)
43 macro(check_flag_supported flag)
44 mangle_name("${flag}" flagname)
45 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
48 macro(append_flags DEST)
49 foreach(value ${ARGN})
50 list(APPEND ${DEST} ${value})
51 list(APPEND ${DEST} ${value})
55 # If the specified 'condition' is true then append the specified list of flags to DEST
56 macro(append_flags_if condition DEST)
58 list(APPEND ${DEST} ${ARGN})
62 # Add each flag in the list specified by DEST if that flag is supported by the current compiler.
63 macro(append_flags_if_supported DEST)
65 mangle_name("${flag}" flagname)
66 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
67 append_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${DEST} ${flag})
71 # Add a macro definition if condition is true.
72 macro(define_if condition def)
74 add_definitions(${def})
78 # Add a macro definition if condition is not true.
79 macro(define_if_not condition def)
81 add_definitions(${def})
85 # Add a macro definition to the __config_site file if the specified condition
86 # is 'true'. Note that '-D${def}' is not added. Instead it is expected that
87 # the build include the '__config_site' header.
88 macro(config_define_if condition def)
91 set(LIBCXX_NEEDS_SITE_CONFIG ON)
95 macro(config_define_if_not condition def)
98 set(LIBCXX_NEEDS_SITE_CONFIG ON)
102 macro(config_define value def)
104 set(LIBCXX_NEEDS_SITE_CONFIG ON)
107 # Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS',
108 # 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
109 macro(add_target_flags)
110 foreach(value ${ARGN})
111 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
112 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
113 list(APPEND LIBCXX_COMPILE_FLAGS ${value})
114 list(APPEND LIBCXX_LINK_FLAGS ${value})
118 # If the specified 'condition' is true then add a list of flags to
119 # all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXX_COMPILE_FLAGS'
120 # and 'LIBCXX_LINK_FLAGS'.
121 macro(add_target_flags_if condition)
123 add_target_flags(${ARGN})
127 # Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
128 # 'LIBCXX_LINK_FLAGS'.
130 foreach(value ${ARGN})
131 list(APPEND LIBCXX_COMPILE_FLAGS ${value})
132 list(APPEND LIBCXX_LINK_FLAGS ${value})
136 # If the specified 'condition' is true then add a list of flags to both
137 # 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
138 macro(add_flags_if condition)
144 # Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS
145 # if that flag is supported by the current compiler.
146 macro(add_flags_if_supported)
147 foreach(flag ${ARGN})
148 mangle_name("${flag}" flagname)
149 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
150 add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
154 # Add a list of flags to 'LIBCXX_COMPILE_FLAGS'.
155 macro(add_compile_flags)
157 list(APPEND LIBCXX_COMPILE_FLAGS ${f})
161 # If 'condition' is true then add the specified list of flags to
162 # 'LIBCXX_COMPILE_FLAGS'
163 macro(add_compile_flags_if condition)
165 add_compile_flags(${ARGN})
169 # For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the
170 # flag is supported by the C++ compiler.
171 macro(add_compile_flags_if_supported)
172 foreach(flag ${ARGN})
173 mangle_name("${flag}" flagname)
174 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
175 add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
179 # Add a list of flags to 'LIBCXX_LINK_FLAGS'.
180 macro(add_link_flags)
182 list(APPEND LIBCXX_LINK_FLAGS ${f})
186 # If 'condition' is true then add the specified list of flags to
187 # 'LIBCXX_LINK_FLAGS'
188 macro(add_link_flags_if condition)
190 add_link_flags(${ARGN})
194 # For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the
195 # flag is supported by the C++ compiler.
196 macro(add_link_flags_if_supported)
197 foreach(flag ${ARGN})
198 mangle_name("${flag}" flagname)
199 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
200 add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
204 # Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
205 macro(add_library_flags)
207 list(APPEND LIBCXX_LIBRARIES ${lib})
211 # if 'condition' is true then add the specified list of libraries and flags
212 # to 'LIBCXX_LIBRARIES'.
213 macro(add_library_flags_if condition)
215 add_library_flags(${ARGN})
219 # Turn a comma separated CMake list into a space separated string.
220 macro(split_list listname)
221 string(REPLACE ";" " " ${listname} "${${listname}}")
224 # For each specified flag, add that link flag to the provided target.
225 # The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.
226 function(target_add_link_flags_if_supported target visibility)
227 foreach(flag ${ARGN})
228 mangle_name("${flag}" flagname)
229 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
230 if (LIBCXX_SUPPORTS_${flagname}_FLAG)
231 target_link_libraries(${target} ${visibility} ${flag})
236 # For each specified flag, add that compile flag to the provided target.
237 # The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.
238 function(target_add_compile_flags_if_supported target visibility)
239 foreach(flag ${ARGN})
240 mangle_name("${flag}" flagname)
241 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
242 if (LIBCXX_SUPPORTS_${flagname}_FLAG)
243 target_compile_options(${target} ${visibility} ${flag})