blob: 3eddd7bdc6285e43fe0e7d3ce4d03c71aac44c70 [file] [log] [blame]
Martell Malone380a70a2017-06-01 00:09:20 +00001# HandleLibcxxFlags - A set of macros used to setup the flags used to compile
2# and link libc++abi. These macros add flags to the following CMake variables.
3# - LIBCXXABI_COMPILE_FLAGS: flags used to compile libc++abi
4# - LIBCXXABI_LINK_FLAGS: flags used to link libc++abi
5# - LIBCXXABI_LIBRARIES: libraries to link libc++abi 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}")
19 string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
20 string(REPLACE "+" "X" strippedStr "${strippedStr}")
21 string(TOUPPER "${strippedStr}" ${output})
22endmacro()
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.
27macro(remove_flags)
28 foreach(var ${ARGN})
29 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
30 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
31 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
32 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
33 string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
34 string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
35 string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
36 string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
37 string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}")
38 remove_definitions(${var})
39 endforeach()
40endmacro(remove_flags)
41
42macro(check_flag_supported flag)
43 mangle_name("${flag}" flagname)
44 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
45endmacro()
46
47# Add a macro definition if condition is true.
48macro(define_if condition def)
49 if (${condition})
50 add_definitions(${def})
51 endif()
52endmacro()
53
54# Add a macro definition if condition is not true.
55macro(define_if_not condition def)
56 if (NOT ${condition})
57 add_definitions(${def})
58 endif()
59endmacro()
60
61# Add a macro definition to the __config_site file if the specified condition
62# is 'true'. Note that '-D${def}' is not added. Instead it is expected that
63# the build include the '__config_site' header.
64macro(config_define_if condition def)
65 if (${condition})
66 set(${def} ON)
67 set(LIBCXXABI_NEEDS_SITE_CONFIG ON)
68 endif()
69endmacro()
70
71macro(config_define_if_not condition def)
72 if (NOT ${condition})
73 set(${def} ON)
74 set(LIBCXXABI_NEEDS_SITE_CONFIG ON)
75 endif()
76endmacro()
77
78macro(config_define value def)
79 set(${def} ${value})
80 set(LIBCXXABI_NEEDS_SITE_CONFIG ON)
81endmacro()
82
83# Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS',
84# 'LIBCXXABI_COMPILE_FLAGS' and 'LIBCXXABI_LINK_FLAGS'.
85macro(add_target_flags)
86 foreach(value ${ARGN})
87 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
88 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
89 list(APPEND LIBCXXABI_COMPILE_FLAGS ${value})
90 list(APPEND LIBCXXABI_LINK_FLAGS ${value})
91 endforeach()
92endmacro()
93
94# If the specified 'condition' is true then add a list of flags to
95# all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXXABI_COMPILE_FLAGS'
96# and 'LIBCXXABI_LINK_FLAGS'.
97macro(add_target_flags_if condition)
98 if (${condition})
99 add_target_flags(${ARGN})
100 endif()
101endmacro()
102
103# Add a specified list of flags to both 'LIBCXXABI_COMPILE_FLAGS' and
104# 'LIBCXXABI_LINK_FLAGS'.
105macro(add_flags)
106 foreach(value ${ARGN})
107 list(APPEND LIBCXXABI_COMPILE_FLAGS ${value})
108 list(APPEND LIBCXXABI_LINK_FLAGS ${value})
109 endforeach()
110endmacro()
111
112# If the specified 'condition' is true then add a list of flags to both
113# 'LIBCXXABI_COMPILE_FLAGS' and 'LIBCXXABI_LINK_FLAGS'.
114macro(add_flags_if condition)
115 if (${condition})
116 add_flags(${ARGN})
117 endif()
118endmacro()
119
120# Add each flag in the list to LIBCXXABI_COMPILE_FLAGS and LIBCXXABI_LINK_FLAGS
121# if that flag is supported by the current compiler.
122macro(add_flags_if_supported)
123 foreach(flag ${ARGN})
124 mangle_name("${flag}" flagname)
125 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
126 add_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
127 endforeach()
128endmacro()
129
130# Add a list of flags to 'LIBCXXABI_COMPILE_FLAGS'.
131macro(add_compile_flags)
132 foreach(f ${ARGN})
133 list(APPEND LIBCXXABI_COMPILE_FLAGS ${f})
134 endforeach()
135endmacro()
136
137# If 'condition' is true then add the specified list of flags to
138# 'LIBCXXABI_COMPILE_FLAGS'
139macro(add_compile_flags_if condition)
140 if (${condition})
141 add_compile_flags(${ARGN})
142 endif()
143endmacro()
144
145# For each specified flag, add that flag to 'LIBCXXABI_COMPILE_FLAGS' if the
146# flag is supported by the C++ compiler.
147macro(add_compile_flags_if_supported)
148 foreach(flag ${ARGN})
149 mangle_name("${flag}" flagname)
150 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
151 add_compile_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
152 endforeach()
153endmacro()
154
155# For each specified flag, add that flag to 'LIBCXXABI_COMPILE_FLAGS' if the
156# flag is supported by the C compiler.
157macro(add_c_compile_flags_if_supported)
158 foreach(flag ${ARGN})
159 mangle_name("${flag}" flagname)
160 check_c_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
161 add_compile_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
162 endforeach()
163endmacro()
164
165# Add a list of flags to 'LIBCXXABI_LINK_FLAGS'.
166macro(add_link_flags)
167 foreach(f ${ARGN})
168 list(APPEND LIBCXXABI_LINK_FLAGS ${f})
169 endforeach()
170endmacro()
171
172# If 'condition' is true then add the specified list of flags to
173# 'LIBCXXABI_LINK_FLAGS'
174macro(add_link_flags_if condition)
175 if (${condition})
176 add_link_flags(${ARGN})
177 endif()
178endmacro()
179
180# For each specified flag, add that flag to 'LIBCXXABI_LINK_FLAGS' if the
181# flag is supported by the C++ compiler.
182macro(add_link_flags_if_supported)
183 foreach(flag ${ARGN})
184 mangle_name("${flag}" flagname)
185 check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
186 add_link_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${flag})
187 endforeach()
188endmacro()
189
190# Add a list of libraries or link flags to 'LIBCXXABI_LIBRARIES'.
191macro(add_library_flags)
192 foreach(lib ${ARGN})
193 list(APPEND LIBCXXABI_LIBRARIES ${lib})
194 endforeach()
195endmacro()
196
197# if 'condition' is true then add the specified list of libraries and flags
198# to 'LIBCXXABI_LIBRARIES'.
199macro(add_library_flags_if condition)
200 if(${condition})
201 add_library_flags(${ARGN})
202 endif()
203endmacro()
204
205# Turn a comma separated CMake list into a space separated string.
206macro(split_list listname)
207 string(REPLACE ";" " " ${listname} "${${listname}}")
208endmacro()