1999-06-04 H.J. Lu <hjl@gnu.org>
[platform/upstream/binutils.git] / ld / testsuite / lib / ld-lib.exp
1 #
2 # default_ld_version 
3 #       extract and print the version number of ld
4 #
5 proc default_ld_version { ld } {
6     global host_triplet
7
8     if { [which $ld] == 0 } then {
9         perror "$ld does not exist"
10         exit 1
11     }
12     
13     catch "exec $ld --version" tmp
14     set tmp [prune_warnings $tmp]
15     regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
16     if [info exists number] then {
17         clone_output "$ld $number\n"
18     }
19 }
20
21 #
22 # default_ld_relocate 
23 #       link an object using relocation
24 #
25 proc default_ld_relocate { ld target objects } {
26     global HOSTING_EMU
27     global host_triplet
28     
29     if { [which $ld] == 0 } then {
30         perror "$ld does not exist"
31         return 0
32     }
33     
34     verbose -log "$ld $HOSTING_EMU -o $target -r $objects"
35     
36     catch "exec $ld $HOSTING_EMU -o $target -r $objects" exec_output
37     set exec_output [prune_warnings $exec_output]
38     if [string match "" $exec_output] then {
39         return 1
40     } else {
41         verbose -log "$exec_output"
42         return 0
43     }
44 }
45
46
47 #
48 # default_ld_link 
49 #       link a program using ld
50 #
51 proc default_ld_link { ld target objects } {
52     global HOSTING_EMU
53     global HOSTING_CRT0
54     global HOSTING_LIBS
55     global host_triplet
56     
57     set objs "$HOSTING_CRT0 $objects"
58     set libs "$HOSTING_LIBS"
59     
60     if { [which $ld] == 0 } then {
61         perror "$ld does not exist"
62         return 0
63     }
64     
65     verbose -log "$ld $HOSTING_EMU -o $target $objs $libs"
66     
67     catch "exec $ld $HOSTING_EMU -o $target $objs $libs" exec_output
68     set exec_output [prune_warnings $exec_output]
69     if [string match "" $exec_output] then {
70         return 1
71     } else {
72         verbose -log "$exec_output"
73         return 0
74     }
75 }
76
77 #
78 # default_ld_simple_link 
79 #       link a program using ld, without including any libraries
80 #
81 proc default_ld_simple_link { ld target objects } {
82     global host_triplet
83     
84     if { [which $ld] == 0 } then {
85         perror "$ld does not exist"
86         return 0
87     }
88     
89     verbose -log "$ld -o $target $objects"
90     
91     catch "exec $ld -o $target $objects" exec_output
92     set exec_output [prune_warnings $exec_output]
93
94     # We don't care if we get a warning about a non-existent start
95     # symbol, since the default linker script might use ENTRY.
96     regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
97
98     if [string match "" $exec_output] then {
99         return 1
100     } else {
101         verbose -log "$exec_output"
102         return 0
103     }
104 }
105
106 #
107 # default_ld_compile 
108 #       compile an object using cc
109 #
110 proc default_ld_compile { cc source object } {
111     global CFLAGS
112     global srcdir
113     global subdir
114     global host_triplet
115     global gcc_gas_flag
116
117     set cc_prog $cc
118     if {[llength $cc_prog] > 1} then {
119         set cc_prog [lindex $cc_prog 0]
120     }
121     if {[which $cc_prog] == 0} then {
122         perror "$cc_prog does not exist"
123         return 0
124     }
125
126     catch "exec rm -f $object" exec_output
127
128     set flags "-I$srcdir/$subdir $CFLAGS"
129
130     # If we are compiling with gcc, we want to add gcc_gas_flag to
131     # flags.  Rather than determine this in some complex way, we guess
132     # based on the name of the compiler.
133     if {[string match "*gcc*" $cc] || [string match "*++*" $cc]} then {
134         set flags "$gcc_gas_flag $flags"
135     }
136
137     verbose -log "$cc $flags -c $source -o $object"
138
139     catch "exec $cc $flags -c $source -o $object" exec_output
140     set exec_output [prune_warnings $exec_output]
141     if [string match "" $exec_output] then {
142         if {![file exists $object]} then {
143             regexp ".*/(\[^/\]*)$" $source all dobj
144             regsub "\\.c" $dobj ".o" realobj
145             verbose "looking for $realobj"
146             if {[file exists $realobj]} then {
147                 verbose -log "mv $realobj $object"
148                 catch "exec mv $realobj $object" exec_output
149                 set exec_output [prune_warnings $exec_output]
150                 if {![string match "" $exec_output]} then {
151                     verbose -log "$exec_output"
152                     perror "could not move $realobj to $object"
153                     return 0
154                 }
155             } else {
156                 perror "$object not found after compilation"
157                 return 0
158             }
159         }
160         return 1
161     } else {
162         verbose -log "$exec_output"
163         perror "$source: compilation failed"
164         return 0
165     }
166 }
167
168 #
169 # default_ld_assemble
170 #       assemble a file
171 #
172 proc default_ld_assemble { as source object } {
173     global ASFLAGS
174     global host_triplet
175     
176     if {[which $as] == 0} then {
177         perror "$as does not exist"
178         return 0
179     }
180
181     if ![info exists ASFLAGS] { set ASFLAGS "" }
182
183     verbose -log "$as $ASFLAGS -o $object $source"
184
185     catch "exec $as $ASFLAGS -o $object $source" exec_output
186     set exec_output [prune_warnings $exec_output]
187     if [string match "" $exec_output] then {
188         return 1
189     } else {
190         verbose -log "$exec_output"
191         perror "$source: assembly failed"
192         return 0
193     }
194 }
195
196 #
197 # default_ld_nm
198 #       run nm on a file, putting the result in the array nm_output
199 #
200 proc default_ld_nm { nm object } {
201     global NMFLAGS
202     global nm_output
203     global host_triplet
204
205     if {[which $nm] == 0} then {
206         perror "$nm does not exist"
207         return 0
208     }
209
210     if {[info exists nm_output]} {
211       unset nm_output
212     }
213
214     if ![info exists NMFLAGS] { set NMFLAGS "" }
215
216     verbose -log "$nm $NMFLAGS $object >tmpdir/nm.out"
217
218     catch "exec $nm $NMFLAGS $object >tmpdir/nm.out" exec_output
219     set exec_output [prune_warnings $exec_output]
220     if [string match "" $exec_output] then {
221         set file [open tmpdir/nm.out r]
222         while { [gets $file line] != -1 } {
223             verbose "$line" 2
224             if [regexp "^(\[0-9a-fA-F\]+) \[a-zA-Z0-9\] (.+)$" $line whole value name] {
225                 set name [string trimleft $name "_"]
226                 verbose "Setting nm_output($name) to 0x$value" 2
227                 set nm_output($name) 0x$value
228             }
229         }
230         close $file
231         return 1
232     } else {
233         verbose -log "$exec_output"
234         perror "$object: nm failed"
235         return 0
236     }
237 }
238
239 #
240 # simple_diff
241 #       compares two files line-by-line
242 #       returns differences if exist
243 #       returns null if file(s) cannot be opened
244 #
245 proc simple_diff { file_1 file_2 } {
246     global target
247         
248     set eof -1
249     set differences 0
250     
251     if [file exists $file_1] then {
252         set file_a [open $file_1 r]
253     } else {
254         warning "$file_1 doesn't exist"
255         return
256     }
257     
258     if [file exists $file_2] then {
259         set file_b [open $file_2 r]
260     } else {
261         fail "$file_2 doesn't exist"
262         return
263     }
264     
265     verbose "# Diff'ing: $file_1 $file_2\n" 2
266     
267     while { [gets $file_a line] != $eof } {
268         if [regexp "^#.*$" $line] then {
269             continue
270         } else {
271             lappend list_a $line
272         }
273     }
274     close $file_a
275     
276     while { [gets $file_b line] != $eof } {
277         if [regexp "^#.*$" $line] then {
278             continue
279         } else {
280             lappend list_b $line
281         }
282     }
283     close $file_b
284
285     for { set i 0 } { $i < [llength $list_a] } { incr i } {
286         set line_a [lindex $list_a $i]
287         set line_b [lindex $list_b $i]
288
289         verbose "\t$file_1: $i: $line_a\n" 3
290         verbose "\t$file_2: $i: $line_b\n" 3
291         if [string compare $line_a $line_b] then {
292             verbose -log "\t$file_1: $i: $line_a\n"
293             verbose -log "\t$file_2: $i: $line_b\n"
294
295             fail "Test: $target"
296             return
297         }
298     }
299     
300     if { [llength $list_a] != [llength $list_b] } {
301         fail "Test: $target"
302         return
303     }
304
305     if $differences<1 then {
306         pass "Test: $target"
307     }
308 }
309
310 # This definition is taken from an unreleased version of DejaGnu.  Once
311 # that version gets released, and has been out in the world for a few
312 # months at least, it may be safe to delete this copy.
313 if ![string length [info proc prune_warnings]] {
314     #
315     # prune_warnings -- delete various system verbosities from TEXT
316     #
317     # An example is:
318     # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
319     #
320     # Sites with particular verbose os's may wish to override this in site.exp.
321     #
322     proc prune_warnings { text } {
323         # This is from sun4's.  Do it for all machines for now.
324         # The "\\1" is to try to preserve a "\n" but only if necessary.
325         regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
326
327         # It might be tempting to get carried away and delete blank lines, etc.
328         # Just delete *exactly* what we're ask to, and that's it.
329         return $text
330     }
331 }