Daniel Dunbar | 10fa2df | 2011-11-04 19:04:35 +0000 | [diff] [blame] | 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 | |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 5 | include(AddLLVMDefinitions) |
Joe Abbey | 15d9834 | 2012-11-26 02:02:08 +0000 | [diff] [blame] | 6 | include(CheckCCompilerFlag) |
Jordan Rose | 643aa0e | 2013-03-02 01:00:40 +0000 | [diff] [blame] | 7 | include(CheckCXXCompilerFlag) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 8 | |
Oscar Fuentes | ab84a7b | 2011-05-11 13:53:08 +0000 | [diff] [blame] | 9 | if( CMAKE_COMPILER_IS_GNUCXX ) |
| 10 | set(LLVM_COMPILER_IS_GCC_COMPATIBLE ON) |
Hans Wennborg | 3e67a65 | 2013-10-07 22:03:23 +0000 | [diff] [blame] | 11 | elseif( MSVC ) |
| 12 | set(LLVM_COMPILER_IS_GCC_COMPATIBLE OFF) |
Oscar Fuentes | ab84a7b | 2011-05-11 13:53:08 +0000 | [diff] [blame] | 13 | elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) |
| 14 | set(LLVM_COMPILER_IS_GCC_COMPATIBLE ON) |
| 15 | endif() |
| 16 | |
Chandler Carruth | 8388597 | 2014-01-13 22:21:34 +0000 | [diff] [blame] | 17 | if(NOT LLVM_FORCE_USE_OLD_TOOLCHAIN) |
| 18 | if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 19 | if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7) |
| 20 | message(FATAL_ERROR "Host GCC version must be at least 4.7!") |
| 21 | endif() |
| 22 | elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") |
| 23 | if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.1) |
| 24 | message(FATAL_ERROR "Host Clang version must be at least 3.1!") |
| 25 | endif() |
Chandler Carruth | f8f0015 | 2014-01-17 09:47:55 +0000 | [diff] [blame] | 26 | |
| 27 | # Also test that we aren't using too old of a version of libstdc++ with the |
| 28 | # Clang compiler. This is tricky as there is no real way to check the |
| 29 | # version of libstdc++ directly. Instead we test for a known bug in |
| 30 | # libstdc++4.6 that is fixed in libstdc++4.7. |
| 31 | if(NOT LLVM_ENABLE_LIBCXX) |
Chandler Carruth | f4144ad | 2014-01-21 18:09:19 +0000 | [diff] [blame] | 32 | set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
| 33 | set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) |
Chandler Carruth | f8f0015 | 2014-01-17 09:47:55 +0000 | [diff] [blame] | 34 | set(CMAKE_REQUIRED_FLAGS "-std=c++0x") |
Evgeniy Stepanov | 131585a | 2014-01-21 09:00:30 +0000 | [diff] [blame] | 35 | if (ANDROID) |
| 36 | set(CMAKE_REQUIRED_LIBRARIES "atomic") |
| 37 | endif() |
Chandler Carruth | f8f0015 | 2014-01-17 09:47:55 +0000 | [diff] [blame] | 38 | check_cxx_source_compiles(" |
| 39 | #include <atomic> |
| 40 | std::atomic<float> x(0.0f); |
| 41 | int main() { return (float)x; }" |
| 42 | LLVM_NO_OLD_LIBSTDCXX) |
| 43 | if(NOT LLVM_NO_OLD_LIBSTDCXX) |
| 44 | message(FATAL_ERROR "Host Clang must be able to find libstdc++4.7 or newer!") |
| 45 | endif() |
Chandler Carruth | f4144ad | 2014-01-21 18:09:19 +0000 | [diff] [blame] | 46 | set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 47 | set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES}) |
Chandler Carruth | f8f0015 | 2014-01-17 09:47:55 +0000 | [diff] [blame] | 48 | endif() |
Amara Emerson | 5569381a | 2014-01-21 16:41:07 +0000 | [diff] [blame] | 49 | elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") |
Chandler Carruth | 8388597 | 2014-01-13 22:21:34 +0000 | [diff] [blame] | 50 | if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17.0) |
| 51 | message(FATAL_ERROR "Host Visual Studio must be at least 2012 (MSVC 17.0)") |
| 52 | endif() |
| 53 | endif() |
| 54 | endif() |
| 55 | |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 56 | if( LLVM_ENABLE_ASSERTIONS ) |
| 57 | # MSVC doesn't like _DEBUG on release builds. See PR 4379. |
| 58 | if( NOT MSVC ) |
| 59 | add_definitions( -D_DEBUG ) |
| 60 | endif() |
Duncan Sands | 80f122f | 2013-07-17 09:34:51 +0000 | [diff] [blame] | 61 | # On non-Debug builds cmake automatically defines NDEBUG, so we |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 62 | # explicitly undefine it: |
Duncan Sands | 80f122f | 2013-07-17 09:34:51 +0000 | [diff] [blame] | 63 | if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" ) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 64 | add_definitions( -UNDEBUG ) |
Reid Kleckner | c1c01d5 | 2013-04-07 01:45:01 +0000 | [diff] [blame] | 65 | # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines. |
Nico Weber | d16b48a | 2013-12-28 23:26:51 +0000 | [diff] [blame] | 66 | set(REGEXP_NDEBUG "(^| )[/-]D *NDEBUG($| )") |
| 67 | string (REGEX REPLACE "${REGEXP_NDEBUG}" " " |
| 68 | CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") |
| 69 | string (REGEX REPLACE "${REGEXP_NDEBUG}" " " |
| 70 | CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") |
| 71 | string (REGEX REPLACE "${REGEXP_NDEBUG}" " " |
| 72 | CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}") |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 73 | endif() |
| 74 | else() |
| 75 | if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" ) |
Oscar Fuentes | 7dc9dce | 2011-02-06 19:07:06 +0000 | [diff] [blame] | 76 | if( NOT MSVC_IDE AND NOT XCODE ) |
| 77 | add_definitions( -DNDEBUG ) |
| 78 | endif() |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 79 | endif() |
| 80 | endif() |
| 81 | |
| 82 | if(WIN32) |
Nico Weber | c27118d | 2013-12-28 23:31:44 +0000 | [diff] [blame] | 83 | set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 84 | if(CYGWIN) |
| 85 | set(LLVM_ON_WIN32 0) |
| 86 | set(LLVM_ON_UNIX 1) |
| 87 | else(CYGWIN) |
| 88 | set(LLVM_ON_WIN32 1) |
| 89 | set(LLVM_ON_UNIX 0) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 90 | endif(CYGWIN) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 91 | # Maximum path length is 160 for non-unicode paths |
| 92 | set(MAXPATHLEN 160) |
| 93 | else(WIN32) |
| 94 | if(UNIX) |
| 95 | set(LLVM_ON_WIN32 0) |
| 96 | set(LLVM_ON_UNIX 1) |
| 97 | if(APPLE) |
Nico Weber | c27118d | 2013-12-28 23:31:44 +0000 | [diff] [blame] | 98 | set(LLVM_HAVE_LINK_VERSION_SCRIPT 0) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 99 | else(APPLE) |
Nico Weber | c27118d | 2013-12-28 23:31:44 +0000 | [diff] [blame] | 100 | set(LLVM_HAVE_LINK_VERSION_SCRIPT 1) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 101 | endif(APPLE) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 102 | # FIXME: Maximum path length is currently set to 'safe' fixed value |
| 103 | set(MAXPATHLEN 2024) |
| 104 | else(UNIX) |
| 105 | MESSAGE(SEND_ERROR "Unable to determine platform") |
| 106 | endif(UNIX) |
| 107 | endif(WIN32) |
| 108 | |
Alp Toker | c0b7bb5 | 2014-01-08 11:10:24 +0000 | [diff] [blame] | 109 | set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX}) |
| 110 | set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX}) |
| 111 | set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_MODULE_SUFFIX}) |
| 112 | |
Alexey Samsonov | 6706578 | 2013-03-13 20:50:23 +0000 | [diff] [blame] | 113 | function(add_flag_or_print_warning flag) |
Alexey Samsonov | 75789a2 | 2013-03-26 07:49:46 +0000 | [diff] [blame] | 114 | check_c_compiler_flag(${flag} C_SUPPORTS_FLAG) |
| 115 | check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG) |
| 116 | if (C_SUPPORTS_FLAG AND CXX_SUPPORTS_FLAG) |
Alexey Samsonov | 6706578 | 2013-03-13 20:50:23 +0000 | [diff] [blame] | 117 | message(STATUS "Building with ${flag}") |
| 118 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE) |
| 119 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE) |
| 120 | else() |
| 121 | message(WARNING "${flag} is not supported.") |
| 122 | endif() |
| 123 | endfunction() |
| 124 | |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 125 | function(append value) |
| 126 | foreach(variable ${ARGN}) |
Alexey Samsonov | 6706578 | 2013-03-13 20:50:23 +0000 | [diff] [blame] | 127 | set(${variable} "${${variable}} ${value}" PARENT_SCOPE) |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 128 | endforeach(variable) |
| 129 | endfunction() |
| 130 | |
| 131 | function(append_if condition value) |
| 132 | if (${condition}) |
| 133 | foreach(variable ${ARGN}) |
| 134 | set(${variable} "${${variable}} ${value}" PARENT_SCOPE) |
| 135 | endforeach(variable) |
Alexey Samsonov | 6706578 | 2013-03-13 20:50:23 +0000 | [diff] [blame] | 136 | endif() |
| 137 | endfunction() |
| 138 | |
Alexey Samsonov | 75789a2 | 2013-03-26 07:49:46 +0000 | [diff] [blame] | 139 | macro(add_flag_if_supported flag) |
| 140 | check_c_compiler_flag(${flag} C_SUPPORTS_FLAG) |
| 141 | append_if(C_SUPPORTS_FLAG "${flag}" CMAKE_C_FLAGS) |
| 142 | check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG) |
| 143 | append_if(CXX_SUPPORTS_FLAG "${flag}" CMAKE_CXX_FLAGS) |
| 144 | endmacro() |
| 145 | |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 146 | if( LLVM_ENABLE_PIC ) |
| 147 | if( XCODE ) |
| 148 | # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't |
| 149 | # know how to disable this, so just force ENABLE_PIC off for now. |
| 150 | message(WARNING "-fPIC not supported with Xcode.") |
NAKAMURA Takumi | 1b745d0 | 2011-12-16 06:21:08 +0000 | [diff] [blame] | 151 | elseif( WIN32 OR CYGWIN) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 152 | # On Windows all code is PIC. MinGW warns if -fPIC is used. |
| 153 | else() |
Alexey Samsonov | 6706578 | 2013-03-13 20:50:23 +0000 | [diff] [blame] | 154 | add_flag_or_print_warning("-fPIC") |
Rafael Espindola | 5258ab8 | 2012-01-20 04:07:48 +0000 | [diff] [blame] | 155 | |
Rafael Espindola | 9f404cc | 2012-01-20 13:10:10 +0000 | [diff] [blame] | 156 | if( WIN32 OR CYGWIN) |
| 157 | # MinGW warns if -fvisibility-inlines-hidden is used. |
| 158 | else() |
| 159 | check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG) |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 160 | append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS) |
Alexey Samsonov | 6706578 | 2013-03-13 20:50:23 +0000 | [diff] [blame] | 161 | endif() |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 162 | endif() |
| 163 | endif() |
| 164 | |
| 165 | if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 ) |
| 166 | # TODO: support other platforms and toolchains. |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 167 | if( LLVM_BUILD_32_BITS ) |
| 168 | message(STATUS "Building 32 bits executables and libraries.") |
| 169 | add_llvm_definitions( -m32 ) |
Tim Northover | f6e29c4 | 2012-05-23 18:42:02 +0000 | [diff] [blame] | 170 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32") |
| 171 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32") |
Tobias Grosser | b32ad40 | 2012-06-08 09:41:23 +0000 | [diff] [blame] | 172 | set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32") |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 173 | endif( LLVM_BUILD_32_BITS ) |
| 174 | endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 ) |
| 175 | |
NAKAMURA Takumi | a83601d | 2012-04-21 14:50:56 +0000 | [diff] [blame] | 176 | # On Win32 using MS tools, provide an option to set the number of parallel jobs |
| 177 | # to use. |
NAKAMURA Takumi | 75bfe69 | 2012-04-21 14:51:02 +0000 | [diff] [blame] | 178 | if( MSVC_IDE ) |
Oscar Fuentes | 318c3f1 | 2011-03-02 17:47:37 +0000 | [diff] [blame] | 179 | set(LLVM_COMPILER_JOBS "0" CACHE STRING |
| 180 | "Number of parallel compiler jobs. 0 means use all processors. Default is 0.") |
| 181 | if( NOT LLVM_COMPILER_JOBS STREQUAL "1" ) |
| 182 | if( LLVM_COMPILER_JOBS STREQUAL "0" ) |
| 183 | add_llvm_definitions( /MP ) |
| 184 | else() |
| 185 | if (MSVC10) |
| 186 | message(FATAL_ERROR |
| 187 | "Due to a bug in CMake only 0 and 1 is supported for " |
| 188 | "LLVM_COMPILER_JOBS when generating for Visual Studio 2010") |
| 189 | else() |
| 190 | message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS}) |
| 191 | add_llvm_definitions( /MP${LLVM_COMPILER_JOBS} ) |
| 192 | endif() |
| 193 | endif() |
| 194 | else() |
| 195 | message(STATUS "Parallel compilation disabled") |
| 196 | endif() |
| 197 | endif() |
| 198 | |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 199 | if( MSVC ) |
| 200 | include(ChooseMSVCCRT) |
| 201 | |
Hans Wennborg | bef50ab | 2013-10-17 18:39:47 +0000 | [diff] [blame] | 202 | if( NOT (${CMAKE_VERSION} VERSION_LESS 2.8.11) ) |
| 203 | # set stack reserved size to ~10MB |
| 204 | # CMake previously automatically set this value for MSVC builds, but the |
| 205 | # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default |
| 206 | # value (1 MB) which is not enough for us in tasks such as parsing recursive |
| 207 | # C++ templates in Clang. |
| 208 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000") |
| 209 | endif() |
Hans Wennborg | fd541f0 | 2013-10-17 17:49:57 +0000 | [diff] [blame] | 210 | |
Aaron Ballman | f5aacd3 | 2013-07-29 13:02:08 +0000 | [diff] [blame] | 211 | if( MSVC10 ) |
| 212 | # MSVC 10 will complain about headers in the STL not being exported, but |
| 213 | # will not complain in MSVC 11. |
| 214 | add_llvm_definitions( |
| 215 | -wd4275 # Suppress 'An exported class was derived from a class that was not exported.' |
| 216 | ) |
| 217 | elseif( MSVC11 ) |
Michael J. Spencer | 35145f8 | 2012-03-01 22:42:52 +0000 | [diff] [blame] | 218 | add_llvm_definitions(-D_VARIADIC_MAX=10) |
| 219 | endif() |
Aaron Ballman | f5aacd3 | 2013-07-29 13:02:08 +0000 | [diff] [blame] | 220 | |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 221 | # Add definitions that make MSVC much less annoying. |
| 222 | add_llvm_definitions( |
| 223 | # For some reason MS wants to deprecate a bunch of standard functions... |
| 224 | -D_CRT_SECURE_NO_DEPRECATE |
| 225 | -D_CRT_SECURE_NO_WARNINGS |
| 226 | -D_CRT_NONSTDC_NO_DEPRECATE |
| 227 | -D_CRT_NONSTDC_NO_WARNINGS |
| 228 | -D_SCL_SECURE_NO_DEPRECATE |
| 229 | -D_SCL_SECURE_NO_WARNINGS |
| 230 | |
Michael J. Spencer | 6d45d83 | 2012-06-07 21:34:15 +0000 | [diff] [blame] | 231 | # Disabled warnings. |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 232 | -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned' |
| 233 | -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored' |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 234 | -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data' |
| 235 | -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data' |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 236 | -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized' |
| 237 | -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized' |
| 238 | -wd4355 # Suppress ''this' : used in base member initializer list' |
| 239 | -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated' |
| 240 | -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible' |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 241 | -wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)' |
Aaron Ballman | 99ea291 | 2013-08-16 03:06:38 +0000 | [diff] [blame] | 242 | -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception' |
Aaron Ballman | d1594bd | 2013-07-28 18:04:26 +0000 | [diff] [blame] | 243 | |
Michael J. Spencer | 6d45d83 | 2012-06-07 21:34:15 +0000 | [diff] [blame] | 244 | # Promoted warnings. |
| 245 | -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning. |
Michael J. Spencer | aeb59e1 | 2012-06-07 23:33:56 +0000 | [diff] [blame] | 246 | |
| 247 | # Promoted warnings to errors. |
| 248 | -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error. |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 249 | ) |
| 250 | |
| 251 | # Enable warnings |
| 252 | if (LLVM_ENABLE_WARNINGS) |
Michael J. Spencer | 6d45d83 | 2012-06-07 21:34:15 +0000 | [diff] [blame] | 253 | add_llvm_definitions( /W4 ) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 254 | if (LLVM_ENABLE_PEDANTIC) |
| 255 | # No MSVC equivalent available |
| 256 | endif (LLVM_ENABLE_PEDANTIC) |
| 257 | endif (LLVM_ENABLE_WARNINGS) |
| 258 | if (LLVM_ENABLE_WERROR) |
| 259 | add_llvm_definitions( /WX ) |
| 260 | endif (LLVM_ENABLE_WERROR) |
Oscar Fuentes | ab84a7b | 2011-05-11 13:53:08 +0000 | [diff] [blame] | 261 | elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE ) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 262 | if (LLVM_ENABLE_WARNINGS) |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 263 | append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
Edwin Vane | c641084 | 2013-01-31 18:05:54 +0000 | [diff] [blame] | 264 | |
| 265 | # Turn off missing field initializer warnings for gcc to avoid noise from |
| 266 | # false positives with empty {}. Turn them on otherwise (they're off by |
| 267 | # default for clang). |
| 268 | check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG) |
| 269 | if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG) |
| 270 | if (CMAKE_COMPILER_IS_GNUCXX) |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 271 | append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
Edwin Vane | c641084 | 2013-01-31 18:05:54 +0000 | [diff] [blame] | 272 | else() |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 273 | append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
Edwin Vane | c641084 | 2013-01-31 18:05:54 +0000 | [diff] [blame] | 274 | endif() |
| 275 | endif() |
| 276 | |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 277 | append_if(LLVM_ENABLE_PEDANTIC "-pedantic -Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
Joe Abbey | 15d9834 | 2012-11-26 02:02:08 +0000 | [diff] [blame] | 278 | check_cxx_compiler_flag("-Werror -Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG) |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 279 | append_if(CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_CXX_FLAGS) |
Joe Abbey | 15d9834 | 2012-11-26 02:02:08 +0000 | [diff] [blame] | 280 | check_c_compiler_flag("-Werror -Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG) |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 281 | append_if(C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG "-Wcovered-switch-default" CMAKE_C_FLAGS) |
| 282 | append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS) |
| 283 | append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS) |
Alexey Samsonov | ffb4ca8 | 2013-03-19 10:10:03 +0000 | [diff] [blame] | 284 | check_cxx_compiler_flag("-Werror -Wnon-virtual-dtor" CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG) |
Duncan Sands | 71de6dc | 2013-03-25 13:25:34 +0000 | [diff] [blame] | 285 | append_if(CXX_SUPPORTS_NON_VIRTUAL_DTOR_FLAG "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS) |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 286 | endif (LLVM_ENABLE_WARNINGS) |
| 287 | if (LLVM_ENABLE_WERROR) |
| 288 | add_llvm_definitions( -Werror ) |
| 289 | endif (LLVM_ENABLE_WERROR) |
Arnaud A. de Grandmaison | b697b53 | 2013-11-26 10:33:53 +0000 | [diff] [blame] | 290 | if (LLVM_ENABLE_CXX11) |
| 291 | check_cxx_compiler_flag("-std=c++11" CXX_SUPPORTS_CXX11) |
| 292 | append_if(CXX_SUPPORTS_CXX11 "-std=c++11" CMAKE_CXX_FLAGS) |
| 293 | endif (LLVM_ENABLE_CXX11) |
Jean-Daniel Dupas | edad1b4 | 2014-01-06 18:27:27 +0000 | [diff] [blame] | 294 | if(LLVM_ENABLE_LIBCXX) |
| 295 | check_cxx_compiler_flag("-stdlib=libc++" CXX_SUPPORTS_STDLIB) |
| 296 | append_if(CXX_SUPPORTS_STDLIB "-stdlib=libc++" CMAKE_CXX_FLAGS) |
| 297 | append_if(CXX_SUPPORTS_STDLIB "-stdlib=libc++" CMAKE_EXE_LINKER_FLAGS) |
| 298 | append_if(CXX_SUPPORTS_STDLIB "-stdlib=libc++" CMAKE_SHARED_LINKER_FLAGS) |
| 299 | append_if(CXX_SUPPORTS_STDLIB "-stdlib=libc++" CMAKE_MODULE_LINKER_FLAGS) |
| 300 | endif () |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 301 | endif( MSVC ) |
| 302 | |
Alexey Samsonov | 75789a2 | 2013-03-26 07:49:46 +0000 | [diff] [blame] | 303 | macro(append_common_sanitizer_flags) |
| 304 | # Append -fno-omit-frame-pointer and turn on debug info to get better |
| 305 | # stack traces. |
| 306 | add_flag_if_supported("-fno-omit-frame-pointer") |
| 307 | if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND |
| 308 | NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO") |
| 309 | add_flag_if_supported("-gline-tables-only") |
| 310 | endif() |
Alexey Samsonov | f9ab351 | 2013-09-02 09:15:01 +0000 | [diff] [blame] | 311 | # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large. |
| 312 | if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") |
| 313 | add_flag_if_supported("-O1") |
| 314 | endif() |
Alexey Samsonov | 75789a2 | 2013-03-26 07:49:46 +0000 | [diff] [blame] | 315 | endmacro() |
| 316 | |
| 317 | # Turn on sanitizers if necessary. |
| 318 | if(LLVM_USE_SANITIZER) |
| 319 | if (LLVM_ON_UNIX) |
| 320 | if (LLVM_USE_SANITIZER STREQUAL "Address") |
| 321 | append_common_sanitizer_flags() |
| 322 | add_flag_or_print_warning("-fsanitize=address") |
| 323 | elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") |
| 324 | append_common_sanitizer_flags() |
| 325 | add_flag_or_print_warning("-fsanitize=memory") |
Alexey Samsonov | 75789a2 | 2013-03-26 07:49:46 +0000 | [diff] [blame] | 326 | if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") |
| 327 | add_flag_or_print_warning("-fsanitize-memory-track-origins") |
| 328 | endif() |
| 329 | else() |
| 330 | message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") |
| 331 | endif() |
| 332 | else() |
| 333 | message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.") |
| 334 | endif() |
| 335 | endif() |
| 336 | |
Eric Christopher | 3987806 | 2013-07-30 21:44:10 +0000 | [diff] [blame] | 337 | # Turn on -gsplit-dwarf if requested |
| 338 | if(LLVM_USE_SPLIT_DWARF) |
Eric Christopher | 83afc1e | 2013-07-30 22:34:30 +0000 | [diff] [blame] | 339 | add_llvm_definitions("-gsplit-dwarf") |
Eric Christopher | 3987806 | 2013-07-30 21:44:10 +0000 | [diff] [blame] | 340 | endif() |
| 341 | |
Oscar Fuentes | f4202ba | 2011-02-03 20:57:36 +0000 | [diff] [blame] | 342 | add_llvm_definitions( -D__STDC_CONSTANT_MACROS ) |
NAKAMURA Takumi | c5554c9 | 2011-10-11 12:51:44 +0000 | [diff] [blame] | 343 | add_llvm_definitions( -D__STDC_FORMAT_MACROS ) |
NAKAMURA Takumi | e63cd19 | 2011-10-11 12:51:36 +0000 | [diff] [blame] | 344 | add_llvm_definitions( -D__STDC_LIMIT_MACROS ) |
Arnaud A. de Grandmaison | 916f3cc | 2013-05-29 20:41:35 +0000 | [diff] [blame] | 345 | |
| 346 | # clang doesn't print colored diagnostics when invoked from Ninja |
Rafael Espindola | 6f2564c | 2013-06-14 19:41:05 +0000 | [diff] [blame] | 347 | if (UNIX AND |
| 348 | CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND |
| 349 | CMAKE_GENERATOR STREQUAL "Ninja") |
| 350 | append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
Arnaud A. de Grandmaison | 916f3cc | 2013-05-29 20:41:35 +0000 | [diff] [blame] | 351 | endif() |
NAKAMURA Takumi | bb50bce | 2014-01-28 09:43:55 +0000 | [diff] [blame] | 352 | |
| 353 | # Add flags for add_dead_strip(). |
| 354 | # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF? |
| 355 | # But MinSizeRel seems to add that automatically, so maybe disable these |
| 356 | # flags instead if LLVM_NO_DEAD_STRIP is set. |
| 357 | if(NOT CYGWIN AND NOT WIN32) |
| 358 | if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") |
NAKAMURA Takumi | e20725b | 2014-01-28 09:44:00 +0000 | [diff] [blame] | 359 | append("-ffunction-sections -fdata-sections" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) |
NAKAMURA Takumi | bb50bce | 2014-01-28 09:43:55 +0000 | [diff] [blame] | 360 | endif() |
| 361 | endif() |
NAKAMURA Takumi | a679f43 | 2014-01-28 09:44:06 +0000 | [diff] [blame] | 362 | |
| 363 | if(MSVC) |
| 364 | # Remove flags here, for exceptions and RTTI. |
NAKAMURA Takumi | ac62464 | 2014-01-31 17:32:36 +0000 | [diff] [blame^] | 365 | # Each target property or source property should be responsible to control |
| 366 | # them. |
NAKAMURA Takumi | a679f43 | 2014-01-28 09:44:06 +0000 | [diff] [blame] | 367 | # CL.EXE complains to override flags like "/GR /GR-". |
| 368 | string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 369 | string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 370 | endif() |