1# Copyright (C) 2002 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17# This file was written by Janis Johnson, <janis187@us.ibm.com>
18
19
20# Test interoperability of two compilers that follow the same ABI, or
21# compatibility of two versions of GCC.
22#
23# Each test has a main program that does nothing but call a function,
24# plus two additional source files that contain parts of a program that
25# rely on the ABI.  those source files are compiled into relocatable
26# object files with both compilers.  Executables are built using various
27# combinations of those object files, with the main program compiled
28# with the compiler under test and using that compiler's runtime support.
29
30# The including .exp file must define these callback procedures.
31if [string match "" [info procs "compat-use-alt-compiler"]] then {
32    error "Proc compat-use-alt-compiler is not defined."
33}
34if [string match "" [info procs "compat-use-tst-compiler"]] then {
35    error "Proc compat-use-tst-compiler is not defined."
36}
37
38# Each test is run with each pair of compiler options from this list.
39# The first set of options in each pair is used by the compiler under
40# test, and the second set is used by the alternate compiler.
41# The default option lists can be overridden by
42# COMPAT_OPTIONS="[list [list {tst_1} {alt_1}]...[list {tst_n} {alt_n}]]"
43# where tst_i and alt_i are lists of options.  You can put this in the
44# environment before site.exp is written or add it to site.exp directly.
45if ![info exists COMPAT_OPTIONS] {
46    set COMPAT_OPTIONS [list \
47	[list {} {}]]
48}
49
50set option_list $COMPAT_OPTIONS
51
52#
53# compat-obj -- compile to an object file
54#
55# SOURCE is the source file
56# DEST is the object file
57# OPTIONS is the list of compiler options
58# OPTSTR is the options to print with test messages
59#
60proc compat-obj { source dest options optstr } {
61    global testcase
62    global tool
63
64    set comp_output [${tool}_target_compile "$source" "$dest" object $options]
65    ${tool}_check_compile "$testcase $dest compile" $optstr $dest $comp_output
66}
67
68# compat-run -- link and run an executable
69#
70# TESTNAME is the mixture of object files to link
71# OBJLIST is the list of object files to link
72# DEST is the name of the executable
73# OPTIONS is a list of compiler and linker options to use
74# OPTSTR is the list of options to list in messages
75#
76proc compat-run { testname objlist dest options optstr } {
77    global testcase
78    global tool
79
80    # Check that all of the objects were built successfully.
81    foreach obj [split $objlist] {
82	if ![file exists $obj] then {
83	    unresolved "$testcase $testname link $optstr"
84	    unresolved "$testcase $testname execute $optstr"
85	    return
86	}
87    }
88
89    # Link the objects into an executable.
90    set comp_output [${tool}_target_compile "$objlist" $dest executable \
91		     "$options"]
92    if ![${tool}_check_compile "$testcase $testname link" "" \
93	 $dest $comp_output] then {
94	unresolved "$testcase $testname execute $optstr"
95	return
96    }
97
98    # Run the self-checking executable.
99    if ![string match "*/*" $dest] then {
100	set dest "./$dest"
101    }
102    set result [${tool}_load $dest "" ""]
103    set status [lindex $result 0]
104    if { $status == "pass" } then {
105	remote_file build delete $dest
106    }
107    $status "$testcase $testname execute $optstr"
108}
109
110#
111# compat-execute -- compile with compatible compilers
112#
113# SRC1 is the full pathname of the main file of the testcase.
114# USE_ALT is nonzero if we're using an alternate compiler as well as
115#   the compiler under test.
116#
117proc compat-execute { src1 use_alt } {
118    global srcdir tmpdir
119    global option_list
120    global tool
121    global verbose
122    global testcase
123    global gluefile
124
125    # Use the dg-options mechanism to specify extra flags for this test.
126    # FIXME: This does not handle other uses of dg-options, and it only
127    # processes the first one.
128    set extra_tool_flags ""
129    set tmp [grep $src1 "{\[ \t\]\*dg-options.*\[ \t\]\+}"]
130    if ![string match "" $tmp] {
131	set tmp0 [lindex $tmp 0]
132	# Extract the compiler options.
133	regexp "dg-options\[ \t\]\+(.*)\[ \t\]\+\}" \
134	       $tmp0 all args
135	# Sometime the options are in quotes, sometimes not.
136	regsub -all "\"" $args "" args
137	set extra_tool_flags $args
138    }
139
140    # Set up the names of the other source files.
141    regsub "_main.*" $src1 "" base
142    regsub ".*/" $base "" base
143    regsub "_main" $src1 "_x" src2
144    regsub "_main" $src1 "_y" src3
145
146    # Define the names of the object files.
147    set obj1 "main_tst.o"
148    set obj2_tst "x_tst.o"
149    set obj2_alt "x_alt.o"
150    set obj3_tst "y_tst.o"
151    set obj3_alt "y_alt.o"
152
153    # Get the base name of this test, for use in messages.
154    regsub "^$srcdir/?" $src1 "" testcase
155    regsub "_main.*" $testcase "" testcase
156    # Set up the base name of executable files so they'll be unique.
157    regsub -all "\[./\]" $testcase "-" execbase
158
159    # If we couldn't rip $srcdir out of `src1' then just do the best we can.
160    # The point is to reduce the unnecessary noise in the logs.  Don't strip
161    # out too much because different testcases with the same name can confuse
162    # `test-tool'.
163    if [string match "/*" $testcase] then {
164        set testcase "[file tail [file dirname $src1]]/[file tail $src1]"
165    }
166
167    # Loop through all of the option lists used for this test.
168
169    set count 0
170    foreach option_pair $option_list {
171
172	# Pick out each set of options.
173	set tst_option [lindex $option_pair 0]
174	set alt_option [lindex $option_pair 1]
175	set optstr ""
176	if { ![string match $tst_option ""] \
177	     || ![string match $alt_option ""] } then {
178	    set optstr "\"$tst_option\",\"$alt_option\""
179	}
180	verbose "Testing $testcase, $optstr" 1
181
182	set tst_options ""
183	set alt_options ""
184	if ![string match $extra_tool_flags ""] then {
185	    lappend tst_options "additional_flags=$extra_tool_flags $tst_option"
186	    lappend alt_options "additional_flags=$extra_tool_flags $alt_option"
187	}
188
189	# There's a unique name for each executable we generate, based on
190	# the set of options and how the pieces of the tests are compiled.
191	set execname1 "${execbase}-${count}1"
192	set execname2 "${execbase}-${count}2"
193	set execname3 "${execbase}-${count}3"
194	set execname4 "${execbase}-${count}4"
195	incr count
196
197	remote_file build delete $execname1
198	remote_file build delete $execname2
199	remote_file build delete $execname3
200	remote_file build delete $execname4
201
202	# Compile pieces with the alternate compiler; we'll catch problems
203	# later.  Skip this if we don't have an alternate compiler.
204	if { $use_alt != 0 } then {
205	    compat-use-alt-compiler
206	    compat-obj "$src2" "$obj2_alt" $alt_options $optstr
207	    compat-obj "$src3" "$obj3_alt" $alt_options $optstr
208	}
209
210	# Compile pieces with the compiler under test.
211	compat-use-tst-compiler
212	compat-obj "$src1" "$obj1" $tst_options $optstr
213	compat-obj "$src2" "$obj2_tst" $tst_options $optstr
214	compat-obj "$src3" "$obj3_tst" $tst_options $optstr
215
216	# Link (using the compiler under test), run, and clean up tests.
217	compat-run "${obj2_tst}-${obj3_tst}" \
218	    "$obj1 $obj2_tst $obj3_tst" $execname1 $tst_options $optstr
219
220	# If we've got an alternate compiler try some combinations.
221	if { $use_alt != 0 } then {
222	    compat-run "${obj2_tst}-${obj3_alt}" "$obj1 $obj2_tst $obj3_alt" \
223		       $execname2 $tst_options $optstr
224	    compat-run "${obj2_alt}-${obj3_tst}" "$obj1 $obj2_alt $obj3_tst" \
225		       $execname3 $tst_options $optstr
226	    compat-run "${obj2_alt}-${obj3_alt}" "$obj1 $obj2_alt $obj3_alt" \
227		       $execname4 $tst_options $optstr
228	}
229
230	# Clean up object files.
231	set files [glob -nocomplain *.o]
232	if { $files != "" } {
233	    foreach objfile $files {
234		if { ![info exists gluefile] || $objfile != $gluefile } {
235		    eval "remote_file build delete $objfile"
236		}
237	    }
238	}
239    }
240}
241