1#   Copyright (C) 2003, 2005, 2008 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
17proc load_gcc_lib { filename } {
18    global srcdir
19    load_file $srcdir/lib/$filename
20}
21
22load_lib dg.exp
23load_lib libgloss.exp
24load_gcc_lib target-libpath.exp
25load_gcc_lib wrapper.exp
26
27
28# Define libffi callbacks for dg.exp.
29
30proc libffi-dg-test-1 { target_compile prog do_what extra_tool_flags } {
31
32    # To get all \n in dg-output test strings to match printf output
33    # in a system that outputs it as \015\012 (i.e. not just \012), we
34    # need to change all \n into \r?\n.  As there is no dejagnu flag
35    # or hook to do that, we simply change the text being tested.
36    # Unfortunately, we have to know that the variable is called
37    # dg-output-text and lives in the caller of libffi-dg-test, which
38    # is two calls up.  Overriding proc dg-output would be longer and
39    # would necessarily have the same assumption.
40    upvar 2 dg-output-text output_match
41
42    if { [llength $output_match] > 1 } {
43	regsub -all "\n" [lindex $output_match 1] "\r?\n" x
44	set output_match [lreplace $output_match 1 1 $x]
45    }
46
47    # Set up the compiler flags, based on what we're going to do.
48
49    set options [list]
50    switch $do_what {
51	"compile" {
52	    set compile_type "assembly"
53	    set output_file "[file rootname [file tail $prog]].s"
54	}
55	"link" {
56	    set compile_type "executable"
57	    set output_file "[file rootname [file tail $prog]].exe"
58	    # The following line is needed for targets like the i960 where
59	    # the default output file is b.out.  Sigh.
60	}
61	"run" {
62	    set compile_type "executable"
63	    # FIXME: "./" is to cope with "." not being in $PATH.
64	    # Should this be handled elsewhere?
65	    # YES.
66	    set output_file "./[file rootname [file tail $prog]].exe"
67	    # This is the only place where we care if an executable was
68	    # created or not.  If it was, dg.exp will try to run it.
69	    remote_file build delete $output_file;
70	}
71	default {
72	    perror "$do_what: not a valid dg-do keyword"
73	    return ""
74	}
75    }
76
77    if { $extra_tool_flags != "" } {
78	lappend options "additional_flags=$extra_tool_flags"
79    }
80
81    set comp_output [libffi_target_compile "$prog" "$output_file" "$compile_type" $options];
82
83
84    return [list $comp_output $output_file]
85}
86
87
88proc libffi-dg-test { prog do_what extra_tool_flags } {
89    return [libffi-dg-test-1 target_compile $prog $do_what $extra_tool_flags]
90}
91
92proc libffi-init { args } {
93    global gluefile wrap_flags;
94    global srcdir
95    global blddirffi
96    global objdir
97    global TOOL_OPTIONS
98    global tool
99    global libffi_include
100    global libffi_link_flags
101    global tool_root_dir
102    global ld_library_path
103
104    set blddirffi [pwd]/..
105    verbose "libffi $blddirffi"
106
107    set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
108    if {$gccdir != ""} {
109	set gccdir [file dirname $gccdir]
110    }
111    verbose "gccdir $gccdir"
112
113    set ld_library_path "."
114    append ld_library_path ":${gccdir}"
115
116    set compiler "${gccdir}/xgcc"
117    if { [is_remote host] == 0 && [which $compiler] != 0 } {
118	foreach i "[exec $compiler --print-multi-lib]" {
119	    set mldir ""
120	    regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
121	    set mldir [string trimright $mldir "\;@"]
122	    if { "$mldir" == "." } {
123		continue
124	    }
125	    if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
126		append ld_library_path ":${gccdir}/${mldir}"
127	    }
128	}
129    }
130    # add the library path for libffi.
131    append ld_library_path ":${blddirffi}/.libs"
132
133    verbose "ld_library_path: $ld_library_path"
134
135    # Point to the Libffi headers in libffi.
136    set libffi_include "${blddirffi}/include"
137    verbose "libffi_include $libffi_include"
138
139    set libffi_dir  "${blddirffi}/.libs"
140    verbose "libffi_dir $libffi_dir"
141    if { $libffi_dir != "" } {
142	set libffi_dir [file dirname ${libffi_dir}]
143	set libffi_link_flags "-L${libffi_dir}/.libs"
144    }
145
146    set_ld_library_path_env_vars
147    libffi_maybe_build_wrapper "${objdir}/testglue.o"
148}
149
150proc libffi_exit { } {
151    global gluefile;
152
153    if [info exists gluefile] {
154	file_on_build delete $gluefile;
155	unset gluefile;
156    }
157}
158
159proc libffi_target_compile { source dest type options } {
160    global gluefile wrap_flags;
161    global srcdir
162    global blddirffi
163    global TOOL_OPTIONS
164    global libffi_link_flags
165    global libffi_include
166    global target_triplet
167
168
169    if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
170	lappend options "libs=${gluefile}"
171	lappend options "ldflags=$wrap_flags"
172    }
173
174    # TOOL_OPTIONS must come first, so that it doesn't override testcase
175    # specific options.
176    if [info exists TOOL_OPTIONS] {
177	lappend  options [concat "additional_flags=$TOOL_OPTIONS" $options];
178    }
179
180    # search for ffi_mips.h in srcdir, too
181    lappend options "additional_flags=-I${libffi_include} -I${srcdir}/../include  -I${libffi_include}/.."
182    lappend options "additional_flags=${libffi_link_flags}"
183
184    # If you're building the compiler with --prefix set to a place
185    # where it's not yet installed, then the linker won't be able to
186    # find the libgcc used by libffi.dylib.  We could pass the
187    # -dylib_file option, but that's complicated, and it's much easier
188    # to just make the linker find libgcc using -L options.
189    if { [string match "*-*-darwin*" $target_triplet] } {
190	lappend options "libs= -shared-libgcc"
191    }
192
193    lappend options "libs= -lffi"
194
195    verbose "options: $options"
196    return [target_compile $source $dest $type $options]
197}
198
199# Utility routines.
200
201#
202# search_for -- looks for a string match in a file
203#
204proc search_for { file pattern } {
205    set fd [open $file r]
206    while { [gets $fd cur_line]>=0 } {
207	if [string match "*$pattern*" $cur_line] then {
208	    close $fd
209	    return 1
210	}
211    }
212    close $fd
213    return 0
214}
215
216# Modified dg-runtest that can cycle through a list of optimization options
217# as c-torture does.
218proc libffi-dg-runtest { testcases default-extra-flags } {
219    global runtests
220
221    foreach test $testcases {
222	# If we're only testing specific files and this isn't one of
223	# them, skip it.
224	if ![runtest_file_p $runtests $test] {
225	    continue
226	}
227
228	# Look for a loop within the source code - if we don't find one,
229	# don't pass -funroll[-all]-loops.
230	global torture_with_loops torture_without_loops
231	if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
232	    set option_list $torture_with_loops
233	} else {
234	    set option_list $torture_without_loops
235	}
236
237	set nshort [file tail [file dirname $test]]/[file tail $test]
238
239	foreach flags $option_list {
240	    verbose "Testing $nshort, $flags" 1
241	    dg-test $test $flags ${default-extra-flags}
242	}
243    }
244}
245
246
247# Like check_conditional_xfail, but callable from a dg test.
248
249proc dg-xfail-if { args } {
250    set args [lreplace $args 0 0]
251    set selector "target [join [lindex $args 1]]"
252    if { [dg-process-target $selector] == "S" } {
253	global compiler_conditional_xfail_data
254	set compiler_conditional_xfail_data $args
255    }
256}
257
258
259# We need to make sure that additional_files and additional_sources
260# are both cleared out after every test.  It is not enough to clear
261# them out *before* the next test run because gcc-target-compile gets
262# run directly from some .exp files (outside of any test).  (Those
263# uses should eventually be eliminated.)
264
265# Because the DG framework doesn't provide a hook that is run at the
266# end of a test, we must replace dg-test with a wrapper.
267
268if { [info procs saved-dg-test] == [list] } {
269    rename dg-test saved-dg-test
270
271    proc dg-test { args } {
272	global additional_files
273	global additional_sources
274	global errorInfo
275
276	if { [ catch { eval saved-dg-test $args } errmsg ] } {
277	    set saved_info $errorInfo
278	    set additional_files ""
279	    set additional_sources ""
280	    error $errmsg $saved_info
281	}
282	set additional_files ""
283	set additional_sources ""
284    }
285}
286
287# Local Variables:
288# tcl-indent-level:4
289# End:
290