eb1f3a986f26c51e8646399398cae2d0ad2b0615
[gcc.git] / gcc / testsuite / lib / tsan-dg.exp
1 # Copyright (C) 2013-2016 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
16
17 # Return 1 if compilation with -fsanitize=thread is error-free for trivial
18 # code, 0 otherwise.
19
20 proc check_effective_target_fsanitize_thread {} {
21 return [check_runtime fsanitize_thread {
22 int main (void) { return 0; }
23 } "-fsanitize=thread"]
24 }
25
26 #
27 # tsan_link_flags -- compute library path and flags to find libtsan.
28 # (originally from g++.exp)
29 #
30
31 proc tsan_link_flags { paths } {
32 global srcdir
33 global ld_library_path
34 global shlib_ext
35 global tsan_saved_library_path
36
37 set gccpath ${paths}
38 set flags ""
39
40 set shlib_ext [get_shlib_extension]
41 set tsan_saved_library_path $ld_library_path
42
43 if { $gccpath != "" } {
44 if { [file exists "${gccpath}/libsanitizer/tsan/.libs/libtsan.a"]
45 || [file exists "${gccpath}/libsanitizer/tsan/.libs/libtsan.${shlib_ext}"] } {
46 append flags " -B${gccpath}/libsanitizer/tsan/ "
47 append flags " -L${gccpath}/libsanitizer/tsan/.libs "
48 append ld_library_path ":${gccpath}/libsanitizer/tsan/.libs"
49 }
50 } else {
51 global tool_root_dir
52
53 set libtsan [lookfor_file ${tool_root_dir} libtsan]
54 if { $libtsan != "" } {
55 append flags "-L${libtsan} "
56 append ld_library_path ":${libtsan}"
57 }
58 }
59
60 set_ld_library_path_env_vars
61
62 return "$flags"
63 }
64
65 #
66 # tsan_init -- called at the start of each subdir of tests
67 #
68
69 proc tsan_init { args } {
70 global TEST_ALWAYS_FLAGS
71 global ALWAYS_CXXFLAGS
72 global TOOL_OPTIONS
73 global tsan_saved_TEST_ALWAYS_FLAGS
74 global tsan_saved_ALWAYS_CXXFLAGS
75 global dg-do-what-default
76 global tsan_saved_dg-do-what-default
77
78 set link_flags ""
79 if ![is_remote host] {
80 if [info exists TOOL_OPTIONS] {
81 set link_flags "[tsan_link_flags [get_multilibs ${TOOL_OPTIONS}]]"
82 } else {
83 set link_flags "[tsan_link_flags [get_multilibs]]"
84 }
85 }
86
87 if [info exists dg-do-what-default] {
88 set tsan_saved_dg-do-what-default ${dg-do-what-default}
89 }
90 if [info exists TEST_ALWAYS_FLAGS] {
91 set tsan_saved_TEST_ALWAYS_FLAGS $TEST_ALWAYS_FLAGS
92 }
93 if [info exists ALWAYS_CXXFLAGS] {
94 set tsan_saved_ALWAYS_CXXFLAGS $ALWAYS_CXXFLAGS
95 set ALWAYS_CXXFLAGS [concat "{ldflags=$link_flags}" $ALWAYS_CXXFLAGS]
96 set ALWAYS_CXXFLAGS [concat "{additional_flags=-fsanitize=thread -g}" $ALWAYS_CXXFLAGS]
97 } else {
98 if [info exists TEST_ALWAYS_FLAGS] {
99 set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=thread -g $TEST_ALWAYS_FLAGS"
100 } else {
101 set TEST_ALWAYS_FLAGS "$link_flags -fsanitize=thread -g"
102 }
103 }
104
105 set dg-do-what-default run
106 if { $link_flags != "" } {
107 global individual_timeout
108
109 # Lower timeout value in case test does not terminate properly.
110 set individual_timeout 20
111 if [check_runtime_nocache tsan_works {
112 int main () { return 0; }
113 } "-fsanitize=thread -g"] {
114 set dg-do-what-default run
115 } else {
116 set dg-do-what-default compile
117 }
118 unset individual_timeout
119 }
120 }
121
122 #
123 # tsan_finish -- called at the end of each subdir of tests
124 #
125
126 proc tsan_finish { args } {
127 global TEST_ALWAYS_FLAGS
128 global tsan_saved_TEST_ALWAYS_FLAGS
129 global tsan_saved_ALWAYS_CXXFLAGS
130 global dg-do-what-default
131 global tsan_saved_dg-do-what-default
132 global tsan_saved_library_path
133 global ld_library_path
134
135 if [info exists tsan_saved_ALWAYS_CXXFLAGS ] {
136 set ALWAYS_CXXFLAGS $tsan_saved_ALWAYS_CXXFLAGS
137 } else {
138 if [info exists tsan_saved_TEST_ALWAYS_FLAGS] {
139 set TEST_ALWAYS_FLAGS $tsan_saved_TEST_ALWAYS_FLAGS
140 } else {
141 unset TEST_ALWAYS_FLAGS
142 }
143 }
144
145 if [info exists tsan_saved_dg-do-what-default] {
146 set dg-do-what-default ${tsan_saved_dg-do-what-default}
147 } else {
148 unset dg-do-what-default
149 }
150 set ld_library_path $tsan_saved_library_path
151 set_ld_library_path_env_vars
152 clear_effective_target_cache
153 }