1 # Copyright (C) 2002 Free Software Foundation, Inc.
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.
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.
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.
17 # This file was written by Janis Johnson, <janis187@us.ibm.com>
20 # Test interoperability of two compilers that follow the same ABI, or
21 # compatibility of two versions of GCC.
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.
30 # The including .exp file must define these callback procedures.
31 if [string match "" [info procs "compat-use-alt-compiler"]] then {
32 error "Proc compat-use-alt-compiler is not defined."
34 if [string match "" [info procs "compat-use-tst-compiler"]] then {
35 error "Proc compat-use-tst-compiler is not defined."
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.
45 if ![info exists COMPAT_OPTIONS] {
46 set COMPAT_OPTIONS [list \
50 set option_list $COMPAT_OPTIONS
56 # compat-obj -- compile to an object file
58 # SOURCE is the source file
59 # DEST is the object file
60 # OPTALL is the list of compiler options to use with all tests
61 # OPTFILE is the list of compiler options to use with this file
62 # OPTSTR is the options to print with test messages
63 # XFAILDATA is the xfail data to be passed to the compiler
65 proc compat-obj { source dest optall optfile optstr xfaildata } {
68 global compiler_conditional_xfail_data
70 # Set up the options for compiling this file.
72 lappend options "additional_flags=$optfile $optall"
74 set compiler_conditional_xfail_data $xfaildata
75 set comp_output [${tool}_target_compile "$source" "$dest" object $options]
76 ${tool}_check_compile "$testcase $dest compile" $optstr $dest $comp_output
79 # compat-run -- link and run an executable
81 # TESTNAME is the mixture of object files to link
82 # OBJLIST is the list of object files to link
83 # DEST is the name of the executable
84 # OPTALL is a list of compiler and linker options to use for all tests
85 # OPTFILE is a list of compiler and linker options to use for this test
86 # OPTSTR is the list of options to list in messages
88 proc compat-run { testname objlist dest optall optfile optstr } {
92 # Check that all of the objects were built successfully.
93 foreach obj [split $objlist] {
94 if ![file exists $obj] then {
95 unresolved "$testcase $testname link $optstr"
96 unresolved "$testcase $testname execute $optstr"
101 # Set up the options for linking this test.
103 lappend options "additional_flags=$optfile $optall"
105 # Link the objects into an executable.
106 set comp_output [${tool}_target_compile "$objlist" $dest executable \
108 if ![${tool}_check_compile "$testcase $testname link" "" \
109 $dest $comp_output] then {
110 unresolved "$testcase $testname execute $optstr"
114 # Run the self-checking executable.
115 if ![string match "*/*" $dest] then {
118 set result [${tool}_load $dest "" ""]
119 set status [lindex $result 0]
120 if { $status == "pass" } then {
121 remote_file build delete $dest
123 $status "$testcase $testname execute $optstr"
127 # compat-get-options -- get special tool flags to use for a source file
129 # SRC is the full patchname of the source file.
130 # The result is a list of options to use.
132 # This code is copied from proc dg-test in dg.exp from DejaGNU.
134 proc compat-get-options { src } {
135 # Define our own special function `unknown` so we catch spelling errors.
136 # But first rename the existing one so we can restore it afterwards.
137 catch {rename dg-save-unknown ""}
138 rename unknown dg-save-unknown
139 proc unknown { args } {
140 return -code error "unknown dg option: $args"
143 # dg-options sets a variable called dg-extra-tool-flags.
144 set dg-extra-tool-flags ""
146 # dg-xfail-if sets compiler_conditional_xfail_data.
147 global compiler_conditional_xfail_data
148 set compiler_conditional_xfail_data ""
150 set tmp [dg-get-options $src]
152 set cmd [lindex $op 0]
153 if { ![string compare "dg-options" $cmd] || ![string compare "dg-xfail-if" $cmd] } {
154 set status [catch "$op" errmsg]
155 if { $status != 0 } {
156 perror "src: $errmsg for \"$op\"\n"
157 unresolved "$src: $errmsg for \"$op\""
161 # Ignore unrecognized dg- commands, but warn about them.
162 warning "compat.exp does not support $cmd"
166 # Restore normal error handling.
168 rename dg-save-unknown unknown
170 return ${dg-extra-tool-flags}
174 # compat-execute -- compile with compatible compilers
176 # SRC1 is the full pathname of the main file of the testcase.
177 # SID identifies a test suite in the names of temporary files.
178 # USE_ALT is nonzero if we're using an alternate compiler as well as
179 # the compiler under test.
181 proc compat-execute { src1 sid use_alt } {
188 global compiler_conditional_xfail_data
190 # Set up the names of the other source files.
191 regsub "_main.*" $src1 "" base
192 regsub ".*/" $base "" base
193 regsub "_main" $src1 "_x" src2
194 regsub "_main" $src1 "_y" src3
196 # Use the dg-options mechanism to specify extra flags for this test.
197 # The extra flags in each file are used to compile that file, and the
198 # extra flags in *_main.* are also used for linking.
199 set extra_flags_1 [compat-get-options $src1]
200 set extra_flags_2 [compat-get-options $src2]
201 set compile_xfail_2 $compiler_conditional_xfail_data
202 set extra_flags_3 [compat-get-options $src3]
203 set compile_xfail_3 $compiler_conditional_xfail_data
205 # Define the names of the object files.
206 regsub "sid" "sid_main_tst.o" $sid obj1
207 regsub "sid" "sid_x_tst.o" $sid obj2_tst
208 regsub "sid" "sid_x_alt.o" $sid obj2_alt
209 regsub "sid" "sid_y_tst.o" $sid obj3_tst
210 regsub "sid" "sid_y_alt.o" $sid obj3_alt
212 # Get the base name of this test, for use in messages.
213 regsub "^$srcdir/?" $src1 "" testcase
214 regsub "_main.*" $testcase "" testcase
215 # Set up the base name of executable files so they'll be unique.
216 regsub -all "\[./\]" $testcase "-" execbase
218 # If we couldn't rip $srcdir out of `src1' then just do the best we can.
219 # The point is to reduce the unnecessary noise in the logs. Don't strip
220 # out too much because different testcases with the same name can confuse
222 if [string match "/*" $testcase] then {
223 set testcase "[file tail [file dirname $src1]]/[file tail $src1]"
226 # Loop through all of the option lists used for this test.
229 foreach option_pair $option_list {
231 # Pick out each set of options.
232 set tst_option [lindex $option_pair 0]
233 set alt_option [lindex $option_pair 1]
235 if { ![string match $tst_option ""] \
236 || ![string match $alt_option ""] } then {
237 set optstr "\"$tst_option\",\"$alt_option\""
239 verbose "Testing $testcase, $optstr" 1
241 # There's a unique name for each executable we generate, based on
242 # the set of options and how the pieces of the tests are compiled.
243 set execname1 "${execbase}-${count}1"
244 set execname2 "${execbase}-${count}2"
245 set execname3 "${execbase}-${count}3"
246 set execname4 "${execbase}-${count}4"
249 remote_file build delete $execname1
250 remote_file build delete $execname2
251 remote_file build delete $execname3
252 remote_file build delete $execname4
254 # Compile pieces with the alternate compiler; we'll catch problems
255 # later. Skip this if we don't have an alternate compiler.
256 if { $use_alt != 0 } then {
257 compat-use-alt-compiler
258 compat-obj "$src2" "$obj2_alt" $alt_option $extra_flags_2 $optstr $compile_xfail_2
259 compat-obj "$src3" "$obj3_alt" $alt_option $extra_flags_3 $optstr $compile_xfail_3
262 # Compile pieces with the compiler under test.
263 compat-use-tst-compiler
264 compat-obj "$src1" "$obj1" $tst_option $extra_flags_1 $optstr ""
265 compat-obj "$src2" "$obj2_tst" $tst_option $extra_flags_2 $optstr $compile_xfail_2
266 compat-obj "$src3" "$obj3_tst" $tst_option $extra_flags_3 $optstr $compile_xfail_3
268 # Link (using the compiler under test), run, and clean up tests.
269 compat-run "${obj2_tst}-${obj3_tst}" \
270 "$obj1 $obj2_tst $obj3_tst" $execname1 \
271 $tst_option $extra_flags_1 $optstr
273 # If we've got an alternate compiler try some combinations.
274 if { $use_alt != 0 } then {
275 compat-run "${obj2_tst}-${obj3_alt}" "$obj1 $obj2_tst $obj3_alt" \
276 $execname2 $tst_option $extra_flags_1 $optstr
277 compat-run "${obj2_alt}-${obj3_tst}" "$obj1 $obj2_alt $obj3_tst" \
278 $execname3 $tst_option $extra_flags_1 $optstr
279 compat-run "${obj2_alt}-${obj3_alt}" "$obj1 $obj2_alt $obj3_alt" \
280 $execname4 $tst_option $extra_flags_1 $optstr
283 # Clean up object files.
284 set files [glob -nocomplain ${sid}_*.o]
285 if { $files != "" } {
286 foreach objfile $files {
287 if { ![info exists gluefile] || $objfile != $gluefile } {
288 eval "remote_file build delete $objfile"