libffi.exp 13 KB

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