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)
8 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
9 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
11 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
14 # Check if compiler-rt is built as a standalone project.
15 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR COMPILER_RT_STANDALONE_BUILD)
16 project(CompilerRT C CXX ASM)
17 set(COMPILER_RT_STANDALONE_BUILD TRUE)
18 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
21 # Add path for custom compiler-rt modules.
22 list(INSERT CMAKE_MODULE_PATH 0
23 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
24 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
25 "${LLVM_COMMON_CMAKE_UTILS}"
26 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
29 if(CMAKE_CONFIGURATION_TYPES)
30 set(CMAKE_CFG_RESOLVED_INTDIR "${CMAKE_CFG_INTDIR}/")
32 set(CMAKE_CFG_RESOLVED_INTDIR "")
35 include(SetPlatformToolchainTools)
36 include(base-config-ix)
37 include(CompilerRTUtils)
38 include(CMakeDependentOption)
40 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
41 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
42 option(COMPILER_RT_DISABLE_AARCH64_FMV "Disable AArch64 Function Multi Versioning support" OFF)
43 mark_as_advanced(COMPILER_RT_DISABLE_AARCH64_FMV)
44 option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON)
45 mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS)
46 option(COMPILER_RT_BUILD_XRAY "Build xray" ON)
47 mark_as_advanced(COMPILER_RT_BUILD_XRAY)
48 option(COMPILER_RT_BUILD_LIBFUZZER "Build libFuzzer" ON)
49 mark_as_advanced(COMPILER_RT_BUILD_LIBFUZZER)
50 option(COMPILER_RT_BUILD_PROFILE "Build profile runtime" ON)
51 mark_as_advanced(COMPILER_RT_BUILD_PROFILE)
52 option(COMPILER_RT_BUILD_MEMPROF "Build memory profiling runtime" ON)
53 mark_as_advanced(COMPILER_RT_BUILD_MEMPROF)
54 option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF)
55 mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT)
56 option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON)
57 mark_as_advanced(COMPILER_RT_BUILD_ORC)
58 option(COMPILER_RT_BUILD_GWP_ASAN "Build GWP-ASan, and link it into SCUDO" ON)
59 mark_as_advanced(COMPILER_RT_BUILD_GWP_ASAN)
60 option(COMPILER_RT_ENABLE_CET "Build Compiler RT with CET enabled" OFF)
62 option(COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH "Set custom sysroot for building SCUDO standalone" OFF)
63 mark_as_advanced(COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH)
64 option(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED "Build SCUDO standalone for shared libraries" ON)
65 mark_as_advanced(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED)
66 option(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC "Build SCUDO standalone with LLVM's libc headers" OFF)
67 mark_as_advanced(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC)
70 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT OFF)
72 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT ON)
74 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS ${COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT} CACHE BOOL "Enable libc interceptors in HWASan (testing mode)")
76 set(COMPILER_RT_BAREMETAL_BUILD OFF CACHE BOOL
77 "Build for a bare-metal target.")
79 if (COMPILER_RT_STANDALONE_BUILD)
80 set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
81 set(CMAKE_CXX_STANDARD_REQUIRED YES)
82 set(CMAKE_CXX_EXTENSIONS NO)
84 if (NOT LLVM_RUNTIMES_BUILD)
87 if (TARGET intrinsics_gen)
88 # Loading the llvm config causes this target to be imported so place it
89 # under the appropriate folder in an IDE.
90 set_target_properties(intrinsics_gen PROPERTIES FOLDER "Compiler-RT Misc")
93 find_package(Python3 COMPONENTS Interpreter)
94 if(NOT Python3_Interpreter_FOUND)
95 message(WARNING "Python3 not found, using python2 as a fallback")
96 find_package(Python2 COMPONENTS Interpreter REQUIRED)
97 if(Python2_VERSION VERSION_LESS 2.7)
98 message(SEND_ERROR "Python 2.7 or newer is required")
101 # Treat python2 as python3
102 add_executable(Python3::Interpreter IMPORTED)
103 set_target_properties(Python3::Interpreter PROPERTIES
104 IMPORTED_LOCATION ${Python2_EXECUTABLE})
105 set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
108 # Ensure that fat libraries are built correctly on Darwin
113 # Define default arguments to lit.
114 set(LIT_ARGS_DEFAULT "-sv")
116 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
118 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
119 set(LLVM_LIT_OUTPUT_DIR "${COMPILER_RT_EXEC_OUTPUT_DIR}")
122 construct_compiler_rt_default_triple()
123 if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*hf$")
124 if (${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "^arm")
125 set(COMPILER_RT_DEFAULT_TARGET_ARCH "armhf")
126 CHECK_SYMBOL_EXISTS (__thumb__ "" COMPILER_RT_ARM_THUMB)
129 if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*android.*")
131 string(REGEX MATCH "-target(=| +)[^ ]+android[a-z]*([0-9]+)" ANDROID_API_LEVEL "${CMAKE_C_FLAGS}")
132 set(ANDROID_API_LEVEL ${CMAKE_MATCH_2})
134 pythonize_bool(ANDROID)
136 set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
137 set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
139 pythonize_bool(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
141 # We support running instrumented tests when we're not cross-compiling
142 # and target a UNIX-like system or Windows.
143 # We can run tests on Android even when we are cross-compiling.
144 if((NOT "${CMAKE_CROSSCOMPILING}" AND (UNIX OR WIN32))
145 OR ANDROID OR COMPILER_RT_EMULATOR)
146 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
148 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
151 option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
152 option(COMPILER_RT_EXTERNALIZE_DEBUGINFO
153 "Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
154 # COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
155 pythonize_bool(COMPILER_RT_DEBUG)
157 option(COMPILER_RT_INTERCEPT_LIBDISPATCH
158 "Support interception of libdispatch (GCD). Requires '-fblocks'" OFF)
159 option(COMPILER_RT_LIBDISPATCH_INSTALL_PATH
160 "Specify if libdispatch is installed in a custom location" "")
161 if (COMPILER_RT_INTERCEPT_LIBDISPATCH AND NOT APPLE)
162 set(COMPILER_RT_LIBDISPATCH_CFLAGS -fblocks)
163 set(COMPILER_RT_TEST_LIBDISPATCH_CFLAGS)
164 if (COMPILER_RT_LIBDISPATCH_INSTALL_PATH)
165 list(APPEND COMPILER_RT_TEST_LIBDISPATCH_CFLAGS
166 -I${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/include
167 -L${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/lib
168 -Wl,-rpath=${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/lib)
170 list(APPEND COMPILER_RT_TEST_LIBDISPATCH_CFLAGS -lBlocksRuntime -ldispatch)
172 if (APPLE) # Always enable on Apple platforms.
173 set(COMPILER_RT_INTERCEPT_LIBDISPATCH ON)
175 pythonize_bool(COMPILER_RT_INTERCEPT_LIBDISPATCH)
177 if(APPLE AND SANITIZER_MIN_OSX_VERSION AND SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.9")
178 # Mac OS X prior to 10.9 had problems with exporting symbols from
180 set(cxxabi_supported OFF)
182 set(cxxabi_supported ON)
185 option(SANITIZER_ALLOW_CXXABI "Allow use of C++ ABI details in ubsan" ON)
187 set(SANITIZER_CAN_USE_CXXABI OFF)
188 if (cxxabi_supported AND SANITIZER_ALLOW_CXXABI)
189 set(SANITIZER_CAN_USE_CXXABI ON)
191 pythonize_bool(SANITIZER_CAN_USE_CXXABI)
193 macro(handle_default_cxx_lib var)
194 # Specifying -stdlib= in CMAKE_CXX_FLAGS overrides the defaults.
195 if (CMAKE_CXX_FLAGS MATCHES "-stdlib=([a-zA-Z+]*)")
196 set(${var}_LIBNAME "${CMAKE_MATCH_1}")
198 elseif (${var} STREQUAL "default")
199 if (APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
200 set(${var}_LIBNAME "libc++")
203 set(${var}_LIBNAME "libc++")
206 set(${var}_LIBNAME "libstdc++")
210 set(${var}_LIBNAME "${${var}}")
215 # This is either directly the C++ ABI library or the full C++ library
216 # which pulls in the ABI transitively.
217 # TODO: Mark this as internal flag, most users should use COMPILER_RT_CXX_LIBRARY.
218 set(SANITIZER_CXX_ABI "default" CACHE STRING
219 "Specify C++ ABI library to use.")
220 set(CXXABIS none default libstdc++ libc++ libcxxabi)
221 set_property(CACHE SANITIZER_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
222 handle_default_cxx_lib(SANITIZER_CXX_ABI)
224 # This needs to be a full C++ library for linking gtest and unit tests.
225 # TODO: Mark this as internal flag, most users should use COMPILER_RT_CXX_LIBRARY.
226 set(SANITIZER_TEST_CXX "default" CACHE STRING
227 "Specify C++ library to use for tests.")
228 set(CXXLIBS none default libstdc++ libc++)
229 set_property(CACHE SANITIZER_TEST_CXX PROPERTY STRINGS ;${CXXLIBS})
230 handle_default_cxx_lib(SANITIZER_TEST_CXX)
232 option(COMPILER_RT_USE_LLVM_UNWINDER "Use the LLVM unwinder." OFF)
233 cmake_dependent_option(COMPILER_RT_ENABLE_STATIC_UNWINDER
234 "Statically link the LLVM unwinder." OFF
235 "COMPILER_RT_USE_LLVM_UNWINDER" OFF)
237 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER OFF)
239 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER ON)
240 elseif (DEFINED LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_SHARED)
241 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER ON)
244 option(SANITIZER_USE_STATIC_LLVM_UNWINDER
245 "Use static LLVM unwinder." ${DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER})
246 pythonize_bool(SANITIZER_USE_STATIC_LLVM_UNWINDER)
248 set(DEFAULT_SANITIZER_USE_STATIC_CXX_ABI OFF)
249 if (DEFINED LIBCXXABI_ENABLE_SHARED AND NOT LIBCXXABI_ENABLE_SHARED)
250 set(DEFAULT_SANITIZER_USE_STATIC_CXX_ABI ON)
253 option(SANITIZER_USE_STATIC_CXX_ABI
254 "Use static libc++abi." ${DEFAULT_SANITIZER_USE_STATIC_CXX_ABI})
255 pythonize_bool(SANITIZER_USE_STATIC_CXX_ABI)
257 set(DEFAULT_SANITIZER_USE_STATIC_TEST_CXX OFF)
258 if (DEFINED LIBCXX_ENABLE_SHARED AND NOT LIBCXX_ENABLE_SHARED)
259 set(DEFAULT_SANITIZER_USE_STATIC_TEST_CXX ON)
262 option(SANITIZER_USE_STATIC_TEST_CXX
263 "Use static libc++ for tests." ${DEFAULT_SANITIZER_USE_STATIC_TEST_CXX})
264 pythonize_bool(SANITIZER_USE_STATIC_TEST_CXX)
266 set(COMPILER_RT_SUPPORTED_CXX_LIBRARIES none default libcxx)
267 set(COMPILER_RT_CXX_LIBRARY "default" CACHE STRING "Specify C++ library to use. Supported values are ${COMPILER_RT_SUPPORTED_CXX_LIBRARIES}.")
268 if (NOT "${COMPILER_RT_CXX_LIBRARY}" IN_LIST COMPILER_RT_SUPPORTED_CXX_LIBRARIES)
269 message(FATAL_ERROR "Unsupported C++ library: '${COMPILER_RT_CXX_LIBRARY}'. Supported values are ${COMPILER_RT_SUPPORTED_CXX_LIBRARIES}.")
271 cmake_dependent_option(COMPILER_RT_STATIC_CXX_LIBRARY
272 "Statically link the C++ library." OFF
273 "COMPILER_RT_CXX_LIBRARY" OFF)
275 set(DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY OFF)
277 set(DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY ON)
280 option(COMPILER_RT_USE_BUILTINS_LIBRARY
281 "Use compiler-rt builtins instead of libgcc" ${DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY})
285 #================================
286 # Setup Compiler Flags
287 #================================
289 # fcf-protection is a gcc/clang option for CET support on Linux platforms.
290 # We need to handle MSVC CET option on Windows platforms.
292 if (COMPILER_RT_ENABLE_CET AND NOT COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
293 message(FATAL_ERROR "Compiler used to build compiler-rt doesn't support CET!")
298 # Override any existing /W flags with /W4. This is what LLVM does. Failing to
299 # remove other /W[0-4] flags will result in a warning about overriding a
301 if (COMPILER_RT_HAS_W4_FLAG)
302 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
303 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
304 append_string_if(COMPILER_RT_HAS_W4_FLAG /W4 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
307 append_string_if(COMPILER_RT_HAS_WALL_FLAG -Wall CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
309 if(COMPILER_RT_ENABLE_WERROR)
310 append_string_if(COMPILER_RT_HAS_WERROR_FLAG -Werror CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
311 append_string_if(COMPILER_RT_HAS_WX_FLAG /WX CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
314 # Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP.
315 if(NOT COMPILER_RT_HAS_FUNC_SYMBOL)
316 add_definitions(-D__func__=__FUNCTION__)
319 # Provide some common commandline flags for Sanitizer runtimes.
320 if("${ANDROID_API_LEVEL}" GREATER_EQUAL 29)
321 list(APPEND SANITIZER_COMMON_CFLAGS -fno-emulated-tls)
322 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " -fno-emulated-tls")
325 append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC SANITIZER_COMMON_CFLAGS)
327 append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin SANITIZER_COMMON_CFLAGS)
328 append_list_if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions SANITIZER_COMMON_CFLAGS)
329 if(NOT COMPILER_RT_DEBUG AND NOT APPLE)
330 append_list_if(COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer SANITIZER_COMMON_CFLAGS)
332 append_list_if(COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables SANITIZER_COMMON_CFLAGS)
333 append_list_if(COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector SANITIZER_COMMON_CFLAGS)
334 append_list_if(COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG -fno-sanitize=safe-stack SANITIZER_COMMON_CFLAGS)
335 append_list_if(COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden SANITIZER_COMMON_CFLAGS)
336 if(NOT COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG)
337 append_list_if(COMPILER_RT_HAS_FVISIBILITY_INLINES_HIDDEN_FLAG -fvisibility-inlines-hidden SANITIZER_COMMON_CFLAGS)
339 append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto SANITIZER_COMMON_CFLAGS)
341 # By default do not instrument or use profdata for compiler-rt.
342 if(NOT COMPILER_RT_ENABLE_PGO)
343 if(LLVM_PROFDATA_FILE AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
344 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-instr-use")
346 if(LLVM_BUILD_INSTRUMENTED MATCHES IR AND COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
347 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-generate")
348 elseif((LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE) AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
349 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-instr-generate")
350 if(LLVM_BUILD_INSTRUMENTED_COVERAGE AND COMPILER_RT_HAS_FNO_COVERAGE_MAPPING_FLAG)
351 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-coverage-mapping")
356 # The following is a workaround for powerpc64le. This is the only architecture
357 # that requires -fno-function-sections to work properly. If lacking, the ASan
358 # Linux test function-sections-are-bad.cpp fails with the following error:
359 # 'undefined symbol: __sanitizer_unaligned_load32'.
360 if(DEFINED TARGET_powerpc64le_CFLAGS)
361 if(CMAKE_CXX_COMPILER_ID MATCHES "XL")
362 append("-qnofuncsect" TARGET_powerpc64le_CFLAGS)
364 append_list_if(COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections TARGET_powerpc64le_CFLAGS)
368 # The following is a workaround for s390x. This avoids creation of "partial
369 # inline" function fragments when building the asan libraries with certain
370 # GCC versions. The presence of those fragments, in particular for the
371 # interceptors, changes backtraces seen in asan error cases, which causes
372 # testsuite failures.
373 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
374 append_list_if(COMPILER_RT_HAS_FNO_PARTIAL_INLINING_FLAG -fno-partial-inlining SANITIZER_COMMON_CFLAGS)
378 # FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214.
379 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
381 # Remove any /M[DT][d] flags, and strip any definitions of _DEBUG.
382 # Since we're using CMAKE_MSVC_RUNTIME_LIBRARY (CMP0091 set to NEW),
383 # these options shouldn't be included in these flags variables. However,
384 # package managers that don't know which mechanism is used for passing
385 # CRT choice flags might be passing them both ways - which leads to
386 # duplicate CRT choice options. Thus make sure to strip out these flags
387 # from these variables, when we're forcing a CRT choice other than what
388 # the user requested here.
390 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
391 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
392 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
393 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
394 string(REGEX REPLACE "[/-]M[DT]d" "" ${flag_var} "${${flag_var}}")
395 string(REGEX REPLACE "[/-]MD" "" ${flag_var} "${${flag_var}}")
396 string(REGEX REPLACE "[/-]D_DEBUG" "" ${flag_var} "${${flag_var}}")
399 append_list_if(COMPILER_RT_HAS_Oy_FLAG /Oy- SANITIZER_COMMON_CFLAGS)
400 append_list_if(COMPILER_RT_HAS_GS_FLAG /GS- SANITIZER_COMMON_CFLAGS)
402 # Disable thread safe initialization for static locals. ASan shouldn't need
403 # it. Thread safe initialization assumes that the CRT has already been
404 # initialized, but ASan initializes before the CRT.
405 list(APPEND SANITIZER_COMMON_CFLAGS /Zc:threadSafeInit-)
408 append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 SANITIZER_COMMON_CFLAGS)
410 append_list_if(COMPILER_RT_HAS_WTHREAD_SAFETY_FLAG -Wthread-safety THREAD_SAFETY_FLAGS)
411 append_list_if(COMPILER_RT_HAS_WTHREAD_SAFETY_REFERENCE_FLAG -Wthread-safety-reference THREAD_SAFETY_FLAGS)
412 append_list_if(COMPILER_RT_HAS_WTHREAD_SAFETY_BETA_FLAG -Wthread-safety-beta THREAD_SAFETY_FLAGS)
413 list(APPEND SANITIZER_COMMON_CFLAGS ${THREAD_SAFETY_FLAGS})
414 string(REPLACE ";" " " thread_safety_flags_space_sep "${THREAD_SAFETY_FLAGS}")
415 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " ${thread_safety_flags_space_sep}")
417 # If we're using MSVC,
418 # always respect the optimization flags set by CMAKE_BUILD_TYPE instead.
421 # Build with optimization, unless we're in debug mode.
422 if(COMPILER_RT_DEBUG)
423 list(APPEND SANITIZER_COMMON_CFLAGS -O1)
425 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
429 # Determine if we should restrict stack frame sizes.
430 # Stack frames on PowerPC, Mips, SystemZ and in debug build can be much larger than
432 # FIXME: Fix all sanitizers and add -Wframe-larger-than to
433 # SANITIZER_COMMON_FLAGS
434 if(COMPILER_RT_HAS_WFRAME_LARGER_THAN_FLAG AND NOT COMPILER_RT_DEBUG
435 AND NOT ${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "powerpc|mips|s390x")
436 set(SANITIZER_LIMIT_FRAME_SIZE TRUE)
438 set(SANITIZER_LIMIT_FRAME_SIZE FALSE)
442 set(SANITIZER_USE_SYMBOLS TRUE)
444 set(SANITIZER_USE_SYMBOLS FALSE)
447 # Build sanitizer runtimes with debug info.
449 # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
450 # warning from the MS linker complaining that it can't find the 'vc140.pdb'
451 # file used by our object library compilations.
452 list(APPEND SANITIZER_COMMON_CFLAGS /Z7)
453 foreach(var_to_update
455 CMAKE_CXX_FLAGS_DEBUG
456 CMAKE_CXX_FLAGS_RELWITHDEBINFO)
457 string(REGEX REPLACE "(^| )/Z[i7I]($| )" " /Z7 "
458 "${var_to_update}" "${${var_to_update}}")
461 # On Apple platforms use full debug info (i.e. not `-gline-tables-only`)
462 # for all build types so that the runtime can be debugged.
463 if(NOT COMPILER_RT_HAS_G_FLAG)
464 message(FATAL_ERROR "-g is not supported by host compiler")
466 list(APPEND SANITIZER_COMMON_CFLAGS -g)
467 elseif(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
468 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
469 elseif(COMPILER_RT_HAS_G_FLAG)
470 list(APPEND SANITIZER_COMMON_CFLAGS -g)
473 if(LLVM_ENABLE_MODULES)
474 # Sanitizers cannot be built with -fmodules. The interceptors intentionally
475 # don't include system headers, which is incompatible with modules.
476 list(APPEND SANITIZER_COMMON_CFLAGS -fno-modules)
479 # Turn off several warnings.
480 append_list_if(COMPILER_RT_HAS_WGNU_FLAG -Wno-gnu SANITIZER_COMMON_CFLAGS)
481 append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros SANITIZER_COMMON_CFLAGS)
482 append_list_if(COMPILER_RT_HAS_WC99_EXTENSIONS_FLAG -Wno-c99-extensions SANITIZER_COMMON_CFLAGS)
483 # format-pedantic warns about passing T* for %p, which is not useful.
484 append_list_if(COMPILER_RT_HAS_WD4146_FLAG /wd4146 SANITIZER_COMMON_CFLAGS)
485 append_list_if(COMPILER_RT_HAS_WD4291_FLAG /wd4291 SANITIZER_COMMON_CFLAGS)
486 append_list_if(COMPILER_RT_HAS_WD4391_FLAG /wd4391 SANITIZER_COMMON_CFLAGS)
487 append_list_if(COMPILER_RT_HAS_WD4722_FLAG /wd4722 SANITIZER_COMMON_CFLAGS)
488 append_list_if(COMPILER_RT_HAS_WD4800_FLAG /wd4800 SANITIZER_COMMON_CFLAGS)
490 append_list_if(MINGW -fms-extensions SANITIZER_COMMON_CFLAGS)
492 # When lsan scans the stack for detecting reachable pointers, it's possible for
493 # a leaked pointer, which was pushed to the stack on an earlier function call,
494 # to still exist on the stack when doing a leak check if that part of the stack
495 # was not overwritten. In particular, if there's any uninitialized data in the
496 # lsan runtime, and the SP we start from is sufficiently deep into the runtime,
497 # then a leaked pointer could be marked as reachable. Such instances could be
498 # mitigated by clobbering any uninitialized data. Note that this won't cover
499 # all possible uninitialized stack contents, such as those used for register
500 # spill slots, unused portions for alignment, or even local variables not
501 # yet in scope at a certain point in the function.
503 # Note that this type of issue was discovered with lsan, but can apply to other
505 append_list_if(COMPILER_RT_HAS_TRIVIAL_AUTO_INIT -ftrivial-auto-var-init=pattern SANITIZER_COMMON_CFLAGS)
507 # Set common link flags.
508 # TODO: We should consider using the same model as libc++, that is use either
509 # -nostdlib++ and --unwindlib=none if supported, or -nodefaultlibs otherwise.
510 append_list_if(C_SUPPORTS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMON_LINK_FLAGS)
511 append_list_if(COMPILER_RT_HAS_Z_TEXT -Wl,-z,text SANITIZER_COMMON_LINK_FLAGS)
513 # Only necessary for 32-bit SPARC. Solaris 11.2+ ld uses -z ignore/-z record
514 # natively, but supports --as-needed/--no-as-needed for GNU ld compatibility.
515 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "sparc")
516 list(APPEND SANITIZER_COMMON_LINK_LIBS -Wl,--as-needed atomic -Wl,--no-as-needed)
519 if (COMPILER_RT_USE_BUILTINS_LIBRARY)
520 string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
523 append_list_if(COMPILER_RT_HAS_GCC_LIB gcc SANITIZER_COMMON_LINK_LIBS)
525 append_list_if(COMPILER_RT_HAS_GCC_S_LIB gcc_s SANITIZER_COMMON_LINK_LIBS)
529 append_list_if(COMPILER_RT_HAS_LIBC c SANITIZER_COMMON_LINK_LIBS)
530 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
531 list(APPEND SANITIZER_COMMON_LINK_LIBS zircon)
534 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
535 set(SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT ON)
537 set(SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT OFF)
539 option(SANITIZER_NO_UNDEFINED_SYMBOLS "Report error on unresolved symbol references" ${SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT})
540 if (SANITIZER_NO_UNDEFINED_SYMBOLS)
541 list(APPEND SANITIZER_COMMON_LINK_FLAGS -Wl,-z,defs)
544 # TODO: COMPILER_RT_COMMON_CFLAGS and COMPILER_RT_COMMON_LINK_FLAGS are
545 # intended for use in non-sanitizer runtimes such as libFuzzer, profile or XRay,
546 # move these higher to include common flags, then derive SANITIZER_COMMON_CFLAGS
547 # and SANITIZER_COMMON_LINK_FLAGS from those and append sanitizer-specific flags.
548 set(COMPILER_RT_COMMON_CFLAGS ${SANITIZER_COMMON_CFLAGS})
549 set(COMPILER_RT_COMMON_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
551 # We don't use the C++ standard library, so avoid including it by mistake.
552 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ SANITIZER_COMMON_CFLAGS)
553 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ SANITIZER_COMMON_LINK_FLAGS)
555 # Remove -stdlib= which is unused when passing -nostdinc++...
556 string(REGEX MATCHALL "-stdlib=[a-zA-Z+]*" stdlib_flag "${CMAKE_CXX_FLAGS}")
557 string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
559 # ...we need it to build some runtimes and tests so readd it where appropriate.
560 list(APPEND COMPILER_RT_COMMON_CFLAGS ${stdlib_flag})
561 list(APPEND COMPILER_RT_COMMON_LINK_FLAGS ${stdlib_flag})
563 # TODO: There's a lot of duplication across lib/*/tests/CMakeLists.txt files,
564 # move some of the common flags to COMPILER_RT_UNITTEST_CFLAGS.
566 # Unittests need access to C++ standard library.
567 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " ${stdlib_flag}")
569 # When cross-compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help in compilation
570 # and linking of unittests.
571 string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
572 set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
574 if(COMPILER_RT_USE_LLVM_UNWINDER)
575 # We're linking directly against the libunwind that we're building so don't
576 # try to link in the toolchain's default libunwind which may be missing.
577 append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none COMPILER_RT_COMMON_LINK_FLAGS)
578 append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none COMPILER_RT_UNITTEST_LINK_FLAGS)
579 if (COMPILER_RT_ENABLE_STATIC_UNWINDER)
580 list(APPEND COMPILER_RT_UNWINDER_LINK_LIBS "$<TARGET_LINKER_FILE:unwind_static>")
582 list(APPEND COMPILER_RT_UNWINDER_LINK_LIBS "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:unwind_shared>,unwind_shared,unwind_static>>")
586 if (COMPILER_RT_CXX_LIBRARY STREQUAL "libcxx")
587 # We are using the in-tree libc++ so avoid including the default one.
588 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ COMPILER_RT_COMMON_CFLAGS)
589 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ COMPILER_RT_COMMON_LINK_FLAGS)
590 # Use the in-tree libc++ through explicit include and library paths.
591 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>>>")
592 if (COMPILER_RT_STATIC_CXX_LIBRARY)
593 set(COMPILER_RT_CXX_LINK_LIBS "$<TARGET_LINKER_FILE:cxx_static>")
595 set(COMPILER_RT_CXX_LINK_LIBS "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:cxx_shared>,cxx_shared,cxx_static>>")
597 elseif (COMPILER_RT_CXX_LIBRARY STREQUAL "none")
598 # We aren't using any C++ standard library so avoid including the default one.
599 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ COMPILER_RT_COMMON_CFLAGS)
600 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ COMPILER_RT_COMMON_LINK_FLAGS)
602 # Nothing to be done for `default`.
605 if (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libc++")
606 if (SANITIZER_CXX_ABI_INTREE)
607 # TODO: We don't need to add --unwindlib=none to SANITIZER_COMMON_LINK_FLAGS
608 # because we added -nodefaultlibs there earlier, and adding would result in
609 # a warning, but if we switch to -nostdlib++, we would need to add it here.
610 # append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none SANITIZER_COMMON_LINK_FLAGS)
611 if(SANITIZER_USE_STATIC_CXX_ABI)
612 if(TARGET libcxx-abi-static)
613 set(SANITIZER_CXX_ABI_LIBRARIES libcxx-abi-static)
616 if(TARGET libcxx-abi-shared)
617 set(SANITIZER_CXX_ABI_LIBRARIES libcxx-abi-shared)
618 elseif(TARGET libcxx-abi-static)
619 set(SANITIZER_CXX_ABI_LIBRARIES libcxx-abi-static)
623 append_list_if(COMPILER_RT_HAS_LIBCXX c++ SANITIZER_CXX_ABI_LIBRARIES)
625 elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libcxxabi")
626 list(APPEND SANITIZER_CXX_ABI_LIBRARIES "c++abi")
627 elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libstdc++")
628 append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ SANITIZER_CXX_ABI_LIBRARIES)
631 if (SANITIZER_TEST_CXX_LIBNAME STREQUAL "libc++")
632 if (SANITIZER_TEST_CXX_INTREE)
633 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>>>")
634 if (SANITIZER_USE_STATIC_TEST_CXX)
635 list(APPEND SANITIZER_TEST_CXX_LIBRARIES "$<TARGET_LINKER_FILE:cxx_static>")
637 list(APPEND SANITIZER_TEST_CXX_LIBRARIES "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:cxx_shared>,cxx_shared,cxx_static>>")
639 # We are using the in tree libc++ so avoid including the default one.
640 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ COMPILER_RT_UNITTEST_CFLAGS)
641 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ COMPILER_RT_UNITTEST_LINK_FLAGS)
643 append_list_if(COMPILER_RT_HAS_LIBCXX -lc++ SANITIZER_TEST_CXX_LIBRARIES)
645 elseif (SANITIZER_TEST_CXX_LIBNAME STREQUAL "libstdc++")
646 append_list_if(COMPILER_RT_HAS_LIBSTDCXX -lstdc++ SANITIZER_TEST_CXX_LIBRARIES)
650 # FIXME: When compiler-rt is build using -DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON, then
651 # The LLVM_THIRD_PARTY_DIR variable is not set.
652 if (NOT LLVM_THIRD_PARTY_DIR)
653 set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
656 set(COMPILER_RT_GTEST_PATH ${LLVM_THIRD_PARTY_DIR}/unittest/googletest)
657 set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
658 set(COMPILER_RT_GTEST_CFLAGS
659 -DGTEST_NO_LLVM_SUPPORT=1
661 -I${COMPILER_RT_GTEST_PATH}/include
662 -I${COMPILER_RT_GTEST_PATH}
666 set(COMPILER_RT_GMOCK_PATH ${LLVM_THIRD_PARTY_DIR}/unittest/googlemock)
667 set(COMPILER_RT_GMOCK_SOURCE ${COMPILER_RT_GMOCK_PATH}/src/gmock-all.cc)
668 set(COMPILER_RT_GMOCK_CFLAGS
669 -DGTEST_NO_LLVM_SUPPORT=1
671 -I${COMPILER_RT_GMOCK_PATH}/include
672 -I${COMPILER_RT_GMOCK_PATH}
675 if(COMPILER_RT_HAS_G_FLAG)
676 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -g)
678 append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
679 append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS)
680 append_list_if(COMPILER_RT_HAS_WSUGGEST_OVERRIDE_FLAG -Wno-suggest-override COMPILER_RT_UNITTEST_CFLAGS)
683 # gtest use a lot of stuff marked as deprecated on Windows.
684 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
687 # Warnings to turn off for all libraries, not just sanitizers.
688 append_string_if(COMPILER_RT_HAS_WUNUSED_PARAMETER_FLAG -Wno-unused-parameter CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
690 if (CMAKE_LINKER MATCHES "link.exe$")
691 # Silence MSVC linker warnings caused by empty object files. The
692 # sanitizer libraries intentionally use ifdefs that result in empty
693 # files, rather than skipping these files in the build system.
694 # Ideally, we would pass this flag only for the libraries that need
695 # it, but CMake doesn't seem to have a way to set linker flags for
696 # individual static libraries, so we enable the suppression flag for
697 # the whole compiler-rt project.
698 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
701 add_subdirectory(include)
703 option(COMPILER_RT_USE_LIBCXX
704 "Enable compiler-rt to use libc++ from the source tree" ON)
705 if(COMPILER_RT_USE_LIBCXX)
706 if(LLVM_ENABLE_PROJECTS_USED)
707 # Don't use libcxx if LLVM_ENABLE_PROJECTS does not enable it.
708 set(COMPILER_RT_LIBCXX_PATH ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR})
709 set(COMPILER_RT_LIBCXXABI_PATH ${LLVM_EXTERNAL_LIBCXXABI_SOURCE_DIR})
711 foreach(path IN ITEMS ${LLVM_MAIN_SRC_DIR}/projects/libcxx
712 ${LLVM_MAIN_SRC_DIR}/runtimes/libcxx
713 ${LLVM_MAIN_SRC_DIR}/../libcxx
714 ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR})
715 if(IS_DIRECTORY ${path})
716 set(COMPILER_RT_LIBCXX_PATH ${path})
720 foreach(path IN ITEMS ${LLVM_MAIN_SRC_DIR}/projects/libcxxabi
721 ${LLVM_MAIN_SRC_DIR}/runtimes/libcxxabi
722 ${LLVM_MAIN_SRC_DIR}/../libcxxabi
723 ${LLVM_EXTERNAL_LIBCXXABI_SOURCE_DIR})
724 if(IS_DIRECTORY ${path})
725 set(COMPILER_RT_LIBCXXABI_PATH ${path})
732 if (LINKER_IS_LLD OR LLVM_TOOL_LLD_BUILD)
733 set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/tools/lld)
734 if(EXISTS ${COMPILER_RT_LLD_PATH}/)
735 set(COMPILER_RT_HAS_LLD TRUE)
737 set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/../lld)
738 if(EXISTS ${COMPILER_RT_LLD_PATH}/)
739 set(COMPILER_RT_HAS_LLD TRUE)
745 set(COMPILER_RT_HAS_LLD TRUE)
746 set(COMPILER_RT_TEST_USE_LLD TRUE)
747 append_list_if(COMPILER_RT_HAS_FUSE_LD_LLD_FLAG -fuse-ld=lld SANITIZER_COMMON_LINK_FLAGS)
748 append_list_if(COMPILER_RT_HAS_LLD -fuse-ld=lld COMPILER_RT_UNITTEST_LINK_FLAGS)
750 pythonize_bool(COMPILER_RT_HAS_LLD)
751 pythonize_bool(COMPILER_RT_TEST_USE_LLD)
753 option(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER "Build Compiler RT linked with in LLVM symbolizer" OFF)
754 mark_as_advanced(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER)
756 add_subdirectory(lib)
758 if(COMPILER_RT_INCLUDE_TESTS)
759 add_subdirectory(unittests)
760 add_subdirectory(test)
761 # Don't build llvm-lit for runtimes-build, it will clean up map_config.
762 if (COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
763 # If we have a valid source tree, generate llvm-lit into the bin directory.
764 # The user can still choose to have the check targets *use* a different lit
765 # by specifying -DLLVM_EXTERNAL_LIT, but we generate it regardless.
766 if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
767 # Needed for lit support in standalone builds.
769 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
770 elseif(NOT EXISTS ${LLVM_EXTERNAL_LIT})
771 message(WARNING "Could not find LLVM source directory and LLVM_EXTERNAL_LIT does not"
772 "point to a valid file. You will not be able to run tests.")
777 add_subdirectory(tools)