HandleLLVMOptions.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. # This is commonly needed so make sure it's defined before we include anything
  5. # else.
  6. string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
  7. include(HandleLLVMStdlib)
  8. include(AddLLVMDefinitions)
  9. include(CheckCCompilerFlag)
  10. include(CheckCXXCompilerFlag)
  11. if(NOT LLVM_FORCE_USE_OLD_TOOLCHAIN)
  12. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  13. if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
  14. message(FATAL_ERROR "Host GCC version must be at least 4.7!")
  15. endif()
  16. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  17. if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.1)
  18. message(FATAL_ERROR "Host Clang version must be at least 3.1!")
  19. endif()
  20. if (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
  21. if (CMAKE_CXX_SIMULATE_VERSION VERSION_LESS 18.0)
  22. message(FATAL_ERROR "Host Clang must have at least -fms-compatibility-version=18.0")
  23. endif()
  24. set(CLANG_CL 1)
  25. elseif(NOT LLVM_ENABLE_LIBCXX)
  26. # Otherwise, test that we aren't using too old of a version of libstdc++
  27. # with the Clang compiler. This is tricky as there is no real way to
  28. # check the version of libstdc++ directly. Instead we test for a known
  29. # bug in libstdc++4.6 that is fixed in libstdc++4.7.
  30. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  31. set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  32. set(CMAKE_REQUIRED_FLAGS "-std=c++0x")
  33. check_cxx_source_compiles("
  34. #include <atomic>
  35. std::atomic<float> x(0.0f);
  36. int main() { return (float)x; }"
  37. LLVM_NO_OLD_LIBSTDCXX)
  38. if(NOT LLVM_NO_OLD_LIBSTDCXX)
  39. message(FATAL_ERROR "Host Clang must be able to find libstdc++4.7 or newer!")
  40. endif()
  41. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  42. set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
  43. endif()
  44. elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  45. if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18.0)
  46. message(FATAL_ERROR "Host Visual Studio must be at least 2013")
  47. elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18.0.31101)
  48. message(WARNING "Host Visual Studio should at least be 2013 Update 4 (MSVC 18.0.31101)"
  49. " due to miscompiles from earlier versions")
  50. endif()
  51. endif()
  52. endif()
  53. if( LLVM_ENABLE_ASSERTIONS )
  54. # MSVC doesn't like _DEBUG on release builds. See PR 4379.
  55. if( NOT MSVC )
  56. add_definitions( -D_DEBUG )
  57. endif()
  58. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDBG") # HLSL Change
  59. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -UNDEBUG") # HLSL Change
  60. if (0) # HLSL Change Starts
  61. # On non-Debug builds cmake automatically defines NDEBUG, so we
  62. # explicitly undefine it:
  63. if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
  64. add_definitions( -UNDEBUG )
  65. # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
  66. foreach (flags_var_to_scrub
  67. CMAKE_CXX_FLAGS_RELEASE
  68. CMAKE_CXX_FLAGS_RELWITHDEBINFO
  69. CMAKE_CXX_FLAGS_MINSIZEREL
  70. CMAKE_C_FLAGS_RELEASE
  71. CMAKE_C_FLAGS_RELWITHDEBINFO
  72. CMAKE_C_FLAGS_MINSIZEREL)
  73. string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
  74. "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
  75. endforeach()
  76. endif()
  77. endif (0) # HLSL Change Ends
  78. endif()
  79. string(TOUPPER "${LLVM_ABI_BREAKING_CHECKS}" uppercase_LLVM_ABI_BREAKING_CHECKS)
  80. if( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "WITH_ASSERTS" )
  81. if( LLVM_ENABLE_ASSERTIONS )
  82. set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
  83. endif()
  84. elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_ON" )
  85. set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
  86. elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_OFF" )
  87. # We don't need to do anything special to turn off ABI breaking checks.
  88. elseif( NOT DEFINED LLVM_ABI_BREAKING_CHECKS )
  89. # Treat LLVM_ABI_BREAKING_CHECKS like "FORCE_OFF" when it has not been
  90. # defined.
  91. else()
  92. message(FATAL_ERROR "Unknown value for LLVM_ABI_BREAKING_CHECKS: \"${LLVM_ABI_BREAKING_CHECKS}\"!")
  93. endif()
  94. if(WIN32)
  95. set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
  96. if(CYGWIN)
  97. set(LLVM_ON_WIN32 0)
  98. set(LLVM_ON_UNIX 1)
  99. else(CYGWIN)
  100. set(LLVM_ON_WIN32 1)
  101. set(LLVM_ON_UNIX 0)
  102. endif(CYGWIN)
  103. else(WIN32)
  104. if(UNIX)
  105. set(LLVM_ON_WIN32 0)
  106. set(LLVM_ON_UNIX 1)
  107. if(APPLE)
  108. set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
  109. else(APPLE)
  110. set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
  111. endif(APPLE)
  112. else(UNIX)
  113. MESSAGE(SEND_ERROR "Unable to determine platform")
  114. endif(UNIX)
  115. endif(WIN32)
  116. set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX})
  117. set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
  118. # We use *.dylib rather than *.so on darwin.
  119. set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
  120. if(APPLE)
  121. # Darwin-specific linker flags for loadable modules.
  122. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
  123. endif()
  124. # Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise a DSO
  125. # build might work on ELF but fail on MachO/COFF.
  126. if(NOT (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" OR WIN32 OR CYGWIN OR
  127. ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") AND
  128. NOT LLVM_USE_SANITIZER)
  129. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs")
  130. endif()
  131. function(append value)
  132. foreach(variable ${ARGN})
  133. set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
  134. endforeach(variable)
  135. endfunction()
  136. function(append_if condition value)
  137. if (${condition})
  138. foreach(variable ${ARGN})
  139. set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
  140. endforeach(variable)
  141. endif()
  142. endfunction()
  143. macro(add_flag_if_supported flag name)
  144. check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
  145. append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS)
  146. check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
  147. append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS)
  148. endmacro()
  149. function(add_flag_or_print_warning flag name)
  150. check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
  151. check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
  152. if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name})
  153. message(STATUS "Building with ${flag}")
  154. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
  155. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
  156. else()
  157. message(WARNING "${flag} is not supported.")
  158. endif()
  159. endfunction()
  160. if( LLVM_ENABLE_PIC )
  161. if( XCODE )
  162. # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
  163. # know how to disable this, so just force ENABLE_PIC off for now.
  164. message(WARNING "-fPIC not supported with Xcode.")
  165. elseif( WIN32 OR CYGWIN)
  166. # On Windows all code is PIC. MinGW warns if -fPIC is used.
  167. else()
  168. add_flag_or_print_warning("-fPIC" FPIC)
  169. if( WIN32 OR CYGWIN)
  170. # MinGW warns if -fvisibility-inlines-hidden is used.
  171. else()
  172. check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
  173. append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden -fvisibility=hidden" CMAKE_CXX_FLAGS)
  174. endif()
  175. endif()
  176. endif()
  177. if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
  178. # TODO: support other platforms and toolchains.
  179. if( LLVM_BUILD_32_BITS )
  180. message(STATUS "Building 32 bits executables and libraries.")
  181. add_llvm_definitions( -m32 )
  182. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
  183. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
  184. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
  185. endif( LLVM_BUILD_32_BITS )
  186. endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
  187. if (LLVM_BUILD_STATIC)
  188. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
  189. endif()
  190. if( XCODE )
  191. # For Xcode enable several build settings that correspond to
  192. # many warnings that are on by default in Clang but are
  193. # not enabled for historical reasons. For versions of Xcode
  194. # that do not support these options they will simply
  195. # be ignored.
  196. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_RETURN_TYPE "YES")
  197. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_MISSING_NEWLINE "YES")
  198. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VALUE "YES")
  199. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VARIABLE "YES")
  200. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_SIGN_COMPARE "YES")
  201. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_FUNCTION "YES")
  202. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED "YES")
  203. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS "YES")
  204. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNINITIALIZED_AUTOS "YES")
  205. set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_BOOL_CONVERSION "YES")
  206. set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_EMPTY_BODY "YES")
  207. set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_ENUM_CONVERSION "YES")
  208. set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_INT_CONVERSION "YES")
  209. set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_CONSTANT_CONVERSION "YES")
  210. set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_NON_VIRTUAL_DESTRUCTOR "YES")
  211. endif()
  212. # On Win32 using MS tools, provide an option to set the number of parallel jobs
  213. # to use.
  214. if( MSVC_IDE )
  215. set(LLVM_COMPILER_JOBS "0" CACHE STRING
  216. "Number of parallel compiler jobs. 0 means use all processors. Default is 0.")
  217. if( NOT LLVM_COMPILER_JOBS STREQUAL "1" )
  218. if( LLVM_COMPILER_JOBS STREQUAL "0" )
  219. add_llvm_definitions( /MP )
  220. else()
  221. message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS})
  222. add_llvm_definitions( /MP${LLVM_COMPILER_JOBS} )
  223. endif()
  224. else()
  225. message(STATUS "Parallel compilation disabled")
  226. endif()
  227. endif()
  228. if( MSVC )
  229. include(ChooseMSVCCRT)
  230. if( NOT (${CMAKE_VERSION} VERSION_LESS 2.8.11) )
  231. # set stack reserved size to ~10MB
  232. # CMake previously automatically set this value for MSVC builds, but the
  233. # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default
  234. # value (1 MB) which is not enough for us in tasks such as parsing recursive
  235. # C++ templates in Clang.
  236. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000")
  237. endif()
  238. if( MSVC11 )
  239. add_llvm_definitions(-D_VARIADIC_MAX=10)
  240. endif()
  241. # Add definitions that make MSVC much less annoying.
  242. add_llvm_definitions(
  243. # For some reason MS wants to deprecate a bunch of standard functions...
  244. -D_CRT_SECURE_NO_DEPRECATE
  245. -D_CRT_SECURE_NO_WARNINGS
  246. -D_CRT_NONSTDC_NO_DEPRECATE
  247. -D_CRT_NONSTDC_NO_WARNINGS
  248. -D_SCL_SECURE_NO_DEPRECATE
  249. -D_SCL_SECURE_NO_WARNINGS
  250. )
  251. set(msvc_warning_flags
  252. # Disabled warnings.
  253. -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
  254. -wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
  255. -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
  256. -wd4258 # Suppress ''var' : definition from the for loop is ignored; the definition from the enclosing scope is used'
  257. -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
  258. -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
  259. -wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
  260. -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
  261. -wd4355 # Suppress ''this' : used in base member initializer list'
  262. -wd4456 # Suppress 'declaration of 'var' hides local variable'
  263. -wd4457 # Suppress 'declaration of 'var' hides function parameter'
  264. -wd4458 # Suppress 'declaration of 'var' hides class member'
  265. -wd4459 # Suppress 'declaration of 'var' hides global declaration'
  266. -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
  267. -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
  268. -wd4722 # Suppress 'function' : destructor never returns, potential memory leak
  269. -wd4800 # Suppress ''type' : forcing value to bool 'true' or 'false' (performance warning)'
  270. -wd4100 # Suppress 'unreferenced formal parameter'
  271. -wd4127 # Suppress 'conditional expression is constant'
  272. -wd4512 # Suppress 'assignment operator could not be generated'
  273. -wd4505 # Suppress 'unreferenced local function has been removed'
  274. -wd4610 # Suppress '<class> can never be instantiated'
  275. -wd4510 # Suppress 'default constructor could not be generated'
  276. -wd4702 # Suppress 'unreachable code'
  277. -wd4245 # Suppress 'signed/unsigned mismatch'
  278. -wd4706 # Suppress 'assignment within conditional expression'
  279. -wd4310 # Suppress 'cast truncates constant value'
  280. -wd4701 # Suppress 'potentially uninitialized local variable'
  281. -wd4703 # Suppress 'potentially uninitialized local pointer variable'
  282. -wd4389 # Suppress 'signed/unsigned mismatch'
  283. -wd4611 # Suppress 'interaction between '_setjmp' and C++ object destruction is non-portable'
  284. -wd4805 # Suppress 'unsafe mix of type <type> and type <type> in operation'
  285. -wd4204 # Suppress 'nonstandard extension used : non-constant aggregate initializer'
  286. # Ideally, we'd like this warning to be enabled, but MSVC 2013 doesn't
  287. # support the 'aligned' attribute in the way that clang sources requires (for
  288. # any code that uses the LLVM_ALIGNAS macro), so this is must be disabled to
  289. # avoid unwanted alignment warnings.
  290. # When we switch to requiring a version of MSVC that supports the 'alignas'
  291. # specifier (MSVC 2015?) this warning can be re-enabled.
  292. -wd4324 # Suppress 'structure was padded due to __declspec(align())'
  293. # Promoted warnings.
  294. # HLSL Change - don't do this - -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
  295. # Promoted warnings to errors.
  296. -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
  297. )
  298. # HLSL Changes Start
  299. if (HLSL_ENABLE_ANALYZE)
  300. append("/analyze" CMAKE_CXX_FLAGS)
  301. endif ()
  302. # Change release to always build debug information out-of-line, but
  303. # also enable Reference optimization, ie dead function elimination.
  304. append("/Zi" CMAKE_CXX_FLAGS_RELEASE)
  305. append("/DEBUG /OPT:REF" CMAKE_SHARED_LINKER_FLAGS_RELEASE)
  306. append("/DEBUG /OPT:REF" CMAKE_EXE_LINKER_FLAGS_RELEASE)
  307. # HLSL Changes End
  308. # Enable warnings
  309. if (LLVM_ENABLE_WARNINGS)
  310. append("/W4" msvc_warning_flags)
  311. # CMake appends /W3 by default, and having /W3 followed by /W4 will result in
  312. # cl : Command line warning D9025 : overriding '/W3' with '/W4'. Since this is
  313. # a command line warning and not a compiler warning, it cannot be suppressed except
  314. # by fixing the command line.
  315. string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  316. string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  317. if (LLVM_ENABLE_PEDANTIC)
  318. # No MSVC equivalent available
  319. endif (LLVM_ENABLE_PEDANTIC)
  320. endif (LLVM_ENABLE_WARNINGS)
  321. if (LLVM_ENABLE_WERROR)
  322. append("/WX" msvc_warning_flags)
  323. endif (LLVM_ENABLE_WERROR)
  324. foreach(flag ${msvc_warning_flags})
  325. append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  326. endforeach(flag)
  327. # Disable sized deallocation if the flag is supported. MSVC fails to compile
  328. # the operator new overload in User otherwise.
  329. check_c_compiler_flag("/WX /Zc:sizedDealloc-" SUPPORTS_SIZED_DEALLOC)
  330. append_if(SUPPORTS_SIZED_DEALLOC "/Zc:sizedDealloc-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  331. elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
  332. if (LLVM_ENABLE_WARNINGS)
  333. append("-Wall -W -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  334. append("-Wcast-qual" CMAKE_CXX_FLAGS)
  335. # Disable unknown pragma warnings because the output is just too long with them.
  336. append("-Wno-unknown-pragmas" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  337. # Colorize GCC output even with ninja's stdout redirection.
  338. if (CMAKE_COMPILER_IS_GNUCXX)
  339. append("-fdiagnostics-color" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  340. append("-std=c++11" CMAKE_CXX_FLAGS)
  341. endif (CMAKE_COMPILER_IS_GNUCXX)
  342. # Turn off missing field initializer warnings for gcc to avoid noise from
  343. # false positives with empty {}. Turn them on otherwise (they're off by
  344. # default for clang).
  345. check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
  346. if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
  347. if (CMAKE_COMPILER_IS_GNUCXX)
  348. append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  349. else()
  350. append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  351. endif()
  352. endif()
  353. append_if(LLVM_ENABLE_PEDANTIC "-pedantic -Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  354. # add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG) # HLSL Change
  355. append("-Wno-switch" CMAKE_CXX_FLAGS) # HLSL Change
  356. append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
  357. append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
  358. # Check if -Wnon-virtual-dtor warns even though the class is marked final.
  359. # If it does, don't add it. So it won't be added on clang 3.4 and older.
  360. # This also catches cases when -Wnon-virtual-dtor isn't supported by
  361. # the compiler at all.
  362. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  363. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11 -Werror=non-virtual-dtor")
  364. CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();protected: ~base();};
  365. class derived final : public base { public: ~derived();};
  366. int main() { return 0; }"
  367. CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR)
  368. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  369. append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR
  370. "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
  371. # Check if -Wcomment is OK with an // comment ending with '\' if the next
  372. # line is also a // comment.
  373. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  374. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment")
  375. CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main() {return 0;}"
  376. C_WCOMMENT_ALLOWS_LINE_WRAP)
  377. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  378. if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP)
  379. append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  380. endif()
  381. endif (LLVM_ENABLE_WARNINGS)
  382. append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  383. if (NOT LLVM_ENABLE_TIMESTAMPS)
  384. add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME)
  385. endif ()
  386. if (LLVM_ENABLE_CXX1Y)
  387. check_cxx_compiler_flag("-std=c++1y" CXX_SUPPORTS_CXX1Y)
  388. append_if(CXX_SUPPORTS_CXX1Y "-std=c++1y" CMAKE_CXX_FLAGS)
  389. else()
  390. check_cxx_compiler_flag("-std=c++11" CXX_SUPPORTS_CXX11)
  391. if (CXX_SUPPORTS_CXX11)
  392. if (CYGWIN OR MINGW)
  393. # MinGW and Cygwin are a bit stricter and lack things like
  394. # 'strdup', 'stricmp', etc in c++11 mode.
  395. append("-std=gnu++11" CMAKE_CXX_FLAGS)
  396. else()
  397. append("-std=c++11" CMAKE_CXX_FLAGS)
  398. endif()
  399. else()
  400. message(FATAL_ERROR "LLVM requires C++11 support but the '-std=c++11' flag isn't supported.")
  401. endif()
  402. endif()
  403. if (LLVM_ENABLE_MODULES)
  404. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  405. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fmodules -fcxx-modules")
  406. # Check that we can build code with modules enabled, and that repeatedly
  407. # including <cassert> still manages to respect NDEBUG properly.
  408. CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG
  409. #include <cassert>
  410. #define NDEBUG
  411. #include <cassert>
  412. int main() { assert(this code is not compiled); }"
  413. CXX_SUPPORTS_MODULES)
  414. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  415. if (CXX_SUPPORTS_MODULES)
  416. append_if(CXX_SUPPORTS_MODULES "-fmodules" CMAKE_C_FLAGS)
  417. append_if(CXX_SUPPORTS_MODULES "-fmodules -fcxx-modules" CMAKE_CXX_FLAGS)
  418. else()
  419. message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler")
  420. endif()
  421. endif(LLVM_ENABLE_MODULES)
  422. endif( MSVC )
  423. macro(append_common_sanitizer_flags)
  424. # Append -fno-omit-frame-pointer and turn on debug info to get better
  425. # stack traces.
  426. add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER)
  427. if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
  428. NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
  429. add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY)
  430. endif()
  431. # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
  432. if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
  433. add_flag_if_supported("-O1" O1)
  434. endif()
  435. endmacro()
  436. # Turn on sanitizers if necessary.
  437. if(LLVM_USE_SANITIZER)
  438. if (LLVM_ON_UNIX)
  439. if (LLVM_USE_SANITIZER STREQUAL "Address")
  440. append_common_sanitizer_flags()
  441. append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  442. elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
  443. append_common_sanitizer_flags()
  444. append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  445. if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
  446. append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  447. endif()
  448. elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
  449. append_common_sanitizer_flags()
  450. append("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all"
  451. CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  452. elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
  453. append_common_sanitizer_flags()
  454. append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  455. elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
  456. LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
  457. append_common_sanitizer_flags()
  458. append("-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all"
  459. CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  460. else()
  461. message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
  462. endif()
  463. else()
  464. message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
  465. endif()
  466. if (LLVM_USE_SANITIZE_COVERAGE)
  467. append("-fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  468. endif()
  469. endif()
  470. # Turn on -gsplit-dwarf if requested
  471. if(LLVM_USE_SPLIT_DWARF)
  472. add_definitions("-gsplit-dwarf")
  473. endif()
  474. add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
  475. add_llvm_definitions( -D__STDC_FORMAT_MACROS )
  476. add_llvm_definitions( -D__STDC_LIMIT_MACROS )
  477. # clang doesn't print colored diagnostics when invoked from Ninja
  478. if (UNIX AND
  479. CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
  480. CMAKE_GENERATOR STREQUAL "Ninja")
  481. append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  482. endif()
  483. # HLSL Change Starts
  484. # Enable -fms-extensions for clang to use MS uuid extensions for COM.
  485. if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  486. append("-fms-extensions -Wno-language-extension-token" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  487. endif()
  488. # HLSL Change Ends
  489. # Add flags for add_dead_strip().
  490. # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
  491. # But MinSizeRel seems to add that automatically, so maybe disable these
  492. # flags instead if LLVM_NO_DEAD_STRIP is set.
  493. if(NOT CYGWIN AND NOT WIN32)
  494. if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND
  495. NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
  496. check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS)
  497. if (C_SUPPORTS_FNO_FUNCTION_SECTIONS)
  498. # Don't add -ffunction-section if it can be disabled with -fno-function-sections.
  499. # Doing so will break sanitizers.
  500. add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS)
  501. endif()
  502. add_flag_if_supported("-fdata-sections" FDATA_SECTIONS)
  503. endif()
  504. endif()
  505. if(CYGWIN OR MINGW)
  506. # Prune --out-implib from executables. It doesn't make sense even
  507. # with --export-all-symbols.
  508. string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " "
  509. CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
  510. string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " "
  511. CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
  512. endif()
  513. if(MSVC)
  514. # Remove flags here, for exceptions and RTTI.
  515. # Each target property or source property should be responsible to control
  516. # them.
  517. # CL.EXE complains to override flags like "/GR /GR-".
  518. string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  519. string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  520. endif()
  521. # Provide public options to globally control RTTI and EH
  522. option(LLVM_ENABLE_EH "Enable Exception handling" OFF)
  523. option(LLVM_ENABLE_RTTI "Enable run time type information" OFF)
  524. if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI)
  525. message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON")
  526. endif()
  527. # Plugin support
  528. # FIXME: Make this configurable.
  529. if(WIN32 OR CYGWIN)
  530. if(BUILD_SHARED_LIBS)
  531. set(LLVM_ENABLE_PLUGINS ON)
  532. else()
  533. set(LLVM_ENABLE_PLUGINS OFF)
  534. endif()
  535. else()
  536. set(LLVM_ENABLE_PLUGINS ON)
  537. endif()