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 loaded_libs
19
20    load_file $srcdir/../../gcc/testsuite/lib/$filename
21    set loaded_libs($filename) ""
22}
23
24load_lib dg.exp
25load_lib libgloss.exp
26load_gcc_lib target-supports.exp
27load_gcc_lib target-supports-dg.exp
28load_gcc_lib target-libpath.exp
29load_gcc_lib wrapper.exp
30
31
32# Define libffi callbacks for dg.exp.
33
34proc libffi-dg-test-1 { target_compile prog do_what extra_tool_flags } {
35
36    # To get all \n in dg-output test strings to match printf output
37    # in a system that outputs it as \015\012 (i.e. not just \012), we
38    # need to change all \n into \r?\n.  As there is no dejagnu flag
39    # or hook to do that, we simply change the text being tested.
40    # Unfortunately, we have to know that the variable is called
41    # dg-output-text and lives in the caller of libffi-dg-test, which
42    # is two calls up.  Overriding proc dg-output would be longer and
43    # would necessarily have the same assumption.
44    upvar 2 dg-output-text output_match
45
46    if { [llength $output_match] > 1 } {
47	regsub -all "\n" [lindex $output_match 1] "\r?\n" x
48	set output_match [lreplace $output_match 1 1 $x]
49    }
50
51    # Set up the compiler flags, based on what we're going to do.
52
53    set options [list]
54    switch $do_what {
55	"compile" {
56	    set compile_type "assembly"
57	    set output_file "[file rootname [file tail $prog]].s"
58	}
59	"link" {
60	    set compile_type "executable"
61	    set output_file "[file rootname [file tail $prog]].exe"
62	    # The following line is needed for targets like the i960 where
63	    # the default output file is b.out.  Sigh.
64	}
65	"run" {
66	    set compile_type "executable"
67	    # FIXME: "./" is to cope with "." not being in $PATH.
68	    # Should this be handled elsewhere?
69	    # YES.
70	    set output_file "./[file rootname [file tail $prog]].exe"
71	    # This is the only place where we care if an executable was
72	    # created or not.  If it was, dg.exp will try to run it.
73	    remote_file build delete $output_file;
74	}
75	default {
76	    perror "$do_what: not a valid dg-do keyword"
77	    return ""
78	}
79    }
80
81    if { $extra_tool_flags != "" } {
82	lappend options "additional_flags=$extra_tool_flags"
83    }
84
85    set comp_output [libffi_target_compile "$prog" "$output_file" "$compile_type" $options];
86
87
88    return [list $comp_output $output_file]
89}
90
91
92proc libffi-dg-test { prog do_what extra_tool_flags } {
93    return [libffi-dg-test-1 target_compile $prog $do_what $extra_tool_flags]
94}
95
96proc libffi-init { args } {
97    global gluefile wrap_flags;
98    global srcdir
99    global blddirffi
100    global objdir
101    global blddircxx
102    global TOOL_OPTIONS
103    global tool
104    global libffi_include
105    global libffi_link_flags
106    global tool_root_dir
107    global ld_library_path
108    global compiler_vendor
109
110    set blddirffi [lookfor_file [get_multilibs] libffi]
111    verbose "libffi $blddirffi"
112    set blddircxx [lookfor_file [get_multilibs] libstdc++-v3]
113    verbose "libstdc++ $blddircxx"
114
115    set compiler_vendor "gnu"
116
117    set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
118    if {$gccdir != ""} {
119	set gccdir [file dirname $gccdir]
120    }
121    verbose "gccdir $gccdir"
122
123    set ld_library_path "."
124    append ld_library_path ":${gccdir}"
125
126    set compiler "${gccdir}/xgcc"
127    if { [is_remote host] == 0 && [which $compiler] != 0 } {
128	foreach i "[exec $compiler --print-multi-lib]" {
129	    set mldir ""
130	    regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
131	    set mldir [string trimright $mldir "\;@"]
132	    if { "$mldir" == "." } {
133		continue
134	    }
135	    if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
136		append ld_library_path ":${gccdir}/${mldir}"
137	    }
138	}
139    }
140
141    # add the library path for libffi.
142    append ld_library_path ":${blddirffi}/.libs"
143    # add the library path for libstdc++ as well.
144    append ld_library_path ":${blddircxx}/src/.libs"
145
146    verbose "ld_library_path: $ld_library_path"
147
148    # Point to the Libffi headers in libffi.
149    set libffi_include "${blddirffi}/include"
150    verbose "libffi_include $libffi_include"
151
152    set libffi_dir  "${blddirffi}/.libs"
153    verbose "libffi_dir $libffi_dir"
154    if { $libffi_dir != "" } {
155	set libffi_dir [file dirname ${libffi_dir}]
156	set libffi_link_flags "-L${libffi_dir}/.libs"
157	lappend libffi_link_flags "-L${blddircxx}/src/.libs"
158    }
159
160    set_ld_library_path_env_vars
161    libffi_maybe_build_wrapper "${objdir}/testglue.o"
162}
163
164proc libffi_exit { } {
165    global gluefile;
166
167    if [info exists gluefile] {
168	file_on_build delete $gluefile;
169	unset gluefile;
170    }
171}
172
173proc libffi_target_compile { source dest type options } {
174    global gluefile wrap_flags;
175    global srcdir
176    global blddirffi
177    global TOOL_OPTIONS
178    global libffi_link_flags
179    global libffi_include
180    global target_triplet
181
182
183    if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
184	lappend options "libs=${gluefile}"
185	lappend options "ldflags=$wrap_flags"
186    }
187
188    # TOOL_OPTIONS must come first, so that it doesn't override testcase
189    # specific options.
190    if [info exists TOOL_OPTIONS] {
191	lappend  options [concat "additional_flags=$TOOL_OPTIONS" $options];
192    }
193
194    # search for ffi_mips.h in srcdir, too
195    lappend options "additional_flags=-I${libffi_include} -I${srcdir}/../include  -I${libffi_include}/.."
196    lappend options "additional_flags=${libffi_link_flags}"
197
198    # Darwin needs a stack execution allowed flag.
199
200    if { [istarget "*-*-darwin9*"] || [istarget "*-*-darwin1*"]
201	 || [istarget "*-*-darwin2*"] } {
202	lappend options "additional_flags=-Wl,-allow_stack_execute"
203    }
204
205    # If you're building the compiler with --prefix set to a place
206    # where it's not yet installed, then the linker won't be able to
207    # find the libgcc used by libffi.dylib.  We could pass the
208    # -dylib_file option, but that's complicated, and it's much easier
209    # to just make the linker find libgcc using -L options.
210    if { [string match "*-*-darwin*" $target_triplet] } {
211	lappend options "libs= -shared-libgcc"
212    }
213
214    if { [string match "*-*-openbsd*" $target_triplet] } {
215	lappend options "libs= -lpthread"
216    }
217
218    lappend options "libs= -lffi"
219
220    if { [string match "aarch64*-*-linux*" $target_triplet] } {
221	lappend options "libs= -lpthread"
222    }
223
224    verbose "options: $options"
225    return [target_compile $source $dest $type $options]
226}
227
228# TEST should be a preprocessor condition.  Returns true if it holds.
229proc libffi_feature_test { test } {
230    set src "ffitest[pid].c"
231
232    set f [open $src "w"]
233    puts $f "#include <ffi.h>"
234    puts $f $test
235    puts $f "/* OK */"
236    puts $f "#else"
237    puts $f "# error Failed $test"
238    puts $f "#endif"
239    close $f
240
241    set lines [libffi_target_compile $src /dev/null assembly ""]
242    file delete $src
243
244    return [string match "" $lines]
245}
246
247# Utility routines.
248
249#
250# search_for -- looks for a string match in a file
251#
252proc search_for { file pattern } {
253    set fd [open $file r]
254    while { [gets $fd cur_line]>=0 } {
255	if [string match "*$pattern*" $cur_line] then {
256	    close $fd
257	    return 1
258	}
259    }
260    close $fd
261    return 0
262}
263
264# Modified dg-runtest that can cycle through a list of optimization options
265# as c-torture does.
266proc libffi-dg-runtest { testcases default-extra-flags } {
267    global runtests
268
269    foreach test $testcases {
270	# If we're only testing specific files and this isn't one of
271	# them, skip it.
272	if ![runtest_file_p $runtests $test] {
273	    continue
274	}
275
276	# Look for a loop within the source code - if we don't find one,
277	# don't pass -funroll[-all]-loops.
278	global torture_with_loops torture_without_loops
279	if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
280	    set option_list $torture_with_loops
281	} else {
282	    set option_list $torture_without_loops
283	}
284
285	set nshort [file tail [file dirname $test]]/[file tail $test]
286
287	foreach flags $option_list {
288	    verbose "Testing $nshort, $flags" 1
289	    dg-test $test $flags ${default-extra-flags}
290	}
291    }
292}
293
294proc run-many-tests { testcases extra_flags } {
295    global compiler_vendor
296    switch $compiler_vendor {
297      "clang" {
298	set common "-W -Wall"
299	set optimizations { "-O0" "-O1" "-O2" "-O3" "-Os" }
300      }
301      "gnu" {
302        set common "-W -Wall -Wno-psabi"
303        set optimizations { "-O0" "-O2" "-O3" "-Os" "-O2 -fomit-frame-pointer" }
304      }
305      default {
306        # Assume we are using the vendor compiler.
307        set common ""
308        set optimizations { "" }
309      }
310    }
311
312    set targetabis { "" }
313    if [string match $compiler_vendor "gnu"] {
314        if { ([istarget "i?86-*-*"] || [istarget "x86_64-*-*"])
315	     && [is-effective-target ia32] && ![istarget "*-*-darwin*"] } {
316            set targetabis {
317                ""
318                "-DABI_NUM=FFI_STDCALL -DABI_ATTR=__STDCALL__"
319                "-DABI_NUM=FFI_THISCALL -DABI_ATTR=__THISCALL__"
320                "-DABI_NUM=FFI_FASTCALL -DABI_ATTR=__FASTCALL__"
321            }
322        }
323    }
324
325    set common [ concat $common $extra_flags ]
326    foreach test $testcases {
327        set testname [file tail $test]
328        if [search_for $test "ABI_NUM"] {
329            set abis $targetabis
330        } else {
331            set abis { "" }
332        }
333        foreach opt $optimizations {
334            foreach abi $abis {
335                set options [concat $common $opt $abi]
336                verbose "Testing $testname, $options" 1
337                dg-test $test $options ""
338            }
339        }
340    }
341}
342
343# Like check_conditional_xfail, but callable from a dg test.
344
345proc dg-xfail-if { args } {
346    set args [lreplace $args 0 0]
347    set selector "target [join [lindex $args 1]]"
348    if { [dg-process-target $selector] == "S" } {
349	global compiler_conditional_xfail_data
350	set compiler_conditional_xfail_data $args
351    }
352}
353
354proc check-flags { args } {
355
356    # The args are within another list; pull them out.
357    set args [lindex $args 0]
358
359    # The next two arguments are optional.  If they were not specified,
360    # use the defaults.
361    if { [llength $args] == 2 } {
362	lappend $args [list "*"]
363    }
364    if { [llength $args] == 3 } {
365	lappend $args [list ""]
366    }
367
368    # If the option strings are the defaults, or the same as the
369    # defaults, there is no need to call check_conditional_xfail to
370    # compare them to the actual options.
371    if { [string compare [lindex $args 2] "*"] == 0
372	 && [string compare [lindex $args 3] "" ] == 0 } {
373	set result 1
374    } else {
375	# The target list might be an effective-target keyword, so replace
376	# the original list with "*-*-*", since we already know it matches.
377	set result [check_conditional_xfail [lreplace $args 1 1 "*-*-*"]]
378    }
379
380    return $result
381}
382
383proc dg-skip-if { args } {
384    # Verify the number of arguments.  The last two are optional.
385    set args [lreplace $args 0 0]
386    if { [llength $args] < 2 || [llength $args] > 4 } {
387        error "dg-skip-if 2: need 2, 3, or 4 arguments"
388    }
389
390    # Don't bother if we're already skipping the test.
391    upvar dg-do-what dg-do-what
392    if { [lindex ${dg-do-what} 1] == "N" } {
393      return
394    }
395
396    set selector [list target [lindex $args 1]]
397    if { [dg-process-target $selector] == "S" } {
398        if [check-flags $args] {
399            upvar dg-do-what dg-do-what
400            set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
401        }
402    }
403}
404
405# We need to make sure that additional_files and additional_sources
406# are both cleared out after every test.  It is not enough to clear
407# them out *before* the next test run because gcc-target-compile gets
408# run directly from some .exp files (outside of any test).  (Those
409# uses should eventually be eliminated.)
410
411# Because the DG framework doesn't provide a hook that is run at the
412# end of a test, we must replace dg-test with a wrapper.
413
414if { [info procs saved-dg-test] == [list] } {
415    rename dg-test saved-dg-test
416
417    proc dg-test { args } {
418	global additional_files
419	global additional_sources
420	global errorInfo
421
422	if { [ catch { eval saved-dg-test $args } errmsg ] } {
423	    set saved_info $errorInfo
424	    set additional_files ""
425	    set additional_sources ""
426	    error $errmsg $saved_info
427	}
428	set additional_files ""
429	set additional_sources ""
430    }
431}
432
433# Local Variables:
434# tcl-indent-level:4
435# End:
436