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
104    global using_gcc
105
106    set blddirffi [pwd]/..
107    verbose "libffi $blddirffi"
108
109    # Are we building with GCC?
110    set tmp [grep ../config.status "GCC='yes'"]
111    if { [string match $tmp "GCC='yes'"] } {
112
113      set using_gcc "yes"
114
115    set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
116    if {$gccdir != ""} {
117	set gccdir [file dirname $gccdir]
118    }
119    verbose "gccdir $gccdir"
120
121    set ld_library_path "."
122    append ld_library_path ":${gccdir}"
123
124    set compiler "${gccdir}/xgcc"
125    if { [is_remote host] == 0 && [which $compiler] != 0 } {
126	foreach i "[exec $compiler --print-multi-lib]" {
127	    set mldir ""
128	    regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
129	    set mldir [string trimright $mldir "\;@"]
130	    if { "$mldir" == "." } {
131		continue
132	    }
133	    if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
134		append ld_library_path ":${gccdir}/${mldir}"
135	    }
136	}
137    }
138
139    } else {
140
141      set using_gcc "no"
142
143    }
144
145    # add the library path for libffi.
146    append ld_library_path ":${blddirffi}/.libs"
147
148    verbose "ld_library_path: $ld_library_path"
149
150    # Point to the Libffi headers in libffi.
151    set libffi_include "${blddirffi}/include"
152    verbose "libffi_include $libffi_include"
153
154    set libffi_dir  "${blddirffi}/.libs"
155    verbose "libffi_dir $libffi_dir"
156    if { $libffi_dir != "" } {
157	set libffi_dir [file dirname ${libffi_dir}]
158	set libffi_link_flags "-L${libffi_dir}/.libs"
159    }
160
161    set_ld_library_path_env_vars
162    libffi_maybe_build_wrapper "${objdir}/testglue.o"
163}
164
165proc libffi_exit { } {
166    global gluefile;
167
168    if [info exists gluefile] {
169	file_on_build delete $gluefile;
170	unset gluefile;
171    }
172}
173
174proc libffi_target_compile { source dest type options } {
175    global gluefile wrap_flags;
176    global srcdir
177    global blddirffi
178    global TOOL_OPTIONS
179    global libffi_link_flags
180    global libffi_include
181    global target_triplet
182
183
184    if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
185	lappend options "libs=${gluefile}"
186	lappend options "ldflags=$wrap_flags"
187    }
188
189    # TOOL_OPTIONS must come first, so that it doesn't override testcase
190    # specific options.
191    if [info exists TOOL_OPTIONS] {
192	lappend  options [concat "additional_flags=$TOOL_OPTIONS" $options];
193    }
194
195    # search for ffi_mips.h in srcdir, too
196    lappend options "additional_flags=-I${libffi_include} -I${srcdir}/../include  -I${libffi_include}/.."
197    lappend options "additional_flags=${libffi_link_flags}"
198
199    # Darwin needs a stack execution allowed flag.
200
201    if { [istarget "*-*-darwin9*"] || [istarget "*-*-darwin1*"]
202	 || [istarget "*-*-darwin2*"] } {
203	lappend options "additional_flags=-Wl,-allow_stack_execute"
204    }
205
206    # If you're building the compiler with --prefix set to a place
207    # where it's not yet installed, then the linker won't be able to
208    # find the libgcc used by libffi.dylib.  We could pass the
209    # -dylib_file option, but that's complicated, and it's much easier
210    # to just make the linker find libgcc using -L options.
211    if { [string match "*-*-darwin*" $target_triplet] } {
212	lappend options "libs= -shared-libgcc"
213    }
214
215    if { [string match "*-*-openbsd*" $target_triplet] } {
216	lappend options "libs= -lpthread"
217    }
218
219    lappend options "libs= -lffi"
220
221    if { [string match "aarch64*-*-linux*" $target_triplet] } {
222	lappend options "libs= -lpthread"
223    }
224
225    if { [string match "*.cc" $source] } {
226	lappend options "c++"
227    }
228
229    verbose "options: $options"
230    return [target_compile $source $dest $type $options]
231}
232
233# Utility routines.
234
235#
236# search_for -- looks for a string match in a file
237#
238proc search_for { file pattern } {
239    set fd [open $file r]
240    while { [gets $fd cur_line]>=0 } {
241	if [string match "*$pattern*" $cur_line] then {
242	    close $fd
243	    return 1
244	}
245    }
246    close $fd
247    return 0
248}
249
250# Modified dg-runtest that can cycle through a list of optimization options
251# as c-torture does.
252proc libffi-dg-runtest { testcases default-extra-flags } {
253    global runtests
254
255    foreach test $testcases {
256	# If we're only testing specific files and this isn't one of
257	# them, skip it.
258	if ![runtest_file_p $runtests $test] {
259	    continue
260	}
261
262	# Look for a loop within the source code - if we don't find one,
263	# don't pass -funroll[-all]-loops.
264	global torture_with_loops torture_without_loops
265	if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
266	    set option_list $torture_with_loops
267	} else {
268	    set option_list $torture_without_loops
269	}
270
271	set nshort [file tail [file dirname $test]]/[file tail $test]
272
273	foreach flags $option_list {
274	    verbose "Testing $nshort, $flags" 1
275	    dg-test $test $flags ${default-extra-flags}
276	}
277    }
278}
279
280proc run-many-tests { testcases extra_flags } {
281    global using_gcc
282    if { [string match $using_gcc "yes"] } {
283        set common "-W -Wall -Wno-psabi"
284        set optimizations { "-O0" "-O2" "-O3" "-Os" "-O2 -fomit-frame-pointer" }
285    } else {
286        # Assume we are using the vendor compiler.
287        set common ""
288        set optimizations { "" }
289    }
290
291    set targetabis { "" }
292    if [string match $using_gcc "yes"] {
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