xref: /aosp_15_r20/external/libcxx/cmake/Modules/HandleLibcxxFlags.cmake (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker# HandleLibcxxFlags - A set of macros used to setup the flags used to compile
2*58b9f456SAndroid Build Coastguard Worker# and link libc++. These macros add flags to the following CMake variables.
3*58b9f456SAndroid Build Coastguard Worker# - LIBCXX_COMPILE_FLAGS: flags used to compile libc++
4*58b9f456SAndroid Build Coastguard Worker# - LIBCXX_LINK_FLAGS: flags used to link libc++
5*58b9f456SAndroid Build Coastguard Worker# - LIBCXX_LIBRARIES: libraries to link libc++ to.
6*58b9f456SAndroid Build Coastguard Worker
7*58b9f456SAndroid Build Coastguard Workerinclude(CheckCXXCompilerFlag)
8*58b9f456SAndroid Build Coastguard Worker
9*58b9f456SAndroid Build Coastguard Workerunset(add_flag_if_supported)
10*58b9f456SAndroid Build Coastguard Worker
11*58b9f456SAndroid Build Coastguard Worker# Mangle the name of a compiler flag into a valid CMake identifier.
12*58b9f456SAndroid Build Coastguard Worker# Ex: --std=c++11 -> STD_EQ_CXX11
13*58b9f456SAndroid Build Coastguard Workermacro(mangle_name str output)
14*58b9f456SAndroid Build Coastguard Worker  string(STRIP "${str}" strippedStr)
15*58b9f456SAndroid Build Coastguard Worker  string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
16*58b9f456SAndroid Build Coastguard Worker  string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")
17*58b9f456SAndroid Build Coastguard Worker  string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")
18*58b9f456SAndroid Build Coastguard Worker  string(REPLACE "-" "_" strippedStr "${strippedStr}")
19*58b9f456SAndroid Build Coastguard Worker  string(REPLACE ":" "_COLON_" strippedStr "${strippedStr}")
20*58b9f456SAndroid Build Coastguard Worker  string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
21*58b9f456SAndroid Build Coastguard Worker  string(REPLACE "+" "X" strippedStr "${strippedStr}")
22*58b9f456SAndroid Build Coastguard Worker  string(TOUPPER "${strippedStr}" ${output})
23*58b9f456SAndroid Build Coastguard Workerendmacro()
24*58b9f456SAndroid Build Coastguard Worker
25*58b9f456SAndroid Build Coastguard Worker# Remove a list of flags from all CMake variables that affect compile flags.
26*58b9f456SAndroid Build Coastguard Worker# This can be used to remove unwanted flags specified on the command line
27*58b9f456SAndroid Build Coastguard Worker# or added in other parts of LLVM's cmake configuration.
28*58b9f456SAndroid Build Coastguard Workermacro(remove_flags)
29*58b9f456SAndroid Build Coastguard Worker  foreach(var ${ARGN})
30*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
31*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
32*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
33*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
34*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
35*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
36*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
37*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
38*58b9f456SAndroid Build Coastguard Worker    string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}")
39*58b9f456SAndroid Build Coastguard Worker    remove_definitions(${var})
40*58b9f456SAndroid Build Coastguard Worker  endforeach()
41*58b9f456SAndroid Build Coastguard Workerendmacro(remove_flags)
42*58b9f456SAndroid Build Coastguard Worker
43*58b9f456SAndroid Build Coastguard Workermacro(check_flag_supported flag)
44*58b9f456SAndroid Build Coastguard Worker    mangle_name("${flag}" flagname)
45*58b9f456SAndroid Build Coastguard Worker    check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
46*58b9f456SAndroid Build Coastguard Workerendmacro()
47*58b9f456SAndroid Build Coastguard Worker
48*58b9f456SAndroid Build Coastguard Workermacro(append_flags DEST)
49*58b9f456SAndroid Build Coastguard Worker  foreach(value ${ARGN})
50*58b9f456SAndroid Build Coastguard Worker    list(APPEND ${DEST} ${value})
51*58b9f456SAndroid Build Coastguard Worker    list(APPEND ${DEST} ${value})
52*58b9f456SAndroid Build Coastguard Worker  endforeach()
53*58b9f456SAndroid Build Coastguard Workerendmacro()
54*58b9f456SAndroid Build Coastguard Worker
55*58b9f456SAndroid Build Coastguard Worker# If the specified 'condition' is true then append the specified list of flags to DEST
56*58b9f456SAndroid Build Coastguard Workermacro(append_flags_if condition DEST)
57*58b9f456SAndroid Build Coastguard Worker  if (${condition})
58*58b9f456SAndroid Build Coastguard Worker    list(APPEND ${DEST} ${ARGN})
59*58b9f456SAndroid Build Coastguard Worker  endif()
60*58b9f456SAndroid Build Coastguard Workerendmacro()
61*58b9f456SAndroid Build Coastguard Worker
62*58b9f456SAndroid Build Coastguard Worker# Add each flag in the list specified by DEST if that flag is supported by the current compiler.
63*58b9f456SAndroid Build Coastguard Workermacro(append_flags_if_supported DEST)
64*58b9f456SAndroid Build Coastguard Worker  foreach(flag ${ARGN})
65*58b9f456SAndroid Build Coastguard Worker    mangle_name("${flag}" flagname)
66*58b9f456SAndroid Build Coastguard Worker    check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
67*58b9f456SAndroid Build Coastguard Worker    append_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${DEST} ${flag})
68*58b9f456SAndroid Build Coastguard Worker  endforeach()
69*58b9f456SAndroid Build Coastguard Workerendmacro()
70*58b9f456SAndroid Build Coastguard Worker
71*58b9f456SAndroid Build Coastguard Worker# Add a macro definition if condition is true.
72*58b9f456SAndroid Build Coastguard Workermacro(define_if condition def)
73*58b9f456SAndroid Build Coastguard Worker  if (${condition})
74*58b9f456SAndroid Build Coastguard Worker    add_definitions(${def})
75*58b9f456SAndroid Build Coastguard Worker  endif()
76*58b9f456SAndroid Build Coastguard Workerendmacro()
77*58b9f456SAndroid Build Coastguard Worker
78*58b9f456SAndroid Build Coastguard Worker# Add a macro definition if condition is not true.
79*58b9f456SAndroid Build Coastguard Workermacro(define_if_not condition def)
80*58b9f456SAndroid Build Coastguard Worker  if (NOT ${condition})
81*58b9f456SAndroid Build Coastguard Worker    add_definitions(${def})
82*58b9f456SAndroid Build Coastguard Worker  endif()
83*58b9f456SAndroid Build Coastguard Workerendmacro()
84*58b9f456SAndroid Build Coastguard Worker
85*58b9f456SAndroid Build Coastguard Worker# Add a macro definition to the __config_site file if the specified condition
86*58b9f456SAndroid Build Coastguard Worker# is 'true'. Note that '-D${def}' is not added. Instead it is expected that
87*58b9f456SAndroid Build Coastguard Worker# the build include the '__config_site' header.
88*58b9f456SAndroid Build Coastguard Workermacro(config_define_if condition def)
89*58b9f456SAndroid Build Coastguard Worker  if (${condition})
90*58b9f456SAndroid Build Coastguard Worker    set(${def} ON)
91*58b9f456SAndroid Build Coastguard Worker    set(LIBCXX_NEEDS_SITE_CONFIG ON)
92*58b9f456SAndroid Build Coastguard Worker  endif()
93*58b9f456SAndroid Build Coastguard Workerendmacro()
94*58b9f456SAndroid Build Coastguard Worker
95*58b9f456SAndroid Build Coastguard Workermacro(config_define_if_not condition def)
96*58b9f456SAndroid Build Coastguard Worker  if (NOT ${condition})
97*58b9f456SAndroid Build Coastguard Worker    set(${def} ON)
98*58b9f456SAndroid Build Coastguard Worker    set(LIBCXX_NEEDS_SITE_CONFIG ON)
99*58b9f456SAndroid Build Coastguard Worker  endif()
100*58b9f456SAndroid Build Coastguard Workerendmacro()
101*58b9f456SAndroid Build Coastguard Worker
102*58b9f456SAndroid Build Coastguard Workermacro(config_define value def)
103*58b9f456SAndroid Build Coastguard Worker  set(${def} ${value})
104*58b9f456SAndroid Build Coastguard Worker  set(LIBCXX_NEEDS_SITE_CONFIG ON)
105*58b9f456SAndroid Build Coastguard Workerendmacro()
106*58b9f456SAndroid Build Coastguard Worker
107*58b9f456SAndroid Build Coastguard Worker# Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS',
108*58b9f456SAndroid Build Coastguard Worker# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
109*58b9f456SAndroid Build Coastguard Workermacro(add_target_flags)
110*58b9f456SAndroid Build Coastguard Worker  foreach(value ${ARGN})
111*58b9f456SAndroid Build Coastguard Worker    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
112*58b9f456SAndroid Build Coastguard Worker    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
113*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_COMPILE_FLAGS ${value})
114*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_LINK_FLAGS ${value})
115*58b9f456SAndroid Build Coastguard Worker  endforeach()
116*58b9f456SAndroid Build Coastguard Workerendmacro()
117*58b9f456SAndroid Build Coastguard Worker
118*58b9f456SAndroid Build Coastguard Worker# If the specified 'condition' is true then add a list of flags to
119*58b9f456SAndroid Build Coastguard Worker# all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXX_COMPILE_FLAGS'
120*58b9f456SAndroid Build Coastguard Worker# and 'LIBCXX_LINK_FLAGS'.
121*58b9f456SAndroid Build Coastguard Workermacro(add_target_flags_if condition)
122*58b9f456SAndroid Build Coastguard Worker  if (${condition})
123*58b9f456SAndroid Build Coastguard Worker    add_target_flags(${ARGN})
124*58b9f456SAndroid Build Coastguard Worker  endif()
125*58b9f456SAndroid Build Coastguard Workerendmacro()
126*58b9f456SAndroid Build Coastguard Worker
127*58b9f456SAndroid Build Coastguard Worker# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
128*58b9f456SAndroid Build Coastguard Worker# 'LIBCXX_LINK_FLAGS'.
129*58b9f456SAndroid Build Coastguard Workermacro(add_flags)
130*58b9f456SAndroid Build Coastguard Worker  foreach(value ${ARGN})
131*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_COMPILE_FLAGS ${value})
132*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_LINK_FLAGS ${value})
133*58b9f456SAndroid Build Coastguard Worker  endforeach()
134*58b9f456SAndroid Build Coastguard Workerendmacro()
135*58b9f456SAndroid Build Coastguard Worker
136*58b9f456SAndroid Build Coastguard Worker# If the specified 'condition' is true then add a list of flags to both
137*58b9f456SAndroid Build Coastguard Worker# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
138*58b9f456SAndroid Build Coastguard Workermacro(add_flags_if condition)
139*58b9f456SAndroid Build Coastguard Worker  if (${condition})
140*58b9f456SAndroid Build Coastguard Worker    add_flags(${ARGN})
141*58b9f456SAndroid Build Coastguard Worker  endif()
142*58b9f456SAndroid Build Coastguard Workerendmacro()
143*58b9f456SAndroid Build Coastguard Worker
144*58b9f456SAndroid Build Coastguard Worker# Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS
145*58b9f456SAndroid Build Coastguard Worker# if that flag is supported by the current compiler.
146*58b9f456SAndroid Build Coastguard Workermacro(add_flags_if_supported)
147*58b9f456SAndroid Build Coastguard Worker  foreach(flag ${ARGN})
148*58b9f456SAndroid Build Coastguard Worker      mangle_name("${flag}" flagname)
149*58b9f456SAndroid Build Coastguard Worker      check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
150*58b9f456SAndroid Build Coastguard Worker      add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
151*58b9f456SAndroid Build Coastguard Worker  endforeach()
152*58b9f456SAndroid Build Coastguard Workerendmacro()
153*58b9f456SAndroid Build Coastguard Worker
154*58b9f456SAndroid Build Coastguard Worker# Add a list of flags to 'LIBCXX_COMPILE_FLAGS'.
155*58b9f456SAndroid Build Coastguard Workermacro(add_compile_flags)
156*58b9f456SAndroid Build Coastguard Worker  foreach(f ${ARGN})
157*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_COMPILE_FLAGS ${f})
158*58b9f456SAndroid Build Coastguard Worker  endforeach()
159*58b9f456SAndroid Build Coastguard Workerendmacro()
160*58b9f456SAndroid Build Coastguard Worker
161*58b9f456SAndroid Build Coastguard Worker# If 'condition' is true then add the specified list of flags to
162*58b9f456SAndroid Build Coastguard Worker# 'LIBCXX_COMPILE_FLAGS'
163*58b9f456SAndroid Build Coastguard Workermacro(add_compile_flags_if condition)
164*58b9f456SAndroid Build Coastguard Worker  if (${condition})
165*58b9f456SAndroid Build Coastguard Worker    add_compile_flags(${ARGN})
166*58b9f456SAndroid Build Coastguard Worker  endif()
167*58b9f456SAndroid Build Coastguard Workerendmacro()
168*58b9f456SAndroid Build Coastguard Worker
169*58b9f456SAndroid Build Coastguard Worker# For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the
170*58b9f456SAndroid Build Coastguard Worker# flag is supported by the C++ compiler.
171*58b9f456SAndroid Build Coastguard Workermacro(add_compile_flags_if_supported)
172*58b9f456SAndroid Build Coastguard Worker  foreach(flag ${ARGN})
173*58b9f456SAndroid Build Coastguard Worker      mangle_name("${flag}" flagname)
174*58b9f456SAndroid Build Coastguard Worker      check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
175*58b9f456SAndroid Build Coastguard Worker      add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
176*58b9f456SAndroid Build Coastguard Worker  endforeach()
177*58b9f456SAndroid Build Coastguard Workerendmacro()
178*58b9f456SAndroid Build Coastguard Worker
179*58b9f456SAndroid Build Coastguard Worker# Add a list of flags to 'LIBCXX_LINK_FLAGS'.
180*58b9f456SAndroid Build Coastguard Workermacro(add_link_flags)
181*58b9f456SAndroid Build Coastguard Worker  foreach(f ${ARGN})
182*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_LINK_FLAGS ${f})
183*58b9f456SAndroid Build Coastguard Worker  endforeach()
184*58b9f456SAndroid Build Coastguard Workerendmacro()
185*58b9f456SAndroid Build Coastguard Worker
186*58b9f456SAndroid Build Coastguard Worker# If 'condition' is true then add the specified list of flags to
187*58b9f456SAndroid Build Coastguard Worker# 'LIBCXX_LINK_FLAGS'
188*58b9f456SAndroid Build Coastguard Workermacro(add_link_flags_if condition)
189*58b9f456SAndroid Build Coastguard Worker  if (${condition})
190*58b9f456SAndroid Build Coastguard Worker    add_link_flags(${ARGN})
191*58b9f456SAndroid Build Coastguard Worker  endif()
192*58b9f456SAndroid Build Coastguard Workerendmacro()
193*58b9f456SAndroid Build Coastguard Worker
194*58b9f456SAndroid Build Coastguard Worker# For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the
195*58b9f456SAndroid Build Coastguard Worker# flag is supported by the C++ compiler.
196*58b9f456SAndroid Build Coastguard Workermacro(add_link_flags_if_supported)
197*58b9f456SAndroid Build Coastguard Worker  foreach(flag ${ARGN})
198*58b9f456SAndroid Build Coastguard Worker    mangle_name("${flag}" flagname)
199*58b9f456SAndroid Build Coastguard Worker    check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
200*58b9f456SAndroid Build Coastguard Worker    add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
201*58b9f456SAndroid Build Coastguard Worker  endforeach()
202*58b9f456SAndroid Build Coastguard Workerendmacro()
203*58b9f456SAndroid Build Coastguard Worker
204*58b9f456SAndroid Build Coastguard Worker# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
205*58b9f456SAndroid Build Coastguard Workermacro(add_library_flags)
206*58b9f456SAndroid Build Coastguard Worker  foreach(lib ${ARGN})
207*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_LIBRARIES ${lib})
208*58b9f456SAndroid Build Coastguard Worker  endforeach()
209*58b9f456SAndroid Build Coastguard Workerendmacro()
210*58b9f456SAndroid Build Coastguard Worker
211*58b9f456SAndroid Build Coastguard Worker# if 'condition' is true then add the specified list of libraries and flags
212*58b9f456SAndroid Build Coastguard Worker# to 'LIBCXX_LIBRARIES'.
213*58b9f456SAndroid Build Coastguard Workermacro(add_library_flags_if condition)
214*58b9f456SAndroid Build Coastguard Worker  if(${condition})
215*58b9f456SAndroid Build Coastguard Worker    add_library_flags(${ARGN})
216*58b9f456SAndroid Build Coastguard Worker  endif()
217*58b9f456SAndroid Build Coastguard Workerendmacro()
218*58b9f456SAndroid Build Coastguard Worker
219*58b9f456SAndroid Build Coastguard Worker# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
220*58b9f456SAndroid Build Coastguard Workermacro(add_interface_library)
221*58b9f456SAndroid Build Coastguard Worker  foreach(lib ${ARGN})
222*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_LIBRARIES ${lib})
223*58b9f456SAndroid Build Coastguard Worker    list(APPEND LIBCXX_INTERFACE_LIBRARIES ${lib})
224*58b9f456SAndroid Build Coastguard Worker  endforeach()
225*58b9f456SAndroid Build Coastguard Workerendmacro()
226*58b9f456SAndroid Build Coastguard Worker
227*58b9f456SAndroid Build Coastguard Worker# Turn a comma separated CMake list into a space separated string.
228*58b9f456SAndroid Build Coastguard Workermacro(split_list listname)
229*58b9f456SAndroid Build Coastguard Worker  string(REPLACE ";" " " ${listname} "${${listname}}")
230*58b9f456SAndroid Build Coastguard Workerendmacro()
231