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