1 # CMake build for CompilerRT.
3 # An important constraint of the build is that it only produces libraries
4 # based on the ability of the host toolchain to target various platforms.
6 cmake_minimum_required(VERSION 3.20.0)
7 set(LLVM_SUBPROJECT_TITLE "Compiler-RT")
9 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
10 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
12 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
15 # Check if compiler-rt is built as a standalone project.
16 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR COMPILER_RT_STANDALONE_BUILD)
17 project(CompilerRT C CXX ASM)
18 set(COMPILER_RT_STANDALONE_BUILD TRUE)
19 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
22 # Add path for custom compiler-rt modules.
23 list(INSERT CMAKE_MODULE_PATH 0
24 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
25 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
26 "${LLVM_COMMON_CMAKE_UTILS}"
27 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
30 if(CMAKE_CONFIGURATION_TYPES)
31 set(CMAKE_CFG_RESOLVED_INTDIR "${CMAKE_CFG_INTDIR}/")
33 set(CMAKE_CFG_RESOLVED_INTDIR "")
36 include(SetPlatformToolchainTools)
37 include(base-config-ix)
38 include(CompilerRTUtils)
39 include(CMakeDependentOption)
40 include(GetDarwinLinkerVersion)
42 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
43 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
44 option(COMPILER_RT_DISABLE_AARCH64_FMV "Disable AArch64 Function Multi Versioning support" OFF)
45 mark_as_advanced(COMPILER_RT_DISABLE_AARCH64_FMV)
46 option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON)
47 mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS)
48 option(COMPILER_RT_BUILD_XRAY "Build xray" ON)
49 mark_as_advanced(COMPILER_RT_BUILD_XRAY)
50 option(COMPILER_RT_BUILD_LIBFUZZER "Build libFuzzer" ON)
51 mark_as_advanced(COMPILER_RT_BUILD_LIBFUZZER)
52 option(COMPILER_RT_BUILD_PROFILE "Build profile runtime" ON)
53 mark_as_advanced(COMPILER_RT_BUILD_PROFILE)
54 option(COMPILER_RT_BUILD_CTX_PROFILE "Build ctx profile runtime" ON)
55 mark_as_advanced(COMPILER_RT_BUILD_CTX_PROFILE)
56 option(COMPILER_RT_BUILD_MEMPROF "Build memory profiling runtime" ON)
57 mark_as_advanced(COMPILER_RT_BUILD_MEMPROF)
58 option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF)
59 mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT)
60 option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON)
61 mark_as_advanced(COMPILER_RT_BUILD_ORC)
62 option(COMPILER_RT_BUILD_GWP_ASAN "Build GWP-ASan, and link it into SCUDO" ON)
63 mark_as_advanced(COMPILER_RT_BUILD_GWP_ASAN)
64 option(COMPILER_RT_ENABLE_CET "Build Compiler RT with CET enabled" OFF)
66 option(COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH "Set custom sysroot for building SCUDO standalone" OFF)
67 mark_as_advanced(COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH)
68 option(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED "Build SCUDO standalone for shared libraries" ON)
69 mark_as_advanced(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED)
70 option(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC "Build SCUDO standalone with LLVM's libc headers" OFF)
71 mark_as_advanced(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC)
74 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT OFF)
76 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT ON)
78 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS ${COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT} CACHE BOOL "Enable libc interceptors in HWASan (testing mode)")
80 set(COMPILER_RT_BAREMETAL_BUILD OFF CACHE BOOL
81 "Build for a bare-metal target.")
83 if (COMPILER_RT_STANDALONE_BUILD)
84 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
85 set(CMAKE_CXX_STANDARD_REQUIRED YES)
86 set(CMAKE_CXX_EXTENSIONS NO)
88 if (NOT LLVM_RUNTIMES_BUILD)
91 if (TARGET intrinsics_gen)
92 # Loading the llvm config causes this target to be imported so place it
93 # under the appropriate folder in an IDE.
94 set_target_properties(intrinsics_gen PROPERTIES FOLDER "LLVM/Tablegenning")
97 find_package(Python3 COMPONENTS Interpreter)
98 if(NOT Python3_Interpreter_FOUND)
99 message(WARNING "Python3 not found, using python2 as a fallback")
100 find_package(Python2 COMPONENTS Interpreter REQUIRED)
101 if(Python2_VERSION VERSION_LESS 2.7)
102 message(SEND_ERROR "Python 2.7 or newer is required")
105 # Treat python2 as python3
106 add_executable(Python3::Interpreter IMPORTED)
107 set_target_properties(Python3::Interpreter PROPERTIES
108 IMPORTED_LOCATION ${Python2_EXECUTABLE})
109 set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
112 # Ensure that fat libraries are built correctly on Darwin
117 # Define default arguments to lit.
118 set(LIT_ARGS_DEFAULT "-sv")
120 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
122 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
123 set(LLVM_LIT_OUTPUT_DIR "${COMPILER_RT_EXEC_OUTPUT_DIR}")
126 construct_compiler_rt_default_triple()
127 if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*hf$")
128 if (${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "^arm")
129 set(COMPILER_RT_DEFAULT_TARGET_ARCH "armhf")
130 CHECK_SYMBOL_EXISTS (__thumb__ "" COMPILER_RT_ARM_THUMB)
133 if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*android.*")
135 string(REGEX MATCH "-target(=| +)[^ ]+android[a-z]*([0-9]+)" ANDROID_API_LEVEL "${CMAKE_C_FLAGS}")
136 set(ANDROID_API_LEVEL ${CMAKE_MATCH_2})
138 pythonize_bool(ANDROID)
140 set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
141 set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
143 pythonize_bool(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
145 # We support running instrumented tests when we're not cross-compiling
146 # and target a UNIX-like system or Windows.
147 # We can run tests on Android even when we are cross-compiling.
148 if((NOT "${CMAKE_CROSSCOMPILING}" AND (UNIX OR WIN32))
149 OR ANDROID OR COMPILER_RT_EMULATOR)
150 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
152 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
155 option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
156 option(COMPILER_RT_EXTERNALIZE_DEBUGINFO
157 "Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
158 # COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
159 pythonize_bool(COMPILER_RT_DEBUG)
161 option(COMPILER_RT_INTERCEPT_LIBDISPATCH
162 "Support interception of libdispatch (GCD). Requires '-fblocks'" OFF)
163 option(COMPILER_RT_LIBDISPATCH_INSTALL_PATH
164 "Specify if libdispatch is installed in a custom location" "")
165 if (COMPILER_RT_INTERCEPT_LIBDISPATCH AND NOT APPLE)
166 set(COMPILER_RT_LIBDISPATCH_CFLAGS -fblocks)
167 set(COMPILER_RT_TEST_LIBDISPATCH_CFLAGS)
168 if (COMPILER_RT_LIBDISPATCH_INSTALL_PATH)
169 list(APPEND COMPILER_RT_TEST_LIBDISPATCH_CFLAGS
170 -I${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/include
171 -L${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/lib
172 -Wl,-rpath=${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/lib)
174 list(APPEND COMPILER_RT_TEST_LIBDISPATCH_CFLAGS -lBlocksRuntime -ldispatch)
176 if (APPLE) # Always enable on Apple platforms.
177 set(COMPILER_RT_INTERCEPT_LIBDISPATCH ON)
179 pythonize_bool(COMPILER_RT_INTERCEPT_LIBDISPATCH)
181 if(APPLE AND SANITIZER_MIN_OSX_VERSION AND SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.9")
182 # Mac OS X prior to 10.9 had problems with exporting symbols from
184 set(cxxabi_supported OFF)
186 set(cxxabi_supported ON)
189 option(SANITIZER_ALLOW_CXXABI "Allow use of C++ ABI details in ubsan" ON)
191 set(SANITIZER_CAN_USE_CXXABI OFF)
192 if (cxxabi_supported AND SANITIZER_ALLOW_CXXABI)
193 set(SANITIZER_CAN_USE_CXXABI ON)
195 pythonize_bool(SANITIZER_CAN_USE_CXXABI)
197 macro(handle_default_cxx_lib var)
198 # Specifying -stdlib= in CMAKE_CXX_FLAGS overrides the defaults.
199 if (CMAKE_CXX_FLAGS MATCHES "-stdlib=([a-zA-Z+]*)")
200 set(${var}_LIBNAME "${CMAKE_MATCH_1}")
202 elseif (${var} STREQUAL "default")
203 if (APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
204 set(${var}_LIBNAME "libc++")
207 set(${var}_LIBNAME "libc++")
210 set(${var}_LIBNAME "libstdc++")
214 set(${var}_LIBNAME "${${var}}")
219 # This is either directly the C++ ABI library or the full C++ library
220 # which pulls in the ABI transitively.
221 # TODO: Mark this as internal flag, most users should use COMPILER_RT_CXX_LIBRARY.
222 set(SANITIZER_CXX_ABI "default" CACHE STRING
223 "Specify C++ ABI library to use.")
224 set(CXXABIS none default libstdc++ libc++ libcxxabi)
225 set_property(CACHE SANITIZER_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
226 handle_default_cxx_lib(SANITIZER_CXX_ABI)
228 # This needs to be a full C++ library for linking gtest and unit tests.
229 # TODO: Mark this as internal flag, most users should use COMPILER_RT_CXX_LIBRARY.
230 set(SANITIZER_TEST_CXX "default" CACHE STRING
231 "Specify C++ library to use for tests.")
232 set(CXXLIBS none default libstdc++ libc++)
233 set_property(CACHE SANITIZER_TEST_CXX PROPERTY STRINGS ;${CXXLIBS})
234 handle_default_cxx_lib(SANITIZER_TEST_CXX)
236 option(COMPILER_RT_USE_LLVM_UNWINDER "Use the LLVM unwinder." OFF)
237 cmake_dependent_option(COMPILER_RT_ENABLE_STATIC_UNWINDER
238 "Statically link the LLVM unwinder." OFF
239 "COMPILER_RT_USE_LLVM_UNWINDER" OFF)
241 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER OFF)
243 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER ON)
244 elseif (DEFINED LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_SHARED)
245 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER ON)
248 option(SANITIZER_USE_STATIC_LLVM_UNWINDER
249 "Use static LLVM unwinder." ${DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER})
250 pythonize_bool(SANITIZER_USE_STATIC_LLVM_UNWINDER)
252 set(DEFAULT_SANITIZER_USE_STATIC_CXX_ABI OFF)
253 if (DEFINED LIBCXXABI_ENABLE_SHARED AND NOT LIBCXXABI_ENABLE_SHARED)
254 set(DEFAULT_SANITIZER_USE_STATIC_CXX_ABI ON)
257 option(SANITIZER_USE_STATIC_CXX_ABI
258 "Use static libc++abi." ${DEFAULT_SANITIZER_USE_STATIC_CXX_ABI})
259 pythonize_bool(SANITIZER_USE_STATIC_CXX_ABI)
261 set(DEFAULT_SANITIZER_USE_STATIC_TEST_CXX OFF)
262 if (DEFINED LIBCXX_ENABLE_SHARED AND NOT LIBCXX_ENABLE_SHARED)
263 set(DEFAULT_SANITIZER_USE_STATIC_TEST_CXX ON)
266 option(SANITIZER_USE_STATIC_TEST_CXX
267 "Use static libc++ for tests." ${DEFAULT_SANITIZER_USE_STATIC_TEST_CXX})
268 pythonize_bool(SANITIZER_USE_STATIC_TEST_CXX)
270 set(COMPILER_RT_SUPPORTED_CXX_LIBRARIES none default libcxx)
271 set(COMPILER_RT_CXX_LIBRARY "default" CACHE STRING "Specify C++ library to use. Supported values are ${COMPILER_RT_SUPPORTED_CXX_LIBRARIES}.")
272 if (NOT "${COMPILER_RT_CXX_LIBRARY}" IN_LIST COMPILER_RT_SUPPORTED_CXX_LIBRARIES)
273 message(FATAL_ERROR "Unsupported C++ library: '${COMPILER_RT_CXX_LIBRARY}'. Supported values are ${COMPILER_RT_SUPPORTED_CXX_LIBRARIES}.")
275 cmake_dependent_option(COMPILER_RT_STATIC_CXX_LIBRARY
276 "Statically link the C++ library." OFF
277 "COMPILER_RT_CXX_LIBRARY" OFF)
279 set(DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY OFF)
281 set(DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY ON)
284 option(COMPILER_RT_USE_BUILTINS_LIBRARY
285 "Use compiler-rt builtins instead of libgcc" ${DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY})
289 #================================
290 # Setup Compiler Flags
291 #================================
293 # fcf-protection is a gcc/clang option for CET support on Linux platforms.
294 # We need to handle MSVC CET option on Windows platforms.
296 if (COMPILER_RT_ENABLE_CET AND NOT COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
297 message(FATAL_ERROR "Compiler used to build compiler-rt doesn't support CET!")
302 # Override any existing /W flags with /W4. This is what LLVM does. Failing to
303 # remove other /W[0-4] flags will result in a warning about overriding a
305 if (COMPILER_RT_HAS_W4_FLAG)
306 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
307 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
308 append_string_if(COMPILER_RT_HAS_W4_FLAG /W4 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
311 append_string_if(COMPILER_RT_HAS_WALL_FLAG -Wall CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
313 if(COMPILER_RT_ENABLE_WERROR)
314 append_string_if(COMPILER_RT_HAS_WERROR_FLAG -Werror CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
315 append_string_if(COMPILER_RT_HAS_WX_FLAG /WX CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
318 # Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP.
319 if(NOT COMPILER_RT_HAS_FUNC_SYMBOL)
320 add_definitions(-D__func__=__FUNCTION__)
323 # Provide some common commandline flags for Sanitizer runtimes.
324 if("${ANDROID_API_LEVEL}" GREATER_EQUAL 29)
325 list(APPEND SANITIZER_COMMON_CFLAGS -fno-emulated-tls)
326 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " -fno-emulated-tls")
329 append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC SANITIZER_COMMON_CFLAGS)
331 append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin SANITIZER_COMMON_CFLAGS)
332 append_list_if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions SANITIZER_COMMON_CFLAGS)
333 if(NOT COMPILER_RT_DEBUG AND NOT APPLE)
334 append_list_if(COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer SANITIZER_COMMON_CFLAGS)
336 append_list_if(COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables SANITIZER_COMMON_CFLAGS)
337 append_list_if(COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector SANITIZER_COMMON_CFLAGS)
338 append_list_if(COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG -fno-sanitize=safe-stack SANITIZER_COMMON_CFLAGS)
339 append_list_if(COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden SANITIZER_COMMON_CFLAGS)
340 if(NOT COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG)
341 append_list_if(COMPILER_RT_HAS_FVISIBILITY_INLINES_HIDDEN_FLAG -fvisibility-inlines-hidden SANITIZER_COMMON_CFLAGS)
343 append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto SANITIZER_COMMON_CFLAGS)
345 # By default do not instrument or use profdata for compiler-rt.
346 if(NOT COMPILER_RT_ENABLE_PGO)
347 if(LLVM_PROFDATA_FILE AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
348 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-instr-use")
350 if(LLVM_BUILD_INSTRUMENTED MATCHES IR AND COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
351 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-generate")
352 elseif((LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE) AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
353 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-instr-generate")
354 if(LLVM_BUILD_INSTRUMENTED_COVERAGE AND COMPILER_RT_HAS_FNO_COVERAGE_MAPPING_FLAG)
355 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-coverage-mapping")
360 # The following is a workaround for powerpc64le. This is the only architecture
361 # that requires -fno-function-sections to work properly. If lacking, the ASan
362 # Linux test function-sections-are-bad.cpp fails with the following error:
363 # 'undefined symbol: __sanitizer_unaligned_load32'.
364 if(DEFINED TARGET_powerpc64le_CFLAGS)
365 if(CMAKE_CXX_COMPILER_ID MATCHES "XL")
366 append("-qnofuncsect" TARGET_powerpc64le_CFLAGS)
368 append_list_if(COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections TARGET_powerpc64le_CFLAGS)
372 # The following is a workaround for s390x. This avoids creation of "partial
373 # inline" function fragments when building the asan libraries with certain
374 # GCC versions. The presence of those fragments, in particular for the
375 # interceptors, changes backtraces seen in asan error cases, which causes
376 # testsuite failures.
377 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
378 append_list_if(COMPILER_RT_HAS_FNO_PARTIAL_INLINING_FLAG -fno-partial-inlining SANITIZER_COMMON_CFLAGS)
382 # FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214.
383 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
385 # Remove any /M[DT][d] flags, and strip any definitions of _DEBUG.
386 # Since we're using CMAKE_MSVC_RUNTIME_LIBRARY (CMP0091 set to NEW),
387 # these options shouldn't be included in these flags variables. However,
388 # package managers that don't know which mechanism is used for passing
389 # CRT choice flags might be passing them both ways - which leads to
390 # duplicate CRT choice options. Thus make sure to strip out these flags
391 # from these variables, when we're forcing a CRT choice other than what
392 # the user requested here.
394 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
395 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
396 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
397 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
398 string(REGEX REPLACE "[/-]M[DT]d" "" ${flag_var} "${${flag_var}}")
399 string(REGEX REPLACE "[/-]MD" "" ${flag_var} "${${flag_var}}")
400 string(REGEX REPLACE "[/-]D_DEBUG" "" ${flag_var} "${${flag_var}}")
403 append_list_if(COMPILER_RT_HAS_Oy_FLAG /Oy- SANITIZER_COMMON_CFLAGS)
404 append_list_if(COMPILER_RT_HAS_GS_FLAG /GS- SANITIZER_COMMON_CFLAGS)
406 # Disable thread safe initialization for static locals. ASan shouldn't need
407 # it. Thread safe initialization assumes that the CRT has already been
408 # initialized, but ASan initializes before the CRT.
409 list(APPEND SANITIZER_COMMON_CFLAGS /Zc:threadSafeInit-)
412 append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 SANITIZER_COMMON_CFLAGS)
414 append_list_if(COMPILER_RT_HAS_WTHREAD_SAFETY_FLAG -Wthread-safety THREAD_SAFETY_FLAGS)
415 append_list_if(COMPILER_RT_HAS_WTHREAD_SAFETY_REFERENCE_FLAG -Wthread-safety-reference THREAD_SAFETY_FLAGS)
416 append_list_if(COMPILER_RT_HAS_WTHREAD_SAFETY_BETA_FLAG -Wthread-safety-beta THREAD_SAFETY_FLAGS)
417 list(APPEND SANITIZER_COMMON_CFLAGS ${THREAD_SAFETY_FLAGS})
418 string(REPLACE ";" " " thread_safety_flags_space_sep "${THREAD_SAFETY_FLAGS}")
419 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " ${thread_safety_flags_space_sep}")
421 # If we're using MSVC,
422 # always respect the optimization flags set by CMAKE_BUILD_TYPE instead.
425 # Build with optimization, unless we're in debug mode.
426 if(COMPILER_RT_DEBUG)
427 list(APPEND SANITIZER_COMMON_CFLAGS -O1)
429 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
433 # Determine if we should restrict stack frame sizes.
434 # Stack frames on PowerPC, Mips, SystemZ and in debug build can be much larger than
436 # FIXME: Fix all sanitizers and add -Wframe-larger-than to
437 # SANITIZER_COMMON_FLAGS
438 if(COMPILER_RT_HAS_WFRAME_LARGER_THAN_FLAG AND NOT COMPILER_RT_DEBUG
439 AND NOT ${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "powerpc|mips|s390x")
440 set(SANITIZER_LIMIT_FRAME_SIZE TRUE)
442 set(SANITIZER_LIMIT_FRAME_SIZE FALSE)
446 set(SANITIZER_USE_SYMBOLS TRUE)
448 set(SANITIZER_USE_SYMBOLS FALSE)
451 # Get the linker version while configuring compiler-rt and explicitly pass it
452 # in cflags during testing. This fixes the compiler/linker version mismatch
453 # issue when running a clang built with a newer Xcode in an older Xcode
454 set(COMPILER_RT_DARWIN_LINKER_VERSION)
455 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
456 get_darwin_linker_version(COMPILER_RT_DARWIN_LINKER_VERSION)
457 message(STATUS "Host linker version: ${COMPILER_RT_DARWIN_LINKER_VERSION}")
460 # Build sanitizer runtimes with debug info.
462 # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
463 # warning from the MS linker complaining that it can't find the 'vc140.pdb'
464 # file used by our object library compilations.
465 list(APPEND SANITIZER_COMMON_CFLAGS /Z7)
466 foreach(var_to_update
468 CMAKE_CXX_FLAGS_DEBUG
469 CMAKE_CXX_FLAGS_RELWITHDEBINFO)
470 string(REGEX REPLACE "(^| )/Z[i7I]($| )" " /Z7 "
471 "${var_to_update}" "${${var_to_update}}")
474 # On Apple platforms use full debug info (i.e. not `-gline-tables-only`)
475 # for all build types so that the runtime can be debugged.
476 if(NOT COMPILER_RT_HAS_G_FLAG)
477 message(FATAL_ERROR "-g is not supported by host compiler")
479 list(APPEND SANITIZER_COMMON_CFLAGS -g)
480 elseif(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
481 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
482 elseif(COMPILER_RT_HAS_G_FLAG)
483 list(APPEND SANITIZER_COMMON_CFLAGS -g)
486 if(LLVM_ENABLE_MODULES)
487 # Sanitizers cannot be built with -fmodules. The interceptors intentionally
488 # don't include system headers, which is incompatible with modules.
489 list(APPEND SANITIZER_COMMON_CFLAGS -fno-modules)
492 # Turn off several warnings.
493 append_list_if(COMPILER_RT_HAS_WGNU_FLAG -Wno-gnu SANITIZER_COMMON_CFLAGS)
494 append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros SANITIZER_COMMON_CFLAGS)
495 append_list_if(COMPILER_RT_HAS_WC99_EXTENSIONS_FLAG -Wno-c99-extensions SANITIZER_COMMON_CFLAGS)
496 # format-pedantic warns about passing T* for %p, which is not useful.
497 append_list_if(COMPILER_RT_HAS_WD4146_FLAG /wd4146 SANITIZER_COMMON_CFLAGS)
498 append_list_if(COMPILER_RT_HAS_WD4291_FLAG /wd4291 SANITIZER_COMMON_CFLAGS)
499 append_list_if(COMPILER_RT_HAS_WD4391_FLAG /wd4391 SANITIZER_COMMON_CFLAGS)
500 append_list_if(COMPILER_RT_HAS_WD4722_FLAG /wd4722 SANITIZER_COMMON_CFLAGS)
501 append_list_if(COMPILER_RT_HAS_WD4800_FLAG /wd4800 SANITIZER_COMMON_CFLAGS)
503 append_list_if(MINGW -fms-extensions SANITIZER_COMMON_CFLAGS)
505 # When lsan scans the stack for detecting reachable pointers, it's possible for
506 # a leaked pointer, which was pushed to the stack on an earlier function call,
507 # to still exist on the stack when doing a leak check if that part of the stack
508 # was not overwritten. In particular, if there's any uninitialized data in the
509 # lsan runtime, and the SP we start from is sufficiently deep into the runtime,
510 # then a leaked pointer could be marked as reachable. Such instances could be
511 # mitigated by clobbering any uninitialized data. Note that this won't cover
512 # all possible uninitialized stack contents, such as those used for register
513 # spill slots, unused portions for alignment, or even local variables not
514 # yet in scope at a certain point in the function.
516 # Note that this type of issue was discovered with lsan, but can apply to other
518 # Disable PowerPC because of https://github.com/llvm/llvm-project/issues/84654.
519 if(NOT "${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
520 append_list_if(COMPILER_RT_HAS_TRIVIAL_AUTO_INIT -ftrivial-auto-var-init=pattern SANITIZER_COMMON_CFLAGS)
523 # Set common link flags.
524 # TODO: We should consider using the same model as libc++, that is use either
525 # -nostdlib++ and --unwindlib=none if supported, or -nodefaultlibs otherwise.
526 append_list_if(C_SUPPORTS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMON_LINK_FLAGS)
527 append_list_if(COMPILER_RT_HAS_Z_TEXT -Wl,-z,text SANITIZER_COMMON_LINK_FLAGS)
529 # Only necessary for 32-bit SPARC. Solaris 11.2+ ld uses -z ignore/-z record
530 # natively, but supports --as-needed/--no-as-needed for GNU ld compatibility.
531 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "sparc")
532 list(APPEND SANITIZER_COMMON_LINK_LIBS -Wl,--as-needed atomic -Wl,--no-as-needed)
535 if (COMPILER_RT_USE_BUILTINS_LIBRARY)
536 string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
539 append_list_if(COMPILER_RT_HAS_GCC_LIB gcc SANITIZER_COMMON_LINK_LIBS)
541 append_list_if(COMPILER_RT_HAS_GCC_S_LIB gcc_s SANITIZER_COMMON_LINK_LIBS)
545 append_list_if(COMPILER_RT_HAS_LIBC c SANITIZER_COMMON_LINK_LIBS)
546 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
547 list(APPEND SANITIZER_COMMON_LINK_LIBS zircon)
550 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
551 set(SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT ON)
553 set(SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT OFF)
555 option(SANITIZER_NO_UNDEFINED_SYMBOLS "Report error on unresolved symbol references" ${SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT})
556 if (SANITIZER_NO_UNDEFINED_SYMBOLS)
557 list(APPEND SANITIZER_COMMON_LINK_FLAGS -Wl,-z,defs)
560 # TODO: COMPILER_RT_COMMON_CFLAGS and COMPILER_RT_COMMON_LINK_FLAGS are
561 # intended for use in non-sanitizer runtimes such as libFuzzer, profile or XRay,
562 # move these higher to include common flags, then derive SANITIZER_COMMON_CFLAGS
563 # and SANITIZER_COMMON_LINK_FLAGS from those and append sanitizer-specific flags.
564 set(COMPILER_RT_COMMON_CFLAGS ${SANITIZER_COMMON_CFLAGS})
565 set(COMPILER_RT_COMMON_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
567 # We don't use the C++ standard library, so avoid including it by mistake.
568 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ SANITIZER_COMMON_CFLAGS)
569 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ SANITIZER_COMMON_LINK_FLAGS)
571 # Remove -stdlib= which is unused when passing -nostdinc++...
572 string(REGEX MATCHALL "-stdlib=[a-zA-Z+]*" stdlib_flag "${CMAKE_CXX_FLAGS}")
573 string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
575 # ...we need it to build some runtimes and tests so readd it where appropriate.
576 list(APPEND COMPILER_RT_COMMON_CFLAGS ${stdlib_flag})
577 list(APPEND COMPILER_RT_COMMON_LINK_FLAGS ${stdlib_flag})
579 # TODO: There's a lot of duplication across lib/*/tests/CMakeLists.txt files,
580 # move some of the common flags to COMPILER_RT_UNITTEST_CFLAGS.
582 # Unittests need access to C++ standard library.
583 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " ${stdlib_flag}")
585 # When cross-compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help in compilation
586 # and linking of unittests.
587 string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
588 set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
590 option(COMPILER_RT_TEST_STANDALONE_BUILD_LIBS
591 "When set to ON and testing in a standalone build, test the runtime \
592 libraries built by this standalone build rather than the runtime libraries \
593 shipped with the compiler (used for testing). When set to OFF and testing \
594 in a standalone build, test the runtime libraries shipped with the compiler \
595 (used for testing). This option has no effect if the compiler and this \
596 build are configured to use the same runtime library path."
598 if (COMPILER_RT_TEST_STANDALONE_BUILD_LIBS)
599 # Ensure that the unit tests can find the sanitizer headers prior to installation.
600 list(APPEND COMPILER_RT_UNITTEST_CFLAGS "-I${CMAKE_CURRENT_LIST_DIR}/include")
601 # Ensure that unit tests link against the just-built runtime libraries instead
602 # of the ones bundled with the compiler by overriding the resource directory.
604 if ("${COMPILER_RT_TEST_COMPILER_ID}" MATCHES "Clang")
605 list(APPEND COMPILER_RT_UNITTEST_LINK_FLAGS "-resource-dir=${COMPILER_RT_OUTPUT_DIR}")
609 if(COMPILER_RT_USE_LLVM_UNWINDER)
610 # We're linking directly against the libunwind that we're building so don't
611 # try to link in the toolchain's default libunwind which may be missing.
612 append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none COMPILER_RT_COMMON_LINK_FLAGS)
613 append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none COMPILER_RT_UNITTEST_LINK_FLAGS)
614 if (COMPILER_RT_ENABLE_STATIC_UNWINDER)
615 list(APPEND COMPILER_RT_UNWINDER_LINK_LIBS "$<TARGET_LINKER_FILE:unwind_static>")
617 list(APPEND COMPILER_RT_UNWINDER_LINK_LIBS "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:unwind_shared>,unwind_shared,unwind_static>>")
621 if (COMPILER_RT_CXX_LIBRARY STREQUAL "libcxx")
622 # We are using the in-tree libc++ so avoid including the default one.
623 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ COMPILER_RT_COMMON_CFLAGS)
624 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ COMPILER_RT_COMMON_LINK_FLAGS)
625 # Use the in-tree libc++ through explicit include and library paths.
626 set(COMPILER_RT_CXX_CFLAGS "$<$<TARGET_EXISTS:cxx-headers>:$<IF:$<BOOL:${MSVC}>,/imsvc,-isystem>$<JOIN:$<TARGET_PROPERTY:cxx-headers,INTERFACE_INCLUDE_DIRECTORIES>,$<SEMICOLON>$<IF:$<BOOL:${MSVC}>,/imsvc,-isystem>>>")
627 if (COMPILER_RT_STATIC_CXX_LIBRARY)
628 set(COMPILER_RT_CXX_LINK_LIBS "$<TARGET_LINKER_FILE:cxx_static>")
630 set(COMPILER_RT_CXX_LINK_LIBS "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:cxx_shared>,cxx_shared,cxx_static>>")
632 elseif (COMPILER_RT_CXX_LIBRARY STREQUAL "none")
633 # We aren't using any C++ standard library so avoid including the default one.
634 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ COMPILER_RT_COMMON_CFLAGS)
635 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ COMPILER_RT_COMMON_LINK_FLAGS)
637 # Nothing to be done for `default`.
640 if (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libc++")
641 if (SANITIZER_CXX_ABI_INTREE)
642 # TODO: We don't need to add --unwindlib=none to SANITIZER_COMMON_LINK_FLAGS
643 # because we added -nodefaultlibs there earlier, and adding would result in
644 # a warning, but if we switch to -nostdlib++, we would need to add it here.
645 # append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none SANITIZER_COMMON_LINK_FLAGS)
646 if(SANITIZER_USE_STATIC_CXX_ABI)
647 if(TARGET libcxx-abi-static)
648 set(SANITIZER_CXX_ABI_LIBRARIES libcxx-abi-static)
651 if(TARGET libcxx-abi-shared)
652 set(SANITIZER_CXX_ABI_LIBRARIES libcxx-abi-shared)
653 elseif(TARGET libcxx-abi-static)
654 set(SANITIZER_CXX_ABI_LIBRARIES libcxx-abi-static)
658 append_list_if(COMPILER_RT_HAS_LIBCXX c++ SANITIZER_CXX_ABI_LIBRARIES)
660 elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libcxxabi")
661 list(APPEND SANITIZER_CXX_ABI_LIBRARIES "c++abi")
662 elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libstdc++")
663 append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ SANITIZER_CXX_ABI_LIBRARIES)
666 if (SANITIZER_TEST_CXX_LIBNAME STREQUAL "libc++")
667 if (SANITIZER_TEST_CXX_INTREE)
668 list(APPEND SANITIZER_TEST_CXX_CFLAGS "$<$<TARGET_EXISTS:cxx-headers>:$<IF:$<BOOL:${MSVC}>,/imsvc,-isystem>$<JOIN:$<TARGET_PROPERTY:cxx-headers,INTERFACE_INCLUDE_DIRECTORIES>,$<SEMICOLON>$<IF:$<BOOL:${MSVC}>,/imsvc,-isystem>>>")
669 if (SANITIZER_USE_STATIC_TEST_CXX)
670 list(APPEND SANITIZER_TEST_CXX_LIBRARIES "$<TARGET_LINKER_FILE:cxx_static>")
672 list(APPEND SANITIZER_TEST_CXX_LIBRARIES "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:cxx_shared>,cxx_shared,cxx_static>>")
674 # We are using the in tree libc++ so avoid including the default one.
675 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ COMPILER_RT_UNITTEST_CFLAGS)
676 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ COMPILER_RT_UNITTEST_LINK_FLAGS)
678 append_list_if(COMPILER_RT_HAS_LIBCXX -lc++ SANITIZER_TEST_CXX_LIBRARIES)
680 elseif (SANITIZER_TEST_CXX_LIBNAME STREQUAL "libstdc++")
681 append_list_if(COMPILER_RT_HAS_LIBSTDCXX -lstdc++ SANITIZER_TEST_CXX_LIBRARIES)
685 # FIXME: When compiler-rt is build using -DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON, then
686 # The LLVM_THIRD_PARTY_DIR variable is not set.
687 if (NOT LLVM_THIRD_PARTY_DIR)
688 set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
691 set(COMPILER_RT_GTEST_PATH ${LLVM_THIRD_PARTY_DIR}/unittest/googletest)
692 set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
693 set(COMPILER_RT_GTEST_CFLAGS
694 -DGTEST_NO_LLVM_SUPPORT=1
696 -I${COMPILER_RT_GTEST_PATH}/include
697 -I${COMPILER_RT_GTEST_PATH}
701 set(COMPILER_RT_GMOCK_PATH ${LLVM_THIRD_PARTY_DIR}/unittest/googlemock)
702 set(COMPILER_RT_GMOCK_SOURCE ${COMPILER_RT_GMOCK_PATH}/src/gmock-all.cc)
703 set(COMPILER_RT_GMOCK_CFLAGS
704 -DGTEST_NO_LLVM_SUPPORT=1
706 -I${COMPILER_RT_GMOCK_PATH}/include
707 -I${COMPILER_RT_GMOCK_PATH}
710 if(COMPILER_RT_HAS_G_FLAG)
711 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -g)
713 append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
714 append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS)
715 append_list_if(COMPILER_RT_HAS_WSUGGEST_OVERRIDE_FLAG -Wno-suggest-override COMPILER_RT_UNITTEST_CFLAGS)
716 # Detect if the compiler toolchain includes gcc-toolset-12.
717 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR
718 "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
719 execute_process(COMMAND ${CMAKE_CXX_COMPILER} -v
720 RESULT_VARIABLE compiler_info_rc
721 OUTPUT_VARIABLE compiler_info
722 ERROR_VARIABLE compiler_info)
723 if(compiler_info MATCHES ".*gcc-toolset-12.*")
724 set(USING_GCC_TOOLSET_12 TRUE)
727 if(MSVC OR (LINUX AND DEFINED USING_GCC_TOOLSET_12 AND
728 CMAKE_CXX_STANDARD EQUAL 17))
729 # gtest use a lot of stuff marked as deprecated on Windows or if using
730 # gcc-toolset-12 in the compiler toolchain on Linux; all of the
731 # deprecated declarations in gcc-toolset-12 used in Google Tests have been
732 # observed to be _GLIBCXX17_DEPRECATED and should go away once
733 # CMAKE_CXX_STANDARD is greater than 17.
734 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
737 # Warnings to turn off for all libraries, not just sanitizers.
738 append_string_if(COMPILER_RT_HAS_WUNUSED_PARAMETER_FLAG -Wno-unused-parameter CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
740 if (CMAKE_LINKER MATCHES "link.exe$")
741 # Silence MSVC linker warnings caused by empty object files. The
742 # sanitizer libraries intentionally use ifdefs that result in empty
743 # files, rather than skipping these files in the build system.
744 # Ideally, we would pass this flag only for the libraries that need
745 # it, but CMake doesn't seem to have a way to set linker flags for
746 # individual static libraries, so we enable the suppression flag for
747 # the whole compiler-rt project.
748 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
751 add_subdirectory(include)
753 option(COMPILER_RT_USE_LIBCXX
754 "Enable compiler-rt to use libc++ from the source tree" ON)
755 if(COMPILER_RT_USE_LIBCXX)
756 if(LLVM_ENABLE_PROJECTS_USED)
757 # Don't use libcxx if LLVM_ENABLE_PROJECTS does not enable it.
758 set(COMPILER_RT_LIBCXX_PATH ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR})
759 set(COMPILER_RT_LIBCXXABI_PATH ${LLVM_EXTERNAL_LIBCXXABI_SOURCE_DIR})
761 foreach(path IN ITEMS ${LLVM_MAIN_SRC_DIR}/projects/libcxx
762 ${LLVM_MAIN_SRC_DIR}/runtimes/libcxx
763 ${LLVM_MAIN_SRC_DIR}/../libcxx
764 ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR})
765 if(IS_DIRECTORY ${path})
766 set(COMPILER_RT_LIBCXX_PATH ${path})
770 foreach(path IN ITEMS ${LLVM_MAIN_SRC_DIR}/projects/libcxxabi
771 ${LLVM_MAIN_SRC_DIR}/runtimes/libcxxabi
772 ${LLVM_MAIN_SRC_DIR}/../libcxxabi
773 ${LLVM_EXTERNAL_LIBCXXABI_SOURCE_DIR})
774 if(IS_DIRECTORY ${path})
775 set(COMPILER_RT_LIBCXXABI_PATH ${path})
782 if (LINKER_IS_LLD OR LLVM_TOOL_LLD_BUILD)
783 set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/tools/lld)
784 if(EXISTS ${COMPILER_RT_LLD_PATH}/)
785 set(COMPILER_RT_HAS_LLD TRUE)
787 set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/../lld)
788 if(EXISTS ${COMPILER_RT_LLD_PATH}/)
789 set(COMPILER_RT_HAS_LLD TRUE)
795 set(COMPILER_RT_HAS_LLD TRUE)
796 set(COMPILER_RT_TEST_USE_LLD TRUE)
797 append_list_if(COMPILER_RT_HAS_FUSE_LD_LLD_FLAG -fuse-ld=lld SANITIZER_COMMON_LINK_FLAGS)
798 append_list_if(COMPILER_RT_HAS_LLD -fuse-ld=lld COMPILER_RT_UNITTEST_LINK_FLAGS)
800 if(${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES sparc)
801 # lld has several bugs/limitations on SPARC, so disable (Issue #100320).
802 set(COMPILER_RT_HAS_LLD FALSE)
804 pythonize_bool(COMPILER_RT_HAS_LLD)
805 pythonize_bool(COMPILER_RT_TEST_USE_LLD)
807 option(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER "Build Compiler RT linked with in LLVM symbolizer" OFF)
808 mark_as_advanced(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER)
810 add_subdirectory(lib)
812 if(COMPILER_RT_INCLUDE_TESTS)
813 # Don't build llvm-lit for runtimes-build, it will clean up map_config.
814 if (COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
815 # If we have a valid source tree, generate llvm-lit into the bin directory.
816 # The user can still choose to have the check targets *use* a different lit
817 # by specifying -DLLVM_EXTERNAL_LIT, but we generate it regardless.
818 if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
819 # Needed for lit support in standalone builds.
821 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
822 # Ensure that the testsuite uses the local lit rather than
823 # LLVM_INSTALL_DIR/bin/llvm-lit (which probably does not exist).
824 get_llvm_lit_path(_base_dir _file_name)
825 set(LLVM_EXTERNAL_LIT "${_base_dir}/${_file_name}" CACHE STRING "Command used to spawn lit" FORCE)
826 elseif(NOT EXISTS ${LLVM_EXTERNAL_LIT})
827 message(WARNING "Could not find LLVM source directory and LLVM_EXTERNAL_LIT does not"
828 "point to a valid file. You will not be able to run tests.")
831 add_subdirectory(unittests)
832 add_subdirectory(test)
835 add_subdirectory(tools)