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