OSDN Git Service

* lib/target-libpath.exp (set_ld_library_path_env_vars):
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / c-torture.exp
1 # Copyright (C) 1992-1998, 1999, 2000, 2007, 2008
2 # Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with GCC; see the file COPYING3.  If not see
16 # <http://www.gnu.org/licenses/>.
17
18 # This file was written by Rob Savoye. (rob@cygnus.com)
19
20 load_lib file-format.exp
21 load_lib target-libpath.exp
22
23 # The default option list can be overridden by
24 # TORTURE_OPTIONS="{ { list1 } ... { listN } }"
25
26 if [info exists TORTURE_OPTIONS] {
27     set C_TORTURE_OPTIONS $TORTURE_OPTIONS
28 } else {
29     # It is theoretically beneficial to group all of the O2/O3 options together,
30     # as in many cases the compiler will generate identical executables for
31     # all of them--and the c-torture testsuite will skip testing identical
32     # executables multiple times.
33     # Also note that -finline-functions is explicitly included in one of the
34     # items below, even though -O3 is also specified, because some ports may
35     # choose to disable inlining functions by default, even when optimizing.
36     set C_TORTURE_OPTIONS [list \
37         { -O0 } \
38         { -O1 } \
39         { -O2 } \
40         { -O3 -fomit-frame-pointer } \
41         { -O3 -fomit-frame-pointer -funroll-loops } \
42         { -O3 -fomit-frame-pointer -funroll-all-loops -finline-functions } \
43         { -O3 -g } \
44         { -Os } ]
45 }
46
47 if [info exists ADDITIONAL_TORTURE_OPTIONS] {
48     set C_TORTURE_OPTIONS \
49         [concat $C_TORTURE_OPTIONS $ADDITIONAL_TORTURE_OPTIONS]
50 }
51
52 global GCC_UNDER_TEST
53 if ![info exists GCC_UNDER_TEST] {
54     set GCC_UNDER_TEST "[find_gcc]"
55 }
56
57 global orig_environment_saved
58
59 # This file may be sourced, so don't override environment settings
60 # that have been previously setup.
61 if { $orig_environment_saved == 0 } {
62     append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
63     set_ld_library_path_env_vars
64 }
65
66 #
67 # c-torture-compile -- runs the Tege C-torture test
68 #
69 # SRC is the full pathname of the testcase.
70 # OPTION is the specific compiler flag we're testing (eg: -O2).
71 #
72 proc c-torture-compile { src option } {
73     global output
74     global srcdir tmpdir
75     global host_triplet
76
77     set output "$tmpdir/[file tail [file rootname $src]].o"
78
79     regsub "(?q)$srcdir/" $src "" testcase
80     # If we couldn't rip $srcdir out of `src' then just do the best we can.
81     # The point is to reduce the unnecessary noise in the logs.  Don't strip
82     # out too much because different testcases with the same name can confuse
83     # `test-tool'.
84     if [string match "/*" $testcase] {
85         set testcase "[file tail [file dirname $src]]/[file tail $src]"
86     }
87
88     verbose "Testing $testcase, $option" 1
89
90     # Run the compiler and analyze the results.
91     set options ""
92     lappend options "additional_flags=-w $option"
93
94     set comp_output [gcc_target_compile "$src" "$output" object $options]
95     gcc_check_compile $testcase $option $output $comp_output
96     file_on_host delete $output
97 }
98
99 #
100 # c-torture-execute -- utility to compile and execute a testcase
101 #
102 # SOURCES is a list of full pathnames to the test source files.
103 # The first filename in this list forms the "testcase".
104 #
105 # If the testcase has an associated .x file, we source that to run the
106 # test instead.  We use .x so that we don't lengthen the existing filename
107 # to more than 14 chars.
108 #
109 proc c-torture-execute { sources args } {
110     global tmpdir tool srcdir output compiler_conditional_xfail_data
111
112     # Use the first source filename given as the filename under test.
113     set src [lindex $sources 0]
114
115     if { [llength $args] > 0 } {
116         set additional_flags [lindex $args 0]
117     } else {
118         set additional_flags ""
119     }
120     # Check for alternate driver.
121     if [file exists [file rootname $src].x] {
122         verbose "Using alternate driver [file rootname [file tail $src]].x" 2
123         set done_p 0
124         catch "set done_p \[source [file rootname $src].x\]"
125         if { $done_p } {
126             return
127         }
128     }
129
130     # Look for a loop within the source code - if we don't find one,
131     # don't pass -funroll[-all]-loops.
132     global torture_with_loops torture_without_loops
133     if [expr [search_for $src "for*("]+[search_for $src "while*("]] then {
134         set option_list $torture_with_loops
135     } else {
136         set option_list $torture_without_loops
137     }
138
139     set executable $tmpdir/[file tail [file rootname $src].x]
140
141     regsub "(?q)$srcdir/" $src "" testcase
142     # If we couldn't rip $srcdir out of `src' then just do the best we can.
143     # The point is to reduce the unnecessary noise in the logs.  Don't strip
144     # out too much because different testcases with the same name can confuse
145     # `test-tool'.
146     if [string match "/*" $testcase] {
147         set testcase "[file tail [file dirname $src]]/[file tail $src]"
148     }
149
150     set count 0
151     set oldstatus "foo"
152     foreach option $option_list {
153         if { $count > 0 } {
154             set oldexec $execname
155         }
156         set execname "${executable}${count}"
157         incr count
158
159         # torture_{compile,execute}_xfail are set by the .x script
160         # (if present)
161         if [info exists torture_compile_xfail] {
162             setup_xfail $torture_compile_xfail
163         }
164
165         # torture_execute_before_{compile,execute} can be set by the .x script
166         # (if present)
167         if [info exists torture_eval_before_compile] {
168             set ignore_me [eval $torture_eval_before_compile]
169         }
170
171         file_on_host delete $execname
172         verbose "Testing $testcase, $option" 1
173
174         set options ""
175         lappend options "additional_flags=-w $option"
176         if { $additional_flags != "" } {
177             lappend options "additional_flags=$additional_flags"
178         }
179         set comp_output [gcc_target_compile "$sources" "${execname}" executable $options]
180
181         if ![gcc_check_compile "$testcase compilation" $option $execname $comp_output] {
182             unresolved "$testcase execution, $option"
183             file_on_host delete $execname
184             continue
185         }
186
187         # See if this source file uses "long long" types, if it does, and
188         # no_long_long is set, skip execution of the test.
189         if [target_info exists no_long_long] then {
190             if [expr [search_for $src "long long"]] then {
191                 unsupported "$testcase execution, $option"
192                 continue
193             }
194         }
195
196         if [info exists torture_execute_xfail] {
197             setup_xfail $torture_execute_xfail
198         }
199
200         if [info exists torture_eval_before_execute] {
201             set ignore_me [eval $torture_eval_before_execute]
202         }
203
204
205         # Sometimes we end up creating identical executables for two
206         # consecutive sets of different of compiler options.
207         #
208         # In such cases we know the result of this test will be identical
209         # to the result of the last test.
210         #
211         # So in cases where the time to load and run/simulate the test
212         # is relatively high, compare the two binaries and avoid rerunning
213         # tests if the executables are identical.
214         #
215         # Do not do this for native testing since the cost to load/execute
216         # the test is fairly small and the comparison step actually slows
217         # the entire process down because it usually does not "hit".
218         set skip 0
219         if { ![isnative] && [info exists oldexec] } {
220             if { [file_on_host cmp $oldexec $execname] == 0 } {
221                 set skip 1
222             }
223         }
224         if { $skip == 0 } {
225             set result [gcc_load "$execname" "" ""]
226             set status [lindex $result 0]
227             set output [lindex $result 1]
228         }
229         if { $oldstatus == "pass" } {
230             file_on_host delete $oldexec
231         }
232         $status "$testcase execution, $option"
233         set oldstatus $status
234     }
235     if [info exists status] {
236         if { $status == "pass" } {
237             file_on_host delete $execname
238         }
239     }
240 }
241
242 #
243 # search_for -- looks for a string match in a file
244 #
245 proc search_for { file pattern } {
246     set fd [open $file r]
247     while { [gets $fd cur_line]>=0 } {
248         if [string match "*$pattern*" $cur_line] then {
249             close $fd
250             return 1
251         }
252     }
253     close $fd
254     return 0
255 }
256
257 #
258 # c-torture -- the c-torture testcase source file processor
259 #
260 # This runs compilation only tests (no execute tests).
261 # SRC is the full pathname of the testcase, or just a file name in which case
262 # we prepend $srcdir/$subdir.
263 #
264 # If the testcase has an associated .x file, we source that to run the
265 # test instead.  We use .x so that we don't lengthen the existing filename
266 # to more than 14 chars.
267 #
268 proc c-torture { args } {
269     global srcdir subdir compiler_conditional_xfail_data
270
271     set src [lindex $args 0]
272     if { [llength $args] > 1 } {
273         set options [lindex $args 1]
274     } else {
275         set options ""
276     }
277
278     # Prepend $srdir/$subdir if missing.
279     if ![string match "*/*" $src] {
280         set src "$srcdir/$subdir/$src"
281     }
282
283     # Check for alternate driver.
284     if [file exists [file rootname $src].x] {
285         verbose "Using alternate driver [file rootname [file tail $src]].x" 2
286         set done_p 0
287         catch "set done_p \[source [file rootname $src].x\]"
288         if { $done_p } {
289             return
290         }
291     }
292
293     # Look for a loop within the source code - if we don't find one,
294     # don't pass -funroll[-all]-loops.
295     global torture_with_loops torture_without_loops
296     if [expr [search_for $src "for*("]+[search_for $src "while*("]] then {
297         set option_list $torture_with_loops
298     } else {
299         set option_list $torture_without_loops
300     }
301
302     # loop through all the options
303     foreach option $option_list {
304         # torture_compile_xfail is set by the .x script (if present)
305         if [info exists torture_compile_xfail] {
306             setup_xfail $torture_compile_xfail
307         }
308
309         # torture_execute_before_compile is set by the .x script (if present)
310         if [info exists torture_eval_before_compile] {
311             set ignore_me [eval $torture_eval_before_compile]
312         }
313
314         c-torture-compile $src "$option $options"
315     }
316 }