blob: 4d56438f18de9fb4e89662c64095a93d3beca64c [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)
11elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )
12 set(LLVM_COMPILER_IS_GCC_COMPATIBLE ON)
13endif()
14
Oscar Fuentesd538e242011-02-03 20:57:36 +000015if( 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()
Duncan Sandsa009d532013-07-17 09:34:51 +000020 # On non-Debug builds cmake automatically defines NDEBUG, so we
Oscar Fuentesd538e242011-02-03 20:57:36 +000021 # explicitly undefine it:
Duncan Sandsa009d532013-07-17 09:34:51 +000022 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
Oscar Fuentesd538e242011-02-03 20:57:36 +000023 add_definitions( -UNDEBUG )
Reid Kleckner16f19692013-04-07 01:45:01 +000024 # 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}")
Oscar Fuentesd538e242011-02-03 20:57:36 +000027 endif()
28else()
29 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
Oscar Fuentesdd70cd82011-02-06 19:07:06 +000030 if( NOT MSVC_IDE AND NOT XCODE )
31 add_definitions( -DNDEBUG )
32 endif()
Oscar Fuentesd538e242011-02-03 20:57:36 +000033 endif()
34endif()
35
36if(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)
Oscar Fuentesd538e242011-02-03 20:57:36 +000043 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)
48else(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)
63endif(WIN32)
64
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +000065function(add_flag_or_print_warning flag)
Alexey Samsonov777fccb2013-03-26 07:49:46 +000066 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)
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +000069 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()
75endfunction()
76
Duncan Sands2269c562013-03-25 13:25:34 +000077function(append value)
78 foreach(variable ${ARGN})
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +000079 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
Duncan Sands2269c562013-03-25 13:25:34 +000080 endforeach(variable)
81endfunction()
82
83function(append_if condition value)
84 if (${condition})
85 foreach(variable ${ARGN})
86 set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
87 endforeach(variable)
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +000088 endif()
89endfunction()
90
Alexey Samsonov777fccb2013-03-26 07:49:46 +000091macro(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)
96endmacro()
97
Oscar Fuentesd538e242011-02-03 20:57:36 +000098if( 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.")
NAKAMURA Takumide0cfe82011-12-16 06:21:08 +0000103 elseif( WIN32 OR CYGWIN)
Oscar Fuentesd538e242011-02-03 20:57:36 +0000104 # On Windows all code is PIC. MinGW warns if -fPIC is used.
105 else()
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +0000106 add_flag_or_print_warning("-fPIC")
Rafael Espindolab2888502012-01-20 04:07:48 +0000107
Rafael Espindolaace4f2b2012-01-20 13:10:10 +0000108 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)
Duncan Sands2269c562013-03-25 13:25:34 +0000112 append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS)
Alexey Samsonovb91ce4f2013-03-13 20:50:23 +0000113 endif()
Oscar Fuentesd538e242011-02-03 20:57:36 +0000114 endif()
115endif()
116
117if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
118 # TODO: support other platforms and toolchains.
Oscar Fuentesd538e242011-02-03 20:57:36 +0000119 if( LLVM_BUILD_32_BITS )
120 message(STATUS "Building 32 bits executables and libraries.")
121 add_llvm_definitions( -m32 )
Tim Northover4c8657a2012-05-23 18:42:02 +0000122 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
123 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
Tobias Grosser2563ad22012-06-08 09:41:23 +0000124 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
Oscar Fuentesd538e242011-02-03 20:57:36 +0000125 endif( LLVM_BUILD_32_BITS )
126endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
127
NAKAMURA Takumi5bfe3bf2012-04-21 14:50:56 +0000128# On Win32 using MS tools, provide an option to set the number of parallel jobs
129# to use.
NAKAMURA Takumi4a80d642012-04-21 14:51:02 +0000130if( MSVC_IDE )
Oscar Fuentes0dddbc32011-03-02 17:47:37 +0000131 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()
149endif()
150
Oscar Fuentesd538e242011-02-03 20:57:36 +0000151if( MSVC )
152 include(ChooseMSVCCRT)
153
Aaron Ballmancc6933b2013-07-29 13:02:08 +0000154 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 )
Michael J. Spencer647c0ce2012-03-01 22:42:52 +0000161 add_llvm_definitions(-D_VARIADIC_MAX=10)
162 endif()
Aaron Ballmancc6933b2013-07-29 13:02:08 +0000163
Oscar Fuentesd538e242011-02-03 20:57:36 +0000164 # 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
Michael J. Spencerc3ab9ce2012-06-07 21:34:15 +0000174 # Disabled warnings.
Oscar Fuentesd538e242011-02-03 20:57:36 +0000175 -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
176 -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
Oscar Fuentesd538e242011-02-03 20:57:36 +0000177 -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'
Oscar Fuentesd538e242011-02-03 20:57:36 +0000179 -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'
Oscar Fuentesd538e242011-02-03 20:57:36 +0000184 -wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)'
Aaron Ballman7f92c7b2013-08-16 03:06:38 +0000185 -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 +0000186
Michael J. Spencerc3ab9ce2012-06-07 21:34:15 +0000187 # Promoted warnings.
188 -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
Michael J. Spencer67762d12012-06-07 23:33:56 +0000189
190 # Promoted warnings to errors.
191 -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
Oscar Fuentesd538e242011-02-03 20:57:36 +0000192 )
193
194 # Enable warnings
195 if (LLVM_ENABLE_WARNINGS)
Michael J. Spencerc3ab9ce2012-06-07 21:34:15 +0000196 add_llvm_definitions( /W4 )
Oscar Fuentesd538e242011-02-03 20:57:36 +0000197 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)
Oscar Fuentes104e9922011-05-11 13:53:08 +0000204elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
Oscar Fuentesd538e242011-02-03 20:57:36 +0000205 if (LLVM_ENABLE_WARNINGS)
Duncan Sands2269c562013-03-25 13:25:34 +0000206 append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Edwin Vaneadc4af62013-01-31 18:05:54 +0000207
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)
Duncan Sands2269c562013-03-25 13:25:34 +0000214 append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Edwin Vaneadc4af62013-01-31 18:05:54 +0000215 else()
Duncan Sands2269c562013-03-25 13:25:34 +0000216 append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Edwin Vaneadc4af62013-01-31 18:05:54 +0000217 endif()
218 endif()
219
Duncan Sands2269c562013-03-25 13:25:34 +0000220 append_if(LLVM_ENABLE_PEDANTIC "-pedantic -Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Joe Abbeyd6a93072012-11-26 02:02:08 +0000221 check_cxx_compiler_flag("-Werror -Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
Duncan Sands2269c562013-03-25 13:25:34 +0000222 append_if(CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_CXX_FLAGS)
Joe Abbeyd6a93072012-11-26 02:02:08 +0000223 check_c_compiler_flag("-Werror -Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
Duncan Sands2269c562013-03-25 13:25:34 +0000224 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)
Alexey Samsonov8483b472013-03-19 10:10:03 +0000227 check_cxx_compiler_flag("-Werror -Wnon-virtual-dtor" CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG)
Duncan Sands2269c562013-03-25 13:25:34 +0000228 append_if(CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
Oscar Fuentesd538e242011-02-03 20:57:36 +0000229 endif (LLVM_ENABLE_WARNINGS)
230 if (LLVM_ENABLE_WERROR)
231 add_llvm_definitions( -Werror )
232 endif (LLVM_ENABLE_WERROR)
233endif( MSVC )
234
Alexey Samsonov777fccb2013-03-26 07:49:46 +0000235macro(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()
Alexey Samsonovb775f2f2013-09-02 09:15:01 +0000243 # 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()
Alexey Samsonov777fccb2013-03-26 07:49:46 +0000247endmacro()
248
249# Turn on sanitizers if necessary.
250if(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")
Alexey Samsonov777fccb2013-03-26 07:49:46 +0000258 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()
267endif()
268
Eric Christopher6aebd5f2013-07-30 21:44:10 +0000269# Turn on -gsplit-dwarf if requested
270if(LLVM_USE_SPLIT_DWARF)
Eric Christopherb9db9cd2013-07-30 22:34:30 +0000271 add_llvm_definitions("-gsplit-dwarf")
Eric Christopher6aebd5f2013-07-30 21:44:10 +0000272endif()
273
Oscar Fuentesd538e242011-02-03 20:57:36 +0000274add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
NAKAMURA Takumie2aa5d52011-10-11 12:51:44 +0000275add_llvm_definitions( -D__STDC_FORMAT_MACROS )
NAKAMURA Takumi11b8a002011-10-11 12:51:36 +0000276add_llvm_definitions( -D__STDC_LIMIT_MACROS )
Arnaud A. de Grandmaison745825f52013-05-29 20:41:35 +0000277
278# clang doesn't print colored diagnostics when invoked from Ninja
Rafael Espindolad132ce72013-06-14 19:41:05 +0000279if (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)
Arnaud A. de Grandmaison745825f52013-05-29 20:41:35 +0000283endif()