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