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