blob: c5164713dbe43795ad23eea8eed721696656c268 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001# -*- Python -*-
2
3import os
Stephen Hines6a211c52014-07-21 00:49:56 -07004import platform
Stephen Hines2d1fdb22014-05-28 23:58:16 -07005
Stephen Hines86277eb2015-03-23 12:06:32 -07006import lit.formats
7
Stephen Hines2d1fdb22014-05-28 23:58:16 -07008def get_required_attr(config, attr_name):
9 attr_value = getattr(config, attr_name, None)
10 if attr_value == None:
11 lit_config.fatal(
12 "No attribute %r in test configuration! You may need to run "
13 "tests from your build directory or add this attribute "
14 "to lit.site.cfg " % attr_name)
15 return attr_value
16
Stephen Hines86277eb2015-03-23 12:06:32 -070017def push_dynamic_library_lookup_path(config, new_path):
18 if platform.system() == 'Windows':
19 dynamic_library_lookup_var = 'PATH'
20 elif platform.system() == 'Darwin':
21 dynamic_library_lookup_var = 'DYLD_LIBRARY_PATH'
22 else:
23 dynamic_library_lookup_var = 'LD_LIBRARY_PATH'
24
Stephen Hines2d1fdb22014-05-28 23:58:16 -070025 new_ld_library_path = os.path.pathsep.join(
Stephen Hines86277eb2015-03-23 12:06:32 -070026 (new_path, config.environment.get(dynamic_library_lookup_var, '')))
27 config.environment[dynamic_library_lookup_var] = new_ld_library_path
Stephen Hines2d1fdb22014-05-28 23:58:16 -070028
29# Setup config name.
30config.name = 'AddressSanitizer' + config.name_suffix
31
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070032# Setup default ASAN_OPTIONS
33config.environment['ASAN_OPTIONS'] = 'symbolize_vs_style=false'
34
Stephen Hines86277eb2015-03-23 12:06:32 -070035# testFormat: The test format to use to interpret tests.
36external_bash = (not sys.platform in ['win32'])
37config.test_format = lit.formats.ShTest(external_bash)
38
Stephen Hines2d1fdb22014-05-28 23:58:16 -070039# Setup source root.
40config.test_source_root = os.path.dirname(__file__)
41
Stephen Hines86277eb2015-03-23 12:06:32 -070042# There is no libdl on FreeBSD.
43if config.host_os != 'FreeBSD':
44 libdl_flag = "-ldl"
45else:
46 libdl_flag = ""
47
Stephen Hines2d1fdb22014-05-28 23:58:16 -070048# GCC-ASan doesn't link in all the necessary libraries automatically, so
49# we have to do it ourselves.
50if config.compiler_id == 'GNU':
Stephen Hines86277eb2015-03-23 12:06:32 -070051 extra_linkflags = ["-pthread", "-lstdc++", libdl_flag]
Stephen Hines2d1fdb22014-05-28 23:58:16 -070052else:
53 extra_linkflags = []
Stephen Hines6d186232014-11-26 17:56:19 -080054
Stephen Hines2d1fdb22014-05-28 23:58:16 -070055# Setup default compiler flags used with -fsanitize=address option.
56# FIXME: Review the set of required flags and check if it can be reduced.
57target_cflags = [get_required_attr(config, "target_cflags")] + extra_linkflags
58target_cxxflags = config.cxx_mode_flags + target_cflags
Stephen Hines6d186232014-11-26 17:56:19 -080059clang_asan_static_cflags = (["-fsanitize=address",
Stephen Hines6a211c52014-07-21 00:49:56 -070060 "-mno-omit-leaf-frame-pointer",
61 "-fno-omit-frame-pointer",
Stephen Hines6d186232014-11-26 17:56:19 -080062 "-fno-optimize-sibling-calls"] +
63 config.debug_info_flags + target_cflags)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070064clang_asan_static_cxxflags = config.cxx_mode_flags + clang_asan_static_cflags
65
66if config.asan_dynamic:
67 clang_asan_cflags = clang_asan_static_cflags + ['-shared-libasan']
68 clang_asan_cxxflags = clang_asan_static_cxxflags + ['-shared-libasan']
69 config.available_features.add("asan-dynamic-runtime")
70else:
71 clang_asan_cflags = clang_asan_static_cflags
72 clang_asan_cxxflags = clang_asan_static_cxxflags
73 config.available_features.add("asan-static-runtime")
74
75asan_lit_source_dir = get_required_attr(config, "asan_lit_source_dir")
Stephen Hines6d186232014-11-26 17:56:19 -080076if config.android == "1":
Stephen Hines2d1fdb22014-05-28 23:58:16 -070077 config.available_features.add('android')
78 clang_wrapper = os.path.join(asan_lit_source_dir,
79 "android_commands", "android_compile.py") + " "
80else:
Stephen Hines86277eb2015-03-23 12:06:32 -070081 config.available_features.add('not-android')
Stephen Hines2d1fdb22014-05-28 23:58:16 -070082 clang_wrapper = ""
83
84def build_invocation(compile_flags):
85 return " " + " ".join([clang_wrapper, config.clang] + compile_flags) + " "
86
87config.substitutions.append( ("%clang ", build_invocation(target_cflags)) )
88config.substitutions.append( ("%clangxx ", build_invocation(target_cxxflags)) )
89config.substitutions.append( ("%clang_asan ", build_invocation(clang_asan_cflags)) )
90config.substitutions.append( ("%clangxx_asan ", build_invocation(clang_asan_cxxflags)) )
91config.substitutions.append( ("%shared_libasan", "libclang_rt.asan-%s.so" % config.target_arch))
92if config.asan_dynamic:
93 config.substitutions.append( ("%clang_asan_static ", build_invocation(clang_asan_static_cflags)) )
94 config.substitutions.append( ("%clangxx_asan_static ", build_invocation(clang_asan_static_cxxflags)) )
95
Stephen Hines6a211c52014-07-21 00:49:56 -070096# Windows-specific tests might also use the clang-cl.exe driver.
97if platform.system() == 'Windows':
98 clang_cl_asan_cxxflags = ["-fsanitize=address",
99 "-Wno-deprecated-declarations",
100 "-WX",
101 "-D_HAS_EXCEPTIONS=0",
102 "-Zi"] + target_cflags
Stephen Hines6d186232014-11-26 17:56:19 -0800103 if config.asan_dynamic:
104 clang_cl_asan_cxxflags.append("-MD")
Stephen Hines6a211c52014-07-21 00:49:56 -0700105 clang_invocation = build_invocation(clang_cl_asan_cxxflags)
106 clang_cl_invocation = clang_invocation.replace("clang.exe","clang-cl.exe")
107 config.substitutions.append( ("%clang_cl_asan ", clang_cl_invocation) )
Stephen Hines6d186232014-11-26 17:56:19 -0800108 config.substitutions.append( ("%asan_dll_thunk",
109 os.path.join(config.compiler_rt_libdir, "clang_rt.asan_dll_thunk-i386.lib")))
Stephen Hines6a211c52014-07-21 00:49:56 -0700110
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700111# FIXME: De-hardcode this path.
112asan_source_dir = os.path.join(
113 get_required_attr(config, "compiler_rt_src_root"), "lib", "asan")
114# Setup path to asan_symbolize.py script.
115asan_symbolize = os.path.join(asan_source_dir, "scripts", "asan_symbolize.py")
116if not os.path.exists(asan_symbolize):
117 lit_config.fatal("Can't find script on path %r" % asan_symbolize)
118python_exec = get_required_attr(config, "python_executable")
119config.substitutions.append( ("%asan_symbolize", python_exec + " " + asan_symbolize + " ") )
120# Setup path to sancov.py script.
121sanitizer_common_source_dir = os.path.join(
122 get_required_attr(config, "compiler_rt_src_root"), "lib", "sanitizer_common")
123sancov = os.path.join(sanitizer_common_source_dir, "scripts", "sancov.py")
124if not os.path.exists(sancov):
125 lit_config.fatal("Can't find script on path %r" % sancov)
126python_exec = get_required_attr(config, "python_executable")
127config.substitutions.append( ("%sancov", python_exec + " " + sancov + " ") )
128
129# Determine kernel bitness
Stephen Hines6d186232014-11-26 17:56:19 -0800130if config.host_arch.find('64') != -1 and config.android != "1":
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700131 kernel_bits = '64'
132else:
133 kernel_bits = '32'
134
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700135config.substitutions.append( ('CHECK-%kernel_bits', ("CHECK-kernel-" + kernel_bits + "-bits")))
136
Stephen Hines86277eb2015-03-23 12:06:32 -0700137config.substitutions.append( ("%libdl", libdl_flag) )
138
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700139config.available_features.add("asan-" + config.bits + "-bits")
140
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700141if config.host_os == 'Darwin':
142 config.substitutions.append( ("%ld_flags_rpath_exe", '-Wl,-rpath,@executable_path/ %dynamiclib') )
143 config.substitutions.append( ("%ld_flags_rpath_so", '-install_name @rpath/`basename %dynamiclib`') )
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700144elif config.host_os == 'FreeBSD':
145 config.substitutions.append( ("%ld_flags_rpath_exe", "-Wl,-z,origin -Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec") )
146 config.substitutions.append( ("%ld_flags_rpath_so", '') )
147elif config.host_os == 'Linux':
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700148 config.substitutions.append( ("%ld_flags_rpath_exe", "-Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec") )
149 config.substitutions.append( ("%ld_flags_rpath_so", '') )
150
151# Must be defined after the substitutions that use %dynamiclib.
152config.substitutions.append( ("%dynamiclib", '%T/lib%xdynamiclib_namespec.so') )
153config.substitutions.append( ("%xdynamiclib_namespec", '$(basename %t).dynamic') )
154
Stephen Hines6a211c52014-07-21 00:49:56 -0700155# Allow tests to use REQUIRES=stable-runtime. For use when you cannot use XFAIL
156# because the test hangs.
157if config.target_arch != 'arm':
158 config.available_features.add('stable-runtime')
159
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700160# Turn on leak detection on 64-bit Linux.
Stephen Hines86277eb2015-03-23 12:06:32 -0700161if config.host_os == 'Linux' and config.target_arch == 'x86_64':
162 config.available_features.add('leak-detection')
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700163
164# Set LD_LIBRARY_PATH to pick dynamic runtime up properly.
Stephen Hines86277eb2015-03-23 12:06:32 -0700165push_dynamic_library_lookup_path(config, config.compiler_rt_libdir)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700166
167# GCC-ASan uses dynamic runtime by default.
168if config.compiler_id == 'GNU':
169 gcc_dir = os.path.dirname(config.clang)
170 libasan_dir = os.path.join(gcc_dir, "..", "lib" + config.bits)
Stephen Hines86277eb2015-03-23 12:06:32 -0700171 push_dynamic_library_lookup_path(config, libasan_dir)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700172
173# Default test suffixes.
174config.suffixes = ['.c', '.cc', '.cpp']
175
176if config.host_os == 'Darwin':
177 config.suffixes.append('.mm')
178
Stephen Hines6d186232014-11-26 17:56:19 -0800179# AddressSanitizer tests are currently supported on Linux, Darwin and
180# FreeBSD only.
181if config.host_os not in ['Linux', 'Darwin', 'FreeBSD']:
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700182 config.unsupported = True