OSDN Git Service

e5aaf3a13d60f145b0621965e1b4d92781e00fc7
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / lto.exp
1 # Copyright (C) 2009, 2010 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 # Contributed by Diego Novillo <dnovillo@google.com>
18
19 # Prune messages that aren't useful.
20
21 proc lto_prune_warns { text } {
22
23     verbose "lto_prune_warns: entry: $text" 2
24
25     # Many tests that use visibility will still pass on platforms that don't support it.
26     regsub -all "(^|\n)\[^\n\]*: warning: visibility attribute not supported in this configuration; ignored\[^\n\]*" $text "" text
27
28     # And any stray location lines.
29     regsub -all "(^|\n)\[^\n\]*: In function \[^\n\]*" $text "" text
30     regsub -all "(^|\n)In file included from :\[^\n\]*" $text "" text
31
32     # Sun ld warns about common symbols with differing sizes.  Unlike GNU ld
33     # --warn-common (off by default), they cannot be disabled.
34     regsub -all "(^|\n)ld: warning: symbol `\[^\n\]*' has differing sizes:" $text "" text
35     regsub -all "(^|\n)\[ \t\]*\[\(\]file \[^\n\]* value=\[^\n\]*; file \[^\n\]* value=\[^\n\]*\[)\];" $text "" text
36     regsub -all "(^|\n)\[ \t\]*\[^\n\]* definition taken" $text "" text
37
38     verbose "lto_prune_warns: exit: $text" 2
39
40     return $text
41 }
42
43 # lto_init -- called at the start of each subdir of tests
44
45 proc lto_init { args } {
46     global LTO_OPTIONS
47
48     if {[info exists args] && $args == "no-mathlib"} {
49         global board_info
50         global saved_mathlib
51
52         set dest [target_info name]
53         if [board_info $dest exists mathlib] {
54             set saved_mathlib [board_info $dest mathlib]
55         }
56         set board_info($dest,mathlib) " "
57     }
58
59     # Each test is run with the compiler options from this list.
60     # The default option lists can be overridden by LTO_OPTIONS="[list
61     # {opts_1} {opts_2}... {opts_n}]" where opts_i are lists of options.
62     # You can put this in the environment before site.exp is written or
63     # add it to site.exp directly.
64     if ![info exists LTO_OPTIONS] {
65         set LTO_OPTIONS [list   \
66             {-O0 -fwhopr}       \
67             {-O2 -fwhopr}       \
68             {-O0 -flto}         \
69             {-O2 -flto}         \
70         ]
71     }
72 }
73
74 #
75 # lto_finish -- called at the end of each subdir of tests if mathlib is
76 #               changed.
77 #
78
79 proc lto_finish { } {
80     global board_info
81     global saved_mathlib
82
83     set dest [target_info name]
84     if [info exists saved_mathlib] {
85         set board_info($dest,mathlib) $saved_mathlib
86     } elseif [board_info $dest exists mathlib] {
87         unset board_info($dest,mathlib)
88     }
89 }
90
91 # Subsets of tests can be selectively disabled by members of this list:
92 #  - ATTRIBUTE: disable all tests using the __attribute__ extension,
93 #  - COMPLEX: disable all tests using the complex types feature,
94 #  - COMPLEX_INT: disable all tests using the complex integral types extension,
95 #  - VA: disable all tests using the variable number of arguments feature,
96 #  - VLA_IN_STRUCT: disable all tests using the variable-length arrays as
97 #    structure members extension,
98 #  - ZERO_ARRAY: disable all tests using the zero-sized arrays extension.
99 # The default skip lists can be overriden by
100 # LTO_SKIPS="[list {skip_1}...{skip_n}]"
101 # where skip_i are skip identifiers.  You can put this in the environment
102 # before site.exp is written or add it to site.exp directly.
103 if ![info exists LTO_SKIPS] {
104     set LTO_SKIPS [list {}]
105 }
106
107 global lto_skip_list
108 set lto_skip_list $LTO_SKIPS
109
110 load_lib dg.exp
111 load_lib gcc-dg.exp
112
113 # lto-obj -- compile to an object file
114 #
115 # SOURCE is the source file
116 # DEST is the object file
117 # OPTALL is the list of compiler options to use with all tests
118 # OPTFILE is the list of compiler options to use with this file
119 # OPTSTR is the options to print with test messages
120 # XFAILDATA is the xfail data to be passed to the compiler
121 proc lto-obj { source dest optall optfile optstr xfaildata } {
122     global testcase
123     global tool
124     global compiler_conditional_xfail_data
125     global lto_skip_list
126
127     # Add the skip specifiers.
128     foreach skip $lto_skip_list {
129         if { ![string match $skip ""] } {
130             lappend optall "-DSKIP_$skip"
131         }
132     }
133
134     # Set up the options for compiling this file.
135     set options ""
136     lappend options "additional_flags=$optall $optfile"
137
138     set compiler_conditional_xfail_data $xfaildata
139     set comp_output [${tool}_target_compile "$source" "$dest" object $options]
140     ${tool}_check_compile "$testcase $dest assemble" $optstr $dest $comp_output
141 }
142
143 # lto-link-and-maybe-run -- link the object files and run the executable
144 # if compile_type is set to "run"
145 #
146 # TESTNAME is the mixture of object files to link
147 # OBJLIST is the list of object files to link
148 # DEST is the name of the executable
149 # OPTALL is a list of compiler and linker options to use for all tests
150 # OPTFILE is a list of compiler and linker options to use for this test
151 # OPTSTR is the list of options to list in messages
152 proc lto-link-and-maybe-run { testname objlist dest optall optfile optstr } {
153     global testcase
154     global tool
155     global compile_type
156
157     # Check that all of the objects were built successfully.
158     foreach obj [split $objlist] {
159         if ![file_on_host exists $obj] then {
160             unresolved "$testcase $testname link $optstr"
161             unresolved "$testcase $testname execute $optstr"
162             return
163         }
164     }
165
166     # Set up the options for linking this test.
167     set options ""
168     lappend options "additional_flags=$optall $optfile"
169
170     # Link the objects into an executable.
171     set comp_output [${tool}_target_compile "$objlist" $dest executable \
172                      "$options"]
173
174     # Prune unimportant visibility warnings before checking output.
175     set comp_output [lto_prune_warns $comp_output]
176
177     if ![${tool}_check_compile "$testcase $testname link" "" \
178          $dest $comp_output] then {
179         unresolved "$testcase $testname execute $optstr"
180         return
181     }
182
183     # Return if we only needed to link.
184     if { ![string compare "link" $compile_type] } {
185         return
186     }
187
188     # Run the self-checking executable.
189     if ![string match "*/*" $dest] then {
190         set dest "./$dest"
191     }
192     set result [${tool}_load $dest "" ""]
193     set status [lindex $result 0]
194     if { $status == "pass" } then {
195         file_on_host delete $dest
196     }
197     $status "$testcase $testname execute $optstr"
198 }
199
200 # lto-get-options-main -- get target requirements for a test and
201 # options for the primary source file and the test as a whole
202 #
203 # SRC is the full pathname of the primary source file.
204 proc lto-get-options-main { src } {
205     global compile_type
206     global dg-extra-ld-options
207     global dg-suppress-ld-options
208
209     set dg-extra-ld-options ""
210     set dg-suppress-ld-options ""
211
212     # dg-options sets a variable called dg-extra-tool-flags.
213     set dg-extra-tool-flags ""
214
215     # dg-options sets a variable called tool_flags.
216     set tool_flags ""
217
218     # dg-require-* sets dg-do-what.
219     upvar dg-do-what dg-do-what 
220     upvar dg-final-code dg-final-code
221     set dg-final-code ""
222
223     set tmp [dg-get-options $src]
224     verbose "getting options for $src: $tmp"
225     foreach op $tmp {
226         set cmd [lindex $op 0]
227         verbose "cmd is $cmd"
228         if { [string match "dg-skip-if" $cmd] \
229              || [string match "dg-require-*" $cmd] } {
230             set status [catch "$op" errmsg]
231             if { $status != 0 } {
232                 perror "src: $errmsg for \"$op\"\n"
233                 unresolved "$src: $errmsg for \"$op\""
234                 return
235             }
236         } elseif { [string match "dg-lto-options" $cmd] } {
237             set op [lreplace $op 0 0 "dg-options"]
238             set status [catch "$op" errmsg]
239             if { $status != 0 } {
240                 perror "src: $errmsg for \"$op\"\n"
241                 unresolved "$src: $errmsg for \"$op\""
242                 return
243             }
244         } elseif { ![string compare "dg-xfail-if" $cmd] \
245                    || ![string compare "dg-options" $cmd] } {
246             warning "lto.exp does not support $cmd in primary source file"
247         } elseif { ![string compare "dg-lto-do" $cmd] } {
248             if { [llength $op] > 3 } {
249                 set kw [lindex [lindex $op 3] 0]
250                 if [string match "target" $kw] {
251                     perror "$src: dg-lto-do does not support \"target\""
252                 } elseif [string match "xfail" $kw] {
253                     perror "$src: dg-lto-do does not support \"xfail\""
254                 } else {
255                     perror "$src: dg-lto-do takes a single argument"
256                 }
257             }
258             set dgdo [lindex $op 2]
259             verbose "dg-lto-do command for \"$op\" is $dgdo"
260             if { ![string compare "assemble" $dgdo] } {
261                 set compile_type "assemble"
262             } elseif { ![string compare "run" $dgdo] } {
263                 set compile_type "run"
264             } elseif { ![string compare "link" $dgdo] } {
265                 set compile_type "link"
266             } else {
267                 warning "lto.exp does not support dg-lto-do $dgdo"
268             }
269         } elseif { ![string compare "dg-extra-ld-options" $cmd] } {
270             set dg-extra-ld-options [lindex $op 2]
271             verbose "dg-extra-ld-options for main is ${dg-extra-ld-options}"
272         } elseif { ![string compare "dg-suppress-ld-options" $cmd] } {
273             set dg-suppress-ld-options [lindex $op 2]
274             verbose \
275               "dg-suppress-ld-options for main is ${dg-suppress-ld-options}"
276         } elseif { ![string compare "dg-final" $cmd] } {
277             if { [llength $op] > 3 } {
278                 error "[lindex $op 0]: too many arguments"
279             } else {
280                 append dg-final-code "[lindex $op 2]\n"
281             }
282         } else {
283             # Ignore unrecognized dg- commands, but warn about them.
284             warning "lto.exp does not support $cmd"
285         }
286     }
287
288     # Return flags to use for compiling the primary source file and for
289     # linking.
290     verbose "dg-extra-tool-flags for main is ${dg-extra-tool-flags}"
291     return ${dg-extra-tool-flags}
292 }
293
294
295 # lto-get-options -- get special tool flags to use for a secondary
296 # source file
297 #
298 # SRC is the full pathname of the source file.
299 # The result is a list of options to use.
300 #
301 # This code is copied from proc dg-test in dg.exp from DejaGNU.
302 proc lto-get-options { src } {
303     # dg-options sets a variable called dg-extra-tool-flags.
304     set dg-extra-tool-flags ""
305
306     # dg-xfail-if sets compiler_conditional_xfail_data.
307     global compiler_conditional_xfail_data
308     set compiler_conditional_xfail_data ""
309
310     # dg-xfail-if needs access to dg-do-what.
311     upvar dg-do-what dg-do-what 
312
313     set tmp [dg-get-options $src]
314     foreach op $tmp {
315         set cmd [lindex $op 0]
316         if { ![string compare "dg-options" $cmd] \
317              || ![string compare "dg-xfail-if" $cmd] } {
318             set status [catch "$op" errmsg]
319             if { $status != 0 } {
320                 perror "src: $errmsg for \"$op\"\n"
321                 unresolved "$src: $errmsg for \"$op\""
322                 return
323             }
324         } elseif { [string match "dg-require-*" $cmd] } {
325             warning "lto.exp does not support $cmd in secondary source files"
326         } else {
327             # Ignore unrecognized dg- commands, but warn about them.
328             warning "lto.exp does not support $cmd in secondary source files"
329         }
330     }
331
332     return ${dg-extra-tool-flags}
333 }
334
335 # lto-execute -- compile multi-file tests
336 #
337 # SRC1 is the full pathname of the main file of the testcase.
338 # SID identifies a test suite in the names of temporary files.
339 proc lto-execute { src1 sid } {
340     global srcdir tmpdir
341     global lto_option_list
342     global tool
343     global verbose
344     global testcase
345     global gluefile
346     global compiler_conditional_xfail_data
347     global dg-do-what-default
348     global compile_type
349     global dg-extra-ld-options
350     global dg-suppress-ld-options
351     global LTO_OPTIONS
352     global dg-final-code
353
354     # Get extra flags for this test from the primary source file, and
355     # process other dg-* options that this suite supports.  Warn about
356     # unsupported flags.
357     verbose "lto-execute: $src1" 1
358     set compile_type "run"
359     set dg-do-what [list ${dg-do-what-default} "" P]
360     set extra_flags(0) [lto-get-options-main $src1]
361     set compile_xfail(0) "" 
362
363     # If the main file defines dg-options, those flags are used to
364     # overwrite the default lto_option_list taken from LTO_OPTIONS.
365     if { [string length $extra_flags(0)] > 0 } {
366         set lto_option_list $extra_flags(0)
367         set extra_flags(0) ""
368     } else {
369         set lto_option_list $LTO_OPTIONS
370     }
371
372     # Check whether this test is supported for this target.
373     if { [lindex ${dg-do-what} 1 ] == "N" } {
374         unsupported "$src1"
375         verbose "$src1 not supported on this target, skipping it" 3
376         return
377     }
378
379     # Set up the names of the other source files.
380     set dir [file dirname $src1]
381     set base [file rootname $src1]
382     set base [string range $base [string length $dir] end]
383     regsub "_0" $base "" base
384     regsub "/" $base "" base
385     set src_list $src1
386     set i 1
387     set done 0
388     while { !$done } {
389         set names [glob -nocomplain -types f -- "${dir}/${base}_${i}.*"]
390         if { [llength ${names}] > 1 } {
391             warning "lto-execute: more than one file matched ${dir}/${base}_${i}.*"
392         }
393         if { [llength ${names}] == 1 } {
394             lappend src_list [lindex ${names} 0]
395             incr i
396         } else {
397             set num_srcs ${i}
398             set done 1
399         }
400     }
401
402     # Use the dg-options mechanism to specify extra flags for each
403     # of the secondary files.
404     # The extra flags in each file are used to compile that file, and the
405     # extra flags in *_0.* are also used for linking.
406     verbose "\tsrc_list is: $src_list"
407     for {set i 1} {$i < $num_srcs} {incr i} {
408         set extra_flags($i) [lto-get-options [lindex $src_list $i]]
409         set compile_xfail($i) $compiler_conditional_xfail_data
410     }
411
412     # Define the names of the object files.
413     set obj_list ""
414     for {set i 0} {$i < $num_srcs} {incr i} {
415         lappend obj_list "${sid}_${base}_${i}.o"
416     }
417
418     # Get the base name of this test, for use in messages.
419     set testcase [lindex ${src_list} 0]
420
421     # Remove the $srcdir and $tmpdir prefixes from $src1.  (It would
422     # be possible to use "regsub" here, if we were careful to escape
423     # all regular expression characters in $srcdir and $tmpdir, but
424     # that would be more complicated that this approach.) 
425     if {[string first "$srcdir/" "${testcase}"] == 0} {
426         set testcase [string range "${testcase}" [string length "$srcdir/"] end]
427     }
428     if {[string first "$tmpdir/" "$testcase"] == 0} {
429         set testcase [string range "$testcase" [string length "$tmpdir/"] end]
430         set testcase "tmpdir-$testcase"
431     }
432     regsub "_0.*" $testcase "" testcase
433
434     # Set up the base name of executable files so they'll be unique.
435     regsub -all "\[./\]" $testcase "-" execbase
436
437     # If we couldn't rip $srcdir out of `src1' then just do the best we can.
438     # The point is to reduce the unnecessary noise in the logs.  Don't strip
439     # out too much because different testcases with the same name can confuse
440     # `test-tool'.
441     if [string match "/*" $testcase] then {
442         set testcase "[file tail [file dirname $src1]]/[file tail $src1]"
443     }
444
445     # Loop through all of the option lists used for this test.
446     set count 0
447     foreach option $lto_option_list {
448         verbose "Testing $testcase, $option"
449
450         # There's a unique name for each executable we generate.
451         set execname "${execbase}-${count}1"
452         incr count
453
454         file_on_host delete $execname
455
456         # Compile pieces with the compiler under test.
457         set i 0
458         foreach src $src_list obj $obj_list {
459             lto-obj $src $obj $option $extra_flags($i) $option \
460                     $compile_xfail($i)
461             incr i
462         }
463
464         # Link (using the compiler under test), run, and clean up tests.
465         if { ![string compare "run" $compile_type] \
466              || ![string compare "link" $compile_type] } {
467
468             # Filter out any link options we were asked to suppress.
469             set reduced {}
470             foreach x [split $option] {
471               if {[lsearch ${dg-suppress-ld-options} $x] == -1} {
472                 lappend reduced $x
473               }
474             }
475             set filtered [join $reduced " "]
476
477             lto-link-and-maybe-run \
478                     "[lindex $obj_list 0]-[lindex $obj_list end]" \
479                     $obj_list $execname $filtered ${dg-extra-ld-options} \
480                     $filtered
481         }
482
483
484         # Are there any further tests to perform?
485         # Note that if the program has special run-time requirements, running
486         # of the program can be delayed until here.  Ditto for other situations.
487         # It would be a bit cumbersome though.
488
489         if ![string match ${dg-final-code} ""] {
490             regsub -all "\\\\(\[{}\])" ${dg-final-code} "\\1" dg-final-code
491             # Note that the use of `args' here makes this a varargs proc.
492             proc dg-final-proc { args } ${dg-final-code}
493             verbose "Running dg-final tests." 3
494             verbose "dg-final-proc:\n[info body dg-final-proc]" 4
495             if [catch "dg-final-proc $src1" errmsg] {
496                 perror "$name: error executing dg-final: $errmsg"
497                 # ??? The call to unresolved here is necessary to clear
498                 # `errcnt'.  What we really need is a proc like perror that
499                 # doesn't set errcnt.  It should also set exit_status to 1.
500                 unresolved "$name: error executing dg-final: $errmsg"
501             }
502         }
503
504         # Clean up object files.
505         set files [glob -nocomplain ${sid}_*.o]
506         if { $files != "" } {
507             foreach objfile $files {
508                 if { ![info exists gluefile] || $objfile != $gluefile } {
509                     eval "file_on_host delete $objfile"
510                 }
511             }
512         }
513
514         if { ![string compare "run" $compile_type] \
515              || ![string compare "link" $compile_type] } {
516             file_on_host delete $execname
517         }
518     }
519 }
520
521 # Utility for scanning a symbol in the final executable, invoked via dg-final.
522 # Call pass if pattern is present, otherwise fail.
523 #
524 # Argument 0 is the regexp to match.
525 # Argument 1 handles expected failures and the like
526 proc scan-symbol { args } {
527     global nm
528     global base_dir
529     upvar 2 execname execname
530
531     if { [llength $args] >= 2 } {
532         switch [dg-process-target [lindex $args 1]] {
533             "S" { }
534             "N" { return }
535             "F" { setup_xfail "*-*-*" }
536             "P" { }
537         }
538     }
539
540     # Find nm like we find g++ in g++.exp.
541     if ![info exists nm]  {
542         set nm [findfile $base_dir/../../../binutils/nm \
543                 $base_dir/../../../binutils/nm \
544                 [findfile $base_dir/../../nm $base_dir/../../nm \
545                       [findfile $base_dir/nm $base_dir/nm \
546                        [transform nm]]]]
547         verbose -log "nm is $nm"
548     }
549
550     set output_file "[glob -nocomplain $execname]"
551     if { $output_file == "" } {
552         fail "scan-symbol $args: dump file does not exist"
553         return
554     }
555
556     set fd [open "| $nm $output_file" r]
557     set text [read $fd]
558     close $fd
559
560     if [regexp -- [lindex $args 0] $text] {
561         pass "scan-symbol $args"
562     } else {
563         fail "scan-symbol $args"
564     }
565 }