import os
import platform

# Add "ASan" and "LSan" features, assuming the compiler-rt library is available
config.available_features.add('ASan')
config.available_features.add('LSan')

# FreeBSD TSan needs https://reviews.llvm.org/D85292,
# Linux TSan currently only works with static druntime,
# and there's no Windows TSan (yet?).
sys = platform.system()
if (sys != 'FreeBSD') and (sys != 'Windows') and not (sys == 'Linux' and config.shared_rt_libs_only):
    config.available_features.add('TSan')

# MSan is supported on Linux, FreeBSD, and OpenBSD: https://clang.llvm.org/docs/MemorySanitizer.html#supported-platforms
if (sys == 'Linux') or (sys == 'FreeBSD') or (sys == 'OpenBSD'):
    config.available_features.add('MSan')

# Add "Fuzzer" feature, assuming the compiler-rt library is available
config.available_features.add('Fuzzer')

if 'ASan' in config.available_features:
    # On Darwin, ASan defaults to `abort_on_error=1`, which would make tests run
    # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
    # Also, make sure we do not overwhelm the syslog while testing.
    default_asan_options = 'abort_on_error=0:log_to_syslog=0'
    config.environment['ASAN_OPTIONS'] = default_asan_options
    # And add a convenience substitution so we can append to the default ASAN_OPTIONS
    config.substitutions.append(('%env_asan_opts=',
                                 'env ASAN_OPTIONS=' + default_asan_options + ':'))

# Add the %deflake substitution, to help with flaky tests.
# Usage: "%deflake <count> <program>", runs <program> a maximum of <count> times until a failure occurs.
config.substitutions.append( ("%deflake", os.path.join(os.path.dirname(__file__), "deflake.bash")))
