blob: b8ea1344644576c54ce296a002956678b9799275 [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}")
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 "${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()
36endmacro(remove_flags)
37
Eric Fiselier6065f052016-05-10 16:17:43 +000038macro(check_flag_supported flag)
39 mangle_name("${flag}" flagname)
40 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
41endmacro()
42
Eric Fiselier156a46d2015-07-30 22:30:34 +000043# Add a macro definition if condition is true.
44macro(define_if condition def)
45 if (${condition})
46 add_definitions(${def})
47 endif()
48endmacro()
49
50# Add a macro definition if condition is not true.
51macro(define_if_not condition def)
52 if (NOT ${condition})
53 add_definitions(${def})
54 endif()
55endmacro()
56
Eric Fiselier1a1c74b2015-10-14 00:22:05 +000057# Add a macro definition to the __config_site file if the specified condition
58# is 'true'. Note that '-D${def}' is not added. Instead it is expected that
59# the build include the '__config_site' header.
Eric Fiselier5cf9a822015-10-13 22:12:02 +000060macro(config_define_if condition def)
61 if (${condition})
62 set(${def} ON)
Eric Fiselier5cf9a822015-10-13 22:12:02 +000063 set(LIBCXX_NEEDS_SITE_CONFIG ON)
64 endif()
65endmacro()
66
67macro(config_define_if_not condition def)
68 if (NOT ${condition})
69 set(${def} ON)
Eric Fiselier5cf9a822015-10-13 22:12:02 +000070 set(LIBCXX_NEEDS_SITE_CONFIG ON)
71 endif()
72endmacro()
73
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +000074macro(config_define value def)
75 set(${def} ${value})
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +000076 set(LIBCXX_NEEDS_SITE_CONFIG ON)
77endmacro()
78
Eric Fiselier156a46d2015-07-30 22:30:34 +000079# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
80# 'LIBCXX_LINK_FLAGS'.
81macro(add_flags)
82 foreach(value ${ARGN})
83 list(APPEND LIBCXX_COMPILE_FLAGS ${value})
84 list(APPEND LIBCXX_LINK_FLAGS ${value})
85 endforeach()
86endmacro()
87
88# If the specified 'condition' is true then add a list of flags to both
89# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
90macro(add_flags_if condition)
91 if (${condition})
92 add_flags(${ARGN})
93 endif()
94endmacro()
95
96# Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS
97# if that flag is supported by the current compiler.
98macro(add_flags_if_supported)
99 foreach(flag ${ARGN})
100 mangle_name("${flag}" flagname)
101 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
102 add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
103 endforeach()
104endmacro()
105
106# Add a list of flags to 'LIBCXX_COMPILE_FLAGS'.
107macro(add_compile_flags)
108 foreach(f ${ARGN})
109 list(APPEND LIBCXX_COMPILE_FLAGS ${f})
110 endforeach()
111endmacro()
112
113# If 'condition' is true then add the specified list of flags to
114# 'LIBCXX_COMPILE_FLAGS'
115macro(add_compile_flags_if condition)
116 if (${condition})
117 add_compile_flags(${ARGN})
118 endif()
119endmacro()
120
121# For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the
122# flag is supported by the C++ compiler.
123macro(add_compile_flags_if_supported)
124 foreach(flag ${ARGN})
125 mangle_name("${flag}" flagname)
Eric Fiselier6cc285b2015-07-31 21:09:38 +0000126 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
Eric Fiselier156a46d2015-07-30 22:30:34 +0000127 add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
128 endforeach()
129endmacro()
130
131# Add a list of flags to 'LIBCXX_LINK_FLAGS'.
132macro(add_link_flags)
133 foreach(f ${ARGN})
134 list(APPEND LIBCXX_LINK_FLAGS ${f})
135 endforeach()
136endmacro()
137
138# If 'condition' is true then add the specified list of flags to
139# 'LIBCXX_LINK_FLAGS'
140macro(add_link_flags_if condition)
141 if (${condition})
142 add_link_flags(${ARGN})
143 endif()
144endmacro()
145
146# For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the
147# flag is supported by the C++ compiler.
148macro(add_link_flags_if_supported)
149 foreach(flag ${ARGN})
150 mangle_name("${flag}" flagname)
151 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
152 add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
153 endforeach()
154endmacro()
155
156# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
157macro(add_library_flags)
158 foreach(lib ${ARGN})
159 list(APPEND LIBCXX_LIBRARIES ${lib})
160 endforeach()
161endmacro()
162
163# if 'condition' is true then add the specified list of libraries and flags
164# to 'LIBCXX_LIBRARIES'.
165macro(add_library_flags_if condition)
166 if(${condition})
167 add_library_flags(${ARGN})
168 endif()
169endmacro()
170
171# Turn a comma separated CMake list into a space separated string.
172macro(split_list listname)
173 string(REPLACE ";" " " ${listname} "${${listname}}")
174endmacro()