blob: a9e43eaece9893cc46871d222188391bc070e547 [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
38# Add a macro definition if condition is true.
39macro(define_if condition def)
40 if (${condition})
41 add_definitions(${def})
42 endif()
43endmacro()
44
45# Add a macro definition if condition is not true.
46macro(define_if_not condition def)
47 if (NOT ${condition})
48 add_definitions(${def})
49 endif()
50endmacro()
51
Eric Fiselier5cf9a822015-10-13 22:12:02 +000052macro(config_define_if condition def)
53 if (${condition})
54 set(${def} ON)
55 add_definitions(-D${def})
56 set(LIBCXX_NEEDS_SITE_CONFIG ON)
57 endif()
58endmacro()
59
60macro(config_define_if_not condition def)
61 if (NOT ${condition})
62 set(${def} ON)
63 add_definitions(-D${def})
64 set(LIBCXX_NEEDS_SITE_CONFIG ON)
65 endif()
66endmacro()
67
Eric Fiselier156a46d2015-07-30 22:30:34 +000068# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
69# 'LIBCXX_LINK_FLAGS'.
70macro(add_flags)
71 foreach(value ${ARGN})
72 list(APPEND LIBCXX_COMPILE_FLAGS ${value})
73 list(APPEND LIBCXX_LINK_FLAGS ${value})
74 endforeach()
75endmacro()
76
77# If the specified 'condition' is true then add a list of flags to both
78# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
79macro(add_flags_if condition)
80 if (${condition})
81 add_flags(${ARGN})
82 endif()
83endmacro()
84
85# Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS
86# if that flag is supported by the current compiler.
87macro(add_flags_if_supported)
88 foreach(flag ${ARGN})
89 mangle_name("${flag}" flagname)
90 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
91 add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
92 endforeach()
93endmacro()
94
95# Add a list of flags to 'LIBCXX_COMPILE_FLAGS'.
96macro(add_compile_flags)
97 foreach(f ${ARGN})
98 list(APPEND LIBCXX_COMPILE_FLAGS ${f})
99 endforeach()
100endmacro()
101
102# If 'condition' is true then add the specified list of flags to
103# 'LIBCXX_COMPILE_FLAGS'
104macro(add_compile_flags_if condition)
105 if (${condition})
106 add_compile_flags(${ARGN})
107 endif()
108endmacro()
109
110# For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the
111# flag is supported by the C++ compiler.
112macro(add_compile_flags_if_supported)
113 foreach(flag ${ARGN})
114 mangle_name("${flag}" flagname)
Eric Fiselier6cc285b2015-07-31 21:09:38 +0000115 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
Eric Fiselier156a46d2015-07-30 22:30:34 +0000116 add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
117 endforeach()
118endmacro()
119
120# Add a list of flags to 'LIBCXX_LINK_FLAGS'.
121macro(add_link_flags)
122 foreach(f ${ARGN})
123 list(APPEND LIBCXX_LINK_FLAGS ${f})
124 endforeach()
125endmacro()
126
127# If 'condition' is true then add the specified list of flags to
128# 'LIBCXX_LINK_FLAGS'
129macro(add_link_flags_if condition)
130 if (${condition})
131 add_link_flags(${ARGN})
132 endif()
133endmacro()
134
135# For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the
136# flag is supported by the C++ compiler.
137macro(add_link_flags_if_supported)
138 foreach(flag ${ARGN})
139 mangle_name("${flag}" flagname)
140 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
141 add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
142 endforeach()
143endmacro()
144
145# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
146macro(add_library_flags)
147 foreach(lib ${ARGN})
148 list(APPEND LIBCXX_LIBRARIES ${lib})
149 endforeach()
150endmacro()
151
152# if 'condition' is true then add the specified list of libraries and flags
153# to 'LIBCXX_LIBRARIES'.
154macro(add_library_flags_if condition)
155 if(${condition})
156 add_library_flags(${ARGN})
157 endif()
158endmacro()
159
160# Turn a comma separated CMake list into a space separated string.
161macro(split_list listname)
162 string(REPLACE ";" " " ${listname} "${${listname}}")
163endmacro()