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