1 # Copyright 2004-2019 Free Software Foundation, Inc.
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.
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.
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/>.
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
24 proc gdb_find_gnatmake {} {
27 set root "$tool_root_dir/gcc"
30 if ![is_remote host] {
31 set file [lookfor_file $root gnatmake]
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";
38 set GM [transform gnatmake]
44 proc gdb_find_gdc {} {
46 print "Tool Root: $tool_root_dir"
48 if {![is_remote host]} {
49 set file [lookfor_file $tool_root_dir gdc]
51 set file [lookfor_file $tool_root_dir gcc/gdc]
54 set CC "$file -B[file dirname $file]/"
56 set CC [transform gdc]
59 set CC [transform gdc]
65 proc gdb_find_gfortran {} {
68 if {![is_remote host]} {
69 set file [lookfor_file $tool_root_dir gfortran]
71 set file [lookfor_file $tool_root_dir gcc/gfortran]
74 set CC "$file -B[file dirname $file]/"
76 set CC [transform gfortran]
79 set CC [transform gfortran]
89 if {![is_remote host]} {
90 set file [lookfor_file $tool_root_dir gccgo]
92 set root [file dirname $file]
93 set GO "$file -B$root/gcc/"
98 set GO [transform gccgo]
104 proc gdb_find_go_linker {} {
108 proc gdb_find_rustc {} {
110 if {![is_remote host]} {
111 set rustc [lookfor_file $tool_root_dir rustc]
119 append rustc " --color never"
124 proc gdb_find_ldd {} {
125 global LDD_FOR_TARGET
126 if [info exists LDD_FOR_TARGET] {
127 set ldd $LDD_FOR_TARGET
134 proc gdb_find_objcopy {} {
135 global OBJCOPY_FOR_TARGET
136 if [info exists OBJCOPY_FOR_TARGET] {
137 set objcopy $OBJCOPY_FOR_TARGET
139 set objcopy [transform objcopy]
144 # find target objdump
145 proc gdb_find_objdump {} {
146 global OBJDUMP_FOR_TARGET
147 if [info exists OBJDUMP_FOR_TARGET] {
148 set objdump $OBJDUMP_FOR_TARGET
150 set objdump [transform objdump]
155 proc gdb_find_readelf {} {
156 global READELF_FOR_TARGET
157 if [info exists READELF_FOR_TARGET] {
158 set readelf $READELF_FOR_TARGET
160 set readelf [transform readelf]
165 proc gdb_find_eu-unstrip {} {
166 global EU_UNSTRIP_FOR_TARGET
167 if [info exists EU_UNSTRIP_FOR_TARGET] {
168 set eu_unstrip $EU_UNSTRIP_FOR_TARGET
170 set eu_unstrip [transform eu-unstrip]
175 proc gdb_default_target_compile {source destfile type options} {
176 global target_triplet
178 global CFLAGS_FOR_TARGET
179 global compiler_flags
181 if { $destfile == "" && $type != "preprocess" && $type != "none" } {
182 error "Must supply an output filename for the compile to default_target_compile"
188 set compiler_type "c"
191 # linker_opts_order is one of "sources-then-flags", "flags-then-sources".
192 # The order matters for things like -Wl,--as-needed. The default is to
193 # preserve existing behavior.
194 set linker_opts_order "sources-then-flags"
196 set dest [target_info name]
198 if {[info exists CFLAGS_FOR_TARGET]} {
199 append add_flags " $CFLAGS_FOR_TARGET"
202 if {[info exists target_info(host,name)]} {
203 set host [host_info name]
211 set compiler_type "ada"
212 if {[board_info $dest exists adaflags]} {
213 append add_flags " [target_info adaflags]"
215 if {[board_info $dest exists gnatmake]} {
216 set compiler [target_info gnatmake]
218 set compiler [find_gnatmake]
223 set compiler_type "c++"
224 if {[board_info $dest exists cxxflags]} {
225 append add_flags " [target_info cxxflags]"
227 append add_flags " [g++_include_flags]"
228 if {[board_info $dest exists c++compiler]} {
229 set compiler [target_info c++compiler]
231 set compiler [find_g++]
236 set compiler_type "d"
237 if {[board_info $dest exists dflags]} {
238 append add_flags " [target_info dflags]"
240 if {[board_info $dest exists dcompiler]} {
241 set compiler [target_info dcompiler]
243 set compiler [find_gdc]
248 set compiler_type "f77"
249 if {[board_info $dest exists f77flags]} {
250 append add_flags " [target_info f77flags]"
252 if {[board_info $dest exists f77compiler]} {
253 set compiler [target_info f77compiler]
255 set compiler [find_g77]
260 set compiler_type "f90"
261 if {[board_info $dest exists f90flags]} {
262 append add_flags " [target_info f90flags]"
264 if {[board_info $dest exists f90compiler]} {
265 set compiler [target_info f90compiler]
267 set compiler [find_gfortran]
272 set compiler_type "go"
273 if {[board_info $dest exists goflags]} {
274 append add_flags " [target_info goflags]"
276 if {[board_info $dest exists gocompiler]} {
277 set compiler [target_info gocompiler]
279 set compiler [find_go]
281 if {[board_info $dest exists golinker]} {
282 set linker [target_info golinker]
284 set linker [find_go_linker]
286 if {[board_info $dest exists golinker_opts_order]} {
287 set linker_opts_order [target_info golinker_opts_order]
291 if { $i == "rust" } {
292 set compiler_type "rust"
293 if {[board_info $dest exists rustflags]} {
294 append add_flags " [target_info rustflags]"
296 if {[board_info $dest exists rustflags]} {
297 set compiler [target_info rustflags]
299 set compiler [find_rustc]
303 if {[regexp "^dest=" $i]} {
304 regsub "^dest=" $i "" tmp
305 if {[board_info $tmp exists name]} {
306 set dest [board_info $tmp name]
311 if {[regexp "^compiler=" $i]} {
312 regsub "^compiler=" $i "" tmp
315 if {[regexp "^early_flags=" $i]} {
316 regsub "^early_flags=" $i "" tmp
317 append early_flags " $tmp"
319 if {[regexp "^additional_flags=" $i]} {
320 regsub "^additional_flags=" $i "" tmp
321 append add_flags " $tmp"
323 if {[regexp "^ldflags=" $i]} {
324 regsub "^ldflags=" $i "" tmp
325 append ldflags " $tmp"
327 if {[regexp "^libs=" $i]} {
328 regsub "^libs=" $i "" tmp
331 if {[regexp "^incdir=" $i]} {
332 regsub "^incdir=" $i "-I" tmp
333 append add_flags " $tmp"
335 if {[regexp "^libdir=" $i]} {
336 regsub "^libdir=" $i "-L" tmp
337 append add_flags " $tmp"
339 if {[regexp "^ldscript=" $i]} {
340 regsub "^ldscript=" $i "" ldscript
342 if {[regexp "^redirect=" $i]} {
343 regsub "^redirect=" $i "" redirect
345 if {[regexp "^optimize=" $i]} {
346 regsub "^optimize=" $i "" optimize
348 if {[regexp "^timeout=" $i]} {
349 regsub "^timeout=" $i "" timeout
353 if {[board_info $host exists cflags_for_target]} {
354 append add_flags " [board_info $host cflags_for_target]"
358 global CXX_FOR_TARGET
360 global F77_FOR_TARGET
361 global F90_FOR_TARGET
362 global GNATMAKE_FOR_TARGET
364 global GO_LD_FOR_TARGET
365 global RUSTC_FOR_TARGET
367 if {[info exists GNATMAKE_FOR_TARGET]} {
368 if { $compiler_type == "ada" } {
369 set compiler $GNATMAKE_FOR_TARGET
373 if {[info exists CC_FOR_TARGET]} {
374 if { $compiler == "" } {
375 set compiler $CC_FOR_TARGET
379 if {[info exists CXX_FOR_TARGET]} {
380 if { $compiler_type == "c++" } {
381 set compiler $CXX_FOR_TARGET
385 if {[info exists D_FOR_TARGET]} {
386 if { $compiler_type == "d" } {
387 set compiler $D_FOR_TARGET
391 if {[info exists F77_FOR_TARGET]} {
392 if { $compiler_type == "f77" } {
393 set compiler $F77_FOR_TARGET
397 if {[info exists F90_FOR_TARGET]} {
398 if { $compiler_type == "f90" } {
399 set compiler $F90_FOR_TARGET
403 if { $compiler_type == "go" } {
404 if {[info exists GO_FOR_TARGET]} {
405 set compiler $GO_FOR_TARGET
407 if {[info exists GO_LD_FOR_TARGET]} {
408 set linker $GO_LD_FOR_TARGET
412 if {[info exists RUSTC_FOR_TARGET]} {
413 if {$compiler_type == "rust"} {
414 set compiler $RUSTC_FOR_TARGET
418 if { $type == "executable" && $linker != "" } {
422 if { $compiler == "" } {
423 set compiler [board_info $dest compiler]
424 if { $compiler == "" } {
425 return "default_target_compile: No compiler to compile with"
429 if {![is_remote host]} {
430 if { [which $compiler] == 0 } {
431 return "default_target_compile: Can't find $compiler."
435 if {$type == "object"} {
436 if {$compiler_type == "rust"} {
437 append add_flags "--emit obj"
439 append add_flags " -c"
443 if { $type == "preprocess" } {
444 append add_flags " -E"
447 if { $type == "assembly" } {
448 append add_flags " -S"
451 if {[board_info $dest exists cflags]} {
452 append add_flags " [board_info $dest cflags]"
455 if { $type == "executable" } {
456 if {[board_info $dest exists ldflags]} {
457 append add_flags " [board_info $dest ldflags]"
459 if { $compiler_type == "c++" } {
460 append add_flags " [g++_link_flags]"
464 catch "glob -nocomplain $tool_root_dir/libstdc++/libstdc++.so* $tool_root_dir/libstdc++/libstdc++.sl" tmp
465 if { ${tmp} != "" } {
466 if {[regexp ".*solaris2.*" $target_triplet]} {
468 append add_flags " -R$tool_root_dir/libstdc++"
469 } elseif {[regexp ".*(osf|irix5|linux).*" $target_triplet]} {
471 append add_flags " -Wl,-rpath,$tool_root_dir/libstdc++"
477 if {![info exists ldscript]} {
478 set ldscript [board_info $dest ldscript]
482 if { $i == "debug" } {
483 if {[board_info $dest exists debug_flags]} {
484 append add_flags " [board_info $dest debug_flags]"
486 append add_flags " -g"
491 if {[info exists optimize]} {
492 append add_flags " $optimize"
495 if { $type == "executable" } {
496 append add_flags " $ldflags"
498 if {[file exists $x]} {
501 append add_flags " $x"
505 if {[board_info $dest exists libs]} {
506 append add_flags " [board_info $dest libs]"
509 # This probably isn't such a good idea, but it avoids nasty
510 # hackiness in the testsuites.
511 # The math library must be linked in before the C library. The C
512 # library is linked in by the linker script, so this must be before
514 if {[board_info $dest exists mathlib]} {
515 append add_flags " [board_info $dest mathlib]"
517 append add_flags " -lm"
520 # This must be added here.
521 append add_flags " $ldscript"
523 if {[board_info $dest exists remote_link]} {
525 append add_flags " -Wl,-r"
527 if {[board_info $dest exists output_format]} {
528 append add_flags " -Wl,-oformat,[board_info $dest output_format]"
532 if {[board_info $dest exists multilib_flags]} {
533 append add_flags " [board_info $dest multilib_flags]"
536 verbose "doing compile"
539 if {[is_remote host]} {
541 set file [remote_download host $x]
543 warning "Unable to download $x to host."
544 return "Unable to download $x to host."
546 append sources " $file"
553 if {[is_remote host]} {
554 append add_flags " -o " [file tail $destfile]
555 remote_file host delete [file tail $destfile]
557 if { $destfile != "" } {
558 append add_flags " -o $destfile"
562 # This is obscure: we put SOURCES at the end when building an
563 # object, because otherwise, in some situations, libtool will
564 # become confused about the name of the actual source file.
567 set opts "$early_flags $add_flags $sources"
570 switch $linker_opts_order {
571 "flags-then-sources" {
572 set opts "$early_flags $add_flags $sources"
574 "sources-then-flags" {
575 set opts "$early_flags $sources $add_flags"
578 error "Invalid value for board_info linker_opts_order"
583 set opts "$early_flags $sources $add_flags"
587 if {[is_remote host]} {
588 if {[host_info exists use_at]} {
589 set fid [open "atfile" "w"]
592 set opts "@[remote_download host atfile]"
593 remote_file build delete atfile
597 verbose "Invoking the compiler as $compiler $opts" 2
599 if {[info exists redirect]} {
600 verbose "Redirecting output to $redirect" 2
601 set status [remote_exec host "$compiler $opts" "" "" $redirect]
603 if {[info exists timeout]} {
604 verbose "Setting timeout to $timeout" 2
605 set status [remote_exec host "$compiler $opts" "" "" "" $timeout]
607 set status [remote_exec host "$compiler $opts"]
611 set compiler_flags $opts
612 if {[is_remote host]} {
613 remote_upload host [file tail $destfile] $destfile
614 remote_file host delete [file tail $destfile]
616 set comp_output [prune_warnings [lindex $status 1]]
617 regsub "^\[\r\n\]+" $comp_output "" comp_output
618 if { [lindex $status 0] != 0 } {
619 verbose -log "compiler exited with status [lindex $status 0]"
621 if { [lindex $status 1] != "" } {
622 verbose -log "output is:\n[lindex $status 1]" 2
624 if { [lindex $status 0] != 0 && "${comp_output}" == "" } {
625 set comp_output "exit status is [lindex $status 0]"
627 return ${comp_output}
630 # See if the version of dejaGNU being used to run the testsuite is
631 # recent enough to contain support for building Ada programs or not.
632 # If not, then use the functions above in place of the ones provided
633 # by dejaGNU. This is only temporary (brobecker/2004-03-31).
635 set use_gdb_compile 0
636 if {[info procs find_gnatmake] == ""} {
637 rename gdb_find_gnatmake find_gnatmake
638 set use_gdb_compile 1
641 if {[info procs find_gfortran] == ""} {
642 rename gdb_find_gfortran find_gfortran
643 set use_gdb_compile 1
646 if {[info procs find_go_linker] == ""} {
647 rename gdb_find_go find_go
648 rename gdb_find_go_linker find_go_linker
649 set use_gdb_compile 1
652 if {[info procs find_gdc] == ""} {
653 rename gdb_find_gdc find_gdc
654 set use_gdb_compile 1
657 if {[info procs find_rustc] == ""} {
658 rename gdb_find_rustc find_rustc
659 set use_gdb_compile 1
662 if {$use_gdb_compile} {
663 catch {rename default_target_compile {}}
664 rename gdb_default_target_compile default_target_compile
668 # Provide 'lreverse' missing in Tcl before 7.5.
670 if {[info procs lreverse] == ""} {
671 proc lreverse { arg } {
673 while { [llength $retval] < [llength $arg] } {
674 lappend retval [lindex $arg end-[llength $retval]]
680 # Various ccache versions provide incorrect debug info such as ignoring
681 # different current directory, breaking GDB testsuite.
682 set env(CCACHE_DISABLE) 1
683 unset -nocomplain env(CCACHE_NODISABLE)