blob: 2cec270e96ddb63a2fdc3eb20aad463c74e5307c [file] [log] [blame]
Daniel Dunbarc8f399d2011-11-04 19:04:35 +00001# 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
Oscar Fuentesd538e242011-02-03 20:57:36 +00005include(AddLLVMDefinitions)
Joe Abbeyd6a93072012-11-26 02:02:08 +00006include(CheckCCompilerFlag)
Jordan Rosee2abe052013-03-02 01:00:40 +00007include(CheckCXXCompilerFlag)
Oscar Fuentesd538e242011-02-03 20:57:36 +00008
Oscar Fuentes104e9922011-05-11 13:53:08 +00009if( CMAKE_COMPILER_IS_GNUCXX )
10 set(LLVM_COMPILER_IS_GCC_COMPATIBLE ON)
Hans Wennborg29150e42013-10-07 22:03:23 +000011elseif( MSVC )
12 set(LLVM_COMPILER_IS_GCC_COMPATIBLE OFF)
Oscar Fuentes104e9922011-05-11 13:53:08 +000013elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )
14 set(LLVM_COMPILER_IS_GCC_COMPATIBLE ON)
15endif()
16
Oscar Fuentesd538e242011-02-03 20:57:36 +000017if( 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()
Duncan Sandsa009d532013-07-17 09:34:51 +000022 # On non-Debug builds cmake automatically defines NDEBUG, so we
Oscar Fuentesd538e242011-02-03 20:57:36 +000023 # explicitly undefine it:
Duncan Sandsa009d532013-07-17 09:34:51 +000024 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
Oscar Fuentesd538e242011-02-03 20:57:36 +000025 add_definitions( -UNDEBUG )
Reid Kleckner16f19692013-04-07 01:45:01 +000026 # 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}")
Oscar Fuentesd538e242011-02-03 20:57:36 +000029 endif()
30else()
31 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
Oscar Fuentesdd70cd82011-02-06 19:07:06 +000032 if( NOT MSVC_IDE AND NOT XCODE )
33 add_definitions( -DNDEBUG )
34 endif()
Oscar Fuentesd538e242011-02-03 20:57:36 +000035 endif()
36endif()
37
38if(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)
Oscar Fuentesd538e242011-02-03 20:57:36 +000045 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)
50else(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)
65endif(WIN32)
66
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +000067function(add_flag_or_print_warning flag)
Alexey Samsonov777fccb2013-03-26 07:49:46 +000068 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)
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +000071 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()
77endfunction()
78
Duncan Sands2269c562013-03-25 13:25:34 +000079function(append value)
80 foreach(variable ${ARGN})
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +000081 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
Duncan Sands2269c562013-03-25 13:25:34 +000082 endforeach(variable)
83endfunction()
84
85function(append_if condition value)
86 if (${condition})
87 foreach(variable ${ARGN})
88 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
89 endforeach(variable)
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +000090 endif()
91endfunction()
92
Alexey Samsonov777fccb2013-03-26 07:49:46 +000093macro(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)
98endmacro()
99
Oscar Fuentesd538e242011-02-03 20:57:36 +0000100if( 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.")
NAKAMURA Takumide0cfe82011-12-16 06:21:08 +0000105 elseif( WIN32 OR CYGWIN)
Oscar Fuentesd538e242011-02-03 20:57:36 +0000106 # On Windows all code is PIC. MinGW warns if -fPIC is used.
107 else()
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +0000108 add_flag_or_print_warning("-fPIC")
Rafael Espindolab2888502012-01-20 04:07:48 +0000109
Rafael Espindolaace4f2b2012-01-20 13:10:10 +0000110 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)
Duncan Sands2269c562013-03-25 13:25:34 +0000114 append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS)
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +0000115 endif()
Oscar Fuentesd538e242011-02-03 20:57:36 +0000116 endif()
117endif()
118
119if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
120 # TODO: support other platforms and toolchains.
Oscar Fuentesd538e242011-02-03 20:57:36 +0000121 if( LLVM_BUILD_32_BITS )
122 message(STATUS "Building 32 bits executables and libraries.")
123 add_llvm_definitions( -m32 )
Tim Northover4c8657a2012-05-23 18:42:02 +0000124 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
125 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
Tobias Grosser2563ad22012-06-08 09:41:23 +0000126 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
Oscar Fuentesd538e242011-02-03 20:57:36 +0000127 endif( LLVM_BUILD_32_BITS )
128endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
129
NAKAMURA Takumi5bfe3bf2012-04-21 14:50:56 +0000130# On Win32 using MS tools, provide an option to set the number of parallel jobs
131# to use.
NAKAMURA Takumi4a80d642012-04-21 14:51:02 +0000132if( MSVC_IDE )
Oscar Fuentes0dddbc32011-03-02 17:47:37 +0000133 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()
151endif()
152
Oscar Fuentesd538e242011-02-03 20:57:36 +0000153if( MSVC )
154 include(ChooseMSVCCRT)
155
Aaron Ballmancc6933b2013-07-29 13:02:08 +0000156 if( MSVC10 )
157 # MSVC 10 will complain about headers in the STL not being exported, but
158 # will not complain in MSVC 11.
159 add_llvm_definitions(
160 -wd4275 # Suppress 'An exported class was derived from a class that was not exported.'
161 )
162 elseif( MSVC11 )
Michael J. Spencer647c0ce2012-03-01 22:42:52 +0000163 add_llvm_definitions(-D_VARIADIC_MAX=10)
164 endif()
Aaron Ballmancc6933b2013-07-29 13:02:08 +0000165
Oscar Fuentesd538e242011-02-03 20:57:36 +0000166 # Add definitions that make MSVC much less annoying.
167 add_llvm_definitions(
168 # For some reason MS wants to deprecate a bunch of standard functions...
169 -D_CRT_SECURE_NO_DEPRECATE
170 -D_CRT_SECURE_NO_WARNINGS
171 -D_CRT_NONSTDC_NO_DEPRECATE
172 -D_CRT_NONSTDC_NO_WARNINGS
173 -D_SCL_SECURE_NO_DEPRECATE
174 -D_SCL_SECURE_NO_WARNINGS
175
Michael J. Spencerc3ab9ce2012-06-07 21:34:15 +0000176 # Disabled warnings.
Oscar Fuentesd538e242011-02-03 20:57:36 +0000177 -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
178 -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
Oscar Fuentesd538e242011-02-03 20:57:36 +0000179 -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
180 -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
Oscar Fuentesd538e242011-02-03 20:57:36 +0000181 -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
182 -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
183 -wd4355 # Suppress ''this' : used in base member initializer list'
184 -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
185 -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
Oscar Fuentesd538e242011-02-03 20:57:36 +0000186 -wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)'
Aaron Ballman7f92c7b2013-08-16 03:06:38 +0000187 -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
Aaron Ballman7f817022013-07-28 18:04:26 +0000188
Michael J. Spencerc3ab9ce2012-06-07 21:34:15 +0000189 # Promoted warnings.
190 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
Michael J. Spencer67762d12012-06-07 23:33:56 +0000191
192 # Promoted warnings to errors.
193 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
Oscar Fuentesd538e242011-02-03 20:57:36 +0000194 )
195
196 # Enable warnings
197 if (LLVM_ENABLE_WARNINGS)
Michael J. Spencerc3ab9ce2012-06-07 21:34:15 +0000198 add_llvm_definitions( /W4 )
Oscar Fuentesd538e242011-02-03 20:57:36 +0000199 if (LLVM_ENABLE_PEDANTIC)
200 # No MSVC equivalent available
201 endif (LLVM_ENABLE_PEDANTIC)
202 endif (LLVM_ENABLE_WARNINGS)
203 if (LLVM_ENABLE_WERROR)
204 add_llvm_definitions( /WX )
205 endif (LLVM_ENABLE_WERROR)
Oscar Fuentes104e9922011-05-11 13:53:08 +0000206elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
Oscar Fuentesd538e242011-02-03 20:57:36 +0000207 if (LLVM_ENABLE_WARNINGS)
Duncan Sands2269c562013-03-25 13:25:34 +0000208 append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Edwin Vaneadc4af62013-01-31 18:05:54 +0000209
210 # Turn off missing field initializer warnings for gcc to avoid noise from
211 # false positives with empty {}. Turn them on otherwise (they're off by
212 # default for clang).
213 check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
214 if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
215 if (CMAKE_COMPILER_IS_GNUCXX)
Duncan Sands2269c562013-03-25 13:25:34 +0000216 append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Edwin Vaneadc4af62013-01-31 18:05:54 +0000217 else()
Duncan Sands2269c562013-03-25 13:25:34 +0000218 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Edwin Vaneadc4af62013-01-31 18:05:54 +0000219 endif()
220 endif()
221
Duncan Sands2269c562013-03-25 13:25:34 +0000222 append_if(LLVM_ENABLE_PEDANTIC "-pedantic -Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Joe Abbeyd6a93072012-11-26 02:02:08 +0000223 check_cxx_compiler_flag("-Werror -Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
Duncan Sands2269c562013-03-25 13:25:34 +0000224 append_if(CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_CXX_FLAGS)
Joe Abbeyd6a93072012-11-26 02:02:08 +0000225 check_c_compiler_flag("-Werror -Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
Duncan Sands2269c562013-03-25 13:25:34 +0000226 append_if(C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_C_FLAGS)
227 append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
228 append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
Alexey Samsonov8483b472013-03-19 10:10:03 +0000229 check_cxx_compiler_flag("-Werror -Wnon-virtual-dtor" CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG)
Duncan Sands2269c562013-03-25 13:25:34 +0000230 append_if(CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
Oscar Fuentesd538e242011-02-03 20:57:36 +0000231 endif (LLVM_ENABLE_WARNINGS)
232 if (LLVM_ENABLE_WERROR)
233 add_llvm_definitions( -Werror )
234 endif (LLVM_ENABLE_WERROR)
235endif( MSVC )
236
Alexey Samsonov777fccb2013-03-26 07:49:46 +0000237macro(append_common_sanitizer_flags)
238 # Append -fno-omit-frame-pointer and turn on debug info to get better
239 # stack traces.
240 add_flag_if_supported("-fno-omit-frame-pointer")
241 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
242 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
243 add_flag_if_supported("-gline-tables-only")
244 endif()
Alexey Samsonovb775f2f2013-09-02 09:15:01 +0000245 # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
246 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
247 add_flag_if_supported("-O1")
248 endif()
Alexey Samsonov777fccb2013-03-26 07:49:46 +0000249endmacro()
250
251# Turn on sanitizers if necessary.
252if(LLVM_USE_SANITIZER)
253 if (LLVM_ON_UNIX)
254 if (LLVM_USE_SANITIZER STREQUAL "Address")
255 append_common_sanitizer_flags()
256 add_flag_or_print_warning("-fsanitize=address")
257 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
258 append_common_sanitizer_flags()
259 add_flag_or_print_warning("-fsanitize=memory")
Alexey Samsonov777fccb2013-03-26 07:49:46 +0000260 if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
261 add_flag_or_print_warning("-fsanitize-memory-track-origins")
262 endif()
263 else()
264 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
265 endif()
266 else()
267 message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
268 endif()
269endif()
270
Eric Christopher6aebd5f2013-07-30 21:44:10 +0000271# Turn on -gsplit-dwarf if requested
272if(LLVM_USE_SPLIT_DWARF)
Eric Christopherb9db9cd2013-07-30 22:34:30 +0000273 add_llvm_definitions("-gsplit-dwarf")
Eric Christopher6aebd5f2013-07-30 21:44:10 +0000274endif()
275
Oscar Fuentesd538e242011-02-03 20:57:36 +0000276add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
NAKAMURA Takumie2aa5d52011-10-11 12:51:44 +0000277add_llvm_definitions( -D__STDC_FORMAT_MACROS )
NAKAMURA Takumi11b8a002011-10-11 12:51:36 +0000278add_llvm_definitions( -D__STDC_LIMIT_MACROS )
Arnaud A. de Grandmaison745825f52013-05-29 20:41:35 +0000279
280# clang doesn't print colored diagnostics when invoked from Ninja
Rafael Espindolad132ce72013-06-14 19:41:05 +0000281if (UNIX AND
282 CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
283 CMAKE_GENERATOR STREQUAL "Ninja")
284 append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Arnaud A. de Grandmaison745825f52013-05-29 20:41:35 +0000285endif()