Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  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.  
  5. # This is commonly needed so make sure it's defined before we include anything
  6. # else.
  7. string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
  8.  
  9. include(CheckCompilerVersion)
  10. include(CheckProblematicConfigurations)
  11. include(HandleLLVMStdlib)
  12. include(CheckCCompilerFlag)
  13. include(CheckCXXCompilerFlag)
  14. include(CheckSymbolExists)
  15. include(CMakeDependentOption)
  16. include(LLVMProcessSources)
  17.  
  18. if(CMAKE_LINKER MATCHES ".*lld" OR (LLVM_USE_LINKER STREQUAL "lld" OR LLVM_ENABLE_LLD))
  19.   set(LINKER_IS_LLD TRUE)
  20. else()
  21.   set(LINKER_IS_LLD FALSE)
  22. endif()
  23.  
  24. if(CMAKE_LINKER MATCHES "lld-link" OR (MSVC AND (LLVM_USE_LINKER STREQUAL "lld" OR LLVM_ENABLE_LLD)))
  25.   set(LINKER_IS_LLD_LINK TRUE)
  26. else()
  27.   set(LINKER_IS_LLD_LINK FALSE)
  28. endif()
  29.  
  30. 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")
  31. string(TOUPPER "${LLVM_ENABLE_LTO}" uppercase_LLVM_ENABLE_LTO)
  32.  
  33. # Ninja Job Pool support
  34. # The following only works with the Ninja generator in CMake >= 3.0.
  35. set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING
  36.   "Define the maximum number of concurrent compilation jobs (Ninja only).")
  37. if(LLVM_PARALLEL_COMPILE_JOBS)
  38.   if(NOT CMAKE_GENERATOR MATCHES "Ninja")
  39.     message(WARNING "Job pooling is only available with Ninja generators.")
  40.   else()
  41.     set_property(GLOBAL APPEND PROPERTY JOB_POOLS compile_job_pool=${LLVM_PARALLEL_COMPILE_JOBS})
  42.     set(CMAKE_JOB_POOL_COMPILE compile_job_pool)
  43.   endif()
  44. endif()
  45.  
  46. set(LLVM_PARALLEL_LINK_JOBS "" CACHE STRING
  47.   "Define the maximum number of concurrent link jobs (Ninja only).")
  48. if(CMAKE_GENERATOR MATCHES "Ninja")
  49.   if(NOT LLVM_PARALLEL_LINK_JOBS AND uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
  50.     message(STATUS "ThinLTO provides its own parallel linking - limiting parallel link jobs to 2.")
  51.     set(LLVM_PARALLEL_LINK_JOBS "2")
  52.   endif()
  53.   if(LLVM_PARALLEL_LINK_JOBS)
  54.     set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=${LLVM_PARALLEL_LINK_JOBS})
  55.     set(CMAKE_JOB_POOL_LINK link_job_pool)
  56.   endif()
  57. elseif(LLVM_PARALLEL_LINK_JOBS)
  58.   message(WARNING "Job pooling is only available with Ninja generators.")
  59. endif()
  60.  
  61. if( LLVM_ENABLE_ASSERTIONS )
  62.   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
  63.   if( NOT MSVC )
  64.     add_compile_definitions(_DEBUG)
  65.   endif()
  66.   # On non-Debug builds cmake automatically defines NDEBUG, so we
  67.   # explicitly undefine it:
  68.   if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
  69.     add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-UNDEBUG>)
  70.     if (MSVC)
  71.       # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
  72.       foreach (flags_var_to_scrub
  73.           CMAKE_CXX_FLAGS_RELEASE
  74.           CMAKE_CXX_FLAGS_RELWITHDEBINFO
  75.           CMAKE_CXX_FLAGS_MINSIZEREL
  76.           CMAKE_C_FLAGS_RELEASE
  77.           CMAKE_C_FLAGS_RELWITHDEBINFO
  78.           CMAKE_C_FLAGS_MINSIZEREL)
  79.         string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
  80.           "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
  81.       endforeach()
  82.     endif()
  83.   endif()
  84. endif()
  85.  
  86. if(LLVM_ENABLE_EXPENSIVE_CHECKS)
  87.   add_compile_definitions(EXPENSIVE_CHECKS)
  88.  
  89.   # In some libstdc++ versions, std::min_element is not constexpr when
  90.   # _GLIBCXX_DEBUG is enabled.
  91.   CHECK_CXX_SOURCE_COMPILES("
  92.    #define _GLIBCXX_DEBUG
  93.    #include <algorithm>
  94.    int main(int argc, char** argv) {
  95.      static constexpr int data[] = {0, 1};
  96.      constexpr const int* min_elt = std::min_element(&data[0], &data[2]);
  97.      return 0;
  98.    }" CXX_SUPPORTS_GLIBCXX_DEBUG)
  99.   if(CXX_SUPPORTS_GLIBCXX_DEBUG)
  100.     add_compile_definitions(_GLIBCXX_DEBUG)
  101.   else()
  102.     add_compile_definitions(_GLIBCXX_ASSERTIONS)
  103.   endif()
  104. endif()
  105.  
  106. if (LLVM_ENABLE_STRICT_FIXED_SIZE_VECTORS)
  107.   add_compile_definitions(STRICT_FIXED_SIZE_VECTORS)
  108. endif()
  109.  
  110. string(TOUPPER "${LLVM_ABI_BREAKING_CHECKS}" uppercase_LLVM_ABI_BREAKING_CHECKS)
  111.  
  112. if( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "WITH_ASSERTS" )
  113.   if( LLVM_ENABLE_ASSERTIONS )
  114.     set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
  115.   endif()
  116. elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_ON" )
  117.   set( LLVM_ENABLE_ABI_BREAKING_CHECKS 1 )
  118. elseif( uppercase_LLVM_ABI_BREAKING_CHECKS STREQUAL "FORCE_OFF" )
  119.   # We don't need to do anything special to turn off ABI breaking checks.
  120. elseif( NOT DEFINED LLVM_ABI_BREAKING_CHECKS )
  121.   # Treat LLVM_ABI_BREAKING_CHECKS like "FORCE_OFF" when it has not been
  122.   # defined.
  123. else()
  124.   message(FATAL_ERROR "Unknown value for LLVM_ABI_BREAKING_CHECKS: \"${LLVM_ABI_BREAKING_CHECKS}\"!")
  125. endif()
  126.  
  127. if( LLVM_REVERSE_ITERATION )
  128.   set( LLVM_ENABLE_REVERSE_ITERATION 1 )
  129. endif()
  130.  
  131. if(WIN32)
  132.   set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
  133.   if(CYGWIN)
  134.     set(LLVM_ON_WIN32 0)
  135.     set(LLVM_ON_UNIX 1)
  136.   else(CYGWIN)
  137.     set(LLVM_ON_WIN32 1)
  138.     set(LLVM_ON_UNIX 0)
  139.   endif(CYGWIN)
  140. else(WIN32)
  141.   if(FUCHSIA OR UNIX)
  142.     set(LLVM_ON_WIN32 0)
  143.     set(LLVM_ON_UNIX 1)
  144.     if(APPLE OR ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
  145.       set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
  146.     else()
  147.       set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
  148.     endif()
  149.   else(FUCHSIA OR UNIX)
  150.     MESSAGE(SEND_ERROR "Unable to determine platform")
  151.   endif(FUCHSIA OR UNIX)
  152. endif(WIN32)
  153.  
  154. if (CMAKE_SYSTEM_NAME MATCHES "OS390")
  155.   set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
  156. endif()
  157.  
  158. set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX})
  159. set(LTDL_SHLIB_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
  160.  
  161. # We use *.dylib rather than *.so on darwin, but we stick with *.so on AIX.
  162. if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
  163.   set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_MODULE_SUFFIX})
  164. else()
  165.   set(LLVM_PLUGIN_EXT ${CMAKE_SHARED_LIBRARY_SUFFIX})
  166. endif()
  167.  
  168. if(APPLE)
  169.   # Darwin-specific linker flags for loadable modules.
  170.   set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
  171. endif()
  172.  
  173. if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  174.   # RHEL7 has ar and ranlib being non-deterministic by default. The D flag forces determinism,
  175.   # however only GNU version of ar and ranlib (2.27) have this option.
  176.   # RHEL DTS7 is also affected by this, which uses GNU binutils 2.28
  177.   execute_process(COMMAND ${CMAKE_AR} rD t.a
  178.                   WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  179.                   RESULT_VARIABLE AR_RESULT
  180.                   OUTPUT_QUIET
  181.                   ERROR_QUIET
  182.                   )
  183.   if(${AR_RESULT} EQUAL 0)
  184.     execute_process(COMMAND ${CMAKE_RANLIB} -D t.a
  185.                     WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  186.                     RESULT_VARIABLE RANLIB_RESULT
  187.                     OUTPUT_QUIET
  188.                     ERROR_QUIET
  189.                     )
  190.     if(${RANLIB_RESULT} EQUAL 0)
  191.       set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>"
  192.           CACHE STRING "archive create command")
  193.       set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq  <TARGET> <LINK_FLAGS> <OBJECTS>")
  194.       set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>" CACHE STRING "ranlib command")
  195.  
  196.       set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>"
  197.           CACHE STRING "archive create command")
  198.       set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> Dq  <TARGET> <LINK_FLAGS> <OBJECTS>")
  199.       set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>" CACHE STRING "ranlib command")
  200.     endif()
  201.     file(REMOVE ${CMAKE_BINARY_DIR}/t.a)
  202.   endif()
  203. endif()
  204.  
  205. if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
  206.   # -fPIC does not enable the large code model for GCC on AIX but does for XL.
  207.   if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
  208.     append("-mcmodel=large" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
  209.   elseif(CMAKE_CXX_COMPILER_ID MATCHES "XL")
  210.     # XL generates a small number of relocations not of the large model, -bbigtoc is needed.
  211.     append("-Wl,-bbigtoc"
  212.            CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  213.     # The default behaviour on AIX processes dynamic initialization of non-local variables with
  214.     # static storage duration even for archive members that are otherwise unreferenced.
  215.     # Since `--whole-archive` is not used by the LLVM build to keep such initializations for Linux,
  216.     # we can limit the processing for archive members to only those that are otherwise referenced.
  217.     append("-bcdtors:mbr"
  218.            CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  219.   endif()
  220.   if(BUILD_SHARED_LIBS)
  221.     # See rpath handling in AddLLVM.cmake
  222.     # FIXME: Remove this warning if this rpath is no longer hardcoded.
  223.     message(WARNING "Build and install environment path info may be exposed; binaries will also be unrelocatable.")
  224.   endif()
  225. endif()
  226.  
  227. # Pass -Wl,-z,defs. This makes sure all symbols are defined. Otherwise a DSO
  228. # build might work on ELF but fail on MachO/COFF.
  229. if(NOT (CMAKE_SYSTEM_NAME MATCHES "Darwin|FreeBSD|OpenBSD|DragonFly|AIX|OS390" OR
  230.         WIN32 OR CYGWIN) AND
  231.    NOT LLVM_USE_SANITIZER)
  232.   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,defs")
  233. endif()
  234.  
  235. # Pass -Wl,-z,nodelete. This makes sure our shared libraries are not unloaded
  236. # by dlclose(). We need that since the CLI API relies on cross-references
  237. # between global objects which became horribly broken when one of the libraries
  238. # is unloaded.
  239. if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  240.   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,nodelete")
  241.   set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-z,nodelete")
  242. endif()
  243.  
  244.  
  245. function(append value)
  246.   foreach(variable ${ARGN})
  247.     set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
  248.   endforeach(variable)
  249. endfunction()
  250.  
  251. function(append_if condition value)
  252.   if (${condition})
  253.     foreach(variable ${ARGN})
  254.       set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
  255.     endforeach(variable)
  256.   endif()
  257. endfunction()
  258.  
  259. macro(add_flag_if_supported flag name)
  260.   check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
  261.   append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS)
  262.   check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
  263.   append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS)
  264. endmacro()
  265.  
  266. function(add_flag_or_print_warning flag name)
  267.   check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
  268.   check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
  269.   if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name})
  270.     message(STATUS "Building with ${flag}")
  271.     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
  272.     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
  273.     set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${flag}" PARENT_SCOPE)
  274.   else()
  275.     message(WARNING "${flag} is not supported.")
  276.   endif()
  277. endfunction()
  278.  
  279. function(has_msvc_incremental_no_flag flags incr_no_flag_on)
  280.   set(${incr_no_flag_on} OFF PARENT_SCOPE)
  281.   string(FIND "${flags}" "/INCREMENTAL" idx REVERSE)
  282.   if (${idx} GREATER -1)
  283.     string(SUBSTRING "${flags}" ${idx} 15 no_flag)
  284.     if (${no_flag} MATCHES "/INCREMENTAL:NO")
  285.       set(${incr_no_flag_on} ON PARENT_SCOPE)
  286.     endif()
  287.   endif()
  288. endfunction()
  289.  
  290. if( LLVM_ENABLE_LLD )
  291.   if ( LLVM_USE_LINKER )
  292.     message(FATAL_ERROR "LLVM_ENABLE_LLD and LLVM_USE_LINKER can't be set at the same time")
  293.   endif()
  294.  
  295.   # In case of MSVC cmake always invokes the linker directly, so the linker
  296.   # should be specified by CMAKE_LINKER cmake variable instead of by -fuse-ld
  297.   # compiler option.
  298.   if ( MSVC )
  299.     if(NOT CMAKE_LINKER MATCHES "lld-link")
  300.       get_filename_component(CXX_COMPILER_DIR ${CMAKE_CXX_COMPILER} DIRECTORY)
  301.       get_filename_component(C_COMPILER_DIR ${CMAKE_C_COMPILER} DIRECTORY)
  302.       find_program(LLD_LINK NAMES "lld-link" "lld-link.exe" HINTS ${CXX_COMPILER_DIR} ${C_COMPILER_DIR} DOC "lld linker")
  303.       if(NOT LLD_LINK)
  304.         message(FATAL_ERROR
  305.           "LLVM_ENABLE_LLD set, but cannot find lld-link. "
  306.           "Consider setting CMAKE_LINKER to lld-link path.")
  307.       endif()
  308.       set(CMAKE_LINKER ${LLD_LINK})
  309.     endif()
  310.   else()
  311.     set(LLVM_USE_LINKER "lld")
  312.   endif()
  313. endif()
  314.  
  315. if( LLVM_USE_LINKER )
  316.   append("-fuse-ld=${LLVM_USE_LINKER}"
  317.     CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  318.   check_cxx_source_compiles("int main() { return 0; }" CXX_SUPPORTS_CUSTOM_LINKER)
  319.   if ( NOT CXX_SUPPORTS_CUSTOM_LINKER )
  320.     message(FATAL_ERROR "Host compiler does not support '-fuse-ld=${LLVM_USE_LINKER}'")
  321.   endif()
  322. endif()
  323.  
  324. if( LLVM_ENABLE_PIC )
  325.   if( XCODE )
  326.     # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
  327.     # know how to disable this, so just force ENABLE_PIC off for now.
  328.     message(WARNING "-fPIC not supported with Xcode.")
  329.   elseif( WIN32 OR CYGWIN)
  330.     # On Windows all code is PIC. MinGW warns if -fPIC is used.
  331.   else()
  332.     add_flag_or_print_warning("-fPIC" FPIC)
  333.     # Enable interprocedural optimizations for non-inline functions which would
  334.     # otherwise be disabled due to GCC -fPIC's default.
  335.     # Note: GCC<10.3 has a bug on SystemZ.
  336.     #
  337.     # Note: Clang allows IPO for -fPIC so this optimization is less effective.
  338.     # Clang 13 has a bug related to -fsanitize-coverage
  339.     # -fno-semantic-interposition (https://reviews.llvm.org/D117183).
  340.     if ((CMAKE_COMPILER_IS_GNUCXX AND
  341.          NOT (LLVM_NATIVE_ARCH STREQUAL "SystemZ" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.3))
  342.        OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL 14))
  343.       add_flag_if_supported("-fno-semantic-interposition" FNO_SEMANTIC_INTERPOSITION)
  344.     endif()
  345.   endif()
  346.   # GCC for MIPS can miscompile LLVM due to PR37701.
  347.   if(CMAKE_COMPILER_IS_GNUCXX AND LLVM_NATIVE_ARCH STREQUAL "Mips" AND
  348.          NOT Uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
  349.     add_flag_or_print_warning("-fno-shrink-wrap" FNO_SHRINK_WRAP)
  350.   endif()
  351.   # gcc with -O3 -fPIC generates TLS sequences that violate the spec on
  352.   # Solaris/sparcv9, causing executables created with the system linker
  353.   # to SEGV (GCC PR target/96607).
  354.   # clang with -O3 -fPIC generates code that SEGVs.
  355.   # Both can be worked around by compiling with -O instead.
  356.   if(${CMAKE_SYSTEM_NAME} STREQUAL "SunOS" AND LLVM_NATIVE_ARCH STREQUAL "Sparc")
  357.     llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O[23]" "-O")
  358.     llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O[23]" "-O")
  359.   endif()
  360. endif()
  361.  
  362. if((NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX")) AND
  363.    (NOT (WIN32 OR CYGWIN) OR (MINGW AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")))
  364.   # GCC for MinGW does nothing about -fvisibility-inlines-hidden, but warns
  365.   # about use of the attributes. As long as we don't use the attributes (to
  366.   # override the default) we shouldn't set the command line options either.
  367.   # GCC on AIX warns if -fvisibility-inlines-hidden is used and Clang on AIX doesn't currently support visibility.
  368.   check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
  369.   append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS)
  370. endif()
  371.  
  372. if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND MINGW)
  373.   add_compile_definitions(_FILE_OFFSET_BITS=64)
  374. endif()
  375.  
  376. if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
  377.   # TODO: support other platforms and toolchains.
  378.   if( LLVM_BUILD_32_BITS )
  379.     message(STATUS "Building 32 bits executables and libraries.")
  380.     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
  381.     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
  382.     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
  383.     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32")
  384.     set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -m32")
  385.  
  386.     # FIXME: CMAKE_SIZEOF_VOID_P is still 8
  387.     add_compile_definitions(_LARGEFILE_SOURCE)
  388.     add_compile_definitions(_FILE_OFFSET_BITS=64)
  389.   endif( LLVM_BUILD_32_BITS )
  390. endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
  391.  
  392. # If building on a GNU specific 32-bit system, make sure off_t is 64 bits
  393. # so that off_t can stored offset > 2GB.
  394. # Android until version N (API 24) doesn't support it.
  395. if (ANDROID AND (ANDROID_NATIVE_API_LEVEL LESS 24))
  396.   set(LLVM_FORCE_SMALLFILE_FOR_ANDROID TRUE)
  397. endif()
  398. if( CMAKE_SIZEOF_VOID_P EQUAL 4 AND NOT LLVM_FORCE_SMALLFILE_FOR_ANDROID)
  399.   # FIXME: It isn't handled in LLVM_BUILD_32_BITS.
  400.   add_compile_definitions(_LARGEFILE_SOURCE)
  401.   add_compile_definitions(_FILE_OFFSET_BITS=64)
  402. endif()
  403.  
  404. if( XCODE )
  405.   # For Xcode enable several build settings that correspond to
  406.   # many warnings that are on by default in Clang but are
  407.   # not enabled for historical reasons.  For versions of Xcode
  408.   # that do not support these options they will simply
  409.   # be ignored.
  410.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_RETURN_TYPE "YES")
  411.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_ABOUT_MISSING_NEWLINE "YES")
  412.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VALUE "YES")
  413.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_VARIABLE "YES")
  414.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_SIGN_COMPARE "YES")
  415.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNUSED_FUNCTION "YES")
  416.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED "YES")
  417.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS "YES")
  418.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_UNINITIALIZED_AUTOS "YES")
  419.   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_BOOL_CONVERSION "YES")
  420.   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_EMPTY_BODY "YES")
  421.   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_ENUM_CONVERSION "YES")
  422.   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_INT_CONVERSION "YES")
  423.   set(CMAKE_XCODE_ATTRIBUTE_CLANG_WARN_CONSTANT_CONVERSION "YES")
  424.   set(CMAKE_XCODE_ATTRIBUTE_GCC_WARN_NON_VIRTUAL_DESTRUCTOR "YES")
  425. endif()
  426.  
  427. # On Win32 using MS tools, provide an option to set the number of parallel jobs
  428. # to use.
  429. if( MSVC_IDE )
  430.   set(LLVM_COMPILER_JOBS "0" CACHE STRING
  431.     "Number of parallel compiler jobs. 0 means use all processors. Default is 0.")
  432.   if( NOT LLVM_COMPILER_JOBS STREQUAL "1" )
  433.     if( LLVM_COMPILER_JOBS STREQUAL "0" )
  434.       add_compile_options(/MP)
  435.     else()
  436.       message(STATUS "Number of parallel compiler jobs set to " ${LLVM_COMPILER_JOBS})
  437.       add_compile_options(/MP${LLVM_COMPILER_JOBS})
  438.     endif()
  439.   else()
  440.     message(STATUS "Parallel compilation disabled")
  441.   endif()
  442. endif()
  443.  
  444. # set stack reserved size to ~10MB
  445. if(MSVC)
  446.   # CMake previously automatically set this value for MSVC builds, but the
  447.   # behavior was changed in CMake 2.8.11 (Issue 12437) to use the MSVC default
  448.   # value (1 MB) which is not enough for us in tasks such as parsing recursive
  449.   # C++ templates in Clang.
  450.   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000")
  451. elseif(MINGW) # FIXME: Also cygwin?
  452.   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,16777216")
  453.  
  454.   # Pass -mbig-obj to mingw gas to avoid COFF 2**16 section limit.
  455.   if (NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  456.     append("-Wa,-mbig-obj" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  457.   endif()
  458. endif()
  459.  
  460. option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
  461.  
  462. if( MSVC )
  463.   include(ChooseMSVCCRT)
  464.  
  465.   # Add definitions that make MSVC much less annoying.
  466.   add_compile_definitions(
  467.     # For some reason MS wants to deprecate a bunch of standard functions...
  468.     _CRT_SECURE_NO_DEPRECATE
  469.     _CRT_SECURE_NO_WARNINGS
  470.     _CRT_NONSTDC_NO_DEPRECATE
  471.     _CRT_NONSTDC_NO_WARNINGS
  472.     _SCL_SECURE_NO_DEPRECATE
  473.     _SCL_SECURE_NO_WARNINGS
  474.     )
  475.  
  476.   # Tell MSVC to use the Unicode version of the Win32 APIs instead of ANSI.
  477.   add_compile_definitions(
  478.     UNICODE
  479.     _UNICODE
  480.   )
  481.  
  482.   if (LLVM_WINSYSROOT)
  483.     if (NOT CLANG_CL)
  484.       message(ERROR "LLVM_WINSYSROOT requires clang-cl")
  485.     endif()
  486.     append("/winsysroot${LLVM_WINSYSROOT}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  487.   endif()
  488.  
  489.   if (LLVM_ENABLE_WERROR)
  490.     append("/WX" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  491.   endif (LLVM_ENABLE_WERROR)
  492.  
  493.   append("/Zc:inline" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  494.  
  495.   # Some projects use the __cplusplus preprocessor macro to check support for
  496.   # a particular version of the C++ standard. When this option is not specified
  497.   # explicitly, macro's value is "199711L" that implies C++98 Standard.
  498.   # https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
  499.   append("/Zc:__cplusplus" CMAKE_CXX_FLAGS)
  500.  
  501.   # Allow users to request PDBs in release mode. CMake offeres the
  502.   # RelWithDebInfo configuration, but it uses different optimization settings
  503.   # (/Ob1 vs /Ob2 or -O2 vs -O3). LLVM provides this flag so that users can get
  504.   # PDBs without changing codegen.
  505.   option(LLVM_ENABLE_PDB OFF)
  506.   if (LLVM_ENABLE_PDB AND uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
  507.     append("/Zi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  508.     # /DEBUG disables linker GC and ICF, but we want those in Release mode.
  509.     append("/DEBUG /OPT:REF /OPT:ICF"
  510.           CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS
  511.           CMAKE_SHARED_LINKER_FLAGS)
  512.   endif()
  513.  
  514.   # Get all linker flags in upper case form so we can search them.
  515.   string(CONCAT all_linker_flags_uppercase
  516.      ${CMAKE_EXE_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} " "
  517.      ${CMAKE_EXE_LINKER_FLAGS} " "
  518.      ${CMAKE_MODULE_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} " "
  519.      ${CMAKE_MODULE_LINKER_FLAGS} " "
  520.      ${CMAKE_SHARED_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} " "
  521.      ${CMAKE_SHARED_LINKER_FLAGS})
  522.   string(TOUPPER "${all_linker_flags_uppercase}" all_linker_flags_uppercase)
  523.  
  524.   if (CLANG_CL AND LINKER_IS_LLD)
  525.     # If we are using clang-cl with lld-link and /debug is present in any of the
  526.     # linker flag variables, pass -gcodeview-ghash to the compiler to speed up
  527.     # linking. This flag is orthogonal from /Zi, /Z7, and other flags that
  528.     # enable debug info emission, and only has an effect if those are also in
  529.     # use.
  530.     string(FIND "${all_linker_flags_uppercase}" "/DEBUG" linker_flag_idx)
  531.     if (${linker_flag_idx} GREATER -1)
  532.       add_flag_if_supported("-gcodeview-ghash" GCODEVIEW_GHASH)
  533.     endif()
  534.   endif()
  535.  
  536.   # "Generate Intrinsic Functions".
  537.   append("/Oi" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  538.  
  539.   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT LLVM_ENABLE_LTO)
  540.     # clang-cl and cl by default produce non-deterministic binaries because
  541.     # link.exe /incremental requires a timestamp in the .obj file.  clang-cl
  542.     # has the flag /Brepro to force deterministic binaries. We want to pass that
  543.     # whenever you're building with clang unless you're passing /incremental
  544.     # or using LTO (/Brepro with LTO would result in a warning about the flag
  545.     # being unused, because we're not generating object files).
  546.     # This checks CMAKE_CXX_COMPILER_ID in addition to check_cxx_compiler_flag()
  547.     # because cl.exe does not emit an error on flags it doesn't understand,
  548.     # letting check_cxx_compiler_flag() claim it understands all flags.
  549.     check_cxx_compiler_flag("/Brepro" SUPPORTS_BREPRO)
  550.     if (SUPPORTS_BREPRO)
  551.       # Check if /INCREMENTAL is passed to the linker and complain that it
  552.       # won't work with /Brepro.
  553.       has_msvc_incremental_no_flag("${CMAKE_EXE_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${CMAKE_EXE_LINKER_FLAGS}" NO_INCR_EXE)
  554.       has_msvc_incremental_no_flag("${CMAKE_MODULE_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${CMAKE_MODULE_LINKER_FLAGS}" NO_INCR_MODULE)
  555.       has_msvc_incremental_no_flag("${CMAKE_SHARED_LINKER_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${CMAKE_SHARED_LINKER_FLAGS}" NO_INCR_SHARED)
  556.       if (NO_INCR_EXE AND NO_INCR_MODULE AND NO_INCR_SHARED)
  557.         append("/Brepro" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  558.       else()
  559.         message(WARNING "/Brepro not compatible with /INCREMENTAL linking - builds will be non-deterministic")
  560.       endif()
  561.     endif()
  562.   endif()
  563.   # By default MSVC has a 2^16 limit on the number of sections in an object file,
  564.   # but in many objects files need more than that. This flag is to increase the
  565.   # number of sections.
  566.   append("/bigobj" CMAKE_CXX_FLAGS)
  567.  
  568.   # Enable standards conformance mode.
  569.   # This ensures handling of various C/C++ constructs is more similar to other compilers.
  570.   append("/permissive-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  571. endif( MSVC )
  572.  
  573. # Warnings-as-errors handling for GCC-compatible compilers:
  574. if ( LLVM_COMPILER_IS_GCC_COMPATIBLE )
  575.   append_if(LLVM_ENABLE_WERROR "-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  576.   append_if(LLVM_ENABLE_WERROR "-Wno-error" CMAKE_REQUIRED_FLAGS)
  577. endif( LLVM_COMPILER_IS_GCC_COMPATIBLE )
  578.  
  579. # Specific default warnings-as-errors for compilers accepting GCC-compatible warning flags:
  580. if ( LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL" )
  581.   add_flag_if_supported("-Werror=date-time" WERROR_DATE_TIME)
  582.   add_flag_if_supported("-Werror=unguarded-availability-new" WERROR_UNGUARDED_AVAILABILITY_NEW)
  583. endif( LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL" )
  584.  
  585. # Modules enablement for GCC-compatible compilers:
  586. if ( LLVM_COMPILER_IS_GCC_COMPATIBLE AND LLVM_ENABLE_MODULES )
  587.   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  588.   set(module_flags "-fmodules -fmodules-cache-path=${PROJECT_BINARY_DIR}/module.cache")
  589.   if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  590.     # On Darwin -fmodules does not imply -fcxx-modules.
  591.     set(module_flags "${module_flags} -fcxx-modules")
  592.   endif()
  593.   if (LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY)
  594.     set(module_flags "${module_flags} -Xclang -fmodules-local-submodule-visibility")
  595.   endif()
  596.   if (LLVM_ENABLE_MODULE_DEBUGGING AND
  597.       ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR
  598.        (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")))
  599.     set(module_flags "${module_flags} -gmodules")
  600.   endif()
  601.   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${module_flags}")
  602.  
  603.   # Check that we can build code with modules enabled, and that repeatedly
  604.   # including <cassert> still manages to respect NDEBUG properly.
  605.   CHECK_CXX_SOURCE_COMPILES("#undef NDEBUG
  606.                             #include <cassert>
  607.                             #define NDEBUG
  608.                             #include <cassert>
  609.                             int main() { assert(this code is not compiled); }"
  610.                              CXX_SUPPORTS_MODULES)
  611.   set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  612.   if (CXX_SUPPORTS_MODULES)
  613.     append("${module_flags}" CMAKE_CXX_FLAGS)
  614.   else()
  615.     message(FATAL_ERROR "LLVM_ENABLE_MODULES is not supported by this compiler")
  616.   endif()
  617. endif( LLVM_COMPILER_IS_GCC_COMPATIBLE AND LLVM_ENABLE_MODULES )
  618.  
  619. if (MSVC)
  620.   if (NOT CLANG_CL)
  621.     set(msvc_warning_flags
  622.       # Disabled warnings.
  623.       -wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline)
  624.       -wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
  625.       -wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
  626.       -wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
  627.       -wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
  628.       -wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
  629.       -wd4456 # Suppress 'declaration of 'var' hides local variable'
  630.       -wd4457 # Suppress 'declaration of 'var' hides function parameter'
  631.       -wd4458 # Suppress 'declaration of 'var' hides class member'
  632.       -wd4459 # Suppress 'declaration of 'var' hides global declaration'
  633.       -wd4503 # Suppress ''identifier' : decorated name length exceeded, name was truncated'
  634.       -wd4624 # Suppress ''derived class' : destructor could not be generated because a base class destructor is inaccessible'
  635.       -wd4722 # Suppress 'function' : destructor never returns, potential memory leak
  636.       -wd4100 # Suppress 'unreferenced formal parameter'
  637.       -wd4127 # Suppress 'conditional expression is constant'
  638.       -wd4512 # Suppress 'assignment operator could not be generated'
  639.       -wd4505 # Suppress 'unreferenced local function has been removed'
  640.       -wd4610 # Suppress '<class> can never be instantiated'
  641.       -wd4510 # Suppress 'default constructor could not be generated'
  642.       -wd4702 # Suppress 'unreachable code'
  643.       -wd4245 # Suppress ''conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch'
  644.       -wd4706 # Suppress 'assignment within conditional expression'
  645.       -wd4310 # Suppress 'cast truncates constant value'
  646.       -wd4701 # Suppress 'potentially uninitialized local variable'
  647.       -wd4703 # Suppress 'potentially uninitialized local pointer variable'
  648.       -wd4389 # Suppress 'signed/unsigned mismatch'
  649.       -wd4611 # Suppress 'interaction between '_setjmp' and C++ object destruction is non-portable'
  650.       -wd4805 # Suppress 'unsafe mix of type <type> and type <type> in operation'
  651.       -wd4204 # Suppress 'nonstandard extension used : non-constant aggregate initializer'
  652.       -wd4577 # Suppress 'noexcept used with no exception handling mode specified; termination on exception is not guaranteed'
  653.       -wd4091 # Suppress 'typedef: ignored on left of '' when no variable is declared'
  654.           # C4592 is disabled because of false positives in Visual Studio 2015
  655.           # Update 1. Re-evaluate the usefulness of this diagnostic with Update 2.
  656.       -wd4592 # Suppress ''var': symbol will be dynamically initialized (implementation limitation)
  657.       -wd4319 # Suppress ''operator' : zero extending 'type' to 'type' of greater size'
  658.           # C4709 is disabled because of a bug with Visual Studio 2017 as of
  659.           # v15.8.8. Re-evaluate the usefulness of this diagnostic when the bug
  660.           # is fixed.
  661.       -wd4709 # Suppress comma operator within array index expression
  662.  
  663.       # Ideally, we'd like this warning to be enabled, but even MSVC 2019 doesn't
  664.       # support the 'aligned' attribute in the way that clang sources requires (for
  665.       # any code that uses the LLVM_ALIGNAS macro), so this is must be disabled to
  666.       # avoid unwanted alignment warnings.
  667.       -wd4324 # Suppress 'structure was padded due to __declspec(align())'
  668.  
  669.       # Promoted warnings.
  670.       -w14062 # Promote 'enumerator in switch of enum is not handled' to level 1 warning.
  671.  
  672.       # Promoted warnings to errors.
  673.       -we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
  674.       )
  675.   endif(NOT CLANG_CL)
  676.  
  677.   # Enable warnings
  678.   if (LLVM_ENABLE_WARNINGS)
  679.     # Put /W4 in front of all the -we flags. cl.exe doesn't care, but for
  680.     # clang-cl having /W4 after the -we flags will re-enable the warnings
  681.     # disabled by -we.
  682.     set(msvc_warning_flags "/W4 ${msvc_warning_flags}")
  683.     # CMake appends /W3 by default, and having /W3 followed by /W4 will result in
  684.     # cl : Command line warning D9025 : overriding '/W3' with '/W4'.  Since this is
  685.     # a command line warning and not a compiler warning, it cannot be suppressed except
  686.     # by fixing the command line.
  687.     string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  688.     string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  689.  
  690.     if (LLVM_ENABLE_PEDANTIC)
  691.       # No MSVC equivalent available
  692.     endif (LLVM_ENABLE_PEDANTIC)
  693.   endif (LLVM_ENABLE_WARNINGS)
  694.  
  695.   foreach(flag ${msvc_warning_flags})
  696.     append("${flag}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  697.   endforeach(flag)
  698. endif (MSVC)
  699.  
  700. if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
  701.  
  702.   # Don't add -Wall for clang-cl, because it maps -Wall to -Weverything for
  703.   # MSVC compatibility.  /W4 is added above instead.
  704.   if (NOT CLANG_CL)
  705.     append("-Wall" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  706.   endif()
  707.  
  708.   append("-Wextra -Wno-unused-parameter -Wwrite-strings" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  709.   append("-Wcast-qual" CMAKE_CXX_FLAGS)
  710.  
  711.   # Turn off missing field initializer warnings for gcc to avoid noise from
  712.   # false positives with empty {}. Turn them on otherwise (they're off by
  713.   # default for clang).
  714.   check_cxx_compiler_flag("-Wmissing-field-initializers" CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
  715.   if (CXX_SUPPORTS_MISSING_FIELD_INITIALIZERS_FLAG)
  716.     if (CMAKE_COMPILER_IS_GNUCXX)
  717.       append("-Wno-missing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  718.     else()
  719.       append("-Wmissing-field-initializers" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  720.     endif()
  721.   endif()
  722.  
  723.   if (LLVM_ENABLE_PEDANTIC AND LLVM_COMPILER_IS_GCC_COMPATIBLE)
  724.     append("-pedantic" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  725.     append("-Wno-long-long" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  726.  
  727.     # GCC warns about redundant toplevel semicolons (enabled by -pedantic
  728.     # above), while Clang doesn't. Enable the corresponding Clang option to
  729.     # pick up on these even in builds with Clang.
  730.     add_flag_if_supported("-Wc++98-compat-extra-semi" CXX98_COMPAT_EXTRA_SEMI_FLAG)
  731.   endif()
  732.  
  733.   add_flag_if_supported("-Wimplicit-fallthrough" IMPLICIT_FALLTHROUGH_FLAG)
  734.   add_flag_if_supported("-Wcovered-switch-default" COVERED_SWITCH_DEFAULT_FLAG)
  735.   append_if(USE_NO_UNINITIALIZED "-Wno-uninitialized" CMAKE_CXX_FLAGS)
  736.   append_if(USE_NO_MAYBE_UNINITIALIZED "-Wno-maybe-uninitialized" CMAKE_CXX_FLAGS)
  737.  
  738.   # Disable -Wclass-memaccess, a C++-only warning from GCC 8 that fires on
  739.   # LLVM's ADT classes.
  740.   check_cxx_compiler_flag("-Wclass-memaccess" CXX_SUPPORTS_CLASS_MEMACCESS_FLAG)
  741.   append_if(CXX_SUPPORTS_CLASS_MEMACCESS_FLAG "-Wno-class-memaccess" CMAKE_CXX_FLAGS)
  742.  
  743.   # Disable -Wredundant-move and -Wpessimizing-move on GCC>=9. GCC wants to
  744.   # remove std::move in code like "A foo(ConvertibleToA a) {
  745.   # return std::move(a); }", but this code does not compile (or uses the copy
  746.   # constructor instead) on clang<=3.8. Clang also has a -Wredundant-move and
  747.   # -Wpessimizing-move, but they only fire when the types match exactly, so we
  748.   # can keep them here.
  749.   if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  750.     check_cxx_compiler_flag("-Wredundant-move" CXX_SUPPORTS_REDUNDANT_MOVE_FLAG)
  751.     append_if(CXX_SUPPORTS_REDUNDANT_MOVE_FLAG "-Wno-redundant-move" CMAKE_CXX_FLAGS)
  752.     check_cxx_compiler_flag("-Wpessimizing-move" CXX_SUPPORTS_PESSIMIZING_MOVE_FLAG)
  753.     append_if(CXX_SUPPORTS_PESSIMIZING_MOVE_FLAG "-Wno-pessimizing-move" CMAKE_CXX_FLAGS)
  754.   endif()
  755.  
  756.   # The LLVM libraries have no stable C++ API, so -Wnoexcept-type is not useful.
  757.   check_cxx_compiler_flag("-Wnoexcept-type" CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG)
  758.   append_if(CXX_SUPPORTS_NOEXCEPT_TYPE_FLAG "-Wno-noexcept-type" CMAKE_CXX_FLAGS)
  759.  
  760.   # Check if -Wnon-virtual-dtor warns for a class marked final, when it has a
  761.   # friend declaration. If it does, don't add -Wnon-virtual-dtor. The case is
  762.   # considered unhelpful (https://gcc.gnu.org/PR102168).
  763.   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  764.   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=non-virtual-dtor")
  765.   CHECK_CXX_SOURCE_COMPILES("class f {};
  766.                             class base {friend f; public: virtual void anchor();protected: ~base();};
  767.                             int main() { return 0; }"
  768.                             CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR)
  769.   set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  770.   append_if(CXX_WONT_WARN_ON_FINAL_NONVIRTUALDTOR "-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
  771.  
  772.   append("-Wdelete-non-virtual-dtor" CMAKE_CXX_FLAGS)
  773.  
  774.   # Enable -Wsuggest-override if it's available, and only if it doesn't
  775.   # suggest adding 'override' to functions that are already marked 'final'
  776.   # (which means it is disabled for GCC < 9.2).
  777.   check_cxx_compiler_flag("-Wsuggest-override" CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG)
  778.   if (CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG)
  779.     set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  780.     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=suggest-override")
  781.     CHECK_CXX_SOURCE_COMPILES("class base {public: virtual void anchor();};
  782.                               class derived : base {public: void anchor() final;};
  783.                               int main() { return 0; }"
  784.                               CXX_WSUGGEST_OVERRIDE_ALLOWS_ONLY_FINAL)
  785.     set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  786.     append_if(CXX_WSUGGEST_OVERRIDE_ALLOWS_ONLY_FINAL "-Wsuggest-override" CMAKE_CXX_FLAGS)
  787.   endif()
  788.  
  789.   # Check if -Wcomment is OK with an // comment ending with '\' if the next
  790.   # line is also a // comment.
  791.   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  792.   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror -Wcomment")
  793.   CHECK_C_SOURCE_COMPILES("// \\\\\\n//\\nint main(void) {return 0;}"
  794.                           C_WCOMMENT_ALLOWS_LINE_WRAP)
  795.   set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  796.   if (NOT C_WCOMMENT_ALLOWS_LINE_WRAP)
  797.     append("-Wno-comment" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  798.   endif()
  799.  
  800.   # Enable -Wstring-conversion to catch misuse of string literals.
  801.   add_flag_if_supported("-Wstring-conversion" STRING_CONVERSION_FLAG)
  802.  
  803.   if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  804.     # Disable the misleading indentation warning with GCC; GCC can
  805.     # produce noisy notes about this getting disabled in large files.
  806.     # See e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89549
  807.     check_cxx_compiler_flag("-Wmisleading-indentation" CXX_SUPPORTS_MISLEADING_INDENTATION_FLAG)
  808.     append_if(CXX_SUPPORTS_MISLEADING_INDENTATION_FLAG "-Wno-misleading-indentation" CMAKE_CXX_FLAGS)
  809.   else()
  810.     # Prevent bugs that can happen with llvm's brace style.
  811.     add_flag_if_supported("-Wmisleading-indentation" MISLEADING_INDENTATION_FLAG)
  812.   endif()
  813.  
  814.   # Enable -Wctad-maybe-unsupported to catch unintended use of CTAD.
  815.   add_flag_if_supported("-Wctad-maybe-unsupported" CTAD_MAYBE_UNSPPORTED_FLAG)
  816. endif (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
  817.  
  818. if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND NOT LLVM_ENABLE_WARNINGS)
  819.   append("-w" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  820. endif()
  821.  
  822. macro(append_common_sanitizer_flags)
  823.   if (NOT MSVC)
  824.     # Append -fno-omit-frame-pointer and turn on debug info to get better
  825.     # stack traces.
  826.     add_flag_if_supported("-fno-omit-frame-pointer" FNO_OMIT_FRAME_POINTER)
  827.     if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
  828.         NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
  829.       add_flag_if_supported("-gline-tables-only" GLINE_TABLES_ONLY)
  830.     endif()
  831.     # Use -O1 even in debug mode, otherwise sanitizers slowdown is too large.
  832.     if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND LLVM_OPTIMIZE_SANITIZED_BUILDS)
  833.       add_flag_if_supported("-O1" O1)
  834.     endif()
  835.   else()
  836.     # Always ask the linker to produce symbols with asan.
  837.     append("/Z7" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  838.     append("/debug" CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  839.     # Not compatible with /INCREMENTAL link.
  840.     foreach (flags_opt_to_scrub
  841.         CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  842.       string (REGEX REPLACE "(^| )/INCREMENTAL($| )" " /INCREMENTAL:NO "
  843.         "${flags_opt_to_scrub}" "${${flags_opt_to_scrub}}")
  844.     endforeach()
  845.     if (LLVM_HOST_TRIPLE MATCHES "i[2-6]86-.*")
  846.       # Keep frame pointers around.
  847.       append("/Oy-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  848.     endif()
  849.   endif()
  850. endmacro()
  851.  
  852. # Turn on sanitizers if necessary.
  853. if(LLVM_USE_SANITIZER)
  854.   if (LLVM_ON_UNIX)
  855.     if (LLVM_USE_SANITIZER STREQUAL "Address")
  856.       append_common_sanitizer_flags()
  857.       append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  858.     elseif (LLVM_USE_SANITIZER STREQUAL "HWAddress")
  859.       append_common_sanitizer_flags()
  860.       append("-fsanitize=hwaddress" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  861.     elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
  862.       append_common_sanitizer_flags()
  863.       append("-fsanitize=memory" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  864.       if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
  865.         append("-fsanitize-memory-track-origins" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  866.       endif()
  867.     elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
  868.       append_common_sanitizer_flags()
  869.       append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  870.     elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
  871.       append_common_sanitizer_flags()
  872.       append("-fsanitize=thread" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  873.     elseif (LLVM_USE_SANITIZER STREQUAL "DataFlow")
  874.       append("-fsanitize=dataflow" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  875.     elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
  876.             LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
  877.       append_common_sanitizer_flags()
  878.       append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  879.       append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  880.     elseif (LLVM_USE_SANITIZER STREQUAL "Leaks")
  881.       append_common_sanitizer_flags()
  882.       append("-fsanitize=leak" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  883.     else()
  884.       message(FATAL_ERROR "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
  885.     endif()
  886.   elseif(MINGW)
  887.     if (LLVM_USE_SANITIZER STREQUAL "Address")
  888.       append_common_sanitizer_flags()
  889.       append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  890.     elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
  891.       append_common_sanitizer_flags()
  892.       append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  893.     elseif (LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
  894.             LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
  895.       append_common_sanitizer_flags()
  896.       append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  897.       append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  898.     else()
  899.       message(FATAL_ERROR "This sanitizer not yet supported in a MinGW environment: ${LLVM_USE_SANITIZER}")
  900.     endif()
  901.   elseif(MSVC)
  902.     if (LLVM_USE_SANITIZER STREQUAL "Address")
  903.       append_common_sanitizer_flags()
  904.       append("/fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  905.       if (NOT CLANG_CL)
  906.         # Not compatible with /RTC flags.
  907.         foreach (flags_opt_to_scrub
  908.             CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE})
  909.           string (REGEX REPLACE "(^| )/RTC[1csu]*($| )" " "
  910.             "${flags_opt_to_scrub}" "${${flags_opt_to_scrub}}")
  911.         endforeach()
  912.       endif()
  913.       if (LINKER_IS_LLD_LINK)
  914.         if (LLVM_HOST_TRIPLE MATCHES "i[2-6]86-.*")
  915.           set(arch "i386")
  916.         else()
  917.           set(arch "x86_64")
  918.         endif()
  919.         if (${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$")
  920.           append("/wholearchive:clang_rt.asan-${arch}.lib /wholearchive:clang_rt.asan_cxx-${arch}.lib"
  921.             CMAKE_EXE_LINKER_FLAGS)
  922.           append("/wholearchive:clang_rt.asan_dll_thunk-${arch}.lib"
  923.             CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  924.         else()
  925.           append("clang_rt.asan_dynamic-${arch}.lib /wholearchive:clang_rt.asan_dynamic_runtime_thunk-${arch}.lib"
  926.             CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  927.         endif()
  928.       endif()
  929.     else()
  930.       message(FATAL_ERROR "This sanitizer not yet supported in the MSVC environment: ${LLVM_USE_SANITIZER}")
  931.     endif()
  932.   else()
  933.     message(FATAL_ERROR "LLVM_USE_SANITIZER is not supported on this platform.")
  934.   endif()
  935.   if (LLVM_USE_SANITIZE_COVERAGE)
  936.     append("-fsanitize=fuzzer-no-link" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  937.   endif()
  938.   if (LLVM_USE_SANITIZER MATCHES ".*Undefined.*")
  939.     set(IGNORELIST_FILE "${PROJECT_SOURCE_DIR}/utils/sanitizers/ubsan_ignorelist.txt")
  940.     if (EXISTS "${IGNORELIST_FILE}")
  941.       # Use this option name version since -fsanitize-ignorelist is only
  942.       # accepted with clang 13.0 or newer.
  943.       append("-fsanitize-blacklist=${IGNORELIST_FILE}"
  944.              CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  945.     endif()
  946.   endif()
  947. endif()
  948.  
  949. # Turn on -gsplit-dwarf if requested in debug builds.
  950. if (LLVM_USE_SPLIT_DWARF AND
  951.     ((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR
  952.      (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")))
  953.   # Limit to clang and gcc so far. Add compilers supporting this option.
  954.   if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
  955.       CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  956.     add_compile_options(-gsplit-dwarf)
  957.     include(LLVMCheckLinkerFlag)
  958.     llvm_check_linker_flag(CXX "-Wl,--gdb-index" LINKER_SUPPORTS_GDB_INDEX)
  959.     append_if(LINKER_SUPPORTS_GDB_INDEX "-Wl,--gdb-index"
  960.       CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  961.   endif()
  962. endif()
  963.  
  964. add_compile_definitions(__STDC_CONSTANT_MACROS)
  965. add_compile_definitions(__STDC_FORMAT_MACROS)
  966. add_compile_definitions(__STDC_LIMIT_MACROS)
  967.  
  968. # clang and gcc don't default-print colored diagnostics when invoked from Ninja.
  969. if (UNIX AND
  970.     CMAKE_GENERATOR MATCHES "Ninja" AND
  971.     (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
  972.      (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
  973.       NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9))))
  974.   append("-fdiagnostics-color" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  975. endif()
  976.  
  977. # lld doesn't print colored diagnostics when invoked from Ninja
  978. if (UNIX AND CMAKE_GENERATOR MATCHES "Ninja")
  979.   include(LLVMCheckLinkerFlag)
  980.   llvm_check_linker_flag(CXX "-Wl,--color-diagnostics" LINKER_SUPPORTS_COLOR_DIAGNOSTICS)
  981.   append_if(LINKER_SUPPORTS_COLOR_DIAGNOSTICS "-Wl,--color-diagnostics"
  982.     CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  983. endif()
  984.  
  985. # Add flags for add_dead_strip().
  986. # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
  987. # But MinSizeRel seems to add that automatically, so maybe disable these
  988. # flags instead if LLVM_NO_DEAD_STRIP is set.
  989. if(NOT CYGWIN AND NOT MSVC)
  990.   if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND
  991.      NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
  992.     check_c_compiler_flag("-Werror -fno-function-sections" C_SUPPORTS_FNO_FUNCTION_SECTIONS)
  993.     if (C_SUPPORTS_FNO_FUNCTION_SECTIONS)
  994.       # Don't add -ffunction-sections if it can't be disabled with -fno-function-sections.
  995.       # Doing so will break sanitizers.
  996.       add_flag_if_supported("-ffunction-sections" FFUNCTION_SECTIONS)
  997.     elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
  998.       append("-qfuncsect" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  999.     endif()
  1000.     add_flag_if_supported("-fdata-sections" FDATA_SECTIONS)
  1001.   endif()
  1002. elseif(MSVC)
  1003.   if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
  1004.     append("/Gw" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  1005.   endif()
  1006. endif()
  1007.  
  1008. if(MSVC)
  1009.   # Remove flags here, for exceptions and RTTI.
  1010.   # Each target property or source property should be responsible to control
  1011.   # them.
  1012.   # CL.EXE complains to override flags like "/GR /GR-".
  1013.   string(REGEX REPLACE "(^| ) */EH[-cs]+ *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  1014.   string(REGEX REPLACE "(^| ) */GR-? *( |$)" "\\1 \\2" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  1015. endif()
  1016.  
  1017. # Provide public options to globally control RTTI and EH
  1018. option(LLVM_ENABLE_EH "Enable Exception handling" OFF)
  1019. option(LLVM_ENABLE_RTTI "Enable run time type information" OFF)
  1020. if(LLVM_ENABLE_EH AND NOT LLVM_ENABLE_RTTI)
  1021.   message(FATAL_ERROR "Exception handling requires RTTI. You must set LLVM_ENABLE_RTTI to ON")
  1022. endif()
  1023.  
  1024. option(LLVM_ENABLE_IR_PGO "Build LLVM and tools with IR PGO instrumentation (deprecated)" Off)
  1025. mark_as_advanced(LLVM_ENABLE_IR_PGO)
  1026.  
  1027. set(LLVM_BUILD_INSTRUMENTED OFF CACHE STRING "Build LLVM and tools with PGO instrumentation. May be specified as IR or Frontend")
  1028. set(LLVM_VP_COUNTERS_PER_SITE "1.5" CACHE STRING "Value profile counters to use per site for IR PGO with Clang")
  1029. mark_as_advanced(LLVM_BUILD_INSTRUMENTED LLVM_VP_COUNTERS_PER_SITE)
  1030. string(TOUPPER "${LLVM_BUILD_INSTRUMENTED}" uppercase_LLVM_BUILD_INSTRUMENTED)
  1031.  
  1032. if (LLVM_BUILD_INSTRUMENTED)
  1033.   if (LLVM_ENABLE_IR_PGO OR uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "IR")
  1034.     append("-fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\""
  1035.       CMAKE_CXX_FLAGS
  1036.       CMAKE_C_FLAGS)
  1037.     if(NOT LINKER_IS_LLD_LINK)
  1038.       append("-fprofile-generate=\"${LLVM_PROFILE_DATA_DIR}\""
  1039.         CMAKE_EXE_LINKER_FLAGS
  1040.         CMAKE_SHARED_LINKER_FLAGS)
  1041.     endif()
  1042.     # Set this to avoid running out of the value profile node section
  1043.     # under clang in dynamic linking mode.
  1044.     if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND
  1045.         CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 11 AND
  1046.         LLVM_LINK_LLVM_DYLIB)
  1047.       append("-Xclang -mllvm -Xclang -vp-counters-per-site=${LLVM_VP_COUNTERS_PER_SITE}"
  1048.         CMAKE_CXX_FLAGS
  1049.         CMAKE_C_FLAGS)
  1050.     endif()
  1051.   elseif(uppercase_LLVM_BUILD_INSTRUMENTED STREQUAL "CSIR")
  1052.     append("-fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\""
  1053.       CMAKE_CXX_FLAGS
  1054.       CMAKE_C_FLAGS)
  1055.     if(NOT LINKER_IS_LLD_LINK)
  1056.       append("-fcs-profile-generate=\"${LLVM_CSPROFILE_DATA_DIR}\""
  1057.         CMAKE_EXE_LINKER_FLAGS
  1058.         CMAKE_SHARED_LINKER_FLAGS)
  1059.     endif()
  1060.   else()
  1061.     append("-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\""
  1062.       CMAKE_CXX_FLAGS
  1063.       CMAKE_C_FLAGS)
  1064.     if(NOT LINKER_IS_LLD_LINK)
  1065.       append("-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\""
  1066.         CMAKE_EXE_LINKER_FLAGS
  1067.         CMAKE_SHARED_LINKER_FLAGS)
  1068.     endif()
  1069.   endif()
  1070. endif()
  1071.  
  1072. # When using clang-cl with an instrumentation-based tool, add clang's library
  1073. # resource directory to the library search path. Because cmake invokes the
  1074. # linker directly, it isn't sufficient to pass -fsanitize=* to the linker.
  1075. if (CLANG_CL AND (LLVM_BUILD_INSTRUMENTED OR LLVM_USE_SANITIZER))
  1076.   execute_process(
  1077.     COMMAND ${CMAKE_CXX_COMPILER} /clang:-print-libgcc-file-name /clang:--rtlib=compiler-rt
  1078.     OUTPUT_VARIABLE clang_compiler_rt_file
  1079.     ERROR_VARIABLE clang_cl_stderr
  1080.     OUTPUT_STRIP_TRAILING_WHITESPACE
  1081.     ERROR_STRIP_TRAILING_WHITESPACE
  1082.     RESULT_VARIABLE clang_cl_exit_code)
  1083.   if (NOT "${clang_cl_exit_code}" STREQUAL "0")
  1084.     message(FATAL_ERROR
  1085.       "Unable to invoke clang-cl to find resource dir: ${clang_cl_stderr}")
  1086.   endif()
  1087.   file(TO_CMAKE_PATH "${clang_compiler_rt_file}" clang_compiler_rt_file)
  1088.   get_filename_component(clang_runtime_dir "${clang_compiler_rt_file}" DIRECTORY)
  1089.   append("/libpath:${clang_runtime_dir}"
  1090.     CMAKE_EXE_LINKER_FLAGS
  1091.     CMAKE_MODULE_LINKER_FLAGS
  1092.     CMAKE_SHARED_LINKER_FLAGS)
  1093. endif()
  1094.  
  1095. if(LLVM_PROFDATA_FILE AND EXISTS ${LLVM_PROFDATA_FILE})
  1096.   if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )
  1097.     append("-fprofile-instr-use=\"${LLVM_PROFDATA_FILE}\""
  1098.       CMAKE_CXX_FLAGS
  1099.       CMAKE_C_FLAGS)
  1100.     if(NOT LINKER_IS_LLD_LINK)
  1101.       append("-fprofile-instr-use=\"${LLVM_PROFDATA_FILE}\""
  1102.         CMAKE_EXE_LINKER_FLAGS
  1103.         CMAKE_SHARED_LINKER_FLAGS)
  1104.     endif()
  1105.   else()
  1106.     message(FATAL_ERROR "LLVM_PROFDATA_FILE can only be specified when compiling with clang")
  1107.   endif()
  1108. endif()
  1109.  
  1110. option(LLVM_BUILD_INSTRUMENTED_COVERAGE "Build LLVM and tools with Code Coverage instrumentation" Off)
  1111. mark_as_advanced(LLVM_BUILD_INSTRUMENTED_COVERAGE)
  1112. append_if(LLVM_BUILD_INSTRUMENTED_COVERAGE "-fprofile-instr-generate=\"${LLVM_PROFILE_FILE_PATTERN}\" -fcoverage-mapping"
  1113.   CMAKE_CXX_FLAGS
  1114.   CMAKE_C_FLAGS
  1115.   CMAKE_EXE_LINKER_FLAGS
  1116.   CMAKE_SHARED_LINKER_FLAGS)
  1117.  
  1118. if (LLVM_BUILD_INSTRUMENTED AND LLVM_BUILD_INSTRUMENTED_COVERAGE)
  1119.   message(FATAL_ERROR "LLVM_BUILD_INSTRUMENTED and LLVM_BUILD_INSTRUMENTED_COVERAGE cannot both be specified")
  1120. endif()
  1121.  
  1122. set(LLVM_THINLTO_CACHE_PATH "${PROJECT_BINARY_DIR}/lto.cache" CACHE STRING "Set ThinLTO cache path. This can be used when building LLVM from several different directiories.")
  1123.  
  1124. if(LLVM_ENABLE_LTO AND LLVM_ON_WIN32 AND NOT LINKER_IS_LLD_LINK AND NOT MINGW)
  1125.   message(FATAL_ERROR "When compiling for Windows, LLVM_ENABLE_LTO requires using lld as the linker (point CMAKE_LINKER at lld-link.exe)")
  1126. endif()
  1127. if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
  1128.   append("-flto=thin" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
  1129.   if(NOT LINKER_IS_LLD_LINK)
  1130.     append("-flto=thin" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  1131.   endif()
  1132.   # If the linker supports it, enable the lto cache. This improves initial build
  1133.   # time a little since we re-link a lot of the same objects, and significantly
  1134.   # improves incremental build time.
  1135.   # FIXME: We should move all this logic into the clang driver.
  1136.   if(APPLE)
  1137.     append("-Wl,-cache_path_lto,${LLVM_THINLTO_CACHE_PATH}"
  1138.            CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  1139.   elseif((UNIX OR MINGW) AND LLVM_USE_LINKER STREQUAL "lld")
  1140.     append("-Wl,--thinlto-cache-dir=${LLVM_THINLTO_CACHE_PATH}"
  1141.            CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  1142.   elseif(LLVM_USE_LINKER STREQUAL "gold")
  1143.     append("-Wl,--plugin-opt,cache-dir=${LLVM_THINLTO_CACHE_PATH}"
  1144.            CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  1145.   elseif(LINKER_IS_LLD_LINK)
  1146.     append("/lldltocache:${LLVM_THINLTO_CACHE_PATH}"
  1147.            CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  1148.   endif()
  1149. elseif(uppercase_LLVM_ENABLE_LTO STREQUAL "FULL")
  1150.   append("-flto=full" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
  1151.   if(NOT LINKER_IS_LLD_LINK)
  1152.     append("-flto=full" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  1153.   endif()
  1154. elseif(LLVM_ENABLE_LTO)
  1155.   append("-flto" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
  1156.   if(NOT LINKER_IS_LLD_LINK)
  1157.     append("-flto" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
  1158.   endif()
  1159. endif()
  1160.  
  1161. # Set an AIX default for LLVM_EXPORT_SYMBOLS_FOR_PLUGINS based on whether we are
  1162. # doing dynamic linking (see below).
  1163. set(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default OFF)
  1164. if (NOT (BUILD_SHARED_LIBS OR LLVM_LINK_LLVM_DYLIB))
  1165.   set(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default ON)
  1166. endif()
  1167.  
  1168. # This option makes utils/extract_symbols.py be used to determine the list of
  1169. # symbols to export from LLVM tools. This is necessary when on AIX or when using
  1170. # MSVC if you want to allow plugins. On AIX we don't show this option, and we
  1171. # enable it by default except when the LLVM libraries are set up for dynamic
  1172. # linking (due to incompatibility). With MSVC, note that the plugin has to
  1173. # explicitly link against (exactly one) tool so we can't unilaterally turn on
  1174. # LLVM_ENABLE_PLUGINS when it's enabled.
  1175. CMAKE_DEPENDENT_OPTION(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS
  1176.        "Export symbols from LLVM tools so that plugins can import them" OFF
  1177.        "NOT ${CMAKE_SYSTEM_NAME} MATCHES AIX" ${LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default})
  1178. if(BUILD_SHARED_LIBS AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
  1179.   message(FATAL_ERROR "BUILD_SHARED_LIBS not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS")
  1180. endif()
  1181. if(LLVM_LINK_LLVM_DYLIB AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)
  1182.   message(FATAL_ERROR "LLVM_LINK_LLVM_DYLIB not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS")
  1183. endif()
  1184.  
  1185. # By default we should enable LLVM_ENABLE_IDE only for multi-configuration
  1186. # generators. This option disables optional build system features that make IDEs
  1187. # less usable.
  1188. set(LLVM_ENABLE_IDE_default OFF)
  1189. if (CMAKE_CONFIGURATION_TYPES)
  1190.   set(LLVM_ENABLE_IDE_default ON)
  1191. endif()
  1192. option(LLVM_ENABLE_IDE
  1193.        "Disable optional build system features that cause problems for IDE generators"
  1194.        ${LLVM_ENABLE_IDE_default})
  1195. if (CMAKE_CONFIGURATION_TYPES AND NOT LLVM_ENABLE_IDE)
  1196.   message(WARNING "Disabling LLVM_ENABLE_IDE on multi-configuration generators is not recommended.")
  1197. endif()
  1198.  
  1199. function(get_compile_definitions)
  1200.   get_directory_property(top_dir_definitions DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS)
  1201.   foreach(definition ${top_dir_definitions})
  1202.     if(DEFINED result)
  1203.       string(APPEND result " -D${definition}")
  1204.     else()
  1205.       set(result "-D${definition}")
  1206.     endif()
  1207.   endforeach()
  1208.   set(LLVM_DEFINITIONS "${result}" PARENT_SCOPE)
  1209. endfunction()
  1210. get_compile_definitions()
  1211.  
  1212. option(LLVM_FORCE_ENABLE_STATS "Enable statistics collection for builds that wouldn't normally enable it" OFF)
  1213.  
  1214. check_symbol_exists(os_signpost_interval_begin "os/signpost.h" macos_signposts_available)
  1215. if(macos_signposts_available)
  1216.   check_cxx_source_compiles(
  1217.     "#include <os/signpost.h>
  1218.    int main() { os_signpost_interval_begin(nullptr, 0, \"\", \"\"); return 0; }"
  1219.     macos_signposts_usable)
  1220.   if(macos_signposts_usable)
  1221.     set(LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS "WITH_ASSERTS" CACHE STRING
  1222.         "Enable support for Xcode signposts. Can be WITH_ASSERTS, FORCE_ON, FORCE_OFF")
  1223.     string(TOUPPER "${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}"
  1224.                    uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS)
  1225.     if( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "WITH_ASSERTS" )
  1226.       if( LLVM_ENABLE_ASSERTIONS )
  1227.         set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 )
  1228.       endif()
  1229.     elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_ON" )
  1230.       set( LLVM_SUPPORT_XCODE_SIGNPOSTS 1 )
  1231.     elseif( uppercase_LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS STREQUAL "FORCE_OFF" )
  1232.       # We don't need to do anything special to turn off signposts.
  1233.     elseif( NOT DEFINED LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS )
  1234.       # Treat LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS like "FORCE_OFF" when it has not been
  1235.       # defined.
  1236.     else()
  1237.       message(FATAL_ERROR "Unknown value for LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS:"
  1238.                           " \"${LLVM_ENABLE_SUPPORT_XCODE_SIGNPOSTS}\"!")
  1239.     endif()
  1240.   endif()
  1241. endif()
  1242.  
  1243. set(LLVM_SOURCE_PREFIX "" CACHE STRING "Use prefix for sources")
  1244.  
  1245. option(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO "Use relative paths in debug info" OFF)
  1246.  
  1247. if(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO)
  1248.   check_c_compiler_flag("-fdebug-prefix-map=foo=bar" SUPPORTS_FDEBUG_PREFIX_MAP)
  1249.   if(LLVM_ENABLE_PROJECTS_USED)
  1250.     get_filename_component(source_root "${LLVM_MAIN_SRC_DIR}/.." ABSOLUTE)
  1251.   else()
  1252.     set(source_root "${LLVM_MAIN_SRC_DIR}")
  1253.   endif()
  1254.   file(RELATIVE_PATH relative_root "${source_root}" "${CMAKE_BINARY_DIR}")
  1255.   append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  1256.   append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  1257.   add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES)
  1258. endif()
  1259.  
  1260. option(LLVM_USE_RELATIVE_PATHS_IN_FILES "Use relative paths in sources and debug info" OFF)
  1261.  
  1262. if(LLVM_USE_RELATIVE_PATHS_IN_FILES)
  1263.   check_c_compiler_flag("-ffile-prefix-map=foo=bar" SUPPORTS_FFILE_PREFIX_MAP)
  1264.   if(LLVM_ENABLE_PROJECTS_USED)
  1265.     get_filename_component(source_root "${LLVM_MAIN_SRC_DIR}/.." ABSOLUTE)
  1266.   else()
  1267.     set(source_root "${LLVM_MAIN_SRC_DIR}")
  1268.   endif()
  1269.   file(RELATIVE_PATH relative_root "${source_root}" "${CMAKE_BINARY_DIR}")
  1270.   append_if(SUPPORTS_FFILE_PREFIX_MAP "-ffile-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  1271.   append_if(SUPPORTS_FFILE_PREFIX_MAP "-ffile-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  1272.   add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES)
  1273. endif()
  1274.  
  1275. set(LLVM_THIRD_PARTY_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/../third-party CACHE STRING
  1276.     "Directory containing third party software used by LLVM (e.g. googletest)")
  1277.