Debian Codegraph @

testsuite/lib/libffi.exp

raw
1 # Copyright (C) 2003, 2005, 2008, 2009, 2010, 2011, 2014, 2019, 2022, 2025 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
17 proc load_gcc_lib { filename } {
18 global srcdir
19 load_file $srcdir/lib/$filename
20 }
21
22 load_lib dg.exp
23 load_lib libgloss.exp
24 load_gcc_lib target-libpath.exp
25 load_gcc_lib wrapper.exp
26
27 proc check_effective_target_gccbug { } {
28 global has_gccbug
29 return $has_gccbug
30 }
31
32 # Return 1 if the target matches the effective target 'arg', 0 otherwise.
33 # This can be used with any check_* proc that takes no argument and
34 # returns only 1 or 0. It could be used with check_* procs that take
35 # arguments with keywords that pass particular arguments.
36
37 proc is-effective-target { arg } {
38 global et_index
39 set selected 0
40 if { ![info exists et_index] } {
41 # Initialize the effective target index that is used in some
42 # check_effective_target_* procs.
43 set et_index 0
44 }
45 if { [info procs check_effective_target_${arg}] != [list] } {
46 set selected [check_effective_target_${arg}]
47 } else {
48 error "unknown effective target keyword `$arg'"
49 }
50 verbose "is-effective-target: $arg $selected" 2
51 return $selected
52 }
53
54 proc is-effective-target-keyword { arg } {
55 if { [info procs check_effective_target_${arg}] != [list] } {
56 return 1
57 } else {
58 return 0
59 }
60 }
61
62 # Intercept the call to the DejaGnu version of dg-process-target to
63 # support use of an effective-target keyword in place of a list of
64 # target triplets to xfail or skip a test.
65 #
66 # The argument to dg-process-target is the keyword "target" or "xfail"
67 # followed by a selector:
68 # target-triplet-1 ...
69 # effective-target-keyword
70 # selector-expression
71 #
72 # For a target list the result is "S" if the target is selected, "N" otherwise.
73 # For an xfail list the result is "F" if the target is affected, "P" otherwise.
74
75 # In contexts that allow either "target" or "xfail" the argument can be
76 # target selector1 xfail selector2
77 # which returns "N" if selector1 is not selected, otherwise the result of
78 # "xfail selector2".
79 #
80 # A selector expression appears within curly braces and uses a single logical
81 # operator: !, &&, or ||. An operand is another selector expression, an
82 # effective-target keyword, or a list of target triplets within quotes or
83 # curly braces.
84
85 if { [info procs saved-dg-process-target] == [list] } {
86 rename dg-process-target saved-dg-process-target
87
88 # Evaluate an operand within a selector expression.
89 proc selector_opd { op } {
90 set selector "target"
91 lappend selector $op
92 set answer [ expr { [dg-process-target $selector] == "S" } ]
93 verbose "selector_opd: `$op' $answer" 2
94 return $answer
95 }
96
97 # Evaluate a target triplet list within a selector expression.
98 # Unlike other operands, this needs to be expanded from a list to
99 # the same string as "target".
100 proc selector_list { op } {
101 set selector "target [join $op]"
102 set answer [ expr { [dg-process-target $selector] == "S" } ]
103 verbose "selector_list: `$op' $answer" 2
104 return $answer
105 }
106
107 # Evaluate a selector expression.
108 proc selector_expression { exp } {
109 if { [llength $exp] == 2 } {
110 if [string match "!" [lindex $exp 0]] {
111 set op1 [lindex $exp 1]
112 set answer [expr { ! [selector_opd $op1] }]
113 } else {
114 # Assume it's a list of target triplets.
115 set answer [selector_list $exp]
116 }
117 } elseif { [llength $exp] == 3 } {
118 set op1 [lindex $exp 0]
119 set opr [lindex $exp 1]
120 set op2 [lindex $exp 2]
121 if [string match "&&" $opr] {
122 set answer [expr { [selector_opd $op1] && [selector_opd $op2] }]
123 } elseif [string match "||" $opr] {
124 set answer [expr { [selector_opd $op1] || [selector_opd $op2] }]
125 } else {
126 # Assume it's a list of target triplets.
127 set answer [selector_list $exp]
128 }
129 } else {
130 # Assume it's a list of target triplets.
131 set answer [selector_list $exp]
132 }
133
134 verbose "selector_expression: `$exp' $answer" 2
135 return $answer
136 }
137
138 # Evaluate "target selector" or "xfail selector".
139
140 proc dg-process-target-1 { args } {
141 verbose "dg-process-target-1: `$args'" 2
142
143 # Extract the 'what' keyword from the argument list.
144 set selector [string trim [lindex $args 0]]
145 if [regexp "^xfail " $selector] {
146 set what "xfail"
147 } elseif [regexp "^target " $selector] {
148 set what "target"
149 } else {
150 error "syntax error in target selector \"$selector\""
151 }
152
153 # Extract the rest of the list, which might be a keyword.
154 regsub "^${what}" $selector "" rest
155 set rest [string trim $rest]
156
157 if [is-effective-target-keyword $rest] {
158 # The selector is an effective target keyword.
159 if [is-effective-target $rest] {
160 return [expr { $what == "xfail" ? "F" : "S" }]
161 } else {
162 return [expr { $what == "xfail" ? "P" : "N" }]
163 }
164 }
165
166 if [string match "{*}" $rest] {
167 if [selector_expression [lindex $rest 0]] {
168 return [expr { $what == "xfail" ? "F" : "S" }]
169 } else {
170 return [expr { $what == "xfail" ? "P" : "N" }]
171 }
172 }
173
174 # The selector is not an effective-target keyword, so process
175 # the list of target triplets.
176 return [saved-dg-process-target $selector]
177 }
178
179 # Intercept calls to the DejaGnu function. In addition to
180 # processing "target selector" or "xfail selector", handle
181 # "target selector1 xfail selector2".
182
183 proc dg-process-target { args } {
184 verbose "replacement dg-process-target: `$args'" 2
185
186 set selector [string trim [lindex $args 0]]
187
188 # If the argument list contains both 'target' and 'xfail',
189 # process 'target' and, if that succeeds, process 'xfail'.
190 if [regexp "^target .* xfail .*" $selector] {
191 set xfail_index [string first "xfail" $selector]
192 set xfail_selector [string range $selector $xfail_index end]
193 set target_selector [string range $selector 0 [expr $xfail_index-1]]
194 set target_selector [string trim $target_selector]
195 if { [dg-process-target-1 $target_selector] == "N" } {
196 return "N"
197 }
198 return [dg-process-target-1 $xfail_selector]
199
200 }
201 return [dg-process-target-1 $selector]
202 }
203 }
204
205 # Define libffi callbacks for dg.exp.
206
207 proc libffi-dg-test-1 { target_compile prog do_what extra_tool_flags } {
208
209 # To get all \n in dg-output test strings to match printf output
210 # in a system that outputs it as \015\012 (i.e. not just \012), we
211 # need to change all \n into \r?\n. As there is no dejagnu flag
212 # or hook to do that, we simply change the text being tested.
213 # Unfortunately, we have to know that the variable is called
214 # dg-output-text and lives in the caller of libffi-dg-test, which
215 # is two calls up. Overriding proc dg-output would be longer and
216 # would necessarily have the same assumption.
217 upvar 2 dg-output-text output_match
218
219 if { [llength $output_match] > 1 } {
220 regsub -all "\n" [lindex $output_match 1] "\r?\n" x
221 set output_match [lreplace $output_match 1 1 $x]
222 }
223
224 if { [ istarget "wasm32-*-*" ] } {
225 # emscripten will get confused if told to build as .exe
226 set exec_suffix ""
227 } else {
228 set exec_suffix ".exe"
229 }
230
231 # Set up the compiler flags, based on what we're going to do.
232
233 set options [list]
234 switch $do_what {
235 "compile" {
236 set compile_type "assembly"
237 set output_file "[file rootname [file tail $prog]].s"
238 }
239 "link" {
240 set compile_type "executable"
241 set output_file "[file rootname [file tail $prog]]$exec_suffix"
242 # The following line is needed for targets like the i960 where
243 # the default output file is b.out. Sigh.
244 }
245 "run" {
246 set compile_type "executable"
247 # FIXME: "./" is to cope with "." not being in $PATH.
248 # Should this be handled elsewhere?
249 # YES.
250 set output_file "./[file rootname [file tail $prog]]$exec_suffix"
251 # This is the only place where we care if an executable was
252 # created or not. If it was, dg.exp will try to run it.
253 remote_file build delete $output_file;
254 }
255 default {
256 perror "$do_what: not a valid dg-do keyword"
257 return ""
258 }
259 }
260
261 if { $extra_tool_flags != "" } {
262 lappend options "additional_flags=$extra_tool_flags"
263 }
264
265 set comp_output [libffi_target_compile "$prog" "$output_file" "$compile_type" $options];
266
267
268 return [list $comp_output $output_file]
269 }
270
271
272 proc libffi-dg-test { prog do_what extra_tool_flags } {
273 return [libffi-dg-test-1 target_compile $prog $do_what $extra_tool_flags]
274 }
275
276 proc libffi-dg-prune { target_triplet text } {
277 # We get this with some qemu emulated systems (eg. ppc64le-linux-gnu)
278 regsub -all "(^|\n)\[^\n\]*unable to perform all requested operations" $text "" text
279 # We get this from sparc64 linux systems
280 regsub -all "(^|\n)\[^\n\]*warning: .* has a LOAD segment with RWX permissions" $text "" text
281 # Ignore Emscripten INFO messages
282 regsub -all "(^|\n)(cache|shared):INFO:\[^\n\]*" $text "" text
283 return $text
284 }
285
286 proc libffi-init { args } {
287 global gluefile wrap_flags;
288 global srcdir
289 global blddirffi
290 global objdir
291 global TOOL_OPTIONS
292 global tool
293 global libffi_include
294 global libffi_link_flags
295 global tool_root_dir
296 global ld_library_path
297 global compiler_vendor
298
299 if ![info exists blddirffi] {
300 set blddirffi [pwd]/..
301 }
302
303 verbose "libffi $blddirffi"
304
305 # Which compiler are we building with?
306 if { [string match $compiler_vendor "gnu"] } {
307 set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
308 if {$gccdir != ""} {
309 set gccdir [file dirname $gccdir]
310 }
311 verbose "gccdir $gccdir"
312
313 set ld_library_path "."
314 append ld_library_path ":${gccdir}"
315
316 set compiler "${gccdir}/xgcc"
317 if { [is_remote host] == 0 && [which $compiler] != 0 } {
318 foreach i "[exec $compiler --print-multi-lib]" {
319 set mldir ""
320 regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
321 set mldir [string trimright $mldir "\;@"]
322 if { "$mldir" == "." } {
323 continue
324 }
325 if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
326 append ld_library_path ":${gccdir}/${mldir}"
327 }
328 }
329 }
330 }
331
332 # add the library path for libffi.
333 append ld_library_path ":${blddirffi}/.libs"
334
335 verbose "ld_library_path: $ld_library_path"
336
337 # Point to the Libffi headers in libffi.
338 set libffi_include "${blddirffi}/include"
339 verbose "libffi_include $libffi_include"
340
341 set libffi_dir "${blddirffi}/.libs"
342 verbose "libffi_dir $libffi_dir"
343 if { $libffi_dir != "" } {
344 set libffi_dir [file dirname ${libffi_dir}]
345 set libffi_link_flags "-L ../.libs"
346 }
347
348 set_ld_library_path_env_vars
349 libffi_maybe_build_wrapper "${objdir}/testglue.o"
350 }
351
352 proc libffi_exit { } {
353 global gluefile;
354
355 if [info exists gluefile] {
356 file_on_build delete $gluefile;
357 unset gluefile;
358 }
359 }
360
361 proc libffi_target_compile { source dest type options } {
362 global gluefile wrap_flags;
363 global srcdir
364 global blddirffi
365 global TOOL_OPTIONS
366 global libffi_link_flags
367 global libffi_include
368 global target_triplet
369 global compiler_vendor
370
371 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
372 lappend options "libs=${gluefile}"
373 lappend options "ldflags=$wrap_flags"
374 }
375
376 # TOOL_OPTIONS must come first, so that it doesn't override testcase
377 # specific options.
378 if [info exists TOOL_OPTIONS] {
379 lappend options "additional_flags=$TOOL_OPTIONS"
380 }
381
382 # search for ffi_mips.h in srcdir, too
383 # lappend options "additional_flags=-I${libffi_include} -I${srcdir}/../include -I${libffi_include}/.."
384 lappend options "additional_flags=-I ../include -I ${srcdir}/../include -I .."
385 if { [string match $type "executable"] } {
386
387 lappend options "additional_flags=${libffi_link_flags}"
388
389 # Darwin needs a stack execution allowed flag.
390 if { [istarget "*-*-darwin9*"] || [istarget "*-*-darwin1*"]
391 || [istarget "*-*-darwin2*"] } {
392 # lappend options "additional_flags=-Wl,-allow_stack_execute"
393 lappend options "additional_flags=-Wno-unused-command-line-argument"
394 lappend options "additional_flags=-Wl,-search_paths_first"
395 }
396
397 # If you're building the compiler with --prefix set to a place
398 # where it's not yet installed, then the linker won't be able to
399 # find the libgcc used by libffi.dylib. We could pass the
400 # -dylib_file option, but that's complicated, and it's much easier
401 # to just make the linker find libgcc using -L options.
402 if { [string match "*-*-darwin*" $target_triplet] } {
403 lappend options "libs= -shared-libgcc"
404 }
405
406 if { [string match "*-*-openbsd*" $target_triplet] } {
407 lappend options "libs= -lpthread"
408 }
409
410 lappend options "libs= -lffi"
411
412 if { [string match "aarch64*-*-linux*" $target_triplet] } {
413 if { ! [string match "*android*" $target_triplet] } {
414 lappend options "libs= -lpthread"
415 }
416 }
417
418 # this may be required for g++, but just confused clang.
419 if { [string match "*.cc" $source] } {
420 lappend options "c++"
421 if { [string match "*-*-darwin*" $target_triplet] } {
422 if { [string match $compiler_vendor "gnu"] } {
423 lappend options "libs= -lc++"
424 }
425 } elseif { [string match "*android*" $target_triplet] } {
426 lappend options "libs= -lc++"
427 }
428 }
429
430 if { [string match "arc*-*-linux*" $target_triplet] } {
431 lappend options "libs= -lpthread"
432 }
433 }
434
435 # emscripten emits this warning while building the feature test
436 # which causes it to be seen as unsupported.
437 if { [string match "wasm32-*" $target_triplet] } {
438 lappend options "additional_flags=-Wno-unused-command-line-argument"
439 lappend options "libs= -lpthread"
440 }
441
442 verbose "options: $options"
443 return [target_compile $source $dest $type $options]
444 }
445
446 # TEST should be a preprocessor condition. Returns true if it holds.
447 proc libffi_feature_test { test } {
448 set src "ffitest[pid].c"
449
450 set f [open $src "w"]
451 puts $f "#include <ffi.h>"
452 puts $f $test
453 puts $f "/* OK */"
454 puts $f "#else"
455 puts $f "# error Failed $test"
456 puts $f "#endif"
457 puts $f "int main() {return 0;}"
458 close $f
459
460 set lines [libffi_target_compile $src $src.i preprocess ""]
461 file delete $src
462 file delete $src.i
463
464 return [string match "" $lines]
465 }
466
467 # Utility routines.
468
469 #
470 # search_for -- looks for a string match in a file
471 #
472 proc search_for { file pattern } {
473 set fd [open $file r]
474 while { [gets $fd cur_line]>=0 } {
475 if [string match "*$pattern*" $cur_line] then {
476 close $fd
477 return 1
478 }
479 }
480 close $fd
481 return 0
482 }
483
484 # Modified dg-runtest that can cycle through a list of optimization options
485 # as c-torture does.
486 proc libffi-dg-runtest { testcases default-extra-flags } {
487 global runtests
488
489 foreach test $testcases {
490 # If we're only testing specific files and this isn't one of
491 # them, skip it.
492 if ![runtest_file_p $runtests $test] {
493 continue
494 }
495
496 # Look for a loop within the source code - if we don't find one,
497 # don't pass -funroll[-all]-loops.
498 global torture_with_loops torture_without_loops
499 if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
500 set option_list $torture_with_loops
501 } else {
502 set option_list $torture_without_loops
503 }
504
505 set nshort [file tail [file dirname $test]]/[file tail $test]
506
507 foreach flags $option_list {
508 verbose "Testing $nshort, $flags" 1
509 dg-test $test $flags ${default-extra-flags}
510 }
511 }
512 }
513
514 proc run-many-tests { testcases extra_flags } {
515 global compiler_vendor
516 global has_gccbug
517 global env
518 switch $compiler_vendor {
519 "clang" {
520 set common "-W -Wall"
521 if [info exists env(LIBFFI_TEST_OPTIMIZATION)] {
522 set optimizations [ list $env(LIBFFI_TEST_OPTIMIZATION) ]
523 } else {
524 set optimizations { "-O0" "-O2" }
525 }
526 }
527 "gnu" {
528 set common "-W -Wall -Wno-psabi -fno-diagnostics-color"
529 if [info exists env(LIBFFI_TEST_OPTIMIZATION)] {
530 set optimizations [ list $env(LIBFFI_TEST_OPTIMIZATION) ]
531 } else {
532 set optimizations { "-O0" "-O2" }
533 }
534 }
535 default {
536 # Assume we are using the vendor compiler.
537 set common ""
538 if [info exists env(LIBFFI_TEST_OPTIMIZATION)] {
539 set optimizations [ list $env(LIBFFI_TEST_OPTIMIZATION) ]
540 } else {
541 set optimizations { "" }
542 }
543 }
544 }
545
546 info exists env(LD_LIBRARY_PATH)
547
548 set targetabis { "" }
549 if [string match $compiler_vendor "gnu"] {
550 if [libffi_feature_test "#ifdef __i386__"] {
551 set targetabis {
552 ""
553 "-DABI_NUM=FFI_STDCALL -DABI_ATTR=__STDCALL__"
554 "-DABI_NUM=FFI_THISCALL -DABI_ATTR=__THISCALL__"
555 "-DABI_NUM=FFI_FASTCALL -DABI_ATTR=__FASTCALL__"
556 }
557 } elseif { [istarget "x86_64-*-*"] \
558 && [libffi_feature_test "#if !defined __ILP32__ \
559 && !defined __i386__"] } {
560 set targetabis {
561 ""
562 "-DABI_NUM=FFI_GNUW64 -DABI_ATTR=__MSABI__"
563 }
564 }
565 }
566
567 set common [ concat $common $extra_flags ]
568 foreach test $testcases {
569 set testname [file tail $test]
570 if [search_for $test "ABI_NUM"] {
571 set abis $targetabis
572 } else {
573 set abis { "" }
574 }
575 foreach opt $optimizations {
576 foreach abi $abis {
577 set options [concat $common $opt $abi]
578 set has_gccbug false;
579 if { [string match $compiler_vendor "gnu"] \
580 && [string match "*MSABI*" $abi] \
581 && ( ( [string match "*DGTEST=57 *" $common] \
582 && [string match "*call.c*" $testname] ) \
583 || ( [string match "*DGTEST=54 *" $common] \
584 && [string match "*callback*" $testname] ) \
585 || [string match "*DGTEST=55 *" $common] \
586 || [string match "*DGTEST=56 *" $common] ) } then {
587 if [libffi_feature_test "#if (__GNUC__ < 9) || ((__GNUC__ == 9) && (__GNUC_MINOR__ < 3))"] {
588 set has_gccbug true;
589 }
590 }
591 verbose "Testing $testname, $options" 1
592 verbose "has_gccbug = $has_gccbug" 1
593 dg-test $test $options ""
594 }
595 }
596 }
597 }
598
599 # Like check_conditional_xfail, but callable from a dg test.
600
601 proc dg-xfail-if { args } {
602 set args [lreplace $args 0 0]
603 set selector "target [join [lindex $args 1]]"
604 if { [dg-process-target $selector] == "S" } {
605 global compiler_conditional_xfail_data
606 set compiler_conditional_xfail_data $args
607 }
608 }
609
610 proc check-flags { args } {
611
612 # The args are within another list; pull them out.
613 set args [lindex $args 0]
614
615 # The next two arguments are optional. If they were not specified,
616 # use the defaults.
617 if { [llength $args] == 2 } {
618 lappend $args [list "*"]
619 }
620 if { [llength $args] == 3 } {
621 lappend $args [list ""]
622 }
623
624 # If the option strings are the defaults, or the same as the
625 # defaults, there is no need to call check_conditional_xfail to
626 # compare them to the actual options.
627 if { [string compare [lindex $args 2] "*"] == 0
628 && [string compare [lindex $args 3] "" ] == 0 } {
629 set result 1
630 } else {
631 # The target list might be an effective-target keyword, so replace
632 # the original list with "*-*-*", since we already know it matches.
633 set result [check_conditional_xfail [lreplace $args 1 1 "*-*-*"]]
634 }
635
636 return $result
637 }
638
639 proc dg-skip-if { args } {
640 # Verify the number of arguments. The last two are optional.
641 set args [lreplace $args 0 0]
642 if { [llength $args] < 2 || [llength $args] > 4 } {
643 error "dg-skip-if 2: need 2, 3, or 4 arguments"
644 }
645
646 # Don't bother if we're already skipping the test.
647 upvar dg-do-what dg-do-what
648 if { [lindex ${dg-do-what} 1] == "N" } {
649 return
650 }
651
652 set selector [list target [lindex $args 1]]
653 if { [dg-process-target $selector] == "S" } {
654 if [check-flags $args] {
655 upvar dg-do-what dg-do-what
656 set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
657 }
658 }
659 }
660
661 # We need to make sure that additional_files and additional_sources
662 # are both cleared out after every test. It is not enough to clear
663 # them out *before* the next test run because gcc-target-compile gets
664 # run directly from some .exp files (outside of any test). (Those
665 # uses should eventually be eliminated.)
666
667 # Because the DG framework doesn't provide a hook that is run at the
668 # end of a test, we must replace dg-test with a wrapper.
669
670 if { [info procs saved-dg-test] == [list] } {
671 rename dg-test saved-dg-test
672
673 proc dg-test { args } {
674 global additional_files
675 global additional_sources
676 global errorInfo
677
678 if { [ catch { eval saved-dg-test $args } errmsg ] } {
679 set saved_info $errorInfo
680 set additional_files ""
681 set additional_sources ""
682 error $errmsg $saved_info
683 }
684 set additional_files ""
685 set additional_sources ""
686 }
687 }
688
689 # Local Variables:
690 # tcl-indent-level:4
691 # End: