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