1#   Copyright (C) 2003, 2005, 2008, 2009, 2010, 2011, 2014 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 3 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; see the file COPYING3.  If not see
15# <http://www.gnu.org/licenses/>.
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    global compiler_vendor
104
105    set blddirffi [pwd]/..
106    verbose "libffi $blddirffi"
107
108    # Which compiler are we building with?
109    set tmp [grep ../config.log "^ax_cv_c_compiler_vendor.*$"]
110    regexp -- {^[^=]*=(.*)$} $tmp nil compiler_vendor
111
112    if { [string match $compiler_vendor "gnu"] } {
113        set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
114        if {$gccdir != ""} {
115	    set gccdir [file dirname $gccdir]
116        }
117        verbose "gccdir $gccdir"
118
119        set ld_library_path "."
120        append ld_library_path ":${gccdir}"
121
122        set compiler "${gccdir}/xgcc"
123        if { [is_remote host] == 0 && [which $compiler] != 0 } {
124	    foreach i "[exec $compiler --print-multi-lib]" {
125	        set mldir ""
126	        regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
127	        set mldir [string trimright $mldir "\;@"]
128	        if { "$mldir" == "." } {
129		    continue
130	        }
131	        if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
132		    append ld_library_path ":${gccdir}/${mldir}"
133	        }
134	    }
135        }
136    }
137
138    # add the library path for libffi.
139    append ld_library_path ":${blddirffi}/.libs"
140
141    verbose "ld_library_path: $ld_library_path"
142
143    # Point to the Libffi headers in libffi.
144    set libffi_include "${blddirffi}/include"
145    verbose "libffi_include $libffi_include"
146
147    set libffi_dir  "${blddirffi}/.libs"
148    verbose "libffi_dir $libffi_dir"
149    if { $libffi_dir != "" } {
150	set libffi_dir [file dirname ${libffi_dir}]
151	set libffi_link_flags "-L${libffi_dir}/.libs"
152    }
153
154    set_ld_library_path_env_vars
155    libffi_maybe_build_wrapper "${objdir}/testglue.o"
156}
157
158proc libffi_exit { } {
159    global gluefile;
160
161    if [info exists gluefile] {
162	file_on_build delete $gluefile;
163	unset gluefile;
164    }
165}
166
167proc libffi_target_compile { source dest type options } {
168    global gluefile wrap_flags;
169    global srcdir
170    global blddirffi
171    global TOOL_OPTIONS
172    global libffi_link_flags
173    global libffi_include
174    global target_triplet
175
176
177    if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
178	lappend options "libs=${gluefile}"
179	lappend options "ldflags=$wrap_flags"
180    }
181
182    # TOOL_OPTIONS must come first, so that it doesn't override testcase
183    # specific options.
184    if [info exists TOOL_OPTIONS] {
185	lappend  options [concat "additional_flags=$TOOL_OPTIONS" $options];
186    }
187
188    # search for ffi_mips.h in srcdir, too
189    lappend options "additional_flags=-I${libffi_include} -I${srcdir}/../include  -I${libffi_include}/.."
190    lappend options "additional_flags=${libffi_link_flags}"
191
192    # Darwin needs a stack execution allowed flag.
193
194    if { [istarget "*-*-darwin9*"] || [istarget "*-*-darwin1*"]
195	 || [istarget "*-*-darwin2*"] } {
196	lappend options "additional_flags=-Wl,-allow_stack_execute"
197    }
198
199    # If you're building the compiler with --prefix set to a place
200    # where it's not yet installed, then the linker won't be able to
201    # find the libgcc used by libffi.dylib.  We could pass the
202    # -dylib_file option, but that's complicated, and it's much easier
203    # to just make the linker find libgcc using -L options.
204    if { [string match "*-*-darwin*" $target_triplet] } {
205	lappend options "libs= -shared-libgcc"
206    }
207
208    if { [string match "*-*-openbsd*" $target_triplet] } {
209	lappend options "libs= -lpthread"
210    }
211
212    lappend options "libs= -lffi"
213
214    if { [string match "aarch64*-*-linux*" $target_triplet] } {
215	lappend options "libs= -lpthread"
216    }
217
218    if { [string match "*.cc" $source] } {
219	lappend options "c++"
220    }
221
222    verbose "options: $options"
223    return [target_compile $source $dest $type $options]
224}
225
226# Utility routines.
227
228#
229# search_for -- looks for a string match in a file
230#
231proc search_for { file pattern } {
232    set fd [open $file r]
233    while { [gets $fd cur_line]>=0 } {
234	if [string match "*$pattern*" $cur_line] then {
235	    close $fd
236	    return 1
237	}
238    }
239    close $fd
240    return 0
241}
242
243# Modified dg-runtest that can cycle through a list of optimization options
244# as c-torture does.
245proc libffi-dg-runtest { testcases default-extra-flags } {
246    global runtests
247
248    foreach test $testcases {
249	# If we're only testing specific files and this isn't one of
250	# them, skip it.
251	if ![runtest_file_p $runtests $test] {
252	    continue
253	}
254
255	# Look for a loop within the source code - if we don't find one,
256	# don't pass -funroll[-all]-loops.
257	global torture_with_loops torture_without_loops
258	if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
259	    set option_list $torture_with_loops
260	} else {
261	    set option_list $torture_without_loops
262	}
263
264	set nshort [file tail [file dirname $test]]/[file tail $test]
265
266	foreach flags $option_list {
267	    verbose "Testing $nshort, $flags" 1
268	    dg-test $test $flags ${default-extra-flags}
269	}
270    }
271}
272
273proc run-many-tests { testcases extra_flags } {
274    global compiler_vendor
275    switch $compiler_vendor {
276      "clang" {
277	set common "-W -Wall"
278	set optimizations { "-O0" "-O1" "-O2" "-O3" "-Os" }
279      }
280      "gnu" {
281        set common "-W -Wall -Wno-psabi"
282        set optimizations { "-O0" "-O2" "-O3" "-Os" "-O2 -fomit-frame-pointer" }
283      }
284      default {
285        # Assume we are using the vendor compiler.
286        set common ""
287        set optimizations { "" }
288      }
289    }
290
291    set targetabis { "" }
292    if [string match $compiler_vendor "gnu"] {
293        if [istarget "i?86-*-*"] {
294            set targetabis {
295                ""
296                "-DABI_NUM=FFI_STDCALL -DABI_ATTR=__STDCALL__"
297                "-DABI_NUM=FFI_THISCALL -DABI_ATTR=__THISCALL__"
298                "-DABI_NUM=FFI_FASTCALL -DABI_ATTR=__FASTCALL__"
299            }
300        }
301    }
302
303    set common [ concat $common $extra_flags ]
304    foreach test $testcases {
305        set testname [file tail $test]
306        if [search_for $test "ABI_NUM"] {
307            set abis $targetabis
308        } else {
309            set abis { "" }
310        }
311        foreach opt $optimizations {
312            foreach abi $abis {
313                set options [concat $common $opt $abi]
314                verbose "Testing $testname, $options" 1
315                dg-test $test $options ""
316            }
317        }
318    }
319}
320
321# Like check_conditional_xfail, but callable from a dg test.
322
323proc dg-xfail-if { args } {
324    set args [lreplace $args 0 0]
325    set selector "target [join [lindex $args 1]]"
326    if { [dg-process-target $selector] == "S" } {
327	global compiler_conditional_xfail_data
328	set compiler_conditional_xfail_data $args
329    }
330}
331
332proc check-flags { args } {
333
334    # The args are within another list; pull them out.
335    set args [lindex $args 0]
336
337    # The next two arguments are optional.  If they were not specified,
338    # use the defaults.
339    if { [llength $args] == 2 } {
340	lappend $args [list "*"]
341    }
342    if { [llength $args] == 3 } {
343	lappend $args [list ""]
344    }
345
346    # If the option strings are the defaults, or the same as the
347    # defaults, there is no need to call check_conditional_xfail to
348    # compare them to the actual options.
349    if { [string compare [lindex $args 2] "*"] == 0
350	 && [string compare [lindex $args 3] "" ] == 0 } {
351	set result 1
352    } else {
353	# The target list might be an effective-target keyword, so replace
354	# the original list with "*-*-*", since we already know it matches.
355	set result [check_conditional_xfail [lreplace $args 1 1 "*-*-*"]]
356    }
357
358    return $result
359}
360
361proc dg-skip-if { args } {
362    # Verify the number of arguments.  The last two are optional.
363    set args [lreplace $args 0 0]
364    if { [llength $args] < 2 || [llength $args] > 4 } {
365        error "dg-skip-if 2: need 2, 3, or 4 arguments"
366    }
367
368    # Don't bother if we're already skipping the test.
369    upvar dg-do-what dg-do-what
370    if { [lindex ${dg-do-what} 1] == "N" } {
371      return
372    }
373
374    set selector [list target [lindex $args 1]]
375    if { [dg-process-target $selector] == "S" } {
376        if [check-flags $args] {
377            upvar dg-do-what dg-do-what
378            set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
379        }
380    }
381}
382
383# We need to make sure that additional_files and additional_sources
384# are both cleared out after every test.  It is not enough to clear
385# them out *before* the next test run because gcc-target-compile gets
386# run directly from some .exp files (outside of any test).  (Those
387# uses should eventually be eliminated.)
388
389# Because the DG framework doesn't provide a hook that is run at the
390# end of a test, we must replace dg-test with a wrapper.
391
392if { [info procs saved-dg-test] == [list] } {
393    rename dg-test saved-dg-test
394
395    proc dg-test { args } {
396	global additional_files
397	global additional_sources
398	global errorInfo
399
400	if { [ catch { eval saved-dg-test $args } errmsg ] } {
401	    set saved_info $errorInfo
402	    set additional_files ""
403	    set additional_sources ""
404	    error $errmsg $saved_info
405	}
406	set additional_files ""
407	set additional_sources ""
408    }
409}
410
411# Local Variables:
412# tcl-indent-level:4
413# End:
414