1# -*- Python -*-
2
3import os
4
5def get_required_attr(config, attr_name):
6  attr_value = getattr(config, attr_name, None)
7  if not attr_value:
8    lit_config.fatal(
9      "No attribute %r in test configuration! You may need to run "
10      "tests from your build directory or add this attribute "
11      "to lit.site.cfg " % attr_name)
12  return attr_value
13
14# Setup config name.
15config.name = 'ThreadSanitizer'
16
17# Setup source root.
18config.test_source_root = os.path.dirname(__file__)
19
20# Setup environment variables for running ThreadSanitizer.
21tsan_options = "atexit_sleep_ms=0"
22
23config.environment['TSAN_OPTIONS'] = tsan_options
24
25# GCC driver doesn't add necessary compile/link flags with -fsanitize=thread.
26if config.compiler_id == 'GNU':
27  extra_cflags = ["-fPIE", "-pthread", "-ldl", "-lstdc++", "-lrt", "-pie"]
28else:
29  extra_cflags = []
30
31# Setup default compiler flags used with -fsanitize=thread option.
32# FIXME: Review the set of required flags and check if it can be reduced.
33clang_tsan_cflags = ["-fsanitize=thread",
34                     "-g",
35                     "-Wall",
36                     "-m64"] + extra_cflags
37clang_tsan_cxxflags = config.cxx_mode_flags + clang_tsan_cflags
38# Add additional flags if we're using instrumented libc++.
39if config.has_libcxx:
40  # FIXME: Dehardcode this path somehow.
41  libcxx_path = os.path.join(config.compiler_rt_obj_root, "lib",
42                             "tsan", "libcxx_tsan")
43  libcxx_incdir = os.path.join(libcxx_path, "include", "c++", "v1")
44  libcxx_libdir = os.path.join(libcxx_path, "lib")
45  libcxx_so = os.path.join(libcxx_libdir, "libc++.so")
46  clang_tsan_cxxflags += ["-std=c++11",
47                          "-I%s" % libcxx_incdir,
48                          libcxx_so,
49                          "-Wl,-rpath=%s" % libcxx_libdir]
50
51def build_invocation(compile_flags):
52  return " " + " ".join([config.clang] + compile_flags) + " "
53
54config.substitutions.append( ("%clang_tsan ", build_invocation(clang_tsan_cflags)) )
55config.substitutions.append( ("%clangxx_tsan ", build_invocation(clang_tsan_cxxflags)) )
56
57# Define CHECK-%os to check for OS-dependent output.
58config.substitutions.append( ('CHECK-%os', ("CHECK-" + config.host_os)))
59
60config.substitutions.append( ("%deflake ", os.path.join(os.path.dirname(__file__), "deflake.bash")) )
61
62# Default test suffixes.
63config.suffixes = ['.c', '.cc', '.cpp']
64
65# ThreadSanitizer tests are currently supported on Linux only.
66if config.host_os not in ['Linux']:
67  config.unsupported = True
68