blob: 223b7b7b8a6abb21ec0a48ed0520f59aaf70c86f [file] [log] [blame]
Eric Fiseliereb6e2ea2015-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
52# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
53# 'LIBCXX_LINK_FLAGS'.
54macro(add_flags)
55 foreach(value ${ARGN})
56 list(APPEND LIBCXX_COMPILE_FLAGS ${value})
57 list(APPEND LIBCXX_LINK_FLAGS ${value})
58 endforeach()
59endmacro()
60
61# If the specified 'condition' is true then add a list of flags to both
62# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
63macro(add_flags_if condition)
64 if (${condition})
65 add_flags(${ARGN})
66 endif()
67endmacro()
68
69# Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS
70# if that flag is supported by the current compiler.
71macro(add_flags_if_supported)
72 foreach(flag ${ARGN})
73 mangle_name("${flag}" flagname)
74 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
75 add_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
76 endforeach()
77endmacro()
78
79# Add a list of flags to 'LIBCXX_COMPILE_FLAGS'.
80macro(add_compile_flags)
81 foreach(f ${ARGN})
82 list(APPEND LIBCXX_COMPILE_FLAGS ${f})
83 endforeach()
84endmacro()
85
86# If 'condition' is true then add the specified list of flags to
87# 'LIBCXX_COMPILE_FLAGS'
88macro(add_compile_flags_if condition)
89 if (${condition})
90 add_compile_flags(${ARGN})
91 endif()
92endmacro()
93
94# For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the
95# flag is supported by the C++ compiler.
96macro(add_compile_flags_if_supported)
97 foreach(flag ${ARGN})
98 mangle_name("${flag}" flagname)
Eric Fiselierc0e79712015-07-31 21:09:38 +000099 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
Eric Fiseliereb6e2ea2015-07-30 22:30:34 +0000100 add_compile_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
101 endforeach()
102endmacro()
103
104# Add a list of flags to 'LIBCXX_LINK_FLAGS'.
105macro(add_link_flags)
106 foreach(f ${ARGN})
107 list(APPEND LIBCXX_LINK_FLAGS ${f})
108 endforeach()
109endmacro()
110
111# If 'condition' is true then add the specified list of flags to
112# 'LIBCXX_LINK_FLAGS'
113macro(add_link_flags_if condition)
114 if (${condition})
115 add_link_flags(${ARGN})
116 endif()
117endmacro()
118
119# For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the
120# flag is supported by the C++ compiler.
121macro(add_link_flags_if_supported)
122 foreach(flag ${ARGN})
123 mangle_name("${flag}" flagname)
124 check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
125 add_link_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${flag})
126 endforeach()
127endmacro()
128
129# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
130macro(add_library_flags)
131 foreach(lib ${ARGN})
132 list(APPEND LIBCXX_LIBRARIES ${lib})
133 endforeach()
134endmacro()
135
136# if 'condition' is true then add the specified list of libraries and flags
137# to 'LIBCXX_LIBRARIES'.
138macro(add_library_flags_if condition)
139 if(${condition})
140 add_library_flags(${ARGN})
141 endif()
142endmacro()
143
144# Turn a comma separated CMake list into a space separated string.
145macro(split_list listname)
146 string(REPLACE ";" " " ${listname} "${${listname}}")
147endmacro()