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