1 # Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>.
17 # Contributed by Diego Novillo <dnovillo@google.com>
20 # lto_init -- called at the start of each subdir of tests
22 proc lto_init { args } {
25 if {[info exists args] && $args == "no-mathlib"} {
29 set dest [target_info name]
30 if [board_info $dest exists mathlib] {
31 set saved_mathlib [board_info $dest mathlib]
33 set board_info($dest,mathlib) " "
36 # Each test is run with the compiler options from this list.
37 # The default option lists can be overridden by LTO_OPTIONS="[list
38 # {opts_1} {opts_2}... {opts_n}]" where opts_i are lists of options.
39 # You can put this in the environment before site.exp is written or
40 # add it to site.exp directly.
41 if ![info exists LTO_OPTIONS] {
42 set LTO_OPTIONS [list \
52 # lto_finish -- called at the end of each subdir of tests if mathlib is
60 set dest [target_info name]
61 if [info exists saved_mathlib] {
62 set board_info($dest,mathlib) $saved_mathlib
63 } elseif [board_info $dest exists mathlib] {
64 unset board_info($dest,mathlib)
68 # Subsets of tests can be selectively disabled by members of this list:
69 # - ATTRIBUTE: disable all tests using the __attribute__ extension,
70 # - COMPLEX: disable all tests using the complex types feature,
71 # - COMPLEX_INT: disable all tests using the complex integral types extension,
72 # - VA: disable all tests using the variable number of arguments feature,
73 # - VLA_IN_STRUCT: disable all tests using the variable-length arrays as
74 # structure members extension,
75 # - ZERO_ARRAY: disable all tests using the zero-sized arrays extension.
76 # The default skip lists can be overriden by
77 # LTO_SKIPS="[list {skip_1}...{skip_n}]"
78 # where skip_i are skip identifiers. You can put this in the environment
79 # before site.exp is written or add it to site.exp directly.
80 if ![info exists LTO_SKIPS] {
81 set LTO_SKIPS [list {}]
85 set lto_skip_list $LTO_SKIPS
90 # lto-obj -- compile to an object file
92 # SOURCE is the source file
93 # DEST is the object file
94 # OPTALL is the list of compiler options to use with all tests
95 # OPTFILE is the list of compiler options to use with this file
96 # OPTSTR is the options to print with test messages
97 # XFAILDATA is the xfail data to be passed to the compiler
98 proc lto-obj { source dest optall optfile optstr xfaildata } {
101 global compiler_conditional_xfail_data
104 # Add the skip specifiers.
105 foreach skip $lto_skip_list {
106 if { ![string match $skip ""] } {
107 lappend optall "-DSKIP_$skip"
111 # Set up the options for compiling this file.
113 lappend options "additional_flags=$optall $optfile"
115 set compiler_conditional_xfail_data $xfaildata
116 set comp_output [${tool}_target_compile "$source" "$dest" object $options]
117 ${tool}_check_compile "$testcase $dest assemble" $optstr $dest $comp_output
120 # lto-link-and-maybe-run -- link the object files and run the executable
121 # if compile_type is set to "run"
123 # TESTNAME is the mixture of object files to link
124 # OBJLIST is the list of object files to link
125 # DEST is the name of the executable
126 # OPTALL is a list of compiler and linker options to use for all tests
127 # OPTFILE is a list of compiler and linker options to use for this test
128 # OPTSTR is the list of options to list in messages
129 proc lto-link-and-maybe-run { testname objlist dest optall optfile optstr } {
134 # Check that all of the objects were built successfully.
135 foreach obj [split $objlist] {
136 if ![file_on_host exists $obj] then {
137 unresolved "$testcase $testname link $optstr"
138 unresolved "$testcase $testname execute $optstr"
143 # Set up the options for linking this test.
145 lappend options "additional_flags=$optall $optfile"
147 # Link the objects into an executable.
148 set comp_output [${tool}_target_compile "$objlist" $dest executable \
150 if ![${tool}_check_compile "$testcase $testname link" "" \
151 $dest $comp_output] then {
152 unresolved "$testcase $testname execute $optstr"
156 # Return if we only needed to link.
157 if { ![string compare "link" $compile_type] } {
161 # Run the self-checking executable.
162 if ![string match "*/*" $dest] then {
165 set result [${tool}_load $dest "" ""]
166 set status [lindex $result 0]
167 if { $status == "pass" } then {
168 file_on_host delete $dest
170 $status "$testcase $testname execute $optstr"
173 # lto-get-options-main -- get target requirements for a test and
174 # options for the primary source file and the test as a whole
176 # SRC is the full pathname of the primary source file.
177 proc lto-get-options-main { src } {
179 global dg-extra-ld-options
180 global dg-suppress-ld-options
182 set dg-extra-ld-options ""
183 set dg-suppress-ld-options ""
185 # dg-options sets a variable called dg-extra-tool-flags.
186 set dg-extra-tool-flags ""
188 # dg-options sets a variable called tool_flags.
191 # dg-require-* sets dg-do-what.
192 upvar dg-do-what dg-do-what
193 upvar dg-final-code dg-final-code
196 set tmp [dg-get-options $src]
197 verbose "getting options for $src: $tmp"
199 set cmd [lindex $op 0]
200 verbose "cmd is $cmd"
201 if { [string match "dg-skip-if" $cmd] \
202 || [string match "dg-require-*" $cmd] } {
203 set status [catch "$op" errmsg]
204 if { $status != 0 } {
205 perror "src: $errmsg for \"$op\"\n"
206 unresolved "$src: $errmsg for \"$op\""
209 } elseif { [string match "dg-lto-options" $cmd] } {
210 set op [lreplace $op 0 0 "dg-options"]
211 set status [catch "$op" errmsg]
212 if { $status != 0 } {
213 perror "src: $errmsg for \"$op\"\n"
214 unresolved "$src: $errmsg for \"$op\""
217 } elseif { ![string compare "dg-xfail-if" $cmd] \
218 || ![string compare "dg-options" $cmd] } {
219 warning "lto.exp does not support $cmd in primary source file"
220 } elseif { ![string compare "dg-lto-do" $cmd] } {
221 if { [llength $op] > 3 } {
222 set kw [lindex [lindex $op 3] 0]
223 if [string match "target" $kw] {
224 perror "$src: dg-lto-do does not support \"target\""
225 } elseif [string match "xfail" $kw] {
226 perror "$src: dg-lto-do does not support \"xfail\""
228 perror "$src: dg-lto-do takes a single argument"
231 set dgdo [lindex $op 2]
232 verbose "dg-lto-do command for \"$op\" is $dgdo"
233 if { ![string compare "assemble" $dgdo] } {
234 set compile_type "assemble"
235 } elseif { ![string compare "run" $dgdo] } {
236 set compile_type "run"
237 } elseif { ![string compare "link" $dgdo] } {
238 set compile_type "link"
240 warning "lto.exp does not support dg-lto-do $dgdo"
242 } elseif { ![string compare "dg-extra-ld-options" $cmd] } {
243 set dg-extra-ld-options [lindex $op 2]
244 verbose "dg-extra-ld-options for main is ${dg-extra-ld-options}"
245 } elseif { ![string compare "dg-suppress-ld-options" $cmd] } {
246 set dg-suppress-ld-options [lindex $op 2]
248 "dg-suppress-ld-options for main is ${dg-suppress-ld-options}"
249 } elseif { ![string compare "dg-final" $cmd] } {
250 if { [llength $op] > 3 } {
251 error "[lindex $op 0]: too many arguments"
253 append dg-final-code "[lindex $op 2]\n"
256 # Ignore unrecognized dg- commands, but warn about them.
257 warning "lto.exp does not support $cmd"
261 # Return flags to use for compiling the primary source file and for
263 verbose "dg-extra-tool-flags for main is ${dg-extra-tool-flags}"
264 return ${dg-extra-tool-flags}
268 # lto-get-options -- get special tool flags to use for a secondary
271 # SRC is the full pathname of the source file.
272 # The result is a list of options to use.
274 # This code is copied from proc dg-test in dg.exp from DejaGNU.
275 proc lto-get-options { src } {
276 # dg-options sets a variable called dg-extra-tool-flags.
277 set dg-extra-tool-flags ""
279 # dg-xfail-if sets compiler_conditional_xfail_data.
280 global compiler_conditional_xfail_data
281 set compiler_conditional_xfail_data ""
283 # dg-xfail-if needs access to dg-do-what.
284 upvar dg-do-what dg-do-what
286 set tmp [dg-get-options $src]
288 set cmd [lindex $op 0]
289 if { ![string compare "dg-options" $cmd] \
290 || ![string compare "dg-xfail-if" $cmd] } {
291 set status [catch "$op" errmsg]
292 if { $status != 0 } {
293 perror "src: $errmsg for \"$op\"\n"
294 unresolved "$src: $errmsg for \"$op\""
297 } elseif { [string match "dg-require-*" $cmd] } {
298 warning "lto.exp does not support $cmd in secondary source files"
300 # Ignore unrecognized dg- commands, but warn about them.
301 warning "lto.exp does not support $cmd in secondary source files"
305 return ${dg-extra-tool-flags}
308 # lto-execute -- compile multi-file tests
310 # SRC1 is the full pathname of the main file of the testcase.
311 # SID identifies a test suite in the names of temporary files.
312 proc lto-execute { src1 sid } {
319 global compiler_conditional_xfail_data
320 global dg-do-what-default
322 global dg-extra-ld-options
323 global dg-suppress-ld-options
327 # Get extra flags for this test from the primary source file, and
328 # process other dg-* options that this suite supports. Warn about
330 verbose "lto-execute: $src1" 1
331 set compile_type "run"
332 set dg-do-what [list ${dg-do-what-default} "" P]
333 set extra_flags(0) [lto-get-options-main $src1]
334 set compile_xfail(0) ""
336 # If the main file defines dg-options, those flags are used to
337 # overwrite the default option_list taken from LTO_OPTIONS.
338 if { [string length $extra_flags(0)] > 0 } {
339 set option_list $extra_flags(0)
340 set extra_flags(0) ""
342 set option_list $LTO_OPTIONS
345 # Check whether this test is supported for this target.
346 if { [lindex ${dg-do-what} 1 ] == "N" } {
348 verbose "$src1 not supported on this target, skipping it" 3
352 # Set up the names of the other source files.
353 set dir [file dirname $src1]
354 set base [file rootname $src1]
355 set base [string range $base [string length $dir] end]
356 regsub "_0" $base "" base
357 regsub "/" $base "" base
362 set names [glob -nocomplain -types f -- "${dir}/${base}_${i}.*"]
363 if { [llength ${names}] > 1 } {
364 warning "lto-execute: more than one file matched ${dir}/${base}_${i}.*"
366 if { [llength ${names}] == 1 } {
367 lappend src_list [lindex ${names} 0]
375 # Use the dg-options mechanism to specify extra flags for each
376 # of the secondary files.
377 # The extra flags in each file are used to compile that file, and the
378 # extra flags in *_0.* are also used for linking.
379 verbose "\tsrc_list is: $src_list"
380 for {set i 1} {$i < $num_srcs} {incr i} {
381 set extra_flags($i) [lto-get-options [lindex $src_list $i]]
382 set compile_xfail($i) $compiler_conditional_xfail_data
385 # Define the names of the object files.
387 for {set i 0} {$i < $num_srcs} {incr i} {
388 lappend obj_list "${sid}_${base}_${i}.o"
391 # Get the base name of this test, for use in messages.
392 set testcase [lindex ${src_list} 0]
394 # Remove the $srcdir and $tmpdir prefixes from $src1. (It would
395 # be possible to use "regsub" here, if we were careful to escape
396 # all regular expression characters in $srcdir and $tmpdir, but
397 # that would be more complicated that this approach.)
398 if {[string first "$srcdir/" "${testcase}"] == 0} {
399 set testcase [string range "${testcase}" [string length "$srcdir/"] end]
401 if {[string first "$tmpdir/" "$testcase"] == 0} {
402 set testcase [string range "$testcase" [string length "$tmpdir/"] end]
403 set testcase "tmpdir-$testcase"
405 regsub "_0.*" $testcase "" testcase
407 # Set up the base name of executable files so they'll be unique.
408 regsub -all "\[./\]" $testcase "-" execbase
410 # If we couldn't rip $srcdir out of `src1' then just do the best we can.
411 # The point is to reduce the unnecessary noise in the logs. Don't strip
412 # out too much because different testcases with the same name can confuse
414 if [string match "/*" $testcase] then {
415 set testcase "[file tail [file dirname $src1]]/[file tail $src1]"
418 # Loop through all of the option lists used for this test.
420 foreach option $option_list {
421 verbose "Testing $testcase, $option"
423 # There's a unique name for each executable we generate.
424 set execname "${execbase}-${count}1"
427 file_on_host delete $execname
429 # Compile pieces with the compiler under test.
431 foreach src $src_list obj $obj_list {
432 lto-obj $src $obj $option $extra_flags($i) $option \
437 # Link (using the compiler under test), run, and clean up tests.
438 if { ![string compare "run" $compile_type] \
439 || ![string compare "link" $compile_type] } {
441 # Filter out any link options we were asked to suppress.
443 foreach x [split $option] {
444 if {[lsearch ${dg-suppress-ld-options} $x] == -1} {
448 set filtered [join $reduced " "]
450 lto-link-and-maybe-run \
451 "[lindex $obj_list 0]-[lindex $obj_list end]" \
452 $obj_list $execname $filtered ${dg-extra-ld-options} \
457 # Are there any further tests to perform?
458 # Note that if the program has special run-time requirements, running
459 # of the program can be delayed until here. Ditto for other situations.
460 # It would be a bit cumbersome though.
462 if ![string match ${dg-final-code} ""] {
463 regsub -all "\\\\(\[{}\])" ${dg-final-code} "\\1" dg-final-code
464 # Note that the use of `args' here makes this a varargs proc.
465 proc dg-final-proc { args } ${dg-final-code}
466 verbose "Running dg-final tests." 3
467 verbose "dg-final-proc:\n[info body dg-final-proc]" 4
468 if [catch "dg-final-proc $src1" errmsg] {
469 perror "$name: error executing dg-final: $errmsg"
470 # ??? The call to unresolved here is necessary to clear
471 # `errcnt'. What we really need is a proc like perror that
472 # doesn't set errcnt. It should also set exit_status to 1.
473 unresolved "$name: error executing dg-final: $errmsg"
477 # Clean up object files.
478 set files [glob -nocomplain ${sid}_*.o]
479 if { $files != "" } {
480 foreach objfile $files {
481 if { ![info exists gluefile] || $objfile != $gluefile } {
482 eval "file_on_host delete $objfile"
487 if { ![string compare "run" $compile_type] \
488 || ![string compare "link" $compile_type] } {
489 file_on_host delete $execname
494 # Utility for scanning a symbol in the final executable, invoked via dg-final.
495 # Call pass if pattern is present, otherwise fail.
497 # Argument 0 is the regexp to match.
498 # Argument 1 handles expected failures and the like
499 proc scan-symbol { args } {
502 upvar 2 execname execname
504 if { [llength $args] >= 2 } {
505 switch [dg-process-target [lindex $args 1]] {
508 "F" { setup_xfail "*-*-*" }
513 # Find nm like we find g++ in g++.exp.
514 if ![info exists nm] {
515 set nm [findfile $base_dir/../../../binutils/nm \
516 $base_dir/../../../binutils/nm \
517 [findfile $base_dir/../../nm $base_dir/../../nm \
518 [findfile $base_dir/nm $base_dir/nm \
520 verbose -log "nm is $nm"
523 set output_file "[glob -nocomplain $execname]"
524 if { $output_file == "" } {
525 fail "scan-symbol $args: dump file does not exist"
529 set fd [open "| $nm $output_file" r]
533 if [regexp -- [lindex $args 0] $text] {
534 pass "scan-symbol $args"
536 fail "scan-symbol $args"