]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - libctf/testsuite/lib/ctf-lib.exp
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / libctf / testsuite / lib / ctf-lib.exp
1 # Support routines for libctf testsuite.
2 # Copyright (C) 1994-2024 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
6 # This file is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20
21 load_file $srcdir/../../ld/testsuite/lib/ld-lib.exp
22
23 proc run_native_host_cmd { command } {
24 global link_output
25 global ld
26
27 verbose -log "$command"
28 set run_output ""
29 try {
30 set run_output [exec "sh" "-c" "$command" "2>@1"]
31 set status 0
32 } trap CHILDSTATUS {results options} {
33 set status [lindex [dict get $options -errorcode] 2]
34 set run_output $results
35 }
36 regsub "\n$" $run_output "" run_output
37 if { [lindex $status 0] != 0 && [string match "" $run_output] } then {
38 append run_output "child process exited abnormally"
39 }
40
41 if [string match "" $run_output] then {
42 return ""
43 }
44
45 verbose -log "$run_output"
46 return "$run_output"
47 }
48
49 # Compile and link a C source file for execution on the host.
50 proc compile_link_one_host_cc { src output additional_args } {
51 global CC
52 global CFLAGS
53
54 return [run_native_host_cmd "./libtool --quiet --tag=CC --mode=link $CC $CFLAGS $src -o $output $additional_args" ]
55 }
56
57 # run_lookup_test FILE
58 #
59 # Compile with the host compiler and link a .c file into a "lookup" binary, then
60 # compile and optionally link together a bunch of .s or .c files with CTF info
61 # and pass the name of the resulting binary to the "lookup" binary and check the
62 # output. (If none is specified, the binary is expected to generate its own CTF
63 # for testing purposes.)
64 #
65 # As with run_dump_test, this is all driven by a file (in this case, a .lk file)
66 # beginning with zero or more option lines, which specify the names of the
67 # lookup binary's source file, the source file(s) with CTF info to compile
68 # together, and whether to link them. The optional lines have the syntax:
69 #
70 # # OPTION: VALUE
71 #
72 # OPTION is the name of some option, like "name" or "lookup", and
73 # VALUE is OPTION's value. The valid options are described below.
74 # Whitespace is ignored everywhere, except within VALUE. The option
75 # list ends with the first line that doesn't match the above syntax.
76 # However, a line within the options that begins with a #, but doesn't
77 # have a recognizable option name followed by a colon, is considered a
78 # comment and entirely ignored.
79 #
80 # The interesting options are:
81 #
82 # name: TEST-NAME
83 # The name of this test, passed to DejaGNU's `pass' and `fail'
84 # commands. If omitted, this defaults to FILE, the root of the
85 # lookup .c file's name.
86 #
87 # lookup: SOURCE
88 # Compile the file SOURCE.c. If omitted, the lookup source defaults
89 # to FILE.c.
90 #
91 # source: SOURCE
92 # Assemble the file SOURCE.c and pass it to the LOOKUP program.
93 #
94 # nonshared:
95 # If set, do not link with -shared.
96 #
97 # link:
98 # If set, link the SOURCE together even if only one file is specified.
99 #
100 # link_flags:
101 # If set, extra flags to pass to the linker.
102 #
103 # xfail: GLOB|PROC ...
104 # This test is expected to fail on a specified list of targets.
105 #
106 # no_cross:
107 # If set, do not run this test when host != target.
108 #
109 # Each option may occur at most once unless otherwise mentioned.
110 #
111 # After the option lines come regexp lines. run_lookup_test calls
112 # regexp_diff to compare the output of the lookup program against the
113 # regexps in FILE.d.
114 #
115 proc run_lookup_test { name } {
116 global CC_FOR_TARGET CFLAGS_FOR_TARGET LIBS TEST_CROSS
117 global copyfile env runtests srcdir subdir verbose
118
119 if ![runtest_file_p $runtests $name] then {
120 return
121 }
122
123 if [string match "*/*" $name] {
124 set file $name
125 set name [file tail $name]
126 } else {
127 set file "$srcdir/$subdir/$name"
128 }
129
130 set opt_array [slurp_options "${file}.lk"]
131 if { $opt_array == -1 } {
132 perror "error reading options from $file.lk"
133 unresolved $subdir/$name
134 return
135 }
136 set run_ld 0
137 set shared "-shared"
138 set opts(link) {}
139 set opts(link_flags) {}
140 set opts(nonshared) {}
141 set opts(lookup) {}
142 set opts(name) {}
143 set opts(source) {}
144 set opts(xfail) {}
145 set opts(no_cross) {}
146
147 foreach i $opt_array {
148 set opt_name [lindex $i 0]
149 set opt_val [lindex $i 1]
150 if { $opt_name == "" } {
151 set in_extra 1
152 continue
153 }
154 if ![info exists opts($opt_name)] {
155 perror "unknown option $opt_name in file $file.lk"
156 unresolved $subdir/$name
157 return
158 }
159
160 set opts($opt_name) [concat $opts($opt_name) $opt_val]
161 }
162
163 if { [llength $opts(no_cross)] != 0
164 && "$TEST_CROSS" eq "yes" } {
165 untested "$subdir/$name not tested when cross-compiling"
166 return
167 }
168
169 if { [llength $opts(lookup)] == 0 } {
170 set opts(lookup) "$file.c"
171 } else {
172 set opts(lookup) "[file dirname $file]/$opts(lookup)"
173 }
174
175 if { [llength $opts(name)] == 0 } {
176 set opts(name) $opts(lookup)
177 }
178
179 if { [llength $opts(link)] != 0
180 || [llength $opts(source)] > 1 } {
181 set run_ld 1
182 }
183
184 if { [llength $opts(nonshared)] != 0 } {
185 set shared ""
186 }
187
188 set testname $opts(name)
189 if { $opts(name) == "" } {
190 set testname "$subdir/$name"
191 }
192
193 # Compile and link the lookup program.
194 set comp_output [prune_warnings [compile_link_one_host_cc $opts(lookup) "tmpdir/lookup" "libctf.la"]]
195
196 if { $comp_output != ""} {
197 send_log "compilation of lookup program $opts(lookup) failed with <$comp_output>"
198 perror "compilation of lookup program $opts(lookup) failed"
199 fail $testname
200 return 0
201 }
202
203 # Compile the inputs and posibly link them together.
204
205 set lookup_output ""
206 if { [llength $opts(source)] > 0 } {
207 set lookup_flags ""
208 if { $run_ld } {
209 set lookup_output "tmpdir/out.so"
210 set lookup_flags "-gctf -fPIC $shared $opts(link_flags)"
211 } else {
212 set lookup_output "tmpdir/out.o"
213 set lookup_flags "-gctf -fPIC -c"
214 }
215 if [board_info [target_info name] exists cflags] {
216 append lookup_flags " [board_info [target_info name] cflags]"
217 }
218 if [board_info [target_info name] exists ldflags] {
219 append lookup_flags " [board_info [target_info name] ldflags]"
220 }
221 set src {}
222 foreach sfile $opts(source) {
223 if [is_remote host] {
224 lappend src [remote_download host [file join [file dirname $file] $sfile]]
225 } else {
226 lappend src [file join [file dirname $file] $sfile]
227 }
228 }
229
230 set comp_output [prune_warnings [run_host_cmd "$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET $lookup_flags [concat $src] -o $lookup_output"]]
231
232 if { $comp_output != ""} {
233 send_log "compilation of CTF program [concat $src] failed with <$comp_output>"
234 fail $testname
235 return 0
236 }
237 }
238
239 # Time to setup xfailures.
240 foreach targ $opts(xfail) {
241 if [match_target $targ] {
242 setup_xfail "*-*-*"
243 break
244 }
245 }
246
247 # Invoke the lookup program on the outputs.
248
249 set results [run_host_cmd tmpdir/lookup $lookup_output]
250
251 set f [open "tmpdir/lookup.out" "w"]
252 puts $f $results
253 close $f
254
255 if { [regexp_diff "tmpdir/lookup.out" "${file}.lk"] } then {
256 fail $testname
257 if { $verbose == 2 } then { verbose "output is [file_contents tmpdir/lookup.out]" 2 }
258 return 0
259 }
260
261 pass $testname
262 return 0
263 }