# Copyright (C) 1997 Free Software Foundation, Inc.

# 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.  

# Please email any bugs, comments, and/or additions to this file to:
# bug-lib-g++@prep.ai.mit.edu

# This file was written by Bob Manson. (manson@cygnus.com)

load_lib "libgloss.exp"

global base_dir

proc libio_init { args } {
    global wrapper_file;
    global wrap_compile_flags;
    set wrapper_file "";
    set wrap_compile_flags "";
    if [target_info exists needs_status_wrapper] {
	set result [build_wrapper "testglue.o"];
	if { $result != "" } {
	    set wrapper_file [lindex $result 0];
	    set wrap_compile_flags [lindex $result 1];
	} else {
	    warning "Status wrapper failed to build."
	}
    }
}
#
# Run the test specified by srcfile and resultfile. compile_args and
# exec_args are additional arguments to be passed in when compiling and
# running the testcase, respectively.
#

proc test_libio { srcfile compile_args inpfile resultfile exec_args } {
    global LIBIO
    global srcdir subdir objdir
    global TOOL_OPTIONS
    global wrapper_file wrap_compile_flags
    global ld_library_path

    if { $inpfile != "" } {
	set inpfile "$srcdir/../tests/$inpfile"
    }

    set args ""
    set ld_library_path ""
    lappend args "additional_flags=-O3";
    lappend args "additional_flags=-I.. -I$srcdir/.."
    lappend args "additional_flags=$wrap_compile_flags";
    lappend args "libs=$wrapper_file";
    if { $compile_args != "" } {
	lappend args "additional_flags=$compile_args"
    }

    if [regexp "\.cc$" $srcfile] {
	lappend args "additional_flags=-nostdinc++"
	lappend args "additional_flags=[libstdc++_include_flags]";
	lappend args "additional_flags=[libstdc++_link_flags]";
	lappend args "libs=-lstdc++"
    } else {
	if [info exists LIBIO] {
	    lappend args "libdir=$LIBIO"
	} else {
	    lappend args "additional_flags=[libio_link_flags]";
	}
	lappend args "additional_flags=[libio_link_flags]";
	lappend args "libs=-lio"
    }

    if [target_info exists slow_simulator] then {
      lappend args "additional_flags=-DSLOW_SIMULATOR"
    }

    if [info exists TOOL_OPTIONS] {
	lappend args "additional_options=$TOOL_OPTIONS"
    }

    set gp [get_multilibs];
    if { $gp != "" } {
	if [file exists $gp/libiberty/libiberty.a] {
	    lappend args "libdir=$gp/libiberty";
	    set found_libiberty 1;
	}
    }
    if ![info exists found_libiberty] {
	set lib [lookfor_file "$objdir" "libiberty/libiberty.a"];
	if { $lib != "" } {
	    lappend args "libdir=[file dirname $lib]";
	}
    }
    lappend args "libs=-liberty"
    lappend args "debug";

    set errname "$srcfile"
    set srcfile "$srcdir/../tests/$srcfile"
    regsub "\\..*$" "[file tail $srcfile]" "" executable
    set executable "$objdir/$executable"

    if { [target_compile $srcfile $executable executable $args] != "" } {
	fail "$errname compilation $exec_args";
	setup_xfail "*-*-*"
	fail "$errname execution $exec_args"
	if { $resultfile != "" } {
	    setup_xfail "*-*-*"
	    fail "$errname output $exec_args"
	}
	return;
    }
    pass "$errname compilation $exec_args"

    set result [libio_load $executable $exec_args $inpfile];
    set status [lindex $result 0];
    set output [lindex $result 1];
    if { $status == "unsupported" } {
	unsupported "target does not support loading $srcfile"
	return;
    }
    if { $status != "pass" } {
	$status "$errname execution $exec_args"
	setup_xfail "*-*-*"
	fail "$errname output $exec_args"
	return;
    }
    pass "$errname execution $exec_args"

    if { $resultfile != "" } {
	set id [open $srcdir/../tests/$resultfile r];
	set expected ""
	append expected [read $id];
	verbose "expected is $expected"
	verbose "actual is $output"
	regsub -all "\r" $output "" output
	regsub "\n+$" $expected "" expected
	regsub "\n+$" $output "" output
	regsub "^\n+" $expected "" expected
	regsub "^\n+" $output "" output
	if { $expected == $output } {
	    pass "$errname output $exec_args"
	} else {
	    fail "$errname output $exec_args"
	}
	close $id;
    }
}

#
# libio_version -- extract and print the version number of libio
#
proc default_libio_version {} {
    global LIBIO
}

proc default_libio_start { } {
}
