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

if ![info exists tmpdir] {
    set tmpdir "/tmp"
}

#
# Build the status wrapper library as needed.
#
proc libstdc++_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_libstdc++ { options srcfile compile_args inpfile resultfile exec_args } {
    global base_dir
    global LIBSTDCPP
    global srcdir subdir objdir
    global TOOL_OPTIONS
    global ld_library_path

    if [info exists LIBSTDCPP] {
	set libstdcpp $LIBSTDCPP;
    } else {
	set gp [get_multilibs];
	if { $gp != "" } {
	    if [file exists "$gp/libstdc++/libstdc++.a"] {
		set libstdcpp "-L$gp/libstdc++ -lstdc++";
	    }
	}
	if ![info exists libstdcpp] {
	set libstdcpp [findfile $base_dir/../../libstdc++/libstdc++.a "-L$base_dir/../../libstdc++ -lstdc++" -lstdc++]
	}
    }

    verbose "using LIBSTDCPP = $libstdcpp" 2
    set args ""

    # Basically we want to build up a colon separated path list from
    # the value of $libstdcpp.

    # First strip away any -L arguments.
    regsub -all -- "-L" $libstdcpp "" ld_library_path

    # Then remove any -lstdc++ argument.
    regsub -all -- " -lstdc.." $ld_library_path "" ld_library_path

    # That's enough to make things work for the normal case.
    # If we wanted to handle an arbitrary value of libstdcpp,
    # then we'd have to do a lot more work.

    if { $compile_args != "" } {
	lappend args "additional_flags=$compile_args"
    }
    lappend args "incdir=.."
    lappend args "incdir=$srcdir/.."
    lappend args "incdir=$srcdir/../stl"
    lappend args "incdir=."
    if [info exists TOOL_OPTIONS] {
	lappend args "additional_flags=$TOOL_OPTIONS"
    }

    global wrapper_file wrap_compile_flags;
    # We have to include libio, for _G_config.h.
    lappend args "additional_flags=$wrap_compile_flags";
    lappend args "libs=$wrapper_file";
    lappend args "libs=$libstdcpp";
    lappend args "additional_flags=[libio_include_flags]"
    lappend args debug

    regsub "^.*/(\[^/.\]+)\[.\]\[^/]*$" "$srcfile" "\\1" out
    set executable "${objdir}/$out"
    set errname "[file tail $srcfile]"
    if { $compile_args != "" } {
	set errname "$errname $compile_args"
    }

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

    set result [libstdc++_load $executable "$exec_args" "$inpfile"];
    set status [lindex $result 0];
    set output [lindex $result 1];
    $status "$errname execution"
    if { $status != "pass" } {
	setup_xfail "*-*-*"
	fail "$errname output"
	return;
    }

    verbose "resultfile is $resultfile"
    set id [open $resultfile r];
    set expected ""
    append expected [read $id];
    regsub -all "\r" "$output" "" output;
    regsub "\n*$" $expected "" expected
    regsub "\n*$" $output "" output
    regsub "^\n*" $expected "" expected
    regsub "^\n*" $output "" output
    regsub -all "\[ \t\]\[ \t\]*" $expected " " expected
    regsub -all "\[ \t\]*\n\n*" $expected "\n" expected
    regsub -all "\[ \t\]\[ \t\]*" $output " " output
    regsub -all "\[ \t\]*\n\n*" $output "\n" output
    verbose "expected is $expected"
    verbose "actual is $output"
    set passed 0;
    if {$options == "regexp_match"} {
	if [regexp $expected $output] {
	    set passed 1;
	}
    } else {
	if { $expected == $output } {
	    set passed 1;
	}
    }
    if { $passed == 1 } {
	pass "$errname output"
    } else {
	clone_output "expected was $expected"
	clone_output "output was $output"
	fail "$errname output"
    }
    close $id;
}

#
# libstdc++_version -- extract and print the version number of libstdc++p
#
proc default_libstdc++_version {} {
}

proc default_libstdc++_start { } {
}
