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.13.4)
8 # Check if compiler-rt is built as a standalone project.
9 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR COMPILER_RT_STANDALONE_BUILD)
10 project(CompilerRT C CXX ASM)
11 set(COMPILER_RT_STANDALONE_BUILD TRUE)
12 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
15 # Add path for custom compiler-rt modules.
16 list(INSERT CMAKE_MODULE_PATH 0
17 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
18 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
21 if(CMAKE_CONFIGURATION_TYPES)
22 set(CMAKE_CFG_RESOLVED_INTDIR "${CMAKE_CFG_INTDIR}/")
24 set(CMAKE_CFG_RESOLVED_INTDIR "")
27 include(base-config-ix)
28 include(CompilerRTUtils)
30 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
31 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
32 option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON)
33 mark_as_advanced(COMPILER_RT_BUILD_CRT)
34 option(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY "Use eh_frame in crtbegin.o/crtend.o" ON)
35 mark_as_advanced(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY)
36 option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON)
37 mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS)
38 option(COMPILER_RT_BUILD_XRAY "Build xray" ON)
39 mark_as_advanced(COMPILER_RT_BUILD_XRAY)
40 option(COMPILER_RT_BUILD_LIBFUZZER "Build libFuzzer" ON)
41 mark_as_advanced(COMPILER_RT_BUILD_LIBFUZZER)
42 option(COMPILER_RT_BUILD_PROFILE "Build profile runtime" ON)
43 mark_as_advanced(COMPILER_RT_BUILD_PROFILE)
44 option(COMPILER_RT_BUILD_MEMPROF "Build memory profiling runtime" ON)
45 mark_as_advanced(COMPILER_RT_BUILD_MEMPROF)
46 option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF)
47 mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT)
48 option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON)
49 mark_as_advanced(COMPILER_RT_BUILD_ORC)
50 option(COMPILER_RT_BUILD_GWP_ASAN "Build GWP-ASan, and link it into SCUDO" ON)
51 mark_as_advanced(COMPILER_RT_BUILD_GWP_ASAN)
53 set(COMPILER_RT_ASAN_SHADOW_SCALE ""
54 CACHE STRING "Override the shadow scale to be used in ASan runtime")
56 if (NOT COMPILER_RT_ASAN_SHADOW_SCALE STREQUAL "")
57 # Check that the shadow scale value is valid.
58 if (NOT (COMPILER_RT_ASAN_SHADOW_SCALE GREATER -1 AND
59 COMPILER_RT_ASAN_SHADOW_SCALE LESS 8))
61 Invalid ASan Shadow Scale '${COMPILER_RT_ASAN_SHADOW_SCALE}'.")
64 set(COMPILER_RT_ASAN_SHADOW_SCALE_LLVM_FLAG
65 -mllvm -asan-mapping-scale=${COMPILER_RT_ASAN_SHADOW_SCALE})
66 set(COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION
67 ASAN_SHADOW_SCALE=${COMPILER_RT_ASAN_SHADOW_SCALE})
68 set(COMPILER_RT_ASAN_SHADOW_SCALE_FLAG
69 -D${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION})
73 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT OFF)
75 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT ON)
77 set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS ${COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT} CACHE BOOL "Enable libc interceptors in HWASan (testing mode)")
79 set(COMPILER_RT_BAREMETAL_BUILD OFF CACHE BOOL
80 "Build for a bare-metal target.")
82 if (COMPILER_RT_STANDALONE_BUILD)
84 if (TARGET intrinsics_gen)
85 # Loading the llvm config causes this target to be imported so place it
86 # under the appropriate folder in an IDE.
87 set_target_properties(intrinsics_gen PROPERTIES FOLDER "Compiler-RT Misc")
90 find_package(Python3 COMPONENTS Interpreter)
91 if(NOT Python3_Interpreter_FOUND)
92 message(WARNING "Python3 not found, using python2 as a fallback")
93 find_package(Python2 COMPONENTS Interpreter REQUIRED)
94 if(Python2_VERSION VERSION_LESS 2.7)
95 message(SEND_ERROR "Python 2.7 or newer is required")
98 # Treat python2 as python3
99 add_executable(Python3::Interpreter IMPORTED)
100 set_target_properties(Python3::Interpreter PROPERTIES
101 IMPORTED_LOCATION ${Python2_EXECUTABLE})
102 set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
105 # Ensure that fat libraries are built correctly on Darwin
110 # Define default arguments to lit.
111 set(LIT_ARGS_DEFAULT "-sv")
113 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
115 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
116 set(LLVM_LIT_OUTPUT_DIR "${COMPILER_RT_EXEC_OUTPUT_DIR}")
119 construct_compiler_rt_default_triple()
120 if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*hf$")
121 if (${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "^arm")
122 set(COMPILER_RT_DEFAULT_TARGET_ARCH "armhf")
123 CHECK_SYMBOL_EXISTS (__thumb__ "" COMPILER_RT_ARM_THUMB)
126 if ("${COMPILER_RT_DEFAULT_TARGET_TRIPLE}" MATCHES ".*android.*")
128 string(REGEX MATCH "-target(=| +)[^ ]+android[a-z]*([0-9]+)" ANDROID_API_LEVEL "${CMAKE_C_FLAGS}")
129 set(ANDROID_API_LEVEL ${CMAKE_MATCH_2})
131 pythonize_bool(ANDROID)
133 set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
134 set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
136 pythonize_bool(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
138 # We support running instrumented tests when we're not cross compiling
139 # and target a UNIX-like system or Windows.
140 # We can run tests on Android even when we are cross-compiling.
141 if(("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND (UNIX OR WIN32)) OR ANDROID
142 OR COMPILER_RT_EMULATOR)
143 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
145 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
148 option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
149 option(COMPILER_RT_EXTERNALIZE_DEBUGINFO
150 "Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
151 # COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
152 pythonize_bool(COMPILER_RT_DEBUG)
154 option(COMPILER_RT_INTERCEPT_LIBDISPATCH
155 "Support interception of libdispatch (GCD). Requires '-fblocks'" OFF)
156 option(COMPILER_RT_LIBDISPATCH_INSTALL_PATH
157 "Specify if libdispatch is installed in a custom location" "")
158 if (COMPILER_RT_INTERCEPT_LIBDISPATCH AND NOT APPLE)
159 set(COMPILER_RT_LIBDISPATCH_CFLAGS -fblocks)
160 set(COMPILER_RT_TEST_LIBDISPATCH_CFLAGS)
161 if (COMPILER_RT_LIBDISPATCH_INSTALL_PATH)
162 list(APPEND COMPILER_RT_TEST_LIBDISPATCH_CFLAGS
163 -I${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/include
164 -L${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/lib
165 -Wl,-rpath=${COMPILER_RT_LIBDISPATCH_INSTALL_PATH}/lib)
167 list(APPEND COMPILER_RT_TEST_LIBDISPATCH_CFLAGS -lBlocksRuntime -ldispatch)
169 if (APPLE) # Always enable on Apple platforms.
170 set(COMPILER_RT_INTERCEPT_LIBDISPATCH ON)
172 pythonize_bool(COMPILER_RT_INTERCEPT_LIBDISPATCH)
174 if(APPLE AND SANITIZER_MIN_OSX_VERSION AND SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.9")
175 # Mac OS X prior to 10.9 had problems with exporting symbols from
177 set(cxxabi_supported OFF)
179 set(cxxabi_supported ON)
182 option(SANITIZER_ALLOW_CXXABI "Allow use of C++ ABI details in ubsan" ON)
184 set(SANITIZER_CAN_USE_CXXABI OFF)
185 if (cxxabi_supported AND SANITIZER_ALLOW_CXXABI)
186 set(SANITIZER_CAN_USE_CXXABI ON)
188 pythonize_bool(SANITIZER_CAN_USE_CXXABI)
190 macro(handle_default_cxx_lib var)
191 # Specifying -stdlib= in CMAKE_CXX_FLAGS overrides the defaults.
192 if (CMAKE_CXX_FLAGS MATCHES "-stdlib=([a-zA-Z+]*)")
193 set(${var}_LIBNAME "${CMAKE_MATCH_1}")
195 elseif (${var} STREQUAL "default")
196 if (APPLE OR CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
197 set(${var}_LIBNAME "libc++")
200 set(${var}_LIBNAME "libc++")
203 set(${var}_LIBNAME "libstdc++")
207 set(${var}_LIBNAME "${${var}}")
212 # This is either directly the C++ ABI library or the full C++ library
213 # which pulls in the ABI transitively.
214 set(SANITIZER_CXX_ABI "default" CACHE STRING
215 "Specify C++ ABI library to use.")
216 set(CXXABIS none default libstdc++ libc++ libcxxabi)
217 set_property(CACHE SANITIZER_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
218 handle_default_cxx_lib(SANITIZER_CXX_ABI)
220 # This needs to be a full C++ library for linking gtest and unit tests.
221 set(SANITIZER_TEST_CXX "default" CACHE STRING
222 "Specify C++ library to use for tests.")
223 set(CXXLIBS none default libstdc++ libc++)
224 set_property(CACHE SANITIZER_TEST_CXX PROPERTY STRINGS ;${CXXLIBS})
225 handle_default_cxx_lib(SANITIZER_TEST_CXX)
227 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER OFF)
229 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER ON)
230 elseif (DEFINED LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_SHARED)
231 set(DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER ON)
234 option(SANITIZER_USE_STATIC_LLVM_UNWINDER
235 "Use static LLVM unwinder." ${DEFAULT_SANITIZER_USE_STATIC_LLVM_UNWINDER})
237 set(DEFAULT_SANITIZER_USE_STATIC_CXX_ABI OFF)
238 if (DEFINED LIBCXXABI_ENABLE_SHARED AND NOT LIBCXXABI_ENABLE_SHARED)
239 set(DEFAULT_SANITIZER_USE_STATIC_CXX_ABI ON)
242 option(SANITIZER_USE_STATIC_CXX_ABI
243 "Use static libc++abi." ${DEFAULT_SANITIZER_USE_STATIC_CXX_ABI})
245 set(DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY OFF)
247 set(DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY ON)
250 option(COMPILER_RT_USE_BUILTINS_LIBRARY
251 "Use compiler-rt builtins instead of libgcc" ${DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY})
255 #================================
256 # Setup Compiler Flags
257 #================================
260 # Override any existing /W flags with /W4. This is what LLVM does. Failing to
261 # remove other /W[0-4] flags will result in a warning about overriding a
263 if (COMPILER_RT_HAS_W4_FLAG)
264 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
265 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
266 append_string_if(COMPILER_RT_HAS_W4_FLAG /W4 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
269 append_string_if(COMPILER_RT_HAS_WALL_FLAG -Wall CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
271 if(COMPILER_RT_ENABLE_WERROR)
272 append_string_if(COMPILER_RT_HAS_WERROR_FLAG -Werror CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
273 append_string_if(COMPILER_RT_HAS_WX_FLAG /WX CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
276 append_string_if(COMPILER_RT_HAS_STD_CXX14_FLAG -std=c++14 CMAKE_CXX_FLAGS)
278 # Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP.
279 if(NOT COMPILER_RT_HAS_FUNC_SYMBOL)
280 add_definitions(-D__func__=__FUNCTION__)
283 # Provide some common commandline flags for Sanitizer runtimes.
284 if("${ANDROID_API_LEVEL}" GREATER_EQUAL 29)
285 list(APPEND SANITIZER_COMMON_CFLAGS -fno-emulated-tls)
286 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " -fno-emulated-tls")
289 append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC SANITIZER_COMMON_CFLAGS)
291 append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin SANITIZER_COMMON_CFLAGS)
292 append_list_if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions SANITIZER_COMMON_CFLAGS)
293 if(NOT COMPILER_RT_DEBUG AND NOT APPLE)
294 append_list_if(COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer SANITIZER_COMMON_CFLAGS)
296 append_list_if(COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables SANITIZER_COMMON_CFLAGS)
297 append_list_if(COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector SANITIZER_COMMON_CFLAGS)
298 append_list_if(COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG -fno-sanitize=safe-stack SANITIZER_COMMON_CFLAGS)
299 append_list_if(COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden SANITIZER_COMMON_CFLAGS)
300 if(NOT COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG)
301 append_list_if(COMPILER_RT_HAS_FVISIBILITY_INLINES_HIDDEN_FLAG -fvisibility-inlines-hidden SANITIZER_COMMON_CFLAGS)
303 append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto SANITIZER_COMMON_CFLAGS)
305 # By default do not instrument or use profdata for compiler-rt.
306 if(NOT COMPILER_RT_ENABLE_PGO)
307 if(LLVM_PROFDATA_FILE AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
308 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-instr-use")
310 if(LLVM_BUILD_INSTRUMENTED MATCHES IR AND COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
311 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-generate")
312 elseif(LLVM_BUILD_INSTRUMENTED AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
313 list(APPEND SANITIZER_COMMON_CFLAGS "-fno-profile-instr-generate")
317 # The following is a workaround for powerpc64le. This is the only architecture
318 # that requires -fno-function-sections to work properly. If lacking, the ASan
319 # Linux test function-sections-are-bad.cpp fails with the following error:
320 # 'undefined symbol: __sanitizer_unaligned_load32'.
321 if(DEFINED TARGET_powerpc64le_CFLAGS)
322 if(CMAKE_CXX_COMPILER_ID MATCHES "XL")
323 append("-qnofuncsect" TARGET_powerpc64le_CFLAGS)
325 append_list_if(COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections TARGET_powerpc64le_CFLAGS)
329 # The following is a workaround for s390x. This avoids creation of "partial
330 # inline" function fragments when building the asan libraries with certain
331 # GCC versions. The presence of those fragments, in particular for the
332 # interceptors, changes backtraces seen in asan error cases, which causes
333 # testsuite failures.
334 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
335 append_list_if(COMPILER_RT_HAS_FNO_PARTIAL_INLINING_FLAG -fno-partial-inlining SANITIZER_COMMON_CFLAGS)
339 # Replace the /M[DT][d] flags with /MT, and strip any definitions of _DEBUG,
340 # which cause definition mismatches at link time.
341 # FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214.
342 if(COMPILER_RT_HAS_MT_FLAG)
344 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
345 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
346 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
347 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
348 string(REGEX REPLACE "/M[DT]d" "/MT" ${flag_var} "${${flag_var}}")
349 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
350 string(REGEX REPLACE "/D_DEBUG" "" ${flag_var} "${${flag_var}}")
353 append_list_if(COMPILER_RT_HAS_Oy_FLAG /Oy- SANITIZER_COMMON_CFLAGS)
354 append_list_if(COMPILER_RT_HAS_GS_FLAG /GS- SANITIZER_COMMON_CFLAGS)
356 # Disable thread safe initialization for static locals. ASan shouldn't need
357 # it. Thread safe initialization assumes that the CRT has already been
358 # initialized, but ASan initializes before the CRT.
359 list(APPEND SANITIZER_COMMON_CFLAGS /Zc:threadSafeInit-)
362 append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 SANITIZER_COMMON_CFLAGS)
364 if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
365 list(APPEND THREAD_SAFETY_FLAGS
367 "-Wthread-safety-reference"
368 "-Wthread-safety-beta"
370 list(APPEND SANITIZER_COMMON_CFLAGS ${THREAD_SAFETY_FLAGS})
371 string(REPLACE ";" " " thread_safety_flags_space_sep "${THREAD_SAFETY_FLAGS}")
372 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " ${thread_safety_flags_space_sep}")
375 # If we're using MSVC,
376 # always respect the optimization flags set by CMAKE_BUILD_TYPE instead.
379 # Build with optimization, unless we're in debug mode.
380 if(COMPILER_RT_DEBUG)
381 list(APPEND SANITIZER_COMMON_CFLAGS -O1)
383 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
387 # Determine if we should restrict stack frame sizes.
388 # Stack frames on PowerPC, Mips, SystemZ and in debug build can be much larger than
390 # FIXME: Fix all sanitizers and add -Wframe-larger-than to
391 # SANITIZER_COMMON_FLAGS
392 if(COMPILER_RT_HAS_WFRAME_LARGER_THAN_FLAG AND NOT COMPILER_RT_DEBUG
393 AND NOT ${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "powerpc|mips|s390x")
394 set(SANITIZER_LIMIT_FRAME_SIZE TRUE)
396 set(SANITIZER_LIMIT_FRAME_SIZE FALSE)
400 set(SANITIZER_USE_SYMBOLS TRUE)
402 set(SANITIZER_USE_SYMBOLS FALSE)
405 # Build sanitizer runtimes with debug info.
407 # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
408 # warning from the MS linker complaining that it can't find the 'vc140.pdb'
409 # file used by our object library compilations.
410 list(APPEND SANITIZER_COMMON_CFLAGS /Z7)
411 foreach(var_to_update
413 CMAKE_CXX_FLAGS_DEBUG
414 CMAKE_CXX_FLAGS_RELWITHDEBINFO)
415 string(REGEX REPLACE "(^| )/Z[i7I]($| )" " /Z7 "
416 "${var_to_update}" "${${var_to_update}}")
419 # On Apple platforms use full debug info (i.e. not `-gline-tables-only`)
420 # for all build types so that the runtime can be debugged.
421 if(NOT COMPILER_RT_HAS_G_FLAG)
422 message(FATAL_ERROR "-g is not supported by host compiler")
424 list(APPEND SANITIZER_COMMON_CFLAGS -g)
425 elseif(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
426 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
427 elseif(COMPILER_RT_HAS_G_FLAG)
428 list(APPEND SANITIZER_COMMON_CFLAGS -g)
431 if(LLVM_ENABLE_MODULES)
432 # Sanitizers cannot be built with -fmodules. The interceptors intentionally
433 # don't include system headers, which is incompatible with modules.
434 list(APPEND SANITIZER_COMMON_CFLAGS -fno-modules)
437 # Turn off several warnings.
438 append_list_if(COMPILER_RT_HAS_WGNU_FLAG -Wno-gnu SANITIZER_COMMON_CFLAGS)
439 append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros SANITIZER_COMMON_CFLAGS)
440 append_list_if(COMPILER_RT_HAS_WC99_EXTENSIONS_FLAG -Wno-c99-extensions SANITIZER_COMMON_CFLAGS)
441 # format-pedantic warns about passing T* for %p, which is not useful.
442 append_list_if(COMPILER_RT_HAS_WNO_FORMAT_PEDANTIC -Wno-format-pedantic SANITIZER_COMMON_CFLAGS)
443 append_list_if(COMPILER_RT_HAS_WD4146_FLAG /wd4146 SANITIZER_COMMON_CFLAGS)
444 append_list_if(COMPILER_RT_HAS_WD4291_FLAG /wd4291 SANITIZER_COMMON_CFLAGS)
445 append_list_if(COMPILER_RT_HAS_WD4391_FLAG /wd4391 SANITIZER_COMMON_CFLAGS)
446 append_list_if(COMPILER_RT_HAS_WD4722_FLAG /wd4722 SANITIZER_COMMON_CFLAGS)
447 append_list_if(COMPILER_RT_HAS_WD4800_FLAG /wd4800 SANITIZER_COMMON_CFLAGS)
449 append_list_if(MINGW -fms-extensions SANITIZER_COMMON_CFLAGS)
451 # Set common link flags.
452 append_list_if(COMPILER_RT_HAS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMON_LINK_FLAGS)
453 append_list_if(COMPILER_RT_HAS_Z_TEXT -Wl,-z,text SANITIZER_COMMON_LINK_FLAGS)
455 if (COMPILER_RT_USE_BUILTINS_LIBRARY)
456 string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
459 append_list_if(COMPILER_RT_HAS_GCC_LIB gcc SANITIZER_COMMON_LINK_LIBS)
461 append_list_if(COMPILER_RT_HAS_GCC_S_LIB gcc_s SANITIZER_COMMON_LINK_LIBS)
465 append_list_if(COMPILER_RT_HAS_LIBC c SANITIZER_COMMON_LINK_LIBS)
466 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
467 list(APPEND SANITIZER_COMMON_LINK_LIBS zircon)
470 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
471 set(SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT ON)
473 set(SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT OFF)
475 option(SANITIZER_NO_UNDEFINED_SYMBOLS "Report error on unresolved symbol references" ${SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT})
476 if (SANITIZER_NO_UNDEFINED_SYMBOLS)
477 list(APPEND SANITIZER_COMMON_LINK_FLAGS -Wl,-z,defs)
480 # TODO: COMPILER_RT_COMMON_CFLAGS and COMPILER_RT_COMMON_LINK_FLAGS are
481 # intended for use in non-sanitizer runtimes such as libFuzzer, profile or XRay,
482 # move these higher to include common flags, then derive SANITIZER_COMMON_CFLAGS
483 # and SANITIZER_COMMON_LINK_FLAGS from those and append sanitizer-specific flags.
484 set(COMPILER_RT_COMMON_CFLAGS ${SANITIZER_COMMON_CFLAGS})
485 set(COMPILER_RT_COMMON_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
487 # We don't use the C++ standard library, so avoid including it by mistake.
488 append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ SANITIZER_COMMON_CFLAGS)
489 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ SANITIZER_COMMON_LINK_FLAGS)
491 # Remove -stdlib= which is unused when passing -nostdinc++...
492 string(REGEX MATCHALL "-stdlib=[a-zA-Z+]*" stdlib_flag "${CMAKE_CXX_FLAGS}")
493 string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
495 # ...we need it to build some runtimes and tests so readd it where appropriate.
496 list(APPEND COMPILER_RT_COMMON_CFLAGS ${stdlib_flag})
497 list(APPEND COMPILER_RT_COMMON_LINK_FLAGS ${stdlib_flag})
499 macro(append_libcxx_libs var)
501 if (SANITIZER_USE_STATIC_LLVM_UNWINDER AND (TARGET unwind_static OR HAVE_LIBUNWIND))
502 list(APPEND ${var}_LIBRARIES unwind_static)
503 elseif (TARGET unwind_shared OR HAVE_LIBUNWIND)
504 list(APPEND ${var}_LIBRARIES unwind_shared)
507 if (SANITIZER_USE_STATIC_CXX_ABI AND (TARGET cxxabi_static OR HAVE_LIBCXXABI))
508 list(APPEND ${var}_LIBRARIES cxxabi_static)
509 elseif (TARGET cxxabi_shared OR HAVE_LIBCXXABI)
510 list(APPEND ${var}_LIBRARIES cxxabi_shared)
513 append_list_if(COMPILER_RT_HAS_LIBCXX c++ ${var}_LIBRARIES)
517 if (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libc++")
518 append_libcxx_libs(SANITIZER_CXX_ABI)
519 elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libcxxabi")
520 list(APPEND SANITIZER_CXX_ABI_LIBRARIES "c++abi")
521 elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libstdc++")
522 append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ SANITIZER_CXX_ABI_LIBRARIES)
525 if (SANITIZER_TEST_CXX_LIBNAME STREQUAL "libc++")
526 append_libcxx_libs(SANITIZER_TEST_CXX)
527 elseif (SANITIZER_TEST_CXX_LIBNAME STREQUAL "libstdc++")
528 append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ SANITIZER_TEST_CXX_LIBRARIES)
531 # TODO: There's a lot of duplication across lib/*/tests/CMakeLists.txt files,
532 # move some of the common flags to COMPILER_RT_UNITTEST_CFLAGS.
534 # Unittests need access to C++ standard library.
535 string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " ${stdlib_flag}")
537 # When cross-compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help in compilation
538 # and linking of unittests.
539 string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
540 set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
543 set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
544 set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
545 set(COMPILER_RT_GTEST_CFLAGS
546 -DGTEST_NO_LLVM_SUPPORT=1
548 -I${COMPILER_RT_GTEST_PATH}/include
549 -I${COMPILER_RT_GTEST_PATH}
553 set(COMPILER_RT_GMOCK_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock)
554 set(COMPILER_RT_GMOCK_SOURCE ${COMPILER_RT_GMOCK_PATH}/src/gmock-all.cc)
555 set(COMPILER_RT_GMOCK_CFLAGS
556 -DGTEST_NO_LLVM_SUPPORT=1
558 -I${COMPILER_RT_GMOCK_PATH}/include
559 -I${COMPILER_RT_GMOCK_PATH}
562 if(COMPILER_RT_HAS_G_FLAG)
563 list(APPEND COMPILER_RT_UNITTEST_CFLAGS -g)
565 append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
566 append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS)
567 append_list_if(COMPILER_RT_HAS_WSUGGEST_OVERRIDE_FLAG -Wno-suggest-override COMPILER_RT_UNITTEST_CFLAGS)
570 # gtest use a lot of stuff marked as deprecated on Windows.
571 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
574 # Warnings to turn off for all libraries, not just sanitizers.
575 append_string_if(COMPILER_RT_HAS_WUNUSED_PARAMETER_FLAG -Wno-unused-parameter CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
577 if (CMAKE_LINKER MATCHES "link.exe$")
578 # Silence MSVC linker warnings caused by empty object files. The
579 # sanitizer libraries intentionally use ifdefs that result in empty
580 # files, rather than skipping these files in the build system.
581 # Ideally, we would pass this flag only for the libraries that need
582 # it, but CMake doesn't seem to have a way to set linker flags for
583 # individual static libraries, so we enable the suppression flag for
584 # the whole compiler-rt project.
585 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
588 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
589 set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> -X32_64 qc <TARGET> <LINK_FLAGS> <OBJECTS>")
590 set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> -X32_64 qc <TARGET> <LINK_FLAGS> <OBJECTS>")
591 set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> -X32_64 q <TARGET> <LINK_FLAGS> <OBJECTS>")
592 set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> -X32_64 q <TARGET> <LINK_FLAGS> <OBJECTS>")
593 set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -X32_64 <TARGET>")
594 set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -X32_64 <TARGET>")
597 add_subdirectory(include)
599 option(COMPILER_RT_USE_LIBCXX
600 "Enable compiler-rt to use libc++ from the source tree" ON)
601 if(COMPILER_RT_USE_LIBCXX)
602 if(LLVM_ENABLE_PROJECTS_USED)
603 # Don't use libcxx if LLVM_ENABLE_PROJECTS does not enable it.
604 set(COMPILER_RT_LIBCXX_PATH ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR})
605 set(COMPILER_RT_LIBCXXABI_PATH ${LLVM_EXTERNAL_LIBCXXABI_SOURCE_DIR})
607 foreach(path IN ITEMS ${LLVM_MAIN_SRC_DIR}/projects/libcxx
608 ${LLVM_MAIN_SRC_DIR}/runtimes/libcxx
609 ${LLVM_MAIN_SRC_DIR}/../libcxx
610 ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR})
611 if(IS_DIRECTORY ${path})
612 set(COMPILER_RT_LIBCXX_PATH ${path})
616 foreach(path IN ITEMS ${LLVM_MAIN_SRC_DIR}/projects/libcxxabi
617 ${LLVM_MAIN_SRC_DIR}/runtimes/libcxxabi
618 ${LLVM_MAIN_SRC_DIR}/../libcxxabi
619 ${LLVM_EXTERNAL_LIBCXXABI_SOURCE_DIR})
620 if(IS_DIRECTORY ${path})
621 set(COMPILER_RT_LIBCXXABI_PATH ${path})
628 set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/tools/lld)
629 if(EXISTS ${COMPILER_RT_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD)
630 set(COMPILER_RT_HAS_LLD TRUE)
632 set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/../lld)
633 if(EXISTS ${COMPILER_RT_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD)
634 set(COMPILER_RT_HAS_LLD TRUE)
639 set(COMPILER_RT_HAS_LLD TRUE)
640 set(COMPILER_RT_TEST_USE_LLD TRUE)
641 append_list_if(COMPILER_RT_HAS_FUSE_LD_LLD_FLAG -fuse-ld=lld SANITIZER_COMMON_LINK_FLAGS)
642 append_list_if(COMPILER_RT_HAS_LLD -fuse-ld=lld COMPILER_RT_UNITTEST_LINK_FLAGS)
643 if(COMPILER_RT_HAS_FUSE_LD_LLD_FLAG)
644 set(COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT FALSE)
647 pythonize_bool(COMPILER_RT_HAS_LLD)
648 pythonize_bool(COMPILER_RT_TEST_USE_LLD)
650 add_subdirectory(lib)
652 if(COMPILER_RT_INCLUDE_TESTS)
653 add_subdirectory(unittests)
654 add_subdirectory(test)
655 # Don't build llvm-lit for runtimes-build, it will clean up map_config.
656 if (COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
657 # If we have a valid source tree, generate llvm-lit into the bin directory.
658 # The user can still choose to have the check targets *use* a different lit
659 # by specifying -DLLVM_EXTERNAL_LIT, but we generate it regardless.
660 if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
661 # Needed for lit support in standalone builds.
663 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
664 elseif(NOT EXISTS ${LLVM_EXTERNAL_LIT})
665 message(WARNING "Could not find LLVM source directory and LLVM_EXTERNAL_LIT does not"
666 "point to a valid file. You will not be able to run tests.")
671 add_subdirectory(tools)