xref: /llvm-project/compiler-rt/test/tsan/lit.cfg.py (revision f98ee40f4b5d7474fc67e82824bf6abbaedb7b1c)
1# -*- Python -*-
2
3import os
4
5
6def get_required_attr(config, attr_name):
7    attr_value = getattr(config, attr_name, None)
8    if not attr_value:
9        lit_config.fatal(
10            "No attribute %r in test configuration! You may need to run "
11            "tests from your build directory or add this attribute "
12            "to lit.site.cfg.py " % attr_name
13        )
14    return attr_value
15
16
17# Setup config name.
18config.name = "ThreadSanitizer" + config.name_suffix
19
20# Setup source root.
21config.test_source_root = os.path.dirname(__file__)
22
23# Setup environment variables for running ThreadSanitizer.
24default_tsan_opts = "atexit_sleep_ms=0"
25
26if config.host_os == "Darwin":
27    # On Darwin, we default to `abort_on_error=1`, which would make tests run
28    # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
29    default_tsan_opts += ":abort_on_error=0"
30    # On Darwin, we default to ignore_noninstrumented_modules=1, which also
31    # suppresses some races the tests are supposed to find. Let's run without this
32    # setting, but turn it back on for Darwin tests (see Darwin/lit.local.cfg.py).
33    default_tsan_opts += ":ignore_noninstrumented_modules=0"
34    default_tsan_opts += ":ignore_interceptors_accesses=0"
35
36# Platform-specific default TSAN_OPTIONS for lit tests.
37if default_tsan_opts:
38    config.environment["TSAN_OPTIONS"] = default_tsan_opts
39    default_tsan_opts += ":"
40config.substitutions.append(
41    ("%env_tsan_opts=", "env TSAN_OPTIONS=" + default_tsan_opts)
42)
43
44# GCC driver doesn't add necessary compile/link flags with -fsanitize=thread.
45if config.compiler_id == "GNU":
46    extra_cflags = ["-fPIE", "-pthread", "-ldl", "-lrt", "-pie"]
47else:
48    extra_cflags = []
49
50tsan_incdir = config.test_source_root + "/../"
51# Setup default compiler flags used with -fsanitize=thread option.
52clang_tsan_cflags = (
53    ["-fsanitize=thread", "-Wall"]
54    + [config.target_cflags]
55    + config.debug_info_flags
56    + extra_cflags
57    + ["-I%s" % tsan_incdir]
58)
59clang_tsan_cxxflags = (
60    config.cxx_mode_flags + clang_tsan_cflags + ["-std=c++11"] + ["-I%s" % tsan_incdir]
61)
62# Add additional flags if we're using instrumented libc++.
63# Instrumented libcxx currently not supported on Darwin.
64if config.has_libcxx and config.host_os != "Darwin":
65    # FIXME: Dehardcode this path somehow.
66    libcxx_path = os.path.join(
67        config.compiler_rt_obj_root,
68        "lib",
69        "tsan",
70        "libcxx_tsan_%s" % config.target_arch,
71    )
72    libcxx_incdir = os.path.join(libcxx_path, "include", "c++", "v1")
73    libcxx_libdir = os.path.join(libcxx_path, "lib")
74    libcxx_a = os.path.join(libcxx_libdir, "libc++.a")
75    clang_tsan_cxxflags += ["-nostdinc++", "-I%s" % libcxx_incdir]
76    config.substitutions.append(("%link_libcxx_tsan", libcxx_a))
77else:
78    config.substitutions.append(("%link_libcxx_tsan", ""))
79
80
81def build_invocation(compile_flags):
82    return " " + " ".join([config.clang] + compile_flags) + " "
83
84
85config.substitutions.append(("%clang_tsan ", build_invocation(clang_tsan_cflags)))
86config.substitutions.append(("%clangxx_tsan ", build_invocation(clang_tsan_cxxflags)))
87
88# Define CHECK-%os to check for OS-dependent output.
89config.substitutions.append(("CHECK-%os", ("CHECK-" + config.host_os)))
90
91config.substitutions.append(
92    (
93        "%deflake ",
94        os.path.join(os.path.dirname(__file__), "deflake.bash")
95        + " "
96        + config.deflake_threshold
97        + " ",
98    )
99)
100
101# Default test suffixes.
102config.suffixes = [".c", ".cpp", ".m", ".mm"]
103
104if config.host_os not in ["FreeBSD", "Linux", "Darwin", "NetBSD"]:
105    config.unsupported = True
106
107if config.android:
108    config.unsupported = True
109
110if not config.parallelism_group:
111    config.parallelism_group = "shadow-memory"
112
113if config.host_os == "NetBSD":
114    config.substitutions.insert(0, ("%run", config.netbsd_noaslr_prefix))
115