Commit | Line | Data |
---|---|---|
ecd75fc8 | 1 | # Copyright 2004-2014 Free Software Foundation, Inc. |
42159ca5 TT |
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. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | load_lib libgloss.exp | |
17 | ||
18 | # FIXME:brobecker/2004-03-31: | |
19 | # The following functions should eventually be part of dejagnu. Even after | |
20 | # these functions becomes available in dejagnu, we will keep for a while | |
21 | # a copy here in order to avoid increasing the dejagnu version | |
22 | # requirement. | |
23 | ||
24 | proc gdb_find_gnatmake {} { | |
25 | global tool_root_dir | |
26 | ||
27 | set root "$tool_root_dir/gcc" | |
28 | set GM "" | |
29 | ||
30 | if ![is_remote host] { | |
31 | set file [lookfor_file $root gnatmake] | |
32 | if { $file != "" } { | |
33 | set GM "$file -I$root/ada/rts --GCC=$root/xgcc --GNATBIND=$root/gnatbind --GNATLINK=$root/gnatlink -cargs -B$root -largs --GCC=$root/xgcc -margs"; | |
34 | } | |
35 | } | |
36 | ||
37 | if {$GM == ""} { | |
38 | set GM [transform gnatmake] | |
39 | } | |
40 | ||
41 | return $GM | |
42 | } | |
43 | ||
44 | proc gdb_find_gfortran {} { | |
45 | global tool_root_dir | |
46 | ||
47 | if {![is_remote host]} { | |
48 | set file [lookfor_file $tool_root_dir gfortran] | |
49 | if { $file == "" } { | |
50 | set file [lookfor_file $tool_root_dir gcc/gfortran] | |
51 | } | |
52 | if { $file != "" } { | |
53 | set CC "$file -B[file dirname $file]/" | |
54 | } else { | |
55 | set CC [transform gfortran] | |
56 | } | |
57 | } else { | |
58 | set CC [transform gfortran] | |
59 | } | |
60 | return $CC | |
61 | } | |
62 | ||
a766d390 DE |
63 | proc gdb_find_go {} { |
64 | global tool_root_dir | |
65 | ||
66 | set GO "" | |
67 | ||
68 | if {![is_remote host]} { | |
69 | set file [lookfor_file $tool_root_dir gccgo] | |
70 | if { $file != "" } { | |
71 | set root [file dirname $file] | |
72 | set GO "$file -B$root/gcc/" | |
73 | } | |
74 | } | |
75 | ||
76 | if { $GO == "" } { | |
77 | set GO [transform gccgo] | |
78 | } | |
79 | ||
80 | return $GO | |
81 | } | |
82 | ||
83 | proc gdb_find_go_linker {} { | |
84 | return [find_go] | |
85 | } | |
86 | ||
f61d5529 DE |
87 | proc gdb_find_ldd {} { |
88 | global LDD_FOR_TARGET | |
89 | if [info exists LDD_FOR_TARGET] { | |
90 | set ldd $LDD_FOR_TARGET | |
91 | } else { | |
92 | set ldd "ldd" | |
93 | } | |
94 | return $ldd | |
95 | } | |
96 | ||
4fa7d390 DE |
97 | proc gdb_find_objcopy {} { |
98 | global OBJCOPY_FOR_TARGET | |
99 | if [info exists OBJCOPY_FOR_TARGET] { | |
100 | set objcopy $OBJCOPY_FOR_TARGET | |
101 | } else { | |
102 | set objcopy [transform objcopy] | |
103 | } | |
104 | return $objcopy | |
105 | } | |
106 | ||
107 | proc gdb_find_readelf {} { | |
108 | global READELF_FOR_TARGET | |
109 | if [info exists READELF_FOR_TARGET] { | |
110 | set readelf $READELF_FOR_TARGET | |
111 | } else { | |
112 | set readelf [transform readelf] | |
113 | } | |
114 | return $readelf | |
115 | } | |
116 | ||
42159ca5 TT |
117 | proc gdb_default_target_compile {source destfile type options} { |
118 | global target_triplet | |
119 | global tool_root_dir | |
120 | global CFLAGS_FOR_TARGET | |
121 | global compiler_flags | |
122 | ||
123 | if { $destfile == "" && $type != "preprocess" && $type != "none" } { | |
124 | error "Must supply an output filename for the compile to default_target_compile" | |
125 | } | |
126 | ||
127 | set add_flags "" | |
128 | set libs "" | |
129 | set compiler_type "c" | |
130 | set compiler "" | |
a766d390 DE |
131 | set linker "" |
132 | # linker_opts_order is one of "sources-then-flags", "flags-then-sources". | |
133 | # The order shouldn't matter. It's done this way to preserve | |
134 | # existing behavior. | |
135 | set linker_opts_order "sources-then-flags" | |
42159ca5 TT |
136 | set ldflags "" |
137 | set dest [target_info name] | |
138 | ||
139 | if {[info exists CFLAGS_FOR_TARGET]} { | |
140 | append add_flags " $CFLAGS_FOR_TARGET" | |
141 | } | |
142 | ||
143 | if {[info exists target_info(host,name)]} { | |
144 | set host [host_info name] | |
145 | } else { | |
146 | set host "unix" | |
147 | } | |
148 | ||
149 | foreach i $options { | |
150 | ||
151 | if { $i == "ada" } { | |
152 | set compiler_type "ada" | |
153 | if {[board_info $dest exists adaflags]} { | |
154 | append add_flags " [target_info adaflags]" | |
155 | } | |
156 | if {[board_info $dest exists gnatmake]} { | |
157 | set compiler [target_info gnatmake] | |
158 | } else { | |
159 | set compiler [find_gnatmake] | |
160 | } | |
161 | } | |
162 | ||
163 | if { $i == "c++" } { | |
164 | set compiler_type "c++" | |
165 | if {[board_info $dest exists cxxflags]} { | |
166 | append add_flags " [target_info cxxflags]" | |
167 | } | |
168 | append add_flags " [g++_include_flags]" | |
169 | if {[board_info $dest exists c++compiler]} { | |
170 | set compiler [target_info c++compiler] | |
171 | } else { | |
172 | set compiler [find_g++] | |
173 | } | |
174 | } | |
175 | ||
176 | if { $i == "f77" } { | |
177 | set compiler_type "f77" | |
178 | if {[board_info $dest exists f77flags]} { | |
179 | append add_flags " [target_info f77flags]" | |
180 | } | |
181 | if {[board_info $dest exists f77compiler]} { | |
182 | set compiler [target_info f77compiler] | |
183 | } else { | |
184 | set compiler [find_g77] | |
185 | } | |
186 | } | |
187 | ||
188 | if { $i == "f90" } { | |
189 | set compiler_type "f90" | |
190 | if {[board_info $dest exists f90flags]} { | |
191 | append add_flags " [target_info f90flags]" | |
192 | } | |
193 | if {[board_info $dest exists f90compiler]} { | |
194 | set compiler [target_info f90compiler] | |
195 | } else { | |
196 | set compiler [find_gfortran] | |
197 | } | |
198 | } | |
199 | ||
a766d390 DE |
200 | if { $i == "go" } { |
201 | set compiler_type "go" | |
202 | if {[board_info $dest exists goflags]} { | |
203 | append add_flags " [target_info goflags]" | |
204 | } | |
205 | if {[board_info $dest exists gocompiler]} { | |
206 | set compiler [target_info gocompiler] | |
207 | } else { | |
208 | set compiler [find_go] | |
209 | } | |
210 | if {[board_info $dest exists golinker]} { | |
211 | set linker [target_info golinker] | |
212 | } else { | |
213 | set linker [find_go_linker] | |
214 | } | |
215 | if {[board_info $dest exists golinker_opts_order]} { | |
216 | set linker_opts_order [target_info golinker_opts_order] | |
217 | } | |
218 | } | |
219 | ||
42159ca5 TT |
220 | if {[regexp "^dest=" $i]} { |
221 | regsub "^dest=" $i "" tmp | |
222 | if {[board_info $tmp exists name]} { | |
223 | set dest [board_info $tmp name] | |
224 | } else { | |
225 | set dest $tmp | |
226 | } | |
227 | } | |
228 | if {[regexp "^compiler=" $i]} { | |
229 | regsub "^compiler=" $i "" tmp | |
230 | set compiler $tmp | |
231 | } | |
232 | if {[regexp "^additional_flags=" $i]} { | |
233 | regsub "^additional_flags=" $i "" tmp | |
234 | append add_flags " $tmp" | |
235 | } | |
236 | if {[regexp "^ldflags=" $i]} { | |
237 | regsub "^ldflags=" $i "" tmp | |
238 | append ldflags " $tmp" | |
239 | } | |
240 | if {[regexp "^libs=" $i]} { | |
241 | regsub "^libs=" $i "" tmp | |
242 | append libs " $tmp" | |
243 | } | |
244 | if {[regexp "^incdir=" $i]} { | |
245 | regsub "^incdir=" $i "-I" tmp | |
246 | append add_flags " $tmp" | |
247 | } | |
248 | if {[regexp "^libdir=" $i]} { | |
249 | regsub "^libdir=" $i "-L" tmp | |
250 | append add_flags " $tmp" | |
251 | } | |
252 | if {[regexp "^ldscript=" $i]} { | |
253 | regsub "^ldscript=" $i "" ldscript | |
254 | } | |
255 | if {[regexp "^redirect=" $i]} { | |
256 | regsub "^redirect=" $i "" redirect | |
257 | } | |
258 | if {[regexp "^optimize=" $i]} { | |
259 | regsub "^optimize=" $i "" optimize | |
260 | } | |
261 | if {[regexp "^timeout=" $i]} { | |
262 | regsub "^timeout=" $i "" timeout | |
263 | } | |
264 | } | |
265 | ||
266 | if {[board_info $host exists cflags_for_target]} { | |
267 | append add_flags " [board_info $host cflags_for_target]" | |
268 | } | |
269 | ||
270 | global CC_FOR_TARGET | |
271 | global CXX_FOR_TARGET | |
272 | global F77_FOR_TARGET | |
273 | global F90_FOR_TARGET | |
274 | global GNATMAKE_FOR_TARGET | |
a766d390 DE |
275 | global GO_FOR_TARGET |
276 | global GO_LD_FOR_TARGET | |
42159ca5 TT |
277 | |
278 | if {[info exists GNATMAKE_FOR_TARGET]} { | |
279 | if { $compiler_type == "ada" } { | |
280 | set compiler $GNATMAKE_FOR_TARGET | |
281 | } | |
282 | } | |
283 | ||
284 | if {[info exists CC_FOR_TARGET]} { | |
285 | if { $compiler == "" } { | |
286 | set compiler $CC_FOR_TARGET | |
287 | } | |
288 | } | |
289 | ||
290 | if {[info exists CXX_FOR_TARGET]} { | |
291 | if { $compiler_type == "c++" } { | |
292 | set compiler $CXX_FOR_TARGET | |
293 | } | |
294 | } | |
295 | ||
296 | if {[info exists F77_FOR_TARGET]} { | |
297 | if { $compiler_type == "f77" } { | |
298 | set compiler $F77_FOR_TARGET | |
299 | } | |
300 | } | |
301 | ||
302 | if {[info exists F90_FOR_TARGET]} { | |
303 | if { $compiler_type == "f90" } { | |
304 | set compiler $F90_FOR_TARGET | |
305 | } | |
306 | } | |
307 | ||
a766d390 DE |
308 | if { $compiler_type == "go" } { |
309 | if {[info exists GO_FOR_TARGET]} { | |
310 | set compiler $GO_FOR_TARGET | |
311 | } | |
312 | if {[info exists GO_LD_FOR_TARGET]} { | |
313 | set linker $GO_LD_FOR_TARGET | |
314 | } | |
315 | } | |
316 | ||
317 | if { $type == "executable" && $linker != "" } { | |
318 | set compiler $linker | |
319 | } | |
320 | ||
42159ca5 TT |
321 | if { $compiler == "" } { |
322 | set compiler [board_info $dest compiler] | |
323 | if { $compiler == "" } { | |
324 | return "default_target_compile: No compiler to compile with" | |
325 | } | |
326 | } | |
327 | ||
328 | if {![is_remote host]} { | |
329 | if { [which $compiler] == 0 } { | |
330 | return "default_target_compile: Can't find $compiler." | |
331 | } | |
332 | } | |
333 | ||
334 | if {$type == "object"} { | |
335 | append add_flags " -c" | |
336 | } | |
337 | ||
338 | if { $type == "preprocess" } { | |
339 | append add_flags " -E" | |
340 | } | |
341 | ||
342 | if { $type == "assembly" } { | |
343 | append add_flags " -S" | |
344 | } | |
345 | ||
346 | if {[board_info $dest exists cflags]} { | |
347 | append add_flags " [board_info $dest cflags]" | |
348 | } | |
349 | ||
350 | if { $type == "executable" } { | |
351 | if {[board_info $dest exists ldflags]} { | |
352 | append add_flags " [board_info $dest ldflags]" | |
353 | } | |
354 | if { $compiler_type == "c++" } { | |
355 | append add_flags " [g++_link_flags]" | |
356 | } | |
357 | if {[isnative]} { | |
358 | # This is a lose. | |
359 | catch "glob -nocomplain $tool_root_dir/libstdc++/libstdc++.so* $tool_root_dir/libstdc++/libstdc++.sl" tmp | |
360 | if { ${tmp} != "" } { | |
361 | if {[regexp ".*solaris2.*" $target_triplet]} { | |
362 | # Solaris 2 | |
363 | append add_flags " -R$tool_root_dir/libstdc++" | |
364 | } elseif {[regexp ".*(osf|irix5|linux).*" $target_triplet]} { | |
365 | # OSF/1 or IRIX 5 | |
366 | append add_flags " -Wl,-rpath,$tool_root_dir/libstdc++" | |
367 | } elseif {[regexp ".*hppa.*" $target_triplet]} { | |
368 | # HP-UX | |
369 | append add_flags " -Wl,-a,shared_archive" | |
370 | } | |
371 | } | |
372 | } | |
373 | } | |
374 | ||
375 | if {![info exists ldscript]} { | |
376 | set ldscript [board_info $dest ldscript] | |
377 | } | |
378 | ||
379 | foreach i $options { | |
380 | if { $i == "debug" } { | |
381 | if {[board_info $dest exists debug_flags]} { | |
382 | append add_flags " [board_info $dest debug_flags]" | |
383 | } else { | |
384 | append add_flags " -g" | |
385 | } | |
386 | } | |
387 | } | |
388 | ||
389 | if {[info exists optimize]} { | |
390 | append add_flags " $optimize" | |
391 | } | |
392 | ||
393 | if { $type == "executable" } { | |
394 | append add_flags " $ldflags" | |
395 | foreach x $libs { | |
396 | if {[file exists $x]} { | |
397 | append source " $x" | |
398 | } else { | |
399 | append add_flags " $x" | |
400 | } | |
401 | } | |
402 | ||
403 | if {[board_info $dest exists libs]} { | |
404 | append add_flags " [board_info $dest libs]" | |
405 | } | |
406 | ||
407 | # This probably isn't such a good idea, but it avoids nasty | |
408 | # hackiness in the testsuites. | |
409 | # The math library must be linked in before the C library. The C | |
410 | # library is linked in by the linker script, so this must be before | |
411 | # the linker script. | |
412 | if {[board_info $dest exists mathlib]} { | |
413 | append add_flags " [board_info $dest mathlib]" | |
414 | } else { | |
415 | append add_flags " -lm" | |
416 | } | |
417 | ||
418 | # This must be added here. | |
419 | append add_flags " $ldscript" | |
420 | ||
421 | if {[board_info $dest exists remote_link]} { | |
422 | # Relink option. | |
423 | append add_flags " -Wl,-r" | |
424 | } | |
425 | if {[board_info $dest exists output_format]} { | |
426 | append add_flags " -Wl,-oformat,[board_info $dest output_format]" | |
427 | } | |
428 | } | |
429 | ||
430 | if {[board_info $dest exists multilib_flags]} { | |
431 | append add_flags " [board_info $dest multilib_flags]" | |
432 | } | |
433 | ||
434 | verbose "doing compile" | |
435 | ||
436 | set sources "" | |
437 | if {[is_remote host]} { | |
438 | foreach x $source { | |
439 | set file [remote_download host $x] | |
440 | if { $file == "" } { | |
441 | warning "Unable to download $x to host." | |
442 | return "Unable to download $x to host." | |
443 | } else { | |
444 | append sources " $file" | |
445 | } | |
446 | } | |
447 | } else { | |
448 | set sources $source | |
449 | } | |
450 | ||
451 | if {[is_remote host]} { | |
1f74d3b5 YQ |
452 | append add_flags " -o " [file tail $destfile] |
453 | remote_file host delete [file tail $destfile] | |
42159ca5 TT |
454 | } else { |
455 | if { $destfile != "" } { | |
456 | append add_flags " -o $destfile" | |
457 | } | |
458 | } | |
459 | ||
460 | # This is obscure: we put SOURCES at the end when building an | |
461 | # object, because otherwise, in some situations, libtool will | |
462 | # become confused about the name of the actual source file. | |
a766d390 DE |
463 | switch $type { |
464 | "object" { | |
465 | set opts "$add_flags $sources" | |
466 | } | |
467 | "executable" { | |
468 | switch $linker_opts_order { | |
469 | "flags-then-sources" { | |
470 | set opts "$add_flags $sources" | |
471 | } | |
472 | "sources-then-flags" { | |
473 | set opts "$sources $add_flags" | |
474 | } | |
475 | default { | |
476 | error "Invalid value for board_info linker_opts_order" | |
477 | } | |
478 | } | |
479 | } | |
480 | default { | |
481 | set opts "$sources $add_flags" | |
482 | } | |
42159ca5 TT |
483 | } |
484 | ||
485 | if {[is_remote host]} { | |
486 | if {[host_info exists use_at]} { | |
487 | set fid [open "atfile" "w"] | |
488 | puts $fid "$opts" | |
489 | close $fid | |
490 | set opts "@[remote_download host atfile]" | |
491 | remote_file build delete atfile | |
492 | } | |
493 | } | |
494 | ||
495 | verbose "Invoking the compiler as $compiler $opts" 2 | |
496 | ||
497 | if {[info exists redirect]} { | |
498 | verbose "Redirecting output to $redirect" 2 | |
499 | set status [remote_exec host "$compiler $opts" "" "" $redirect] | |
500 | } else { | |
501 | if {[info exists timeout]} { | |
502 | verbose "Setting timeout to $timeout" 2 | |
503 | set status [remote_exec host "$compiler $opts" "" "" "" $timeout] | |
504 | } else { | |
505 | set status [remote_exec host "$compiler $opts"] | |
506 | } | |
507 | } | |
508 | ||
509 | set compiler_flags $opts | |
510 | if {[is_remote host]} { | |
1f74d3b5 YQ |
511 | remote_upload host [file tail $destfile] $destfile |
512 | remote_file host delete [file tail $destfile] | |
42159ca5 TT |
513 | } |
514 | set comp_output [prune_warnings [lindex $status 1]] | |
515 | regsub "^\[\r\n\]+" $comp_output "" comp_output | |
516 | if { [lindex $status 0] != 0 } { | |
517 | verbose -log "compiler exited with status [lindex $status 0]" | |
518 | } | |
519 | if { [lindex $status 1] != "" } { | |
520 | verbose -log "output is:\n[lindex $status 1]" 2 | |
521 | } | |
522 | if { [lindex $status 0] != 0 && "${comp_output}" == "" } { | |
523 | set comp_output "exit status is [lindex $status 0]" | |
524 | } | |
525 | return ${comp_output} | |
526 | } | |
527 | ||
528 | # See if the version of dejaGNU being used to run the testsuite is | |
529 | # recent enough to contain support for building Ada programs or not. | |
530 | # If not, then use the functions above in place of the ones provided | |
531 | # by dejaGNU. This is only temporary (brobecker/2004-03-31). | |
532 | ||
533 | set use_gdb_compile 0 | |
534 | if {[info procs find_gnatmake] == ""} { | |
535 | rename gdb_find_gnatmake find_gnatmake | |
536 | set use_gdb_compile 1 | |
537 | } | |
538 | ||
539 | if {[info procs find_gfortran] == ""} { | |
540 | rename gdb_find_gfortran find_gfortran | |
541 | set use_gdb_compile 1 | |
542 | } | |
543 | ||
a766d390 DE |
544 | if {[info procs find_go_linker] == ""} { |
545 | rename gdb_find_go find_go | |
546 | rename gdb_find_go_linker find_go_linker | |
bb2af9cc | 547 | set use_gdb_compile 1 |
a766d390 DE |
548 | } |
549 | ||
42159ca5 TT |
550 | if {$use_gdb_compile} { |
551 | catch {rename default_target_compile {}} | |
552 | rename gdb_default_target_compile default_target_compile | |
553 | } | |
9cc8e32c JK |
554 | |
555 | ||
556 | # Provide 'lreverse' missing in Tcl before 7.5. | |
557 | ||
558 | if {[info procs lreverse] == ""} { | |
559 | proc lreverse { arg } { | |
560 | set retval {} | |
561 | while { [llength $retval] < [llength $arg] } { | |
562 | lappend retval [lindex $arg end-[llength $retval]] | |
563 | } | |
564 | return $retval | |
565 | } | |
566 | } |