blob: a5b4df6600ee3d1b1649c463d504714a7e57907d [file] [log] [blame]
Eric Fiselier156a46d2015-07-30 22:30:34 +00001# 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}")
Eric Fiselier66fd4312018-10-01 01:00:11 +000019 string(REPLACE ":" "_COLON_" strippedStr "${strippedStr}")
Eric Fiselier156a46d2015-07-30 22:30:34 +000020 string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
21 string(REPLACE "+" "X" strippedStr "${strippedStr}")
22 string(TOUPPER "${strippedStr}" ${output})
23endmacro()
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.
28macro(remove_flags)
29 foreach(var ${ARGN})
Eric Fiselier1e6fdc02017-01-14 06:06:47 +000030 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
Eric Fiselier641e79e2017-01-14 10:22:21 +000031 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 Fiselier156a46d2015-07-30 22:30:34 +000034 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()
41endmacro(remove_flags)
42
Eric Fiselier6065f052016-05-10 16:17:43 +000043macro(check_flag_supported flag)
44 mangle_name("${flag}" flagname)
45 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
46endmacro()
47
Eric Fiselier1116a822018-11-13 23:08:31 +000048macro(append_flags DEST)
49 foreach(value ${ARGN})
50 list(APPEND ${DEST} ${value})
51 list(APPEND ${DEST} ${value})
52 endforeach()
53endmacro()
54
55# If the specified 'condition' is true then append the specified list of flags to DEST
56macro(append_flags_if condition DEST)
57 if (${condition})
58 list(APPEND ${DEST} ${ARGN})
59 endif()
60endmacro()
61
62# Add each flag in the list specified by DEST if that flag is supported by the current compiler.
63macro(append_flags_if_supported DEST)
64 foreach(flag ${ARGN})
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})
68 endforeach()
69endmacro()
70
Eric Fiselier156a46d2015-07-30 22:30:34 +000071# Add a macro definition if condition is true.
72macro(define_if condition def)
73 if (${condition})
74 add_definitions(${def})
75 endif()
76endmacro()
77
78# Add a macro definition if condition is not true.
79macro(define_if_not condition def)
80 if (NOT ${condition})
81 add_definitions(${def})
82 endif()
83endmacro()
84
Eric Fiselier1a1c74b2015-10-14 00:22:05 +000085# 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.
Eric Fiselier5cf9a822015-10-13 22:12:02 +000088macro(config_define_if condition def)
89 if (${condition})
90 set(${def} ON)
Eric Fiselier5cf9a822015-10-13 22:12:02 +000091 endif()
92endmacro()
93
94macro(config_define_if_not condition def)
95 if (NOT ${condition})
96 set(${def} ON)
Eric Fiselier5cf9a822015-10-13 22:12:02 +000097 endif()
98endmacro()
99
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000100macro(config_define value def)
101 set(${def} ${value})
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000102endmacro()
103
Eric Fiselier96addd32016-06-02 01:10:08 +0000104# Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS',
105# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
106macro(add_target_flags)
107 foreach(value ${ARGN})
108 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
109 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
110 list(APPEND LIBCXX_COMPILE_FLAGS ${value})
111 list(APPEND LIBCXX_LINK_FLAGS ${value})
112 endforeach()
113endmacro()
114
115# If the specified 'condition' is true then add a list of flags to
116# all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXX_COMPILE_FLAGS'
117# and 'LIBCXX_LINK_FLAGS'.
118macro(add_target_flags_if condition)
119 if (${condition})
120 add_target_flags(${ARGN})
121 endif()
122endmacro()
123
Eric Fiselier156a46d2015-07-30 22:30:34 +0000124# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
125# 'LIBCXX_LINK_FLAGS'.
126macro(add_flags)
127 foreach(value ${ARGN})
128 list(APPEND LIBCXX_COMPILE_FLAGS ${value})
129 list(APPEND LIBCXX_LINK_FLAGS ${value})
130 endforeach()
131endmacro()
132
133# If the specified 'condition' is true then add a list of flags to both
134# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
135macro(add_flags_if condition)
136 if (${condition})
137 add_flags(${ARGN})
138 endif()
139endmacro()
140
141# Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS
142# if that flag is supported by the current compiler.
143macro(add_flags_if_supported)
144 foreach(flag ${ARGN})
145 mangle_name("${flag}" flagname)
146 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
147 add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
148 endforeach()
149endmacro()
150
151# Add a list of flags to 'LIBCXX_COMPILE_FLAGS'.
152macro(add_compile_flags)
153 foreach(f ${ARGN})
154 list(APPEND LIBCXX_COMPILE_FLAGS ${f})
155 endforeach()
156endmacro()
157
158# If 'condition' is true then add the specified list of flags to
159# 'LIBCXX_COMPILE_FLAGS'
160macro(add_compile_flags_if condition)
161 if (${condition})
162 add_compile_flags(${ARGN})
163 endif()
164endmacro()
165
166# For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the
167# flag is supported by the C++ compiler.
168macro(add_compile_flags_if_supported)
169 foreach(flag ${ARGN})
170 mangle_name("${flag}" flagname)
Eric Fiselier6cc285b2015-07-31 21:09:38 +0000171 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
Eric Fiselier156a46d2015-07-30 22:30:34 +0000172 add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
173 endforeach()
174endmacro()
175
176# Add a list of flags to 'LIBCXX_LINK_FLAGS'.
177macro(add_link_flags)
178 foreach(f ${ARGN})
179 list(APPEND LIBCXX_LINK_FLAGS ${f})
180 endforeach()
181endmacro()
182
183# If 'condition' is true then add the specified list of flags to
184# 'LIBCXX_LINK_FLAGS'
185macro(add_link_flags_if condition)
186 if (${condition})
187 add_link_flags(${ARGN})
188 endif()
189endmacro()
190
191# For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the
192# flag is supported by the C++ compiler.
193macro(add_link_flags_if_supported)
194 foreach(flag ${ARGN})
195 mangle_name("${flag}" flagname)
196 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
197 add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
198 endforeach()
199endmacro()
200
201# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
202macro(add_library_flags)
203 foreach(lib ${ARGN})
204 list(APPEND LIBCXX_LIBRARIES ${lib})
205 endforeach()
206endmacro()
207
208# if 'condition' is true then add the specified list of libraries and flags
209# to 'LIBCXX_LIBRARIES'.
210macro(add_library_flags_if condition)
211 if(${condition})
212 add_library_flags(${ARGN})
213 endif()
214endmacro()
215
Eric Fiselier156a46d2015-07-30 22:30:34 +0000216# Turn a comma separated CMake list into a space separated string.
217macro(split_list listname)
218 string(REPLACE ";" " " ${listname} "${${listname}}")
219endmacro()
Louis Dionne5610b522019-04-30 15:44:19 +0000220
221# For each specified flag, add that link flag to the provided target.
222# The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.
223function(target_add_link_flags_if_supported target visibility)
224 foreach(flag ${ARGN})
225 mangle_name("${flag}" flagname)
226 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
227 if (LIBCXX_SUPPORTS_${flagname}_FLAG)
228 target_link_libraries(${target} ${visibility} ${flag})
229 endif()
230 endforeach()
231endfunction()
232
233# For each specified flag, add that compile flag to the provided target.
234# The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.
235function(target_add_compile_flags_if_supported target visibility)
236 foreach(flag ${ARGN})
237 mangle_name("${flag}" flagname)
238 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
239 if (LIBCXX_SUPPORTS_${flagname}_FLAG)
240 target_compile_options(${target} ${visibility} ${flag})
241 endif()
242 endforeach()
243endfunction()