📄 hosttest.exp
字号:
#===============================================================================## hosttest.exp## Support for host-side testing##===============================================================================######COPYRIGHTBEGIN##### # ----------------------------------------------------------------------------# Copyright (C) 1998, 1999, 2000 Red Hat, Inc.## This file is part of the eCos host tools.## This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2 of the License, or (at your option) # any later version.# # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details.# # You should have received a copy of the GNU General Public License along with# this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.## ----------------------------------------------------------------------------# ######COPYRIGHTEND#####===============================================================================######DESCRIPTIONBEGIN###### Author(s): bartv# Contributors: bartv# Date: 1998-11-25# Note: Arguably this should be a loadable package######DESCRIPTIONEND#####===============================================================================## ----------------------------------------------------------------------------# This script gets loaded by host-side DejaGnu test harnesses to provide# various utilities for testing eCos host applications. It lives in the# host-side infrastructure directory and gets installed in# $(PREFIX)/share/dejagnu.## The script can assume that a number of globals from the site.exp# file have been read in. These include:# tool - name of the tool (i.e. the package)# srcdir - location of the source directory# objdir - location of the build tree# host_alias - config triplet# host_triplet - ditto## The generated Makefile has some additional information that is useful.# CC - name of the C compiler that is used# CXX - name of the C++ compiler# prefix - where everything gets installed# OBJEXT - either o or obj# EXEEXT - either nothing or .exe# VERSION - the version number# CFLAGS - flags to use when compiling C code# CXXFLAGS - flags to use when compiling C++ code## hosttest_initialize# Perform any initialization steps that are needed. Currently this# means reading in the Makefile from the top-level of the build tree# and figuring out the values of CC, CXX, and anything else that is# useful. Any errors should be reported via perror and then the# script should exit.## There is an optional argument, a list of additional variables which# should be present in the makefile and whose values are desired.proc hosttest_initialize { { pkg_vars {} } } { # First check that this script is actually running inside DejaGnu if { [info exists ::objdir] == 0 } { puts "Variable ::objdir is not defined, is this script really running inside DejaGnu?" exit 1 } # The information is stored in an array hosttest_data. Make sure this # array exists. array set ::hosttest_data {} # Now clear out any entries in the array foreach entry [array names ::hosttest_data] { unset ::hosttest_data($entry) } # Now read in the build tree's Makefile (and not the testsuite's Makefile) set filename [file join $::objdir .. Makefile] if { [file exists $filename] == 0 } { perror "Initialization error: the build tree's Makefile $filename does not exist." exit 1 } set status [ catch { set fd [open $filename r] set contents [read $fd] close $fd } message] if { $status != 0 } { perror "Error reading $filename.\n$message" exit 1 } # The data is available. Search it for each of the variables of # interest. Some variables are optional and are given default # values. set ::hosttest_data(CFLAGS) "" set ::hosttest_data(CXXFLAGS) "" set lines [split $contents "\n"] foreach var [concat { CC CXX prefix OBJEXT EXEEXT VERSION CFLAGS CXXFLAGS } $pkg_vars] { set pattern "^$var\[ \t\]*:?=\[ \t\]* (.*)\$" set dummy "" set match "" foreach line $lines { if { [regexp -- $pattern $line dummy match] == 1 } { set ::hosttest_data($var) $match break } } if { [info exists ::hosttest_data($var)] == 0 } { perror "Variable $var is not defined in $filename" exit 1 } } # If compiling with VC++ remove any cygwin-isms from the prefix if { [string match "cl*" $::hosttest_data(CC)] } { set status [catch "exec cygpath -w $::hosttest_data(prefix)" message] if { $status == 0 } { regsub -all -- {\\} $message {/} ::hosttest_data(prefix) } else { perror "Converting cygwin pathname $::hosttest_data(prefix)\n$message" exit 1 } }}# ----------------------------------------------------------------------------# hosttest_extract_version# Assuming there has been a call to initialize, the required information# should be available in the hosttest_data array. The initialize# function should have aborted if the data is not available.proc hosttest_extract_version { } { if { [info exists ::hosttest_data(VERSION)] == 0 } { error "No version information - host testing has not been properly initialized." } if { [info exists ::objdir] == 0 } { error "Variable ::objdir is not defined, is this script really running inside DejaGnu?" } return $::hosttest_data(VERSION)}# ----------------------------------------------------------------------------# hosttest_compile# compile and link one or more source files. The arguments are:# 1) the name of the test case# 2) a list of one or more source files that need to be compiled.# Both .c and .cxx files are supported, and the appropriate# compiler will be used. If this list is empty then the# code will look for a .c or a .cxx file which matches the# name of the test executable. Source files are assumed to# be relative to $::srcdir/$::subdir# 3) a list (possibly empty) of directories that should be in the# include path. The build tree's directory is automatically in# the path, as is $(PREFIX)/include. Note that the build tree# is actually one level above objdir, on the assumption that# objdir is the testsuite subdirectory of the real objdir.# 4) ditto for library search paths.# 5) and a list of additional libraries that should be linked.## Currently it is not possible to pass compiler flags since those# might need translating between gcc and VC++. This may have to be# resolved.## Currently linking is not done via libtool. This may have to change.## The various object files and the executable are placed in a directory# testcase in the build tree, to avoid the risk of name clashes. This# directory must not exist yet. There is a separate routine hosttest_clean# which simply expunges the entire testcase directory.## The output of a succesful compile or built is reported using# verbose at level 2. Unsuccesful compiles or builts are reported using# level 1.proc hosttest_compile { name sources incdirs libdirs libs } { # Make sure that the testcase directory does not yet exist, then # create it. This guarantees a clean system and reasonable access # permissions. Each testcase invocation should involve a call to # the clean function. set dirname [file join $::objdir "testcase"] if { [file exists $dirname] != 0 } { # An empty directory is ok. if { [llength [glob -nocomplain -- [file join $dirname "*"]]] != 0 } { error "hosttest_compile: $dirname already exists" } } set status [catch { file mkdir $dirname } message] if { $status != 0 } { error "hosttest_compile: unable to create directory $dirname" } # The only argument that must be valid is the test name. if { $name == "" } { error "hosttest_compile: invalid test case name" } # If the list of sources is empty then look for a suitable # file in the appropriate directory. if { [llength $sources] == 0 } { set filename [file join $::srcdir $::subdir "${name}.c"] if { [file exists $filename] && [file isfile $filename] } { lappend sources [file tail $filename] } else { set filename [file join $::srcdir $::subdir "${name}.cxx"] if { [file exists $filename] && [file isfile $filename] } { lappend sources [file tail $filename] } else { error "hosttest_compile: no sources listed and unable to find ${name}.c or ${name}.cxx" } } } # For each source file, generate a compile command line and try to execute # it. The command line takes the form: # (CC|CXX) -c (CFLAGS|CXXFLAGS) (INCDIRS) -o xxx yyy # # It is also useful to produce a list of the object files that need to # linked later on, and to work out which tool should be invoked for # linking. set object_files {} set has_cxx_files 0 foreach source $sources { set commandline "" if { [file extension $source] == ".c" } { append commandline "$::hosttest_data(CC) -c $::hosttest_data(CFLAGS) " } elseif { [file extension $source] == ".cxx" } { set has_cxx_files 1 append commandline "$::hosttest_data(CXX) -c $::hosttest_data(CXXFLAGS) " } else { error "hosttest_compile: files of type [file extension $source] ($source) are not yet supported." } # Include path: start with the source tree. Then the build tree. # Then any additional directories specified explicitly by the # testcase. Finish off with the prefix. Note that header files # in the prefix directory may be out of date, depending on whether # or not there has been an install recently. append commandline "-I[file join [pwd] [file dirname $::srcdir]] " append commandline "-I[file join [pwd] [file dirname $::objdir]] " foreach dir $incdirs { append commandline "-I[file join [pwd] $dir] " } append commandline "-I[file join [pwd] $::hosttest_data(prefix) include] " # The output file must go into the testcase directory and have the right suffix set objfile "[file root [file tail $source]].$::hosttest_data(OBJEXT)" lappend object_files $objfile if { [string match "cl*" $::hosttest_data(CC)] } { append commandline "-Fo[file join $::objdir testcase $objfile] " } else { append commandline "-o [file join $::objdir testcase $objfile] " } # Finally provide the source file. append commandline "[file join $::srcdir $::subdir $source]" verbose -log -- $commandline # Time to invoke the compiler. set status [catch { set result [eval exec -keepnewline -- $commandline] } message] if { $status == 0 } { # The compile succeeded and the output is in result. Report the # output. verbose -log -- $result } else { # The compile failed and the output is in message. verbose -log -- $message error "hosttest_compile: failed to compile $source" } } # At this stage all the source files have been compiled, a list of # object files has been constructed, and it is known whether or # not any of the sources were c++. Time to construct a new command # line. set commandline "" if { $has_cxx_files == 0 } { append commandline "$::hosttest_data(CC) $::hosttest_data(CFLAGS) " } else { append commandline "$::hosttest_data(CXX) $::hosttest_data(CXXFLAGS) " } set exename [file join $::objdir "testcase" "$name$::hosttest_data(EXEEXT)"] # List all of the object files foreach obj $object_files { append commandline "[file join $::objdir "testcase" $obj] " } # Now take care of libraries and search paths. This requires different # code for gcc and VC++. if { [string match "cl*" $::hosttest_data(CC)] } { append commandline "-Fe$exename " foreach lib $libs { append commandline "${lib}.lib " } append commandline "-link " append commandline "-libpath=[file join [pwd] [file dirname $::objdir]] " foreach dir $libdirs { append commandline "-libpath=[file join [pwd] $dir] " } append commandline "-libpath=[file join [pwd] $::hosttest_data(prefix) lib] " } else { append commandline "-o $exename " append commandline "-L[file join [pwd] [file dirname $::objdir]] " foreach dir $libdirs { append commandline "-L[file join [pwd] $dir] " } append commandline "-L[file join [pwd] $::hosttest_data(prefix) lib] " foreach lib $libs { append commandline "-l$lib " } } # We have a command line, go for it. verbose -log -- $commandline set status [catch { set result [eval exec -keepnewline -- $commandline] } message] if { $status == 0 } { # The link has succeeded, we have an executable. verbose -log -- $result } else { # The link failed and the output is in message. # Report things are per compilation failures verbose -log -- $message error "hosttest_compile: failed to link $exename" } # There should be a test executable.}# ----------------------------------------------------------------------------# hosttest_clean# Clean up a testcase directory.proc hosttest_clean { } {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -