1 # This CMake module is responsible for interpreting the user defined LLVM_
2 # options and executing the appropriate CMake commands to realize the users'
3 # selections.
5 include(HandleLLVMStdlib)
6 include(AddLLVMDefinitions)
7 include(CheckCCompilerFlag)
8 include(CheckCXXCompilerFlag)
10 if(NOT LLVM_FORCE_USE_OLD_TOOLCHAIN)
11 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
12 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
13 message(FATAL_ERROR "Host GCC version must be at least 4.7!")
14 endif()
15 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
16 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.1)
17 message(FATAL_ERROR "Host Clang version must be at least 3.1!")
18 endif()
20 # Also test that we aren't using too old of a version of libstdc++ with the
21 # Clang compiler. This is tricky as there is no real way to check the
22 # version of libstdc++ directly. Instead we test for a known bug in
23 # libstdc++4.6 that is fixed in libstdc++4.7.
24 if(NOT LLVM_ENABLE_LIBCXX)
25 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
26 set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
27 set(CMAKE_REQUIRED_FLAGS "-std=c++0x")
28 if (ANDROID)
29 set(CMAKE_REQUIRED_LIBRARIES "atomic")
30 endif()
31 check_cxx_source_compiles("
32 #include <atomic>
33 std::atomic<float> x(0.0f);
34 int main() { return (float)x; }"
35 LLVM_NO_OLD_LIBSTDCXX)
36 if(NOT LLVM_NO_OLD_LIBSTDCXX)
37 message(FATAL_ERROR "Host Clang must be able to find libstdc++4.7 or newer!")
38 endif()
39 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
40 set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
41 endif()
42 elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
43 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17.0)
44 message(FATAL_ERROR "Host Visual Studio must be at least 2012 (MSVC 17.0)")
45 endif()
46 endif()
47 endif()
49 if( LLVM_ENABLE_ASSERTIONS )
50 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
51 if( NOT MSVC )
52 add_definitions( -D_DEBUG )
53 endif()
54 # On non-Debug builds cmake automatically defines NDEBUG, so we
55 # explicitly undefine it:
56 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
57 add_definitions( -UNDEBUG )
58 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
59 set(REGEXP_NDEBUG "(^| )[/-]D *NDEBUG($| )")
60 string (REGEX REPLACE "${REGEXP_NDEBUG}" " "
61 CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
62 string (REGEX REPLACE "${REGEXP_NDEBUG}" " "
63 CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
64 string (REGEX REPLACE "${REGEXP_NDEBUG}" " "
65 CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
66 endif()
67 else()
68 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
69 if( NOT MSVC_IDE AND NOT XCODE )
70 add_definitions( -DNDEBUG )
71 endif()
72 endif()
73 endif()
75 if(WIN32)
76 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
77 if(CYGWIN)
78 set(LLVM_ON_WIN32 0)
79 set(LLVM_ON_UNIX 1)
80 else(CYGWIN)
81 set(LLVM_ON_WIN32 1)
82 set(LLVM_ON_UNIX 0)
83 endif(CYGWIN)
84 # Maximum path length is 160 for non-unicode paths
85 set(MAXPATHLEN 160)
86 else(WIN32)
87 if(UNIX)
88 set(LLVM_ON_WIN32 0)
89 set(LLVM_ON_UNIX 1)
90 if(APPLE)
91 set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
92 else(APPLE)
93 set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
94 endif(APPLE)
95 # FIXME: Maximum path length is currently set to 'safe' fixed value
96 set(MAXPATHLEN 2024)
97 else(UNIX)
98 MESSAGE(SEND_ERROR "Unable to determine platform")
99 endif(UNIX)
100 endif(WIN32)
102 set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX})
103 set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
104 set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_MODULE_SUFFIX})
106 function(add_flag_or_print_warning flag)
107 check_c_compiler_flag(${flag} C_SUPPORTS_FLAG)
108 check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG)
109 if (C_SUPPORTS_FLAG AND CXX_SUPPORTS_FLAG)
110 message(STATUS "Building with ${flag}")
111 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
112 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
113 else()
114 message(WARNING "${flag} is not supported.")
115 endif()
116 endfunction()
118 function(append value)
119 foreach(variable ${ARGN})
120 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
121 endforeach(variable)
122 endfunction()
124 function(append_if condition value)
125 if (${condition})
126 foreach(variable ${ARGN})
127 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
128 endforeach(variable)
129 endif()
130 endfunction()
132 macro(add_flag_if_supported flag)
133 check_c_compiler_flag(${flag} C_SUPPORTS_FLAG)
134 append_if(C_SUPPORTS_FLAG "${flag}" CMAKE_C_FLAGS)
135 check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG)
136 append_if(CXX_SUPPORTS_FLAG "${flag}" CMAKE_CXX_FLAGS)
137 endmacro()
139 if( LLVM_ENABLE_PIC )
140 if( XCODE )
141 # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
142 # know how to disable this, so just force ENABLE_PIC off for now.
143 message(WARNING "-fPIC not supported with Xcode.")
144 elseif( WIN32 OR CYGWIN)
145 # On Windows all code is PIC. MinGW warns if -fPIC is used.
146 else()
147 add_flag_or_print_warning("-fPIC")
149 if( WIN32 OR CYGWIN)
150 # MinGW warns if -fvisibility-inlines-hidden is used.
151 else()
152 check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
153 append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS)
154 endif()
155 endif()
156 endif()
158 if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
159 # TODO: support other platforms and toolchains.
160 if( LLVM_BUILD_32_BITS )
161 message(STATUS "Building 32 bits executables and libraries.")
162 add_llvm_definitions( -m32 )
163 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
164 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
165 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
166 endif( LLVM_BUILD_32_BITS )
167 endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
169 # On Win32 using MS tools, provide an option to set the number of parallel jobs
170 # to use.
171 if( MSVC_IDE )
172 set(LLVM_COMPILER_JOBS "0" CACHE STRING
173 "Number of parallel compiler jobs. 0 means use all processors. Default is 0.")
174 if( NOT LLVM_COMPILER_JOBS STREQUAL "1" )
175 if( LLVM_COMPILER_JOBS STREQUAL "0" )
176 add_llvm_definitions( /MP )
177 else()
178 if (MSVC10)
179 message(FATAL_ERROR
180 "Due to a bug in CMake only 0 and 1 is supported for "
181 "LLVM_COMPILER_JOBS when generating for Visual Studio 2010")
182 else()
183 message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS})
184 add_llvm_definitions( /MP${LLVM_COMPILER_JOBS} )
185 endif()
186 endif()
187 else()
188 message(STATUS "Parallel compilation disabled")
189 endif()
190 endif()
192 if( MSVC )
193 include(ChooseMSVCCRT)
195 if( NOT (${CMAKE_VERSION} VERSION_LESS 2.8.11) )
196 # set stack reserved size to ~10MB
197 # CMake previously automatically set this value for MSVC builds, but the
198 # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default
199 # value (1 MB) which is not enough for us in tasks such as parsing recursive
200 # C++ templates in Clang.
201 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000")
202 endif()
204 if( MSVC10 )
205 # MSVC 10 will complain about headers in the STL not being exported, but
206 # will not complain in MSVC 11.
207 add_llvm_definitions(
208 -wd4275 # Suppress 'An exported class was derived from a class that was not exported.'
209 )
210 elseif( MSVC11 )
211 add_llvm_definitions(-D_VARIADIC_MAX=10)
212 endif()
214 # Add definitions that make MSVC much less annoying.
215 add_llvm_definitions(
216 # For some reason MS wants to deprecate a bunch of standard functions...
217 -D_CRT_SECURE_NO_DEPRECATE
218 -D_CRT_SECURE_NO_WARNINGS
219 -D_CRT_NONSTDC_NO_DEPRECATE
220 -D_CRT_NONSTDC_NO_WARNINGS
221 -D_SCL_SECURE_NO_DEPRECATE
222 -D_SCL_SECURE_NO_WARNINGS
224 # Disabled warnings.
225 -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
226 -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
227 -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
228 -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
229 -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
230 -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
231 -wd4355 # Suppress ''this' : used in base member initializer list'
232 -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
233 -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
234 -wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)'
235 -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
237 # Promoted warnings.
238 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
240 # Promoted warnings to errors.
241 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
242 )
244 # Enable warnings
245 if (LLVM_ENABLE_WARNINGS)
246 add_llvm_definitions( /W4 )
247 if (LLVM_ENABLE_PEDANTIC)
248 # No MSVC equivalent available
249 endif (LLVM_ENABLE_PEDANTIC)
250 endif (LLVM_ENABLE_WARNINGS)
251 if (LLVM_ENABLE_WERROR)
252 add_llvm_definitions( /WX )
253 endif (LLVM_ENABLE_WERROR)
254 elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
255 if (LLVM_ENABLE_WARNINGS)
256 append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
258 # Turn off missing field initializer warnings for gcc to avoid noise from
259 # false positives with empty {}. Turn them on otherwise (they're off by
260 # default for clang).
261 check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
262 if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
263 if (CMAKE_COMPILER_IS_GNUCXX)
264 append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
265 else()
266 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
267 endif()
268 endif()
270 append_if(LLVM_ENABLE_PEDANTIC "-pedantic -Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
271 check_cxx_compiler_flag("-Werror -Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
272 append_if(CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_CXX_FLAGS)
273 check_c_compiler_flag("-Werror -Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
274 append_if(C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_C_FLAGS)
275 append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
276 append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
277 check_cxx_compiler_flag("-Werror -Wnon-virtual-dtor" CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG)
278 append_if(CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
279 endif (LLVM_ENABLE_WARNINGS)
280 if (LLVM_ENABLE_WERROR)
281 add_llvm_definitions( -Werror )
282 endif (LLVM_ENABLE_WERROR)
283 if (LLVM_ENABLE_CXX11)
284 check_cxx_compiler_flag("-std=c++11" CXX_SUPPORTS_CXX11)
285 append_if(CXX_SUPPORTS_CXX11 "-std=c++11" CMAKE_CXX_FLAGS)
286 endif (LLVM_ENABLE_CXX11)
287 endif( MSVC )
289 macro(append_common_sanitizer_flags)
290 # Append -fno-omit-frame-pointer and turn on debug info to get better
291 # stack traces.
292 add_flag_if_supported("-fno-omit-frame-pointer")
293 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
294 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
295 add_flag_if_supported("-gline-tables-only")
296 endif()
297 # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
298 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
299 add_flag_if_supported("-O1")
300 endif()
301 endmacro()
303 # Turn on sanitizers if necessary.
304 if(LLVM_USE_SANITIZER)
305 if (LLVM_ON_UNIX)
306 if (LLVM_USE_SANITIZER STREQUAL "Address")
307 append_common_sanitizer_flags()
308 add_flag_or_print_warning("-fsanitize=address")
309 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
310 append_common_sanitizer_flags()
311 add_flag_or_print_warning("-fsanitize=memory")
312 if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
313 add_flag_or_print_warning("-fsanitize-memory-track-origins")
314 endif()
315 else()
316 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
317 endif()
318 else()
319 message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
320 endif()
321 endif()
323 # Turn on -gsplit-dwarf if requested
324 if(LLVM_USE_SPLIT_DWARF)
325 add_llvm_definitions("-gsplit-dwarf")
326 endif()
328 add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
329 add_llvm_definitions( -D__STDC_FORMAT_MACROS )
330 add_llvm_definitions( -D__STDC_LIMIT_MACROS )
332 # clang doesn't print colored diagnostics when invoked from Ninja
333 if (UNIX AND
334 CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
335 CMAKE_GENERATOR STREQUAL "Ninja")
336 append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
337 endif()
339 # Add flags for add_dead_strip().
340 # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
341 # But MinSizeRel seems to add that automatically, so maybe disable these
342 # flags instead if LLVM_NO_DEAD_STRIP is set.
343 if(NOT CYGWIN AND NOT WIN32)
344 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
345 check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS)
346 if (C_SUPPORTS_FNO_FUNCTION_SECTIONS)
347 # Don't add -ffunction-section if it can be disabled with -fno-function-sections.
348 # Doing so will break sanitizers.
349 check_c_compiler_flag("-Werror -ffunction-sections" C_SUPPORTS_FFUNCTION_SECTIONS)
350 check_cxx_compiler_flag("-Werror -ffunction-sections" CXX_SUPPORTS_FFUNCTION_SECTIONS)
351 append_if(C_SUPPORTS_FFUNCTION_SECTIONS "-ffunction-sections" CMAKE_C_FLAGS)
352 append_if(CXX_SUPPORTS_FFUNCTION_SECTIONS "-ffunction-sections" CMAKE_CXX_FLAGS)
353 endif()
354 check_c_compiler_flag("-Werror -fdata-sections" C_SUPPORTS_FDATA_SECTIONS)
355 check_cxx_compiler_flag("-Werror -fdata-sections" CXX_SUPPORTS_FDATA_SECTIONS)
356 append_if(C_SUPPORTS_FDATA_SECTIONS "-fdata-sections" CMAKE_C_FLAGS)
357 append_if(CXX_SUPPORTS_FDATA_SECTIONS "-fdata-sections" CMAKE_CXX_FLAGS)
358 endif()
359 endif()
361 if(MSVC)
362 # Remove flags here, for exceptions and RTTI.
363 # Each target property or source property should be responsible to control
364 # them.
365 # CL.EXE complains to override flags like "/GR /GR-".
366 string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
367 string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
368 endif()