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