2009-09-02 Tristan Gingold <gingold@adacore.com>
[binutils.git] / binutils / testsuite / lib / utils-lib.exp
blob9881f0133f4a6b4f847d30d843371bb9bf5a14e3
1 # Copyright 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2003, 2004, 2006, 2007,
2 # 2009 Free Software Foundation, Inc.
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.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-dejagnu@prep.ai.mit.edu
21 # This file was written by Rob Savoye <rob@cygnus.com>
22 # and extended by Ian Lance Taylor <ian@cygnus.com>
24 proc binutil_version { prog } {
25 if ![is_remote host] {
26 set path [which $prog]
27 if {$path == 0} then {
28 perror "$prog can't be run, file not found."
29 return ""
31 } else {
32 set path $prog
34 set state [remote_exec host $prog --version]
35 set tmp "[lindex $state 1]\n"
36 # Should find a way to discard constant parts, keep whatever's
37 # left, so the version string could be almost anything at all...
38 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" "$tmp" version cyg number
39 if ![info exists number] then {
40 return "$path (no version number)\n"
42 return "$path $number\n"
46 # default_binutils_run
47 # run a program, returning the output
48 # sets binutils_run_failed if the program does not exist
50 proc default_binutils_run { prog progargs } {
51 global binutils_run_failed
52 global host_triplet
54 set binutils_run_failed 0
56 if ![is_remote host] {
57 if {[which $prog] == 0} then {
58 perror "$prog does not exist"
59 set binutils_run_failed 1
60 return ""
64 send_log "$prog $progargs\n"
65 verbose "$prog $progargs"
67 # Gotta quote dollar-signs because they get mangled by the
68 # shell otherwise.
69 regsub -all "\\$" "$progargs" "\\$" progargs
71 set state [remote_exec host $prog $progargs]
72 set exec_output [prune_warnings [lindex $state 1]]
73 if {![string match "" $exec_output]} then {
74 send_log "$exec_output\n"
75 verbose "$exec_output"
76 } else {
77 if { [lindex $state 0] != 0 } {
78 set exec_output "$prog exited with status [lindex $state 0]"
79 send_log "$exec_output\n"
80 verbose "$exec_output"
83 return $exec_output
87 # default_binutils_assemble
88 # assemble a file
90 proc default_binutils_assemble { source object } {
91 global srcdir
92 global host_triplet
94 # The HPPA assembler syntax is a little different than most, to make
95 # the test source file assemble we need to run it through sed.
97 # This is a hack in that it won't scale well if other targets need
98 # similar transformations to assemble. We'll generalize the hack
99 # if/when other targets need similar handling.
100 if { [istarget "hppa*-*-*"] && ![istarget "*-*-linux*" ] } then {
101 set sed_file $srcdir/config/hppa.sed
102 send_log "sed -f $sed_file < $source > asm.s\n"
103 verbose "sed -f $sed_file < $source > asm.s"
104 catch "exec sed -f $sed_file < $source > asm.s"
105 set source asm.s
108 set exec_output [target_assemble $source $object ""]
109 set exec_output [prune_warnings $exec_output]
111 if [string match "" $exec_output] {
112 return 1
113 } else {
114 send_log "$exec_output\n"
115 verbose "$exec_output"
116 perror "$source: assembly failed"
117 return 0
122 # is_elf_format
123 # true if the object format is known to be ELF
125 proc is_elf_format {} {
126 if { ![istarget *-*-sysv4*] \
127 && ![istarget *-*-unixware*] \
128 && ![istarget *-*-elf*] \
129 && ![istarget *-*-eabi*] \
130 && ![istarget hppa*64*-*-hpux*] \
131 && ![istarget ia64-*-hpux*] \
132 && ![istarget *-*-linux*] \
133 && ![istarget *-*-irix5*] \
134 && ![istarget *-*-irix6*] \
135 && ![istarget *-*-netbsd*] \
136 && ![istarget *-*-solaris2*] } {
137 return 0
140 if { [istarget *-*-linux*aout*] \
141 || [istarget *-*-linux*oldld*] } {
142 return 0
145 if { ![istarget *-*-netbsdelf*] \
146 && ([istarget *-*-netbsd*aout*] \
147 || [istarget *-*-netbsdpe*] \
148 || [istarget arm*-*-netbsd*] \
149 || [istarget sparc-*-netbsd*] \
150 || [istarget i*86-*-netbsd*] \
151 || [istarget m68*-*-netbsd*] \
152 || [istarget vax-*-netbsd*] \
153 || [istarget ns32k-*-netbsd*]) } {
154 return 0
156 return 1
160 # exe_ext
161 # Returns target executable extension, if any.
163 proc exe_ext {} {
164 if { [istarget *-*-mingw*] || [istarget *-*-cygwin*] } {
165 return ".exe"
166 } else {
167 return ""
171 # Copied and modified from gas.
173 # run_dump_test FILE (optional:) EXTRA_OPTIONS
175 # Assemble a .s file, then run some utility on it and check the output.
177 # There should be an assembly language file named FILE.s in the test
178 # suite directory, and a pattern file called FILE.d. `run_dump_test'
179 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
180 # `nm' on the .o file to produce textual output, and then analyze that
181 # with regexps. The FILE.d file specifies what program to run, and
182 # what to expect in its output.
184 # The FILE.d file begins with zero or more option lines, which specify
185 # flags to pass to the assembler, the program to run to dump the
186 # assembler's output, and the options it wants. The option lines have
187 # the syntax:
189 # # OPTION: VALUE
191 # OPTION is the name of some option, like "name" or "objdump", and
192 # VALUE is OPTION's value. The valid options are described below.
193 # Whitespace is ignored everywhere, except within VALUE. The option
194 # list ends with the first line that doesn't match the above syntax.
195 # However, a line within the options that begins with a #, but doesn't
196 # have a recognizable option name followed by a colon, is considered a
197 # comment and entirely ignored.
199 # The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
200 # two-element lists. The first element of each is an option name, and
201 # the second additional arguments to be added on to the end of the
202 # option list as given in FILE.d. (If omitted, no additional options
203 # are added.)
205 # The interesting options are:
207 # name: TEST-NAME
208 # The name of this test, passed to DejaGNU's `pass' and `fail'
209 # commands. If omitted, this defaults to FILE, the root of the
210 # .s and .d files' names.
212 # as: FLAGS
213 # When assembling FILE.s, pass FLAGS to the assembler.
215 # PROG: PROGRAM-NAME
216 # The name of the program to run to analyze the .o file produced
217 # by the assembler. This can be omitted; run_dump_test will guess
218 # which program to run by seeing which of the flags options below
219 # is present.
221 # objdump: FLAGS
222 # nm: FLAGS
223 # objcopy: FLAGS
224 # Use the specified program to analyze the .o file, and pass it
225 # FLAGS, in addition to the .o file name. Note that they are run
226 # with LC_ALL=C in the environment to give consistent sorting
227 # of symbols.
229 # source: SOURCE
230 # Assemble the file SOURCE.s. If omitted, this defaults to FILE.s.
231 # This is useful if several .d files want to share a .s file.
233 # target: GLOBS...
234 # Run this test only on a specified list of targets. More precisely,
235 # each glob in the space-separated list is passed to "istarget"; if
236 # it evaluates true for any of them, the test will be run, otherwise
237 # it will be marked unsupported.
239 # not-target: GLOBS...
240 # Do not run this test on a specified list of targets. Again,
241 # the each glob in the space-separated list is passed to
242 # "istarget", and the test is run if it evaluates *false* for
243 # *all* of them. Otherwise it will be marked unsupported.
245 # skip: GLOBS...
246 # not-skip: GLOBS...
247 # These are exactly the same as "not-target" and "target",
248 # respectively, except that they do nothing at all if the check
249 # fails. They should only be used in groups, to construct a single
250 # test which is run on all targets but with variant options or
251 # expected output on some targets. (For example, see
252 # gas/arm/inst.d and gas/arm/wince_inst.d.)
254 # error: REGEX
255 # An error with message matching REGEX must be emitted for the test
256 # to pass. The PROG, objdump, nm and objcopy options have no
257 # meaning and need not supplied if this is present.
259 # warning: REGEX
260 # Expect a gas warning matching REGEX. It is an error to issue
261 # both "error" and "warning".
263 # stderr: FILE
264 # FILE contains regexp lines to be matched against the diagnostic
265 # output of the assembler. This does not preclude the use of
266 # PROG, nm, objdump, or objcopy.
268 # error-output: FILE
269 # Means the same as 'stderr', but also indicates that the assembler
270 # is expected to exit unsuccessfully (therefore PROG, objdump, nm,
271 # and objcopy have no meaning and should not be supplied).
273 # Each option may occur at most once.
275 # After the option lines come regexp lines. `run_dump_test' calls
276 # `regexp_diff' to compare the output of the dumping tool against the
277 # regexps in FILE.d. `regexp_diff' is defined later in this file; see
278 # further comments there.
280 proc run_dump_test { name {extra_options {}} } {
281 global subdir srcdir
282 global OBJDUMP NM OBJCOPY READELF STRIP
283 global OBJDUMPFLAGS NMFLAGS OBJCOPYFLAGS READELFFLAGS STRIPFLAGS
284 global host_triplet
285 global env
286 global copyfile
287 global tempfile
289 if [string match "*/*" $name] {
290 set file $name
291 set name [file tail $name]
292 } else {
293 set file "$srcdir/$subdir/$name"
295 set opt_array [slurp_options "${file}.d"]
296 if { $opt_array == -1 } {
297 perror "error reading options from $file.d"
298 unresolved $subdir/$name
299 return
301 set opts(addr2line) {}
302 set opts(ar) {}
303 set opts(nm) {}
304 set opts(objcopy) {}
305 set opts(objdump) {}
306 set opts(strip) {}
307 set opts(ranlib) {}
308 set opts(readelf) {}
309 set opts(size) {}
310 set opts(strings) {}
311 set opts(name) {}
312 set opts(PROG) {}
313 set opts(DUMPPROG) {}
314 set opts(source) {}
315 set opts(target) {}
316 set opts(not-target) {}
317 set opts(skip) {}
318 set opts(not-skip) {}
320 foreach i $opt_array {
321 set opt_name [lindex $i 0]
322 set opt_val [lindex $i 1]
323 if ![info exists opts($opt_name)] {
324 perror "unknown option $opt_name in file $file.d"
325 unresolved $subdir/$name
326 return
328 if [string length $opts($opt_name)] {
329 perror "option $opt_name multiply set in $file.d"
330 unresolved $subdir/$name
331 return
333 set opts($opt_name) $opt_val
336 foreach i $extra_options {
337 set opt_name [lindex $i 0]
338 set opt_val [lindex $i 1]
339 if ![info exists opts($opt_name)] {
340 perror "unknown option $opt_name given in extra_opts"
341 unresolved $subdir/$name
342 return
344 # add extra option to end of existing option, adding space
345 # if necessary.
346 if [string length $opts($opt_name)] {
347 append opts($opt_name) " "
349 append opts($opt_name) $opt_val
352 if { $opts(name) == "" } {
353 set testname "$subdir/$name"
354 } else {
355 set testname $opts(name)
357 verbose "Testing $testname"
359 if {$opts(PROG) == ""} {
360 perror "program isn't set in $file.d"
361 unresolved $testname
362 return
365 set destopt ""
366 switch -- $opts(PROG) {
367 ar { set program ar }
368 objcopy { set program objcopy }
369 ranlib { set program ranlib }
370 strip {
371 set program strip
372 set destopt "-o"
374 strings { set program strings }
375 default {
376 perror "unrecognized program option $opts(PROG) in $file.d"
377 unresolved $testname
378 return }
381 set dumpprogram ""
382 if { $opts(DUMPPROG) != "" } {
383 switch -- $opts(DUMPPROG) {
384 addr2line { set dumpprogram addr2line }
385 nm { set dumpprogram nm }
386 objdump { set dumpprogram objdump }
387 readelf { set dumpprogram readelf }
388 size { set dumpprogram size }
389 default {
390 perror "unrecognized dump program option $opts(DUMPPROG) in $file.d"
391 unresolved $testname
392 return }
394 } else {
395 # Guess which program to run, by seeing which option was specified.
396 foreach p {objdump nm readelf} {
397 if {$opts($p) != ""} {
398 if {$dumpprogram != ""} {
399 perror "ambiguous dump program in $file.d"
400 unresolved $testname
401 return
402 } else {
403 set dumpprogram $p
409 # Handle skipping the test on specified targets.
410 # You can have both skip/not-skip and target/not-target, but you can't
411 # have both skip and not-skip, or target and not-target, in the same file.
412 if { $opts(skip) != "" } then {
413 if { $opts(not-skip) != "" } then {
414 perror "$testname: mixing skip and not-skip directives is invalid"
415 unresolved $testname
416 return
418 foreach glob $opts(skip) {
419 if {[istarget $glob]} { return }
422 if { $opts(not-skip) != "" } then {
423 set skip 1
424 foreach glob $opts(not-skip) {
425 if {[istarget $glob]} {
426 set skip 0
427 break
430 if {$skip} { return }
432 if { $opts(target) != "" } then {
433 if { $opts(not-target) != "" } then {
434 perror "$testname: mixing target and not-target directives is invalid"
435 unresolved $testname
436 return
438 set skip 1
439 foreach glob $opts(target) {
440 if {[istarget $glob]} {
441 set skip 0
442 break
445 if {$skip} {
446 unsupported $testname
447 return
450 if { $opts(not-target) != "" } then {
451 foreach glob $opts(not-target) {
452 if {[istarget $glob]} {
453 unsupported $testname
454 return
459 if { $opts(source) == "" } {
460 set srcfile ${file}.s
461 } else {
462 set srcfile $srcdir/$subdir/$opts(source)
465 set exec_output [binutils_assemble ${srcfile} $tempfile]
466 if [string match "" $exec_output] then {
467 send_log "$exec_output\n"
468 verbose "$exec_output"
469 fail $testname
470 return
473 set progopts1 $opts($program)
474 eval set progopts \$[string toupper $program]FLAGS
475 eval set binary \$[string toupper $program]
477 set exec_output [binutils_run $binary "$progopts $progopts1 $tempfile $destopt ${copyfile}.o"]
478 if ![string match "" $exec_output] {
479 send_log "$exec_output\n"
480 verbose "$exec_output"
481 fail $testname
482 return
485 set progopts1 $opts($dumpprogram)
486 eval set progopts \$[string toupper $dumpprogram]FLAGS
487 eval set binary \$[string toupper $dumpprogram]
489 if { ![is_remote host] && [which $binary] == 0 } {
490 untested $testname
491 return
494 verbose "running $binary $progopts $progopts1" 3
496 set cmd "$binary $progopts $progopts1 ${copyfile}.o"
498 # Ensure consistent sorting of symbols
499 if {[info exists env(LC_ALL)]} {
500 set old_lc_all $env(LC_ALL)
502 set env(LC_ALL) "C"
503 send_log "$cmd\n"
504 set comp_output [remote_exec host $cmd "" "/dev/null" "tmpdir/dump.out"]
505 if {[info exists old_lc_all]} {
506 set env(LC_ALL) $old_lc_all
507 } else {
508 unset env(LC_ALL)
510 if { [lindex $comp_output 0] != 0 } then {
511 send_log "$comp_output\n"
512 fail $testname
513 return
515 set comp_output [prune_warnings [lindex $comp_output 1]]
516 if ![string match "" $comp_output] then {
517 send_log "$comp_output\n"
518 fail $testname
519 return
522 verbose_eval {[file_contents "tmpdir/dump.out"]} 3
523 if { [regexp_diff "tmpdir/dump.out" "${file}.d"] } then {
524 fail $testname
525 verbose "output is [file_contents "tmpdir/dump.out"]" 2
526 return
529 pass $testname
532 proc slurp_options { file } {
533 if [catch { set f [open $file r] } x] {
534 #perror "couldn't open `$file': $x"
535 perror "$x"
536 return -1
538 set opt_array {}
539 # whitespace expression
540 set ws {[ ]*}
541 set nws {[^ ]*}
542 # whitespace is ignored anywhere except within the options list;
543 # option names are alphabetic plus dash
544 set pat "^#${ws}(\[a-zA-Z-\]*)$ws:${ws}(.*)$ws\$"
545 while { [gets $f line] != -1 } {
546 set line [string trim $line]
547 # Whitespace here is space-tab.
548 if [regexp $pat $line xxx opt_name opt_val] {
549 # match!
550 lappend opt_array [list $opt_name $opt_val]
551 } elseif {![regexp "^#" $line ]} {
552 break
555 close $f
556 return $opt_array
559 # regexp_diff, based on simple_diff taken from ld test suite
560 # compares two files line-by-line
561 # file1 contains strings, file2 contains regexps and #-comments
562 # blank lines are ignored in either file
563 # returns non-zero if differences exist
565 proc regexp_diff { file_1 file_2 } {
567 set eof -1
568 set end_1 0
569 set end_2 0
570 set differences 0
571 set diff_pass 0
573 if [file exists $file_1] then {
574 set file_a [open $file_1 r]
575 } else {
576 perror "$file_1 doesn't exist"
577 return 1
580 if [file exists $file_2] then {
581 set file_b [open $file_2 r]
582 } else {
583 perror "$file_2 doesn't exist"
584 close $file_a
585 return 1
588 verbose " Regexp-diff'ing: $file_1 $file_2" 2
590 while { 1 } {
591 set line_a ""
592 set line_b ""
593 while { [string length $line_a] == 0 } {
594 if { [gets $file_a line_a] == $eof } {
595 set end_1 1
596 break
599 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
600 if [ string match "#pass" $line_b ] {
601 set end_2 1
602 set diff_pass 1
603 break
604 } elseif [ string match "#..." $line_b ] {
605 if { [gets $file_b line_b] == $eof } {
606 set end_2 1
607 set diff_pass 1
608 break
610 verbose "looking for \"^$line_b$\"" 3
611 while { ![regexp "^$line_b$" "$line_a"] } {
612 verbose "skipping \"$line_a\"" 3
613 if { [gets $file_a line_a] == $eof } {
614 set end_1 1
615 break
618 break
620 if { [gets $file_b line_b] == $eof } {
621 set end_2 1
622 break
626 if { $diff_pass } {
627 break
628 } elseif { $end_1 && $end_2 } {
629 break
630 } elseif { $end_1 } {
631 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
632 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
633 set differences 1
634 break
635 } elseif { $end_2 } {
636 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
637 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
638 set differences 1
639 break
640 } else {
641 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
642 if ![regexp "^$line_b$" "$line_a"] {
643 send_log "regexp_diff match failure\n"
644 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
645 verbose "regexp_diff match failure\n" 3
646 set differences 1
651 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
652 send_log "$file_1 and $file_2 are different lengths\n"
653 verbose "$file_1 and $file_2 are different lengths" 3
654 set differences 1
657 close $file_a
658 close $file_b
660 return $differences
663 proc file_contents { filename } {
664 set file [open $filename r]
665 set contents [read $file]
666 close $file
667 return $contents
670 proc verbose_eval { expr { level 1 } } {
671 global verbose
672 if $verbose>$level then { eval verbose "$expr" $level }