[clang] Handle __declspec() attributes in using
[llvm-project.git] / compiler-rt / cmake / config-ix.cmake
blob5f51befc1973feafc0b394cf12d63979159906a0
1 include(CMakePushCheckState)
2 include(LLVMCheckCompilerLinkerFlag)
3 include(CheckCCompilerFlag)
4 include(CheckCXXCompilerFlag)
5 include(CheckIncludeFiles)
6 include(CheckLibraryExists)
7 include(LLVMCheckCompilerLinkerFlag)
8 include(CheckSymbolExists)
9 include(TestBigEndian)
11 # The compiler driver may be implicitly trying to link against libunwind.
12 # This is normally ok (libcxx relies on an unwinder), but if libunwind is
13 # built in the same cmake invocation as compiler-rt and we're using the
14 # in tree version of runtimes, we'd be linking against the just-built
15 # libunwind (and the compiler implicit -lunwind wouldn't succeed as the newly
16 # built libunwind isn't installed yet). For those cases, it'd be good to
17 # link with --uwnindlib=none. Check if that option works.
18 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_NONE_FLAG)
20 check_library_exists(c fopen "" COMPILER_RT_HAS_LIBC)
21 if (COMPILER_RT_USE_BUILTINS_LIBRARY)
22   include(HandleCompilerRT)
23   find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY
24                            FLAGS ${SANITIZER_COMMON_FLAGS})
25   # TODO(PR51389): We should check COMPILER_RT_BUILTINS_LIBRARY and report an
26   # error if the value is NOTFOUND rather than silenty continuing but we first
27   # need to fix find_compiler_rt_library on Darwin.
28 else()
29   if (ANDROID)
30     check_library_exists(gcc __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_LIB)
31   else()
32     check_library_exists(gcc_s __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_S_LIB)
33   endif()
34 endif()
36 check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
37 if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
38   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
39   if (COMPILER_RT_HAS_LIBC)
40     list(APPEND CMAKE_REQUIRED_LIBRARIES c)
41   endif ()
42   if (COMPILER_RT_USE_BUILTINS_LIBRARY)
43     # TODO: remote this check once we address PR51389.
44     if (${COMPILER_RT_BUILTINS_LIBRARY})
45       list(APPEND CMAKE_REQUIRED_LIBRARIES "${COMPILER_RT_BUILTINS_LIBRARY}")
46     endif()
47   elseif (COMPILER_RT_HAS_GCC_S_LIB)
48     list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
49   elseif (COMPILER_RT_HAS_GCC_LIB)
50     list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
51   endif ()
52   if (MINGW)
53     # Mingw64 requires quite a few "C" runtime libraries in order for basic
54     # programs to link successfully with -nodefaultlibs.
55     if (COMPILER_RT_USE_BUILTINS_LIBRARY)
56       set(MINGW_RUNTIME ${COMPILER_RT_BUILTINS_LIBRARY})
57     else ()
58       set(MINGW_RUNTIME gcc_s gcc)
59     endif()
60     set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
61                         shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
62                         moldname mingwex msvcrt)
63     list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
64   endif()
65 endif ()
67 # CodeGen options.
68 check_c_compiler_flag(-ffreestanding         COMPILER_RT_HAS_FFREESTANDING_FLAG)
69 check_c_compiler_flag(-fomit-frame-pointer   COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG)
70 check_c_compiler_flag(-std=c11               COMPILER_RT_HAS_STD_C11_FLAG)
71 check_c_compiler_flag(-fcf-protection=full   COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
72 check_cxx_compiler_flag(-fPIC                COMPILER_RT_HAS_FPIC_FLAG)
73 check_cxx_compiler_flag(-fPIE                COMPILER_RT_HAS_FPIE_FLAG)
74 check_cxx_compiler_flag(-fno-builtin         COMPILER_RT_HAS_FNO_BUILTIN_FLAG)
75 check_cxx_compiler_flag(-fno-exceptions      COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG)
76 check_cxx_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG)
77 check_cxx_compiler_flag(-funwind-tables      COMPILER_RT_HAS_FUNWIND_TABLES_FLAG)
78 check_cxx_compiler_flag(-fno-stack-protector COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG)
79 check_cxx_compiler_flag(-fno-sanitize=safe-stack COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG)
80 check_cxx_compiler_flag(-fvisibility=hidden  COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG)
81 check_cxx_compiler_flag(-frtti               COMPILER_RT_HAS_FRTTI_FLAG)
82 check_cxx_compiler_flag(-fno-rtti            COMPILER_RT_HAS_FNO_RTTI_FLAG)
83 check_cxx_compiler_flag("-Werror -fno-function-sections" COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG)
84 check_cxx_compiler_flag(-ftls-model=initial-exec COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC)
85 check_cxx_compiler_flag(-fno-lto             COMPILER_RT_HAS_FNO_LTO_FLAG)
86 check_cxx_compiler_flag(-fno-profile-generate COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
87 check_cxx_compiler_flag(-fno-profile-instr-generate COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
88 check_cxx_compiler_flag(-fno-profile-instr-use COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
89 check_cxx_compiler_flag(-fno-coverage-mapping COMPILER_RT_HAS_FNO_COVERAGE_MAPPING_FLAG)
90 check_cxx_compiler_flag("-Werror -mcrc32"    COMPILER_RT_HAS_MCRC32_FLAG)
91 check_cxx_compiler_flag("-Werror -msse3" COMPILER_RT_HAS_MSSE3_FLAG)
92 check_cxx_compiler_flag("-Werror -msse4.2"   COMPILER_RT_HAS_MSSE4_2_FLAG)
93 check_cxx_compiler_flag(--sysroot=.          COMPILER_RT_HAS_SYSROOT_FLAG)
94 check_cxx_compiler_flag("-Werror -mcrc"      COMPILER_RT_HAS_MCRC_FLAG)
95 check_cxx_compiler_flag(-fno-partial-inlining COMPILER_RT_HAS_FNO_PARTIAL_INLINING_FLAG)
96 check_cxx_compiler_flag(-Werror -ftrivial-auto-var-init=pattern COMPILER_RT_HAS_TRIVIAL_AUTO_INIT)
98 if(NOT WIN32 AND NOT CYGWIN)
99   # MinGW warns if -fvisibility-inlines-hidden is used.
100   check_cxx_compiler_flag("-fvisibility-inlines-hidden" COMPILER_RT_HAS_FVISIBILITY_INLINES_HIDDEN_FLAG)
101 endif()
103 check_cxx_compiler_flag(/GR COMPILER_RT_HAS_GR_FLAG)
104 check_cxx_compiler_flag(/GS COMPILER_RT_HAS_GS_FLAG)
105 check_cxx_compiler_flag(/MT COMPILER_RT_HAS_MT_FLAG)
106 check_cxx_compiler_flag(/Oy COMPILER_RT_HAS_Oy_FLAG)
108 # Debug info flags.
109 check_cxx_compiler_flag(-gline-tables-only COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG)
110 check_cxx_compiler_flag(-g COMPILER_RT_HAS_G_FLAG)
111 check_cxx_compiler_flag(/Zi COMPILER_RT_HAS_Zi_FLAG)
113 # Warnings.
114 check_cxx_compiler_flag(-Wall COMPILER_RT_HAS_WALL_FLAG)
115 check_cxx_compiler_flag(-Werror COMPILER_RT_HAS_WERROR_FLAG)
116 check_cxx_compiler_flag("-Werror -Wframe-larger-than=512" COMPILER_RT_HAS_WFRAME_LARGER_THAN_FLAG)
117 check_cxx_compiler_flag("-Werror -Wglobal-constructors"   COMPILER_RT_HAS_WGLOBAL_CONSTRUCTORS_FLAG)
118 check_cxx_compiler_flag("-Werror -Wc99-extensions"     COMPILER_RT_HAS_WC99_EXTENSIONS_FLAG)
119 check_cxx_compiler_flag("-Werror -Wgnu"                COMPILER_RT_HAS_WGNU_FLAG)
120 check_cxx_compiler_flag("-Werror -Wnon-virtual-dtor"   COMPILER_RT_HAS_WNON_VIRTUAL_DTOR_FLAG)
121 check_cxx_compiler_flag("-Werror -Wvariadic-macros"    COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG)
122 check_cxx_compiler_flag("-Werror -Wunused-parameter"   COMPILER_RT_HAS_WUNUSED_PARAMETER_FLAG)
123 check_cxx_compiler_flag("-Werror -Wcovered-switch-default" COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG)
124 check_cxx_compiler_flag("-Werror -Wsuggest-override"   COMPILER_RT_HAS_WSUGGEST_OVERRIDE_FLAG)
125 check_cxx_compiler_flag("-Werror -Wthread-safety" COMPILER_RT_HAS_WTHREAD_SAFETY_FLAG)
126 check_cxx_compiler_flag("-Werror -Wthread-safety-reference" COMPILER_RT_HAS_WTHREAD_SAFETY_REFERENCE_FLAG)
127 check_cxx_compiler_flag("-Werror -Wthread-safety-beta" COMPILER_RT_HAS_WTHREAD_SAFETY_BETA_FLAG)
128 check_cxx_compiler_flag(-Wno-pedantic COMPILER_RT_HAS_WNO_PEDANTIC)
129 check_cxx_compiler_flag(-Wno-format COMPILER_RT_HAS_WNO_FORMAT)
130 check_cxx_compiler_flag(-Wno-format-pedantic COMPILER_RT_HAS_WNO_FORMAT_PEDANTIC)
132 check_cxx_compiler_flag("/experimental:external /external:W0" COMPILER_RT_HAS_EXTERNAL_FLAG)
134 check_cxx_compiler_flag(/W4 COMPILER_RT_HAS_W4_FLAG)
135 check_cxx_compiler_flag(/WX COMPILER_RT_HAS_WX_FLAG)
136 check_cxx_compiler_flag(/wd4146 COMPILER_RT_HAS_WD4146_FLAG)
137 check_cxx_compiler_flag(/wd4206 COMPILER_RT_HAS_WD4206_FLAG)
138 check_cxx_compiler_flag(/wd4291 COMPILER_RT_HAS_WD4291_FLAG)
139 check_cxx_compiler_flag(/wd4221 COMPILER_RT_HAS_WD4221_FLAG)
140 check_cxx_compiler_flag(/wd4391 COMPILER_RT_HAS_WD4391_FLAG)
141 check_cxx_compiler_flag(/wd4722 COMPILER_RT_HAS_WD4722_FLAG)
142 check_cxx_compiler_flag(/wd4800 COMPILER_RT_HAS_WD4800_FLAG)
144 check_cxx_compiler_flag(-Werror -Warray-bounds COMPILER_RT_HAS_ARRAY_BOUNDS_FLAG)
145 check_cxx_compiler_flag(-Werror -Wuninitialized COMPILER_RT_HAS_UNINITIALIZED_FLAG)
146 check_cxx_compiler_flag(-Werror -Wshadow COMPILER_RT_HAS_SHADOW_FLAG)
147 check_cxx_compiler_flag(-Werror -Wempty-body COMPILER_RT_HAS_EMPTY_BODY_FLAG)
148 check_cxx_compiler_flag(-Werror -Wsizeof-pointer-memaccess COMPILER_RT_HAS_SIZEOF_POINTER_MEMACCESS_FLAG)
149 check_cxx_compiler_flag(-Werror -Wsizeof-array-argument COMPILER_RT_HAS_SIZEOF_ARRAY_ARGUMENT_FLAG)
150 check_cxx_compiler_flag(-Werror -Wsuspicious-memaccess COMPILER_RT_HAS_SUSPICIOUS_MEMACCESS_FLAG)
151 check_cxx_compiler_flag(-Werror -Wbuiltin-memcpy-chk-size COMPILER_RT_HAS_BUILTIN_MEMCPY_CHK_SIZE_FLAG)
152 check_cxx_compiler_flag(-Werror -Warray-bounds-pointer-arithmetic COMPILER_RT_HAS_ARRAY_BOUNDS_POINTER_ARITHMETIC_FLAG)
153 check_cxx_compiler_flag(-Werror -Wreturn-stack-address COMPILER_RT_HAS_RETURN_STACK_ADDRESS_FLAG)
154 check_cxx_compiler_flag(-Werror -Wsizeof-array-decay COMPILER_RT_HAS_SIZEOF_ARRAY_DECAY_FLAG)
155 check_cxx_compiler_flag(-Werror -Wformat-insufficient-args COMPILER_RT_HAS_FORMAT_INSUFFICIENT_ARGS_FLAG)
156 check_cxx_compiler_flag(-Werror -Wformat-security COMPILER_RT_HAS_BUILTIN_FORMAL_SECURITY_FLAG)
157 check_cxx_compiler_flag(-Werror -Wsizeof-array-div COMPILER_RT_HAS_SIZEOF_ARRAY_DIV_FLAG)
158 check_cxx_compiler_flag(-Werror -Wsizeof-pointer-div COMPILER_RT_HAS_SIZEOF_POINTER_DIV_FLAG)
160 # Symbols.
161 check_symbol_exists(__func__ "" COMPILER_RT_HAS_FUNC_SYMBOL)
163 # Includes.
164 check_cxx_compiler_flag(-nostdinc++ COMPILER_RT_HAS_NOSTDINCXX_FLAG)
165 check_cxx_compiler_flag(-nostdlib++ COMPILER_RT_HAS_NOSTDLIBXX_FLAG)
166 check_include_files("sys/auxv.h"    COMPILER_RT_HAS_AUXV)
168 # Libraries.
169 check_library_exists(dl dlopen "" COMPILER_RT_HAS_LIBDL)
170 check_library_exists(rt shm_open "" COMPILER_RT_HAS_LIBRT)
171 check_library_exists(m pow "" COMPILER_RT_HAS_LIBM)
172 check_library_exists(pthread pthread_create "" COMPILER_RT_HAS_LIBPTHREAD)
173 check_library_exists(execinfo backtrace "" COMPILER_RT_HAS_LIBEXECINFO)
175 # Look for terminfo library, used in unittests that depend on LLVMSupport.
176 if(LLVM_ENABLE_TERMINFO STREQUAL FORCE_ON)
177   set(MAYBE_REQUIRED REQUIRED)
178 else()
179   set(MAYBE_REQUIRED)
180 endif()
181 if(LLVM_ENABLE_TERMINFO)
182   find_library(COMPILER_RT_TERMINFO_LIB NAMES terminfo tinfo curses ncurses ncursesw ${MAYBE_REQUIRED})
183 endif()
184 if(COMPILER_RT_TERMINFO_LIB)
185   set(LLVM_ENABLE_TERMINFO 1)
186 else()
187   set(LLVM_ENABLE_TERMINFO 0)
188 endif()
190 if (ANDROID AND COMPILER_RT_HAS_LIBDL)
191   # Android's libstdc++ has a dependency on libdl.
192   list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
193 endif()
194 check_library_exists(c++ __cxa_throw "" COMPILER_RT_HAS_LIBCXX)
195 check_library_exists(stdc++ __cxa_throw "" COMPILER_RT_HAS_LIBSTDCXX)
197 # Linker flags.
198 llvm_check_compiler_linker_flag(C "-Wl,-z,text" COMPILER_RT_HAS_Z_TEXT)
199 llvm_check_compiler_linker_flag(C "-fuse-ld=lld" COMPILER_RT_HAS_FUSE_LD_LLD_FLAG)
201 if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
202   set(VERS_COMPAT_OPTION "-Wl,-z,gnu-version-script-compat")
203   llvm_check_compiler_linker_flag(C "${VERS_COMPAT_OPTION}" COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
204 endif()
206 set(DUMMY_VERS ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/dummy.vers)
207 file(WRITE ${DUMMY_VERS} "{};")
208 set(VERS_OPTION "-Wl,--version-script,${DUMMY_VERS}")
209 if(COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
210   # Solaris 11.4 ld only supports --version-script with
211   # -z gnu-version-script-compat.
212   string(APPEND VERS_OPTION " ${VERS_COMPAT_OPTION}")
213 endif()
214 llvm_check_compiler_linker_flag(C "${VERS_OPTION}" COMPILER_RT_HAS_VERSION_SCRIPT)
216 if(ANDROID)
217   llvm_check_compiler_linker_flag(C "-Wl,-z,global" COMPILER_RT_HAS_Z_GLOBAL)
218   check_library_exists(log __android_log_write "" COMPILER_RT_HAS_LIBLOG)
219 endif()
221 # Architectures.
223 # List of all architectures we can target.
224 set(COMPILER_RT_SUPPORTED_ARCH)
226 # Try to compile a very simple source file to ensure we can target the given
227 # platform. We use the results of these tests to build only the various target
228 # runtime libraries supported by our current compilers cross-compiling
229 # abilities.
230 set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.cc)
231 file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\n#include <stdio.h>\nint main(void) { printf(\"hello, world\"); }\n")
233 # Detect whether the current target platform is 32-bit or 64-bit, and setup
234 # the correct commandline flags needed to attempt to target 32-bit and 64-bit.
235 # AVR and MSP430 are omitted since they have 16-bit pointers.
236 if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
237     NOT CMAKE_SIZEOF_VOID_P EQUAL 8 AND
238     NOT ${arch} MATCHES "avr|msp430")
239   message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
240 endif()
242 test_targets()
244 # Returns a list of architecture specific target cflags in @out_var list.
245 function(get_target_flags_for_arch arch out_var)
246   list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
247   if(ARCH_INDEX EQUAL -1)
248     message(FATAL_ERROR "Unsupported architecture: ${arch}")
249   else()
250     if (NOT APPLE)
251       set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
252     else()
253       # This is only called in constructing cflags for tests executing on the
254       # host. This will need to all be cleaned up to support building tests
255       # for cross-targeted hardware (i.e. iOS).
256       set(${out_var} -arch ${arch} PARENT_SCOPE)
257     endif()
258   endif()
259 endfunction()
261 # Returns a list of architecture specific target ldflags in @out_var list.
262 function(get_target_link_flags_for_arch arch out_var)
263   list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
264   if(ARCH_INDEX EQUAL -1)
265     message(FATAL_ERROR "Unsupported architecture: ${arch}")
266   else()
267     # Workaround for direct calls to __tls_get_addr on Solaris/amd64.
268     if(OS_NAME MATCHES "SunOS" AND ${arch} MATCHES x86_64)
269       set(${out_var} "-Wl,-z,relax=transtls" PARENT_SCOPE)
270     endif()
271   endif()
272 endfunction()
274 # Returns a compiler and CFLAGS that should be used to run tests for the
275 # specific architecture.  When cross-compiling, this is controled via
276 # COMPILER_RT_TEST_COMPILER and COMPILER_RT_TEST_COMPILER_CFLAGS.
277 macro(get_test_cc_for_arch arch cc_out cflags_out)
278   if (NOT ${ARGC} EQUAL 3)
279     message(FATAL_ERROR "got too many args. expected 3, got ${ARGC} (namely: ${ARGV})")
280   endif()
281   if(ANDROID OR (NOT APPLE AND ${arch} MATCHES "arm|aarch64|riscv32|riscv64"))
282     # This is only true if we are cross-compiling.
283     # Build all tests with host compiler and use host tools.
284     set(${cc_out} ${COMPILER_RT_TEST_COMPILER})
285     set(${cflags_out} ${COMPILER_RT_TEST_COMPILER_CFLAGS})
286   else()
287     get_target_flags_for_arch(${arch} ${cflags_out})
288     if(APPLE)
289       list(APPEND ${cflags_out} ${DARWIN_osx_CFLAGS})
290     endif()
291     string(REPLACE ";" " " ${cflags_out} "${${cflags_out}}")
292   endif()
293 endmacro()
295 # Returns CFLAGS that should be used to run tests for the
296 # specific apple platform and architecture.
297 function(get_test_cflags_for_apple_platform platform arch cflags_out)
298   is_valid_apple_platform("${platform}" is_valid_platform)
299   if (NOT is_valid_platform)
300     message(FATAL_ERROR "\"${platform}\" is not a valid apple platform")
301   endif()
302   set(test_cflags "")
303   get_target_flags_for_arch(${arch} test_cflags)
305   if (NOT "${arch}" STREQUAL "arm64e")
306     list(APPEND test_cflags ${DARWIN_${platform}_CFLAGS})
307   else()
308     # arm64e is not currently ABI stable so we need to build for the
309     # OS version being tested. Rather than querying the device under test
310     # we use the SDK version which "should" be the same as the
311     # device under test (it is a configuration error for these not to match).
312     # FIXME(dliew): We can remove this if we build the runtimes with the appropriate
313     # deployment target for arm64e.
314     foreach (flag ${DARWIN_${platform}_CFLAGS})
315       if ("${flag}" MATCHES "^${DARWIN_${platform}_MIN_VER_FLAG}=.+")
316         # Find the SDK version
317         get_xcrun_platform_from_apple_platform("${platform}" xcrun_platform_name)
318         # TODO(dliew): Remove this check once get_xcrun_platform_from_apple_platform
319         # emits a fatal error for unrecognised platforms.
320         if (NOT "${xcrun_platform_name}" STREQUAL "")
321           find_darwin_sdk_version(platform_sdk_version "${xcrun_platform_name}")
322           # Patch flag with correct deployment target
323           set(replacement_flag "${DARWIN_${platform}_MIN_VER_FLAG}=${platform_sdk_version}")
324           list(APPEND test_cflags "${replacement_flag}")
325         endif()
326       else()
327         # Copy through
328         list(APPEND test_cflags "${flag}")
329       endif()
330     endforeach()
331   endif()
333   string(REPLACE ";" " " test_cflags_str "${test_cflags}")
334   string(APPEND test_cflags_str "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
335   set(${cflags_out} "${test_cflags_str}" PARENT_SCOPE)
336 endfunction()
338 function(get_capitalized_apple_platform platform platform_capitalized)
339   # TODO(dliew): Remove uses of this function. It exists to preserve needlessly complex
340   # directory naming conventions used by the Sanitizer lit test suites.
341   is_valid_apple_platform("${platform}" is_valid_platform)
342   if (NOT is_valid_platform)
343     message(FATAL_ERROR "\"${platform}\" is not a valid apple platform")
344   endif()
345   string(TOUPPER "${platform}" platform_upper)
346   string(REGEX REPLACE "OSSIM$" "OSSim" platform_upper_capitalized "${platform_upper}")
347   set(${platform_capitalized} "${platform_upper_capitalized}" PARENT_SCOPE)
348 endfunction()
350 function(is_valid_apple_platform platform is_valid_out)
351   set(is_valid FALSE)
352   if ("${platform}" STREQUAL "")
353     message(FATAL_ERROR "platform cannot be empty")
354   endif()
355   if ("${platform}" MATCHES "^(osx|((ios|watchos|tvos)(sim)?))$")
356     set(is_valid TRUE)
357   endif()
358   set(${is_valid_out} ${is_valid} PARENT_SCOPE)
359 endfunction()
361 # Maps the Apple platform name used in Compiler-rt's CMake code
362 # to the name recognised by xcrun's `--sdk` argument
363 function(get_xcrun_platform_from_apple_platform platform out_var)
364   set(xcrun_platform "")
365   if ("${platform}" STREQUAL "osx")
366     set(xcrun_platform "macosx")
367   elseif ("${platform}" STREQUAL "iossim")
368     set(xcrun_platform "iphonesimulator")
369   elseif ("${platform}" STREQUAL "ios")
370     set(xcrun_platform "iphoneos")
371   elseif ("${platform}" STREQUAL "watchossim")
372     set(xcrun_platform "watchsimulator")
373   elseif ("${platform}" STREQUAL "watchos")
374     set(xcrun_platform "watchos")
375   elseif ("${platform}" STREQUAL "tvossim")
376     set(xcrun_platform "appletvsimulator")
377   elseif ("${platform}" STREQUAL "tvos")
378     set(xcrun_platform "appletvos")
379   else()
380     # TODO(dliew): Make this an error.
381     message(WARNING "\"${platform}\" is not a handled apple platform")
382   endif()
383   set(${out_var} ${xcrun_platform} PARENT_SCOPE)
384 endfunction()
386 include(AllSupportedArchDefs)
388 if(APPLE)
389   include(CompilerRTDarwinUtils)
391   find_darwin_sdk_dir(DARWIN_osx_SYSROOT macosx)
392   find_darwin_sdk_dir(DARWIN_iossim_SYSROOT iphonesimulator)
393   find_darwin_sdk_dir(DARWIN_ios_SYSROOT iphoneos)
394   find_darwin_sdk_dir(DARWIN_watchossim_SYSROOT watchsimulator)
395   find_darwin_sdk_dir(DARWIN_watchos_SYSROOT watchos)
396   find_darwin_sdk_dir(DARWIN_tvossim_SYSROOT appletvsimulator)
397   find_darwin_sdk_dir(DARWIN_tvos_SYSROOT appletvos)
399   if(NOT DARWIN_osx_SYSROOT)
400     message(WARNING "Could not determine OS X sysroot, trying /usr/include")
401     if(EXISTS /usr/include)
402       set(DARWIN_osx_SYSROOT /)
403     else()
404       message(ERROR "Could not detect OS X Sysroot. Either install Xcode or the Apple Command Line Tools")
405     endif()
406   endif()
408   if(COMPILER_RT_ENABLE_IOS)
409     list(APPEND DARWIN_EMBEDDED_PLATFORMS ios)
410     set(DARWIN_ios_MIN_VER 9.0)
411     set(DARWIN_ios_MIN_VER_FLAG -miphoneos-version-min)
412     set(DARWIN_ios_SANITIZER_MIN_VER_FLAG
413       ${DARWIN_ios_MIN_VER_FLAG}=${DARWIN_ios_MIN_VER})
414     set(DARWIN_iossim_MIN_VER_FLAG -mios-simulator-version-min)
415     set(DARWIN_iossim_SANITIZER_MIN_VER_FLAG
416       ${DARWIN_iossim_MIN_VER_FLAG}=${DARWIN_ios_MIN_VER})
417   endif()
418   if(COMPILER_RT_ENABLE_WATCHOS)
419     list(APPEND DARWIN_EMBEDDED_PLATFORMS watchos)
420     set(DARWIN_watchos_MIN_VER 2.0)
421     set(DARWIN_watchos_MIN_VER_FLAG -mwatchos-version-min)
422     set(DARWIN_watchos_SANITIZER_MIN_VER_FLAG
423       ${DARWIN_watchos_MIN_VER_FLAG}=${DARWIN_watchos_MIN_VER})
424     set(DARWIN_watchossim_MIN_VER_FLAG -mwatchos-simulator-version-min)
425     set(DARWIN_watchossim_SANITIZER_MIN_VER_FLAG
426       ${DARWIN_watchossim_MIN_VER_FLAG}=${DARWIN_watchos_MIN_VER})
427   endif()
428   if(COMPILER_RT_ENABLE_TVOS)
429     list(APPEND DARWIN_EMBEDDED_PLATFORMS tvos)
430     set(DARWIN_tvos_MIN_VER 9.0)
431     set(DARWIN_tvos_MIN_VER_FLAG -mtvos-version-min)
432     set(DARWIN_tvos_SANITIZER_MIN_VER_FLAG
433       ${DARWIN_tvos_MIN_VER_FLAG}=${DARWIN_tvos_MIN_VER})
434     set(DARWIN_tvossim_MIN_VER_FLAG -mtvos-simulator-version-min)
435     set(DARWIN_tvossim_SANITIZER_MIN_VER_FLAG
436       ${DARWIN_tvossim_MIN_VER_FLAG}=${DARWIN_tvos_MIN_VER})
437   endif()
439   set(SANITIZER_COMMON_SUPPORTED_OS osx)
440   set(PROFILE_SUPPORTED_OS osx)
441   set(TSAN_SUPPORTED_OS osx)
442   set(XRAY_SUPPORTED_OS osx)
443   set(FUZZER_SUPPORTED_OS osx)
444   set(ORC_SUPPORTED_OS osx)
445   set(UBSAN_SUPPORTED_OS osx)
446   set(LSAN_SUPPORTED_OS osx)
447   set(STATS_SUPPORTED_OS osx)
449   # Note: In order to target x86_64h on OS X the minimum deployment target must
450   # be 10.8 or higher.
451   set(DEFAULT_SANITIZER_MIN_OSX_VERSION 10.10)
452   set(DARWIN_osx_MIN_VER_FLAG "-mmacosx-version-min")
453   if(NOT SANITIZER_MIN_OSX_VERSION)
454     string(REGEX MATCH "${DARWIN_osx_MIN_VER_FLAG}=([.0-9]+)"
455            MACOSX_VERSION_MIN_FLAG "${CMAKE_CXX_FLAGS}")
456     if(MACOSX_VERSION_MIN_FLAG)
457       set(SANITIZER_MIN_OSX_VERSION "${CMAKE_MATCH_1}")
458     elseif(CMAKE_OSX_DEPLOYMENT_TARGET)
459       set(SANITIZER_MIN_OSX_VERSION ${CMAKE_OSX_DEPLOYMENT_TARGET})
460     else()
461       set(SANITIZER_MIN_OSX_VERSION ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
462     endif()
463     if(SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.7")
464       message(FATAL_ERROR "macOS deployment target '${SANITIZER_MIN_OSX_VERSION}' is too old.")
465     endif()
466     if(SANITIZER_MIN_OSX_VERSION VERSION_GREATER ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
467       message(WARNING "macOS deployment target '${SANITIZER_MIN_OSX_VERSION}' is too new, setting to '${DEFAULT_SANITIZER_MIN_OSX_VERSION}' instead.")
468       set(SANITIZER_MIN_OSX_VERSION ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
469     endif()
470   endif()
472   # We're setting the flag manually for each target OS
473   set(CMAKE_OSX_DEPLOYMENT_TARGET "")
475   set(DARWIN_COMMON_CFLAGS -stdlib=libc++)
476   set(DARWIN_COMMON_LINK_FLAGS
477     -stdlib=libc++
478     -lc++
479     -lc++abi)
481   llvm_check_compiler_linker_flag(C "-fapplication-extension" COMPILER_RT_HAS_APP_EXTENSION)
482   if(COMPILER_RT_HAS_APP_EXTENSION)
483     list(APPEND DARWIN_COMMON_LINK_FLAGS "-fapplication-extension")
484   endif()
486   set(DARWIN_osx_CFLAGS
487     ${DARWIN_COMMON_CFLAGS}
488     ${DARWIN_osx_MIN_VER_FLAG}=${SANITIZER_MIN_OSX_VERSION})
489   set(DARWIN_osx_LINK_FLAGS
490     ${DARWIN_COMMON_LINK_FLAGS}
491     ${DARWIN_osx_MIN_VER_FLAG}=${SANITIZER_MIN_OSX_VERSION})
493   if(DARWIN_osx_SYSROOT)
494     list(APPEND DARWIN_osx_CFLAGS -isysroot ${DARWIN_osx_SYSROOT})
495     list(APPEND DARWIN_osx_LINK_FLAGS -isysroot ${DARWIN_osx_SYSROOT})
496   endif()
498   # Figure out which arches to use for each OS
499   darwin_get_toolchain_supported_archs(toolchain_arches)
500   message(STATUS "Toolchain supported arches: ${toolchain_arches}")
502   if(NOT MACOSX_VERSION_MIN_FLAG)
503     darwin_test_archs(osx
504       DARWIN_osx_ARCHS
505       ${toolchain_arches})
506     message(STATUS "OSX supported arches: ${DARWIN_osx_ARCHS}")
507     foreach(arch ${DARWIN_osx_ARCHS})
508       list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
509       set(CAN_TARGET_${arch} 1)
510     endforeach()
512     foreach(platform ${DARWIN_EMBEDDED_PLATFORMS})
513       if(DARWIN_${platform}sim_SYSROOT)
514         set(DARWIN_${platform}sim_CFLAGS
515           ${DARWIN_COMMON_CFLAGS}
516           ${DARWIN_${platform}sim_SANITIZER_MIN_VER_FLAG}
517           -isysroot ${DARWIN_${platform}sim_SYSROOT})
518         set(DARWIN_${platform}sim_LINK_FLAGS
519           ${DARWIN_COMMON_LINK_FLAGS}
520           ${DARWIN_${platform}sim_SANITIZER_MIN_VER_FLAG}
521           -isysroot ${DARWIN_${platform}sim_SYSROOT})
523         set(DARWIN_${platform}sim_SKIP_CC_KEXT On)
524         darwin_test_archs(${platform}sim
525           DARWIN_${platform}sim_ARCHS
526           ${toolchain_arches})
527         message(STATUS "${platform} Simulator supported arches: ${DARWIN_${platform}sim_ARCHS}")
528         if(DARWIN_${platform}sim_ARCHS)
529           list(APPEND SANITIZER_COMMON_SUPPORTED_OS ${platform}sim)
530           list(APPEND PROFILE_SUPPORTED_OS ${platform}sim)
531           list(APPEND TSAN_SUPPORTED_OS ${platform}sim)
532           list(APPEND FUZZER_SUPPORTED_OS ${platform}sim)
533           list(APPEND ORC_SUPPORTED_OS ${platform}sim)
534           list(APPEND UBSAN_SUPPORTED_OS ${platform}sim)
535           list(APPEND LSAN_SUPPORTED_OS ${platform}sim)
536           list(APPEND STATS_SUPPORTED_OS ${platform}sim)
537         endif()
538         foreach(arch ${DARWIN_${platform}sim_ARCHS})
539           list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
540           set(CAN_TARGET_${arch} 1)
541         endforeach()
542       endif()
544       if(DARWIN_${platform}_SYSROOT)
545         set(DARWIN_${platform}_CFLAGS
546           ${DARWIN_COMMON_CFLAGS}
547           ${DARWIN_${platform}_SANITIZER_MIN_VER_FLAG}
548           -isysroot ${DARWIN_${platform}_SYSROOT})
549         set(DARWIN_${platform}_LINK_FLAGS
550           ${DARWIN_COMMON_LINK_FLAGS}
551           ${DARWIN_${platform}_SANITIZER_MIN_VER_FLAG}
552           -isysroot ${DARWIN_${platform}_SYSROOT})
554         darwin_test_archs(${platform}
555           DARWIN_${platform}_ARCHS
556           ${toolchain_arches})
557         message(STATUS "${platform} supported arches: ${DARWIN_${platform}_ARCHS}")
558         if(DARWIN_${platform}_ARCHS)
559           list(APPEND SANITIZER_COMMON_SUPPORTED_OS ${platform})
560           list(APPEND PROFILE_SUPPORTED_OS ${platform})
562           list_intersect(DARWIN_${platform}_TSAN_ARCHS DARWIN_${platform}_ARCHS ALL_TSAN_SUPPORTED_ARCH)
563           if(DARWIN_${platform}_TSAN_ARCHS)
564             list(APPEND TSAN_SUPPORTED_OS ${platform})
565           endif()
566           list(APPEND FUZZER_SUPPORTED_OS ${platform})
567           list(APPEND ORC_SUPPORTED_OS ${platform})
568           list(APPEND UBSAN_SUPPORTED_OS ${platform})
569           list(APPEND LSAN_SUPPORTED_OS ${platform})
570           list(APPEND STATS_SUPPORTED_OS ${platform})
571         endif()
572         foreach(arch ${DARWIN_${platform}_ARCHS})
573           list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
574           set(CAN_TARGET_${arch} 1)
575         endforeach()
576       endif()
577     endforeach()
578   endif()
580   # Explicitly disable unsupported Sanitizer configurations.
581   list(REMOVE_ITEM FUZZER_SUPPORTED_OS "watchos")
582   list(REMOVE_ITEM FUZZER_SUPPORTED_OS "watchossim")
584   # for list_intersect
585   include(CompilerRTUtils)
587   list_intersect(SANITIZER_COMMON_SUPPORTED_ARCH
588     ALL_SANITIZER_COMMON_SUPPORTED_ARCH
589     COMPILER_RT_SUPPORTED_ARCH
590     )
591   set(LSAN_COMMON_SUPPORTED_ARCH ${SANITIZER_COMMON_SUPPORTED_ARCH})
592   set(UBSAN_COMMON_SUPPORTED_ARCH ${SANITIZER_COMMON_SUPPORTED_ARCH})
593   list_intersect(ASAN_SUPPORTED_ARCH
594     ALL_ASAN_SUPPORTED_ARCH
595     SANITIZER_COMMON_SUPPORTED_ARCH)
596   list_intersect(DFSAN_SUPPORTED_ARCH
597     ALL_DFSAN_SUPPORTED_ARCH
598     SANITIZER_COMMON_SUPPORTED_ARCH)
599   list_intersect(GWP_ASAN_SUPPORTED_ARCH
600     ALL_GWP_ASAN_SUPPORTED_ARCH
601     SANITIZER_COMMON_SUPPORTED_ARCH)
602   list_intersect(LSAN_SUPPORTED_ARCH
603     ALL_LSAN_SUPPORTED_ARCH
604     SANITIZER_COMMON_SUPPORTED_ARCH)
605   list_intersect(MSAN_SUPPORTED_ARCH
606     ALL_MSAN_SUPPORTED_ARCH
607     SANITIZER_COMMON_SUPPORTED_ARCH)
608   list_intersect(HWASAN_SUPPORTED_ARCH
609     ALL_HWASAN_SUPPORTED_ARCH
610     SANITIZER_COMMON_SUPPORTED_ARCH)
611   list_intersect(MEMPROF_SUPPORTED_ARCH
612     ALL_MEMPROF_SUPPORTED_ARCH
613     SANITIZER_COMMON_SUPPORTED_ARCH)
614   list_intersect(PROFILE_SUPPORTED_ARCH
615     ALL_PROFILE_SUPPORTED_ARCH
616     SANITIZER_COMMON_SUPPORTED_ARCH)
617   list_intersect(TSAN_SUPPORTED_ARCH
618     ALL_TSAN_SUPPORTED_ARCH
619     SANITIZER_COMMON_SUPPORTED_ARCH)
620   list_intersect(UBSAN_SUPPORTED_ARCH
621     ALL_UBSAN_SUPPORTED_ARCH
622     SANITIZER_COMMON_SUPPORTED_ARCH)
623   list_intersect(SAFESTACK_SUPPORTED_ARCH
624     ALL_SAFESTACK_SUPPORTED_ARCH
625     SANITIZER_COMMON_SUPPORTED_ARCH)
626   list_intersect(CFI_SUPPORTED_ARCH
627     ALL_CFI_SUPPORTED_ARCH
628     SANITIZER_COMMON_SUPPORTED_ARCH)
629   list_intersect(SCUDO_STANDALONE_SUPPORTED_ARCH
630     ALL_SCUDO_STANDALONE_SUPPORTED_ARCH
631     SANITIZER_COMMON_SUPPORTED_ARCH)
632   list_intersect(FUZZER_SUPPORTED_ARCH
633     ALL_FUZZER_SUPPORTED_ARCH
634     SANITIZER_COMMON_SUPPORTED_ARCH)
635   list_intersect(XRAY_SUPPORTED_ARCH
636     ALL_XRAY_SUPPORTED_ARCH
637     SANITIZER_COMMON_SUPPORTED_ARCH)
638   list_intersect(SHADOWCALLSTACK_SUPPORTED_ARCH
639     ALL_SHADOWCALLSTACK_SUPPORTED_ARCH
640     SANITIZER_COMMON_SUPPORTED_ARCH)
641   list_intersect(ORC_SUPPORTED_ARCH
642     ALL_ORC_SUPPORTED_ARCH
643     SANITIZER_COMMON_SUPPORTED_ARCH)
645 else()
646   # Architectures supported by compiler-rt libraries.
647   filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
648     ${ALL_SANITIZER_COMMON_SUPPORTED_ARCH})
649   # LSan and UBSan common files should be available on all architectures
650   # supported by other sanitizers (even if they build into dummy object files).
651   filter_available_targets(LSAN_COMMON_SUPPORTED_ARCH
652     ${SANITIZER_COMMON_SUPPORTED_ARCH})
653   filter_available_targets(UBSAN_COMMON_SUPPORTED_ARCH
654     ${SANITIZER_COMMON_SUPPORTED_ARCH})
655   filter_available_targets(ASAN_SUPPORTED_ARCH ${ALL_ASAN_SUPPORTED_ARCH})
656   filter_available_targets(FUZZER_SUPPORTED_ARCH ${ALL_FUZZER_SUPPORTED_ARCH})
657   filter_available_targets(DFSAN_SUPPORTED_ARCH ${ALL_DFSAN_SUPPORTED_ARCH})
658   filter_available_targets(LSAN_SUPPORTED_ARCH ${ALL_LSAN_SUPPORTED_ARCH})
659   filter_available_targets(MSAN_SUPPORTED_ARCH ${ALL_MSAN_SUPPORTED_ARCH})
660   filter_available_targets(HWASAN_SUPPORTED_ARCH ${ALL_HWASAN_SUPPORTED_ARCH})
661   filter_available_targets(MEMPROF_SUPPORTED_ARCH ${ALL_MEMPROF_SUPPORTED_ARCH})
662   filter_available_targets(PROFILE_SUPPORTED_ARCH ${ALL_PROFILE_SUPPORTED_ARCH})
663   filter_available_targets(TSAN_SUPPORTED_ARCH ${ALL_TSAN_SUPPORTED_ARCH})
664   filter_available_targets(UBSAN_SUPPORTED_ARCH ${ALL_UBSAN_SUPPORTED_ARCH})
665   filter_available_targets(SAFESTACK_SUPPORTED_ARCH
666     ${ALL_SAFESTACK_SUPPORTED_ARCH})
667   filter_available_targets(CFI_SUPPORTED_ARCH ${ALL_CFI_SUPPORTED_ARCH})
668   filter_available_targets(SCUDO_STANDALONE_SUPPORTED_ARCH ${ALL_SCUDO_STANDALONE_SUPPORTED_ARCH})
669   filter_available_targets(XRAY_SUPPORTED_ARCH ${ALL_XRAY_SUPPORTED_ARCH})
670   filter_available_targets(SHADOWCALLSTACK_SUPPORTED_ARCH
671     ${ALL_SHADOWCALLSTACK_SUPPORTED_ARCH})
672   filter_available_targets(GWP_ASAN_SUPPORTED_ARCH ${ALL_GWP_ASAN_SUPPORTED_ARCH})
673   filter_available_targets(ORC_SUPPORTED_ARCH ${ALL_ORC_SUPPORTED_ARCH})
674 endif()
676 if (MSVC)
677   # Allow setting clang-cl's /winsysroot flag.
678   set(LLVM_WINSYSROOT "" CACHE STRING
679     "If set, argument to clang-cl's /winsysroot")
681   if (LLVM_WINSYSROOT)
682     set(MSVC_DIA_SDK_DIR "${LLVM_WINSYSROOT}/DIA SDK" CACHE PATH
683         "Path to the DIA SDK")
684   else()
685     set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK" CACHE PATH
686         "Path to the DIA SDK")
687   endif()
689   # See if the DIA SDK is available and usable.
690   if (IS_DIRECTORY ${MSVC_DIA_SDK_DIR})
691     set(CAN_SYMBOLIZE 1)
692   else()
693     set(CAN_SYMBOLIZE 0)
694   endif()
695 else()
696   set(CAN_SYMBOLIZE 1)
697 endif()
699 find_program(GNU_LD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.bfd ld.bfd DOC "GNU ld")
700 find_program(GOLD_EXECUTABLE NAMES ${LLVM_DEFAULT_TARGET_TRIPLE}-ld.gold ld.gold DOC "GNU gold")
702 if(COMPILER_RT_SUPPORTED_ARCH)
703   list(REMOVE_DUPLICATES COMPILER_RT_SUPPORTED_ARCH)
704 endif()
705 message(STATUS "Compiler-RT supported architectures: ${COMPILER_RT_SUPPORTED_ARCH}")
707 set(ALL_SANITIZERS asan;dfsan;msan;hwasan;tsan;safestack;cfi;scudo_standalone;ubsan_minimal;gwp_asan)
708 set(COMPILER_RT_SANITIZERS_TO_BUILD all CACHE STRING
709     "sanitizers to build if supported on the target (all;${ALL_SANITIZERS})")
710 list_replace(COMPILER_RT_SANITIZERS_TO_BUILD all "${ALL_SANITIZERS}")
712 if (SANITIZER_COMMON_SUPPORTED_ARCH AND NOT LLVM_USE_SANITIZER AND
713     (OS_NAME MATCHES "Android|Darwin|Linux|FreeBSD|NetBSD|Fuchsia|SunOS" OR
714     (OS_NAME MATCHES "Windows" AND NOT CYGWIN AND
715         (NOT MINGW OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"))))
716   set(COMPILER_RT_HAS_SANITIZER_COMMON TRUE)
717 else()
718   set(COMPILER_RT_HAS_SANITIZER_COMMON FALSE)
719 endif()
721 if (COMPILER_RT_HAS_SANITIZER_COMMON)
722   set(COMPILER_RT_HAS_INTERCEPTION TRUE)
723 else()
724   set(COMPILER_RT_HAS_INTERCEPTION FALSE)
725 endif()
727 if (COMPILER_RT_HAS_SANITIZER_COMMON AND ASAN_SUPPORTED_ARCH)
728   set(COMPILER_RT_HAS_ASAN TRUE)
729 else()
730   set(COMPILER_RT_HAS_ASAN FALSE)
731 endif()
733 if (OS_NAME MATCHES "Linux|FreeBSD|Windows|NetBSD|SunOS")
734   set(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME TRUE)
735 else()
736   set(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME FALSE)
737 endif()
739 # TODO: Add builtins support.
741 if (COMPILER_RT_HAS_SANITIZER_COMMON AND DFSAN_SUPPORTED_ARCH AND
742     OS_NAME MATCHES "Linux")
743   set(COMPILER_RT_HAS_DFSAN TRUE)
744 else()
745   set(COMPILER_RT_HAS_DFSAN FALSE)
746 endif()
748 if (COMPILER_RT_HAS_SANITIZER_COMMON AND LSAN_SUPPORTED_ARCH AND
749     OS_NAME MATCHES "Android|Darwin|Linux|NetBSD|Fuchsia")
750   set(COMPILER_RT_HAS_LSAN TRUE)
751 else()
752   set(COMPILER_RT_HAS_LSAN FALSE)
753 endif()
755 if (COMPILER_RT_HAS_SANITIZER_COMMON AND MSAN_SUPPORTED_ARCH AND
756     OS_NAME MATCHES "Linux|FreeBSD|NetBSD")
757   set(COMPILER_RT_HAS_MSAN TRUE)
758 else()
759   set(COMPILER_RT_HAS_MSAN FALSE)
760 endif()
762 if (COMPILER_RT_HAS_SANITIZER_COMMON AND HWASAN_SUPPORTED_ARCH AND
763     OS_NAME MATCHES "Linux|Android|Fuchsia")
764   set(COMPILER_RT_HAS_HWASAN TRUE)
765 else()
766   set(COMPILER_RT_HAS_HWASAN FALSE)
767 endif()
769 if (COMPILER_RT_HAS_SANITIZER_COMMON AND MEMPROF_SUPPORTED_ARCH AND
770     OS_NAME MATCHES "Linux")
771   set(COMPILER_RT_HAS_MEMPROF TRUE)
772 else()
773   set(COMPILER_RT_HAS_MEMPROF FALSE)
774 endif()
776 if (PROFILE_SUPPORTED_ARCH AND NOT LLVM_USE_SANITIZER AND
777     OS_NAME MATCHES "Darwin|Linux|FreeBSD|Windows|Android|Fuchsia|SunOS|NetBSD|AIX")
778   set(COMPILER_RT_HAS_PROFILE TRUE)
779 else()
780   set(COMPILER_RT_HAS_PROFILE FALSE)
781 endif()
783 if (COMPILER_RT_HAS_SANITIZER_COMMON AND TSAN_SUPPORTED_ARCH)
784   if (OS_NAME MATCHES "Linux|Darwin|FreeBSD|NetBSD")
785     set(COMPILER_RT_HAS_TSAN TRUE)
786   elseif (OS_NAME MATCHES "Android" AND ANDROID_PLATFORM_LEVEL GREATER 23)
787     set(COMPILER_RT_HAS_TSAN TRUE)
788   else()
789     set(COMPILER_RT_HAS_TSAN FALSE)
790   endif()
791 else()
792   set(COMPILER_RT_HAS_TSAN FALSE)
793 endif()
795 if (OS_NAME MATCHES "Linux|FreeBSD|Windows|NetBSD|SunOS")
796   set(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME TRUE)
797 else()
798   set(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME FALSE)
799 endif()
801 if (COMPILER_RT_HAS_SANITIZER_COMMON AND UBSAN_SUPPORTED_ARCH AND
802     OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Windows|Android|Fuchsia|SunOS")
803   set(COMPILER_RT_HAS_UBSAN TRUE)
804 else()
805   set(COMPILER_RT_HAS_UBSAN FALSE)
806 endif()
808 if (COMPILER_RT_HAS_SANITIZER_COMMON AND UBSAN_SUPPORTED_ARCH AND
809     OS_NAME MATCHES "Linux|FreeBSD|NetBSD|Android|Darwin")
810   set(COMPILER_RT_HAS_UBSAN_MINIMAL TRUE)
811 else()
812   set(COMPILER_RT_HAS_UBSAN_MINIMAL FALSE)
813 endif()
815 if (COMPILER_RT_HAS_SANITIZER_COMMON AND SAFESTACK_SUPPORTED_ARCH AND
816     OS_NAME MATCHES "Linux|FreeBSD|NetBSD")
817   set(COMPILER_RT_HAS_SAFESTACK TRUE)
818 else()
819   set(COMPILER_RT_HAS_SAFESTACK FALSE)
820 endif()
822 if (COMPILER_RT_HAS_SANITIZER_COMMON AND CFI_SUPPORTED_ARCH)
823   set(COMPILER_RT_HAS_CFI TRUE)
824 else()
825   set(COMPILER_RT_HAS_CFI FALSE)
826 endif()
828 #TODO(kostyak): add back Android & Fuchsia when the code settles a bit.
829 if (SCUDO_STANDALONE_SUPPORTED_ARCH AND
830     COMPILER_RT_BUILD_SANITIZERS AND
831     "scudo_standalone" IN_LIST COMPILER_RT_SANITIZERS_TO_BUILD AND
832     OS_NAME MATCHES "Linux" AND
833     COMPILER_RT_HAS_AUXV)
834   set(COMPILER_RT_HAS_SCUDO_STANDALONE TRUE)
835 else()
836   set(COMPILER_RT_HAS_SCUDO_STANDALONE FALSE)
837 endif()
839 if (COMPILER_RT_HAS_SANITIZER_COMMON AND XRAY_SUPPORTED_ARCH AND
840     OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Fuchsia")
841   set(COMPILER_RT_HAS_XRAY TRUE)
842 else()
843   set(COMPILER_RT_HAS_XRAY FALSE)
844 endif()
846 if (ORC_SUPPORTED_ARCH)
847   set(COMPILER_RT_HAS_ORC TRUE)
848 else()
849   set(COMPILER_RT_HAS_ORC FALSE)
850 endif()
852 if (COMPILER_RT_HAS_SANITIZER_COMMON AND FUZZER_SUPPORTED_ARCH AND
853     OS_NAME MATCHES "Android|Darwin|Linux|NetBSD|FreeBSD|Fuchsia|Windows")
854   set(COMPILER_RT_HAS_FUZZER TRUE)
855 else()
856   set(COMPILER_RT_HAS_FUZZER FALSE)
857 endif()
859 if (COMPILER_RT_HAS_SANITIZER_COMMON AND SHADOWCALLSTACK_SUPPORTED_ARCH AND
860     OS_NAME MATCHES "Linux|Android")
861   set(COMPILER_RT_HAS_SHADOWCALLSTACK TRUE)
862 else()
863   set(COMPILER_RT_HAS_SHADOWCALLSTACK FALSE)
864 endif()
866 # Note: Fuchsia and Windows are not currently supported by GWP-ASan. Support
867 # is planned for these platforms. Darwin is also not supported due to TLS
868 # calling malloc on first use.
869 # TODO(hctim): Enable this on Android again. Looks like it's causing a SIGSEGV
870 # for Scudo and GWP-ASan, further testing needed.
871 if (GWP_ASAN_SUPPORTED_ARCH AND
872     COMPILER_RT_BUILD_GWP_ASAN AND
873     COMPILER_RT_BUILD_SANITIZERS AND
874     "gwp_asan" IN_LIST COMPILER_RT_SANITIZERS_TO_BUILD AND
875     OS_NAME MATCHES "Linux")
876   set(COMPILER_RT_HAS_GWP_ASAN TRUE)
877 else()
878   set(COMPILER_RT_HAS_GWP_ASAN FALSE)
879 endif()
880 pythonize_bool(COMPILER_RT_HAS_GWP_ASAN)