1 #===============================================================================
5 # Support for host-side testing
7 #===============================================================================
8 ######COPYRIGHTBEGIN####
10 # ----------------------------------------------------------------------------
11 # Copyright (C) 1998, 1999, 2000, 2001 Red Hat, Inc.
13 # This file is part of the eCos host tools.
15 # This program is free software; you can redistribute it and/or modify it
16 # under the terms of the GNU General Public License as published by the Free
17 # Software Foundation; either version 2 of the License, or (at your option)
20 # This program is distributed in the hope that it will be useful, but WITHOUT
21 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 # You should have received a copy of the GNU General Public License along with
26 # this program; if not, write to the Free Software Foundation, Inc.,
27 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 # ----------------------------------------------------------------------------
31 ######COPYRIGHTEND####
32 #===============================================================================
33 ######DESCRIPTIONBEGIN####
38 # Note: Arguably this should be a loadable package
40 #####DESCRIPTIONEND####
41 #===============================================================================
44 # ----------------------------------------------------------------------------
45 # This script gets loaded by host-side DejaGnu test harnesses to provide
46 # various utilities for testing eCos host applications. It lives in the
47 # host-side infrastructure directory and gets installed in
48 # $(PREFIX)/share/dejagnu.
50 # The script can assume that a number of globals from the site.exp
51 # file have been read in. These include:
52 # tool - name of the tool (i.e. the package)
53 # srcdir - location of the source directory
54 # objdir - location of the build tree
55 # host_alias - config triplet
56 # host_triplet - ditto
58 # The generated Makefile has some additional information that is useful.
59 # CC - name of the C compiler that is used
60 # CXX - name of the C++ compiler
61 # prefix - where everything gets installed
62 # OBJEXT - either o or obj
63 # EXEEXT - either nothing or .exe
64 # VERSION - the version number
65 # CFLAGS - flags to use when compiling C code
66 # CXXFLAGS - flags to use when compiling C++ code
68 # Some additional variables should be present in any generated
69 # makefiles in the eCos tree.
70 # INCLUDES - header file search path
71 # LIBS - libraries, search paths, ...
74 # Perform any initialization steps that are needed. Currently this
75 # means reading in the Makefile from the top-level of the build tree
76 # and figuring out the values of CC, CXX, and anything else that is
77 # useful. Any errors should be reported via perror and then the
80 # There is an optional argument, a list of additional variables which
81 # should be present in the makefile and whose values are desired.
83 proc hosttest_initialize { { pkg_vars {} } } {
85 # First check that this script is actually running inside DejaGnu
86 if { [info exists ::objdir] == 0 } {
87 puts "Variable ::objdir is not defined, is this script really running inside DejaGnu?"
91 # The information is stored in an array hosttest_data. Make sure this
93 array set ::hosttest_data {}
95 # Now clear out any entries in the array
96 foreach entry [array names ::hosttest_data] {
97 unset ::hosttest_data($entry)
100 # Now read in the build tree's Makefile (and not the testsuite's Makefile)
101 set filename [file join $::objdir .. Makefile]
102 if { [file exists $filename] == 0 } {
103 perror "Initialization error: the build tree's Makefile $filename does not exist."
107 set fd [open $filename r]
108 set contents [read $fd]
111 if { $status != 0 } {
112 perror "Error reading $filename.\n$message"
116 # The data is available. Search it for each of the variables of
117 # interest. Some variables are optional and are given default
119 set ::hosttest_data(CFLAGS) ""
120 set ::hosttest_data(CXXFLAGS) ""
122 set lines [split $contents "\n"]
124 foreach var [concat { CC CXX prefix OBJEXT EXEEXT VERSION CFLAGS CXXFLAGS INCLUDES LIBS } $pkg_vars] {
126 set pattern "^$var\[ \t\]*:?=\[ \t\]* (.*)\$"
130 foreach line $lines {
131 if { [regexp -- $pattern $line dummy match] == 1 } {
132 set ::hosttest_data($var) $match
136 if { [info exists ::hosttest_data($var)] == 0 } {
137 perror "Variable $var is not defined in $filename"
142 # If compiling with VC++ remove any cygwin-isms from the prefix
143 if { [string match "cl*" $::hosttest_data(CC)] } {
144 set status [catch "exec cygpath -w $::hosttest_data(prefix)" message]
145 if { $status == 0 } {
146 regsub -all -- {\\} $message {/} ::hosttest_data(prefix)
148 perror "Converting cygwin pathname $::hosttest_data(prefix)\n$message"
155 # ----------------------------------------------------------------------------
156 # hosttest_extract_version
157 # Assuming there has been a call to initialize, the required information
158 # should be available in the hosttest_data array. The initialize
159 # function should have aborted if the data is not available.
161 proc hosttest_extract_version { } {
163 if { [info exists ::hosttest_data(VERSION)] == 0 } {
164 error "No version information - host testing has not been properly initialized."
167 if { [info exists ::objdir] == 0 } {
168 error "Variable ::objdir is not defined, is this script really running inside DejaGnu?"
170 return $::hosttest_data(VERSION)
173 # ----------------------------------------------------------------------------
175 # compile and link one or more source files. The arguments are:
176 # 1) the name of the test case
177 # 2) a list of one or more source files that need to be compiled.
178 # Both .c and .cxx files are supported, and the appropriate
179 # compiler will be used. If this list is empty then the
180 # code will look for a .c or a .cxx file which matches the
181 # name of the test executable. Source files are assumed to
182 # be relative to $::srcdir/$::subdir
183 # 3) a list (possibly empty) of directories that should be in the
184 # include path. The build tree's directory is automatically in
185 # the path, as is $(PREFIX)/include. Note that the build tree
186 # is actually one level above objdir, on the assumption that
187 # objdir is the testsuite subdirectory of the real objdir.
188 # 4) ditto for library search paths.
189 # 5) and a list of additional libraries that should be linked.
191 # Currently it is not possible to pass compiler flags since those
192 # might need translating between gcc and VC++. This may have to be
195 # Currently linking is not done via libtool. This may have to change.
197 # The various object files and the executable are placed in a directory
198 # testcase in the build tree, to avoid the risk of name clashes. This
199 # directory must not exist yet. There is a separate routine hosttest_clean
200 # which simply expunges the entire testcase directory.
202 # The output of a succesful compile or built is reported using
203 # verbose at level 2. Unsuccesful compiles or builts are reported using
206 proc hosttest_compile { name sources includes libdirs libs } {
208 # Make sure that the testcase directory does not yet exist, then
209 # create it. This guarantees a clean system and reasonable access
210 # permissions. Each testcase invocation should involve a call to
211 # the clean function.
212 set dirname [file join $::objdir "testcase"]
213 if { [file exists $dirname] != 0 } {
214 # An empty directory is ok.
215 if { [llength [glob -nocomplain -- [file join $dirname "*"]]] != 0 } {
216 error "hosttest_compile: $dirname already exists"
220 set status [catch { file mkdir $dirname } message]
221 if { $status != 0 } {
222 error "hosttest_compile: unable to create directory $dirname"
225 # The only argument that must be valid is the test name.
227 error "hosttest_compile: invalid test case name"
230 # If the list of sources is empty then look for a suitable
231 # file in the appropriate directory.
232 if { [llength $sources] == 0 } {
233 set filename [file join $::srcdir $::subdir "${name}.c"]
234 if { [file exists $filename] && [file isfile $filename] } {
235 lappend sources [file tail $filename]
237 set filename [file join $::srcdir $::subdir "${name}.cxx"]
238 if { [file exists $filename] && [file isfile $filename] } {
239 lappend sources [file tail $filename]
241 error "hosttest_compile: no sources listed and unable to find ${name}.c or ${name}.cxx"
246 # For each source file, generate a compile command line and try to execute
247 # it. The command line takes the form:
248 # (CC|CXX) -c (CFLAGS|CXXFLAGS) (INCDIRS) -o xxx yyy
250 # It is also useful to produce a list of the object files that need to
251 # linked later on, and to work out which tool should be invoked for
256 foreach source $sources {
258 if { [file extension $source] == ".c" } {
259 append commandline "$::hosttest_data(CC) -c $::hosttest_data(CFLAGS) "
260 } elseif { [file extension $source] == ".cxx" } {
262 append commandline "$::hosttest_data(CXX) -c $::hosttest_data(CXXFLAGS) "
264 error "hosttest_compile: files of type [file extension $source] ($source) are not yet supported."
267 # Include path: start with the source tree. Then the build tree.
268 # Then the makefile's INCLUDES variable.
269 # Then any additional directories specified explicitly by the
270 # testcase. Finish off with the prefix. Note that header files
271 # in the prefix directory may be out of date, depending on whether
272 # or not there has been an install recently.
273 append commandline "-I[file join [pwd] [file dirname $::srcdir]] "
274 append commandline "-I[file join [pwd] [file dirname $::objdir]] "
275 append commandline "$::hosttest_data(INCLUDES) "
276 foreach dir $includes {
277 append commandline "-I[file join [pwd] $dir] "
279 append commandline "-I[file join [pwd] $::hosttest_data(prefix) include] "
281 # The output file must go into the testcase directory and have the right suffix
282 set objfile "[file root [file tail $source]].$::hosttest_data(OBJEXT)"
283 lappend object_files $objfile
284 if { [string match "cl*" $::hosttest_data(CC)] } {
285 append commandline "-Fo[file join $::objdir testcase $objfile] "
287 append commandline "-o [file join $::objdir testcase $objfile] "
290 # Finally provide the source file.
291 append commandline "[file join $::srcdir $::subdir $source]"
292 verbose -log -- $commandline
294 # Time to invoke the compiler.
295 set status [catch { set result [eval exec -keepnewline -- $commandline] } message]
296 if { $status == 0 } {
297 # The compile succeeded and the output is in result. Report the
299 verbose -log -- $result
301 # The compile failed and the output is in message.
302 verbose -log -- $message
303 error "hosttest_compile: failed to compile $source"
307 # At this stage all the source files have been compiled, a list of
308 # object files has been constructed, and it is known whether or
309 # not any of the sources were c++. Time to construct a new command
312 if { $has_cxx_files == 0 } {
313 append commandline "$::hosttest_data(CC) $::hosttest_data(CFLAGS) "
315 append commandline "$::hosttest_data(CXX) $::hosttest_data(CXXFLAGS) "
317 set exename [file join $::objdir "testcase" "$name$::hosttest_data(EXEEXT)"]
319 # List all of the object files
320 foreach obj $object_files {
321 append commandline "[file join $::objdir "testcase" $obj] "
324 # Now take care of libraries and search paths. This requires different
325 # code for gcc and VC++.
327 if { [string match "cl*" $::hosttest_data(CC)] } {
328 append commandline "-Fe$exename "
331 append commandline "${lib}.lib "
333 append commandline "$::hosttest_data(LIBS) "
334 append commandline "-libpath=[file join [pwd] [file dirname $::objdir]] "
335 foreach dir $libdirs {
336 append commandline "-libpath=[file join [pwd] $dir] "
338 append commandline "-libpath=[file join [pwd] $::hosttest_data(prefix) lib] "
340 append commandline "-o $exename "
341 append commandline "-L[file join [pwd] [file dirname $::objdir]] "
342 foreach dir $libdirs {
343 append commandline "-L[file join [pwd] $dir] "
345 append commandline "-L[file join [pwd] $::hosttest_data(prefix) lib] "
347 append commandline "-l$lib "
349 append commandline "$::hosttest_data(LIBS) "
352 # We have a command line, go for it.
353 verbose -log -- $commandline
354 set status [catch { set result [eval exec -keepnewline -- $commandline] } message]
355 if { $status == 0 } {
356 # The link has succeeded, we have an executable.
357 verbose -log -- $result
359 # The link failed and the output is in message.
360 # Report things are per compilation failures
361 verbose -log -- $message
362 error "hosttest_compile: failed to link $exename"
365 # There should be a test executable.
368 # ----------------------------------------------------------------------------
370 # Clean up a testcase directory.
372 proc hosttest_clean { } {
374 set dirname [file join $::objdir "testcase"]
375 if { [file exists $dirname] == 0 } {
377 # Something must have gone seriously wrong during the build phase,
378 # there is nothing there.
382 if { [file isdirectory $dirname] == 0 } {
383 error "hosttest_clean: $dirname should be a directory"
386 foreach entry [glob -nocomplain -- [file join $dirname "*"]] {
387 set filename [file join $dirname $entry]
388 if { [file isfile $filename] == 0 } {
389 error "hosttest_clean: $filename is not a file"
391 set status [catch { file delete -force -- $filename } message]
392 if { $status != 0 } {
393 error "hosttest_clean: unable to delete $filename, $message"
396 set status [catch { file delete -force -- $dirname } message]
397 if { $status != 0 } {
398 error "hosttest_clean: unable to delete directory $dirname, $message"
402 # ----------------------------------------------------------------------------
403 # Run a test executable, returning the status code and the output.
404 # The results are returned in variables. It is assumed that some test cases
405 # will fail, so raising an exception is appropriate only if something
406 # has gone wrong at the test harness level. The argument list
407 # should be the name of the test case (from which the executable file name
408 # can be derived) and a list of arguments.
410 proc hosttest_run { result_arg output_arg test args } {
412 upvar $result_arg result
413 upvar $output_arg output
415 # Figure out the filename corresponding to the test and make
417 set filename [file join $::objdir "testcase" $test]
418 append filename $::hosttest_data(EXEEXT)
419 if { ([file exists $filename] == 0) || ([file isfile $filename] == 0) } {
420 error "hosttest_run: testcase file $filename does not exist"
423 # There is no need to worry about interacting with the program,
424 # just exec it. It is a good idea to do this in the testcase directory,
425 # so that any core dumps get cleaned up as well.
426 set current_dir [pwd]
427 set status [ catch { cd [file join $::objdir "testcase"] } message ]
428 if { $status != 0 } {
429 error "unable to change directory to [file join $::objdir testcase]\n$message"
432 verbose -log -- $filename $args
433 set status [ catch { set result [eval exec -keepnewline -- $filename $args] } output]
434 if { $status == 0 } {
435 # The command has succeeded. The exit code is 0 and the output
436 # was returned by the exec.
440 # The command has failed. The exit code is 1 and the output is
441 # already in the right place.
442 verbose -log -- $output
445 set status [catch { cd $current_dir } message]
446 if { $status != 0 } {
447 error "unable to change directory back to $current_dir"
451 # ----------------------------------------------------------------------------
452 # Given some test output, look through it for pass and fail messages.
453 # These should result in appropriate DejaGnu pass and fail calls.
454 # In addition, if the program exited with a non-zero status code but
455 # did not report any failures then a special failure is reported.
457 proc hosttest_handle_output { name result output } {
462 foreach line [split $output "\n"] {
464 # The output should be of one of the following forms:
466 # FAIL:<message> Line: xx File: xx
469 # PASS and FAIL messages will be reported via DejaGnu pass and fail
470 # calls. Everything else gets passed to verbose, so the user gets
471 # to choose how much information gets reported.
477 if { [regexp -- {^PASS:<(.*)>.*$} $line dummy match1] == 1 } {
480 } elseif { [regexp -- {^FAIL:<(.*)>(.*)$} $line dummy match1 match2] == 1 } {
481 fail "$match1 $match2"
488 if { ($result != 0) && ($fails == 0) } {
489 fail "program terminated with non-zero exit code but did not report any failures"
490 } elseif { ($passes == 0) && ($fails == 0) } {
491 unresolved "test case $name did not report any passes or failures"
495 # ----------------------------------------------------------------------------
496 # hosttest_run_test_with_filter
497 # This routines combines the compile, run and clean operations,
498 # plus it invokes a supplied callback to filter the output. The
499 # callback is passed three arguments: the test name, the exit code,
500 # and all of the program output.
502 proc hosttest_run_test_with_filter { name filter sources incdirs libdirs libs args } {
507 set status [ catch { hosttest_compile $name $sources $incdirs $libdirs $libs } message]
508 if { $status != 0 } {
509 fail "unable to compile test case $name, $message"
513 set status [ catch { hosttest_run result output $name $args } message]
514 if { $status != 0 } {
515 fail "unable to run test case $name, $message"
519 set status [ catch { $filter $name $result $output } message]
520 if { $status != 0 } {
521 fail "unable to parse output from test case $name"
529 # ----------------------------------------------------------------------------
530 # hosttest_run_simple_test
531 # This routine combines the compile, run, output, and clean operations.
532 # The arguments are the same as for compilation, plus an additional
533 # list for run-time parameters to the test case.
535 proc hosttest_run_simple_test { name sources incdirs libdirs libs args } {
541 set status [ catch { hosttest_compile $name $sources $incdirs $libdirs $libs } message]
542 if { $status != 0 } {
543 fail "unable to compile test case $name, $message"
547 set status [ catch { hosttest_run result output $name $args } message]
548 if { $status != 0 } {
549 fail "unable to run test case $name, $message"
553 set status [ catch { hosttest_handle_output $name $result $output } message]
554 if { $status != 0 } {
555 fail "unable to parse output from test case $name"
563 # ----------------------------------------------------------------------------
564 # Filename translation. A particular file has been created and must now
565 # be accessed from Tcl.
567 # Under Unix everything just works.
569 # Under Windows, well there is cygwin and there is the Windows world.
570 # A file may have come from either. cygtclsh80 and hence DejaGnu is not
571 # fully integrated with cygwin, for example it does not know about
572 # cygwin mount points. There are also complications because of
573 # volume-relative filenames.
575 # The code here tries a number of different ways of finding a file which
576 # matches the name. It is possible that the result is not actually what
577 # was intended, but hopefully this case will never arise.
579 proc hosttest_translate_existing_filename { name } {
581 if { $::tcl_platform(platform) == "unix" } {
582 # The file should exist. It is worth checking just in case.
583 if { [file exists $name] == 0 } {
590 if { $::tcl_platform(platform) != "windows" } {
591 perror "The testing framework does not know about platform $::tcl_platform(platform)"
595 # Always get rid of any backslashes, they just cause trouble
596 regsub -all -- {\\} $name {/} name
598 # If the name is already valid, great.
599 if { [file exists $name] } {
603 # OK, try to use cygwin's cygpath utility to convert it.
604 set status [catch "exec cygpath -w $name" message]
605 if { $status == 0 } {
607 regsub -all -- {\\} $message {/} cygwin_name
608 if { [file exists $cygwin_name] } {
613 # Is the name volumerelative? If so work out the current volume
614 # from the current directory and prepend this.
615 if { [file pathtype $name] == "volumerelative" } {
616 append fullname [string range [pwd] 0 1] $name
617 if { [file exists $fullname] } {
622 # There are other possibilities, e.g. d:xxx indicating a file
623 # relative to the current directory on drive d:. For now such
624 # Lovecraftian abominations are ignored.
628 # ----------------------------------------------------------------------------
629 # Support for assertion dumps. The infrastructure allows other subsystems
630 # to add their own callbacks which get invoked during a panic and which
631 # can write additional output to the dump file. For example it would be
632 # possible to output full details of the current configuration. These
633 # routines make it easier to write test cases for such callbacks.
635 # hosttest_assert_check(result output)
636 # Make sure that the test case really triggered an assertion.
638 # hosttest_assert_read_dump(output)
639 # Identify the temporary file used for the dump, read it in, delete
640 # it (no point in leaving such temporaries lying around when running
641 # testcases) and return the contents of the file.
643 # hosttest_assert_extract_callback(dump title)
644 # Given a dump output as returned by read_dump, look for a section
645 # generated by a callback with the given title. Return the contents
648 proc hosttest_assert_check { result output } {
650 if { $result == 0 } {
654 foreach line [split $output "\n"] {
655 if { [string match "Assertion failure*" $line] } {
662 # This routine assumes that assert_check has already been called.
663 proc hosttest_assert_read_dump { output } {
665 foreach line [split $output "\n"] {
669 if { [regexp -nocase -- {^writing additional output to (.*)$} $line dummy match] } {
671 # The filename is in match, but it may not be directly accessible.
672 set filename [hosttest_translate_existing_filename $match]
673 if { $filename == "" } {
677 set fd [open $filename r]
680 file delete $filename
682 if { $status != 0 } {
683 unresolved "Unable to process assertion dump file $filename"
684 unresolved "File $filename may have to be deleted manually"
693 # Look for the appropriate markers. Also clean up blank lines
694 # at the start and end.
695 proc hosttest_assert_extract_callback { dump title } {
697 set lines [split $dump "\n"]
700 while { [llength $lines] > 0 } {
701 set line [lindex $lines 0]
702 set lines [lreplace $lines 0 0]
704 if { [regexp -nocase -- "^\# \{\{\{.*${title}.*\$" $line] } {
706 # Skip any blank lines at the start
707 while { [llength $lines] > 0 } {
708 set line [lindex $lines 0]
709 if { [regexp -- {^ *$} $line] == 0} {
712 set lines [lreplace $lines 0 0]
715 # Now add any lines until the close marker.
716 # Nested folds are not supported yet.
717 while { [llength $lines] > 0 } {
718 set line [lindex $lines 0]
719 set lines [lreplace $lines 0 0]
720 if { [regexp -nocase -- {^\# \}\}\}.*$} $line] } {
723 append result $line "\n"