diff options
Diffstat (limited to 'cmake/modules/HandleLLVMOptions.cmake')
-rw-r--r-- | cmake/modules/HandleLLVMOptions.cmake | 191 |
1 files changed, 107 insertions, 84 deletions
diff --git a/cmake/modules/HandleLLVMOptions.cmake b/cmake/modules/HandleLLVMOptions.cmake index 6db258ff66a15..22b4408b1ac0f 100644 --- a/cmake/modules/HandleLLVMOptions.cmake +++ b/cmake/modules/HandleLLVMOptions.cmake @@ -6,53 +6,19 @@ # else. string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) +include(CheckCompilerVersion) include(HandleLLVMStdlib) include(AddLLVMDefinitions) include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) -if(NOT LLVM_FORCE_USE_OLD_TOOLCHAIN) - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7) - message(FATAL_ERROR "Host GCC version must be at least 4.7!") - endif() - elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.1) - message(FATAL_ERROR "Host Clang version must be at least 3.1!") - endif() - if (CMAKE_CXX_SIMULATE_ID MATCHES "MSVC") - if (CMAKE_CXX_SIMULATE_VERSION VERSION_LESS 18.0) - message(FATAL_ERROR "Host Clang must have at least -fms-compatibility-version=18.0") - endif() - set(CLANG_CL 1) - elseif(NOT LLVM_ENABLE_LIBCXX) - # Otherwise, test that we aren't using too old of a version of libstdc++ - # with the Clang compiler. This is tricky as there is no real way to - # check the version of libstdc++ directly. Instead we test for a known - # bug in libstdc++4.6 that is fixed in libstdc++4.7. - set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) - set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) - set(CMAKE_REQUIRED_FLAGS "-std=c++0x") - check_cxx_source_compiles(" -#include <atomic> -std::atomic<float> x(0.0f); -int main() { return (float)x; }" - LLVM_NO_OLD_LIBSTDCXX) - if(NOT LLVM_NO_OLD_LIBSTDCXX) - message(FATAL_ERROR "Host Clang must be able to find libstdc++4.7 or newer!") - endif() - set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) - set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES}) - endif() - elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18.0) - message(FATAL_ERROR "Host Visual Studio must be at least 2013") - elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18.0.31101) - message(WARNING "Host Visual Studio should at least be 2013 Update 4 (MSVC 18.0.31101)" - " due to miscompiles from earlier versions") - endif() - endif() +if (CMAKE_LINKER MATCHES "lld-link.exe") + # Pass /MANIFEST:NO so that CMake doesn't run mt.exe on our binaries. Adding + # manifests with mt.exe breaks LLD's symbol tables and takes as much time as + # the link. See PR24476. + append("/MANIFEST:NO" + CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) endif() if( LLVM_ENABLE_ASSERTIONS ) @@ -78,6 +44,11 @@ if( LLVM_ENABLE_ASSERTIONS ) endif() endif() +if(LLVM_ENABLE_EXPENSIVE_CHECKS) + add_definitions(-DEXPENSIVE_CHECKS) + add_definitions(-D_GLIBCXX_DEBUG) +endif() + string(TOUPPER "${LLVM_ABI_BREAKING_CHECKS}" uppercase_LLVM_ABI_BREAKING_CHECKS) if( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "WITH_ASSERTS" ) @@ -195,7 +166,8 @@ if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 ) # TODO: support other platforms and toolchains. if( LLVM_BUILD_32_BITS ) message(STATUS "Building 32 bits executables and libraries.") - add_llvm_definitions( -m32 ) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32") set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32") @@ -255,14 +227,12 @@ if( MSVC ) include(ChooseMSVCCRT) - if( NOT (${CMAKE_VERSION} VERSION_LESS 2.8.11) ) - # set stack reserved size to ~10MB - # CMake previously automatically set this value for MSVC builds, but the - # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default - # value (1 MB) which is not enough for us in tasks such as parsing recursive - # C++ templates in Clang. - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000") - endif() + # set stack reserved size to ~10MB + # CMake previously automatically set this value for MSVC builds, but the + # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default + # value (1 MB) which is not enough for us in tasks such as parsing recursive + # C++ templates in Clang. + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000") if( MSVC11 ) add_llvm_definitions(-D_VARIADIC_MAX=10) @@ -279,6 +249,12 @@ if( MSVC ) -D_SCL_SECURE_NO_WARNINGS ) + # Tell MSVC to use the Unicode version of the Win32 APIs instead of ANSI. + add_llvm_definitions( + -DUNICODE + -D_UNICODE + ) + set(msvc_warning_flags # Disabled warnings. -wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline) @@ -320,6 +296,7 @@ if( MSVC ) # C4592 is disabled because of false positives in Visual Studio 2015 # Update 1. Re-evaluate the usefulness of this diagnostic with Update 2. -wd4592 # Suppress ''var': symbol will be dynamically initialized (implementation limitation) + -wd4319 # Suppress ''operator' : zero extending 'type' to 'type' of greater size' # Ideally, we'd like this warning to be enabled, but MSVC 2013 doesn't # support the 'aligned' attribute in the way that clang sources requires (for @@ -328,7 +305,7 @@ if( MSVC ) # When we switch to requiring a version of MSVC that supports the 'alignas' # specifier (MSVC 2015?) this warning can be re-enabled. -wd4324 # Suppress 'structure was padded due to __declspec(align())' - + # Promoted warnings. -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning. @@ -363,17 +340,32 @@ if( MSVC ) append("/Zc:inline" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) - if (NOT LLVM_ENABLE_TIMESTAMPS AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # /Zc:strictStrings is incompatible with VS12's (Visual Studio 2013's) + # debug mode headers. Instead of only enabling them in VS2013's debug mode, + # we'll just enable them for Visual Studio 2015 (VS 14, MSVC_VERSION 1900) + # and up. + if (NOT (MSVC_VERSION LESS 1900)) + # Disable string literal const->non-const type conversion. + # "When specified, the compiler requires strict const-qualification + # conformance for pointers initialized by using string literals." + append("/Zc:strictStrings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) + endif(NOT (MSVC_VERSION LESS 1900)) + + # "Generate Intrinsic Functions". + append("/Oi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) + + # "Enforce type conversion rules". + append("/Zc:rvalueCast" CMAKE_CXX_FLAGS) + + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # clang-cl and cl by default produce non-deterministic binaries because # link.exe /incremental requires a timestamp in the .obj file. clang-cl - # has the flag /Brepro to force deterministic binaries, so pass that when - # LLVM_ENABLE_TIMESTAMPS is turned off. + # has the flag /Brepro to force deterministic binaries. We want to pass that + # whenever you're building with clang unless you're passing /incremental. # This checks CMAKE_CXX_COMPILER_ID in addition to check_cxx_compiler_flag() # because cl.exe does not emit an error on flags it doesn't understand, # letting check_cxx_compiler_flag() claim it understands all flags. check_cxx_compiler_flag("/Brepro" SUPPORTS_BREPRO) - append_if(SUPPORTS_BREPRO "/Brepro" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) - if (SUPPORTS_BREPRO) # Check if /INCREMENTAL is passed to the linker and complain that it # won't work with /Brepro. @@ -381,14 +373,13 @@ if( MSVC ) string(TOUPPER "${CMAKE_MODULE_LINKER_FLAGS}" upper_module_flags) string(TOUPPER "${CMAKE_SHARED_LINKER_FLAGS}" upper_shared_flags) - string(FIND "${upper_exe_flags}" "/INCREMENTAL" exe_index) - string(FIND "${upper_module_flags}" "/INCREMENTAL" module_index) - string(FIND "${upper_shared_flags}" "/INCREMENTAL" shared_index) + string(FIND "${upper_exe_flags} ${upper_module_flags} ${upper_shared_flags}" + "/INCREMENTAL" linker_flag_idx) - if (${exe_index} GREATER -1 OR - ${module_index} GREATER -1 OR - ${shared_index} GREATER -1) - message(FATAL_ERROR "LLVM_ENABLE_TIMESTAMPS not compatible with /INCREMENTAL linking") + if (${linker_flag_idx} GREATER -1) + message(WARNING "/Brepro not compatible with /INCREMENTAL linking - builds will be non-deterministic") + else() + append("/Brepro" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) endif() endif() endif() @@ -454,9 +445,7 @@ elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE ) endif() endif (LLVM_ENABLE_WARNINGS) append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) - if (NOT LLVM_ENABLE_TIMESTAMPS) - add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME) - endif () + add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME) if (LLVM_ENABLE_CXX1Y) check_cxx_compiler_flag("-std=c++1y" CXX_SUPPORTS_CXX1Y) append_if(CXX_SUPPORTS_CXX1Y "-std=c++1y" CMAKE_CXX_FLAGS) @@ -476,7 +465,21 @@ elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE ) endif() if (LLVM_ENABLE_MODULES) set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fmodules") + set(module_flags "-fmodules -fmodules-cache-path=module.cache") + if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + # On Darwin -fmodules does not imply -fcxx-modules. + set(module_flags "${module_flags} -fcxx-modules") + endif() + if (LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY) + set(module_flags "${module_flags} -Xclang -fmodules-local-submodule-visibility") + endif() + if (LLVM_ENABLE_MODULE_DEBUGGING AND + ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR + (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))) + set(module_flags "${module_flags} -gmodules") + endif() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${module_flags}") + # Check that we can build code with modules enabled, and that repeatedly # including <cassert> still manages to respect NDEBUG properly. CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG @@ -487,8 +490,7 @@ elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE ) CXX_SUPPORTS_MODULES) set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) if (CXX_SUPPORTS_MODULES) - append_if(CXX_SUPPORTS_MODULES "-fmodules" CMAKE_C_FLAGS) - append_if(CXX_SUPPORTS_MODULES "-fmodules -fcxx-modules" CMAKE_CXX_FLAGS) + append("${module_flags}" CMAKE_CXX_FLAGS) else() message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler") endif() @@ -514,10 +516,6 @@ macro(append_common_sanitizer_flags) if (CMAKE_LINKER MATCHES "lld-link.exe") # Use DWARF debug info with LLD. append("-gdwarf" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) - # Pass /MANIFEST:NO so that CMake doesn't run mt.exe on our binaries. - # Adding manifests with mt.exe breaks LLD's symbol tables. See PR24476. - append("/MANIFEST:NO" - CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) else() # Enable codeview otherwise. append("/Z7" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) @@ -580,7 +578,7 @@ add_llvm_definitions( -D__STDC_LIMIT_MACROS ) # clang doesn't print colored diagnostics when invoked from Ninja if (UNIX AND - CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND + CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_GENERATOR STREQUAL "Ninja") append("-fcolor-diagnostics" CMAKE_C_FLAGS CMAKE_CXX_FLAGS) endif() @@ -602,15 +600,6 @@ if(NOT CYGWIN AND NOT WIN32) endif() endif() -if(CYGWIN OR MINGW) - # Prune --out-implib from executables. It doesn't make sense even - # with --export-all-symbols. - string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " " - CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}") - string(REGEX REPLACE "-Wl,--out-implib,[^ ]+ " " " - CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}") -endif() - if(MSVC) # Remove flags here, for exceptions and RTTI. # Each target property or source property should be responsible to control @@ -629,12 +618,46 @@ endif() option(LLVM_BUILD_INSTRUMENTED "Build LLVM and tools with PGO instrumentation (experimental)" Off) mark_as_advanced(LLVM_BUILD_INSTRUMENTED) -append_if(LLVM_BUILD_INSTRUMENTED "-fprofile-instr-generate" +append_if(LLVM_BUILD_INSTRUMENTED "-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}'" + CMAKE_CXX_FLAGS + CMAKE_C_FLAGS + CMAKE_EXE_LINKER_FLAGS + CMAKE_SHARED_LINKER_FLAGS) + +option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation (experimental)" Off) +mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE) +append_if(LLVM_BUILD_INSTRUMENTED_COVERAGE "-fprofile-instr-generate='${LLVM_PROFILE_FILE_PATTERN}' -fcoverage-mapping" CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) +set(LLVM_ENABLE_LTO OFF CACHE STRING "Build LLVM with LTO. May be specified as Thin or Full to use a particular kind of LTO") +string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO) +if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") + append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) +elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL") + append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) +elseif(LLVM_ENABLE_LTO) + append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS + CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS) +endif() + +# This option makes utils/extract_symbols.py be used to determine the list of +# symbols to export from LLVM tools. This is necessary when using MSVC if you +# want to allow plugins, though note that the plugin has to explicitly link +# against (exactly one) tool so we can't unilaterally turn on +# LLVM_ENABLE_PLUGINS when it's enabled. +option(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS "Export symbols from LLVM tools so that plugins can import them" OFF) +if(BUILD_SHARED_LIBS AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) + message(FATAL_ERROR "BUILD_SHARED_LIBS not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS") +endif() +if(LLVM_LINK_LLVM_DYLIB AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) + message(FATAL_ERROR "LLVM_LINK_LLVM_DYLIB not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS") +endif() + # Plugin support # FIXME: Make this configurable. if(WIN32 OR CYGWIN) |