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