1 # Copyright 2004-2020 Free Software Foundation, Inc.
2 # Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # FIXME:brobecker/2004-03-31:
20 # The following functions should eventually be part of dejagnu. Even after
21 # these functions becomes available in dejagnu, we will keep for a while
22 # a copy here in order to avoid increasing the dejagnu version
25 proc gdb_find_gnatmake {} {
28 set root "$tool_root_dir/gcc"
31 if ![is_remote host] {
32 set file [lookfor_file $root gnatmake]
34 set GM "$file -I$root/ada/rts --GCC=$root/xgcc --GNATBIND=$root/gnatbind --GNATLINK=$root/gnatlink -cargs -B$root -largs --GCC=$root/xgcc -margs";
39 set GM [transform gnatmake]
45 proc gdb_find_gdc {} {
47 print "Tool Root: $tool_root_dir"
49 if {![is_remote host]} {
50 set file [lookfor_file $tool_root_dir gdc]
52 set file [lookfor_file $tool_root_dir gcc/gdc]
55 set CC "$file -B[file dirname $file]/"
57 set CC [transform gdc]
60 set CC [transform gdc]
66 proc gdb_find_gfortran {} {
69 if {![is_remote host]} {
70 set file [lookfor_file $tool_root_dir gfortran]
72 set file [lookfor_file $tool_root_dir gcc/gfortran]
75 set CC "$file -B[file dirname $file]/"
77 set CC [transform gfortran]
80 set CC [transform gfortran]
90 if {![is_remote host]} {
91 set file [lookfor_file $tool_root_dir gccgo]
93 set root [file dirname $file]
94 set GO "$file -B$root/gcc/"
99 set GO [transform gccgo]
105 proc gdb_find_go_linker {} {
109 proc gdb_find_rustc {} {
111 if {![is_remote host]} {
112 set rustc [lookfor_file $tool_root_dir rustc]
120 append rustc " --color never"
125 proc gdb_find_hipcc {} {
127 if {![is_remote host]} {
128 set hipcc [lookfor_file $tool_root_dir hipcc]
130 set hipcc [lookfor_file /opt/rocm/bin hipcc]
138 proc gdb_find_ldd {} {
139 global LDD_FOR_TARGET
140 if [info exists LDD_FOR_TARGET] {
141 set ldd $LDD_FOR_TARGET
148 proc gdb_find_objcopy {} {
149 global OBJCOPY_FOR_TARGET
150 if [info exists OBJCOPY_FOR_TARGET] {
151 set objcopy $OBJCOPY_FOR_TARGET
153 set objcopy [transform objcopy]
158 # find target objdump
159 proc gdb_find_objdump {} {
160 global OBJDUMP_FOR_TARGET
161 if [info exists OBJDUMP_FOR_TARGET] {
162 set objdump $OBJDUMP_FOR_TARGET
164 set objdump [transform objdump]
169 proc gdb_find_readelf {} {
170 global READELF_FOR_TARGET
171 if [info exists READELF_FOR_TARGET] {
172 set readelf $READELF_FOR_TARGET
174 set readelf [transform readelf]
179 proc gdb_find_eu-unstrip {} {
180 global EU_UNSTRIP_FOR_TARGET
181 if [info exists EU_UNSTRIP_FOR_TARGET] {
182 set eu_unstrip $EU_UNSTRIP_FOR_TARGET
184 set eu_unstrip [transform eu-unstrip]
189 proc gdb_default_target_compile {source destfile type options} {
190 global target_triplet
192 global CFLAGS_FOR_TARGET
193 global compiler_flags
195 if { $destfile == "" && $type != "preprocess" && $type != "none" } {
196 error "Must supply an output filename for the compile to default_target_compile"
202 set compiler_type "c"
205 # linker_opts_order is one of "sources-then-flags", "flags-then-sources".
206 # The order matters for things like -Wl,--as-needed. The default is to
207 # preserve existing behavior.
208 set linker_opts_order "sources-then-flags"
210 set dest [target_info name]
212 if {[info exists CFLAGS_FOR_TARGET]} {
213 append add_flags " $CFLAGS_FOR_TARGET"
216 if {[info exists target_info(host,name)]} {
217 set host [host_info name]
225 set compiler_type "ada"
226 if {[board_info $dest exists adaflags]} {
227 append add_flags " [target_info adaflags]"
229 if {[board_info $dest exists gnatmake]} {
230 set compiler [target_info gnatmake]
232 set compiler [find_gnatmake]
237 set compiler_type "c++"
238 if {[board_info $dest exists cxxflags]} {
239 append add_flags " [target_info cxxflags]"
241 append add_flags " [g++_include_flags]"
242 if {[board_info $dest exists c++compiler]} {
243 set compiler [target_info c++compiler]
245 set compiler [find_g++]
250 set compiler_type "d"
251 if {[board_info $dest exists dflags]} {
252 append add_flags " [target_info dflags]"
254 if {[board_info $dest exists dcompiler]} {
255 set compiler [target_info dcompiler]
257 set compiler [find_gdc]
262 set compiler_type "f77"
263 if {[board_info $dest exists f77flags]} {
264 append add_flags " [target_info f77flags]"
266 if {[board_info $dest exists f77compiler]} {
267 set compiler [target_info f77compiler]
269 set compiler [find_g77]
274 set compiler_type "f90"
275 if {[board_info $dest exists f90flags]} {
276 append add_flags " [target_info f90flags]"
278 if {[board_info $dest exists f90compiler]} {
279 set compiler [target_info f90compiler]
281 set compiler [find_gfortran]
286 set compiler_type "go"
287 if {[board_info $dest exists goflags]} {
288 append add_flags " [target_info goflags]"
290 if {[board_info $dest exists gocompiler]} {
291 set compiler [target_info gocompiler]
293 set compiler [find_go]
295 if {[board_info $dest exists golinker]} {
296 set linker [target_info golinker]
298 set linker [find_go_linker]
300 if {[board_info $dest exists golinker_opts_order]} {
301 set linker_opts_order [target_info golinker_opts_order]
305 if { $i == "rust" } {
306 set compiler_type "rust"
307 if {[board_info $dest exists rustflags]} {
308 append add_flags " [target_info rustflags]"
310 if {[board_info $dest exists rustflags]} {
311 set compiler [target_info rustflags]
313 set compiler [find_rustc]
318 set compiler_type "hip"
319 if {[board_info $dest exists hipflags]} {
320 append add_flags " [target_info hipflags]"
322 if {[board_info $dest exists hipcompiler]} {
323 set compiler [target_info hipcompiler]
325 set compiler [find_hipcc]
329 if {[regexp "^dest=" $i]} {
330 regsub "^dest=" $i "" tmp
331 if {[board_info $tmp exists name]} {
332 set dest [board_info $tmp name]
337 if {[regexp "^compiler=" $i]} {
338 regsub "^compiler=" $i "" tmp
341 if {[regexp "^early_flags=" $i]} {
342 regsub "^early_flags=" $i "" tmp
343 append early_flags " $tmp"
345 if {[regexp "^additional_flags=" $i]} {
346 regsub "^additional_flags=" $i "" tmp
347 append add_flags " $tmp"
349 if {[regexp "^ldflags=" $i]} {
350 regsub "^ldflags=" $i "" tmp
351 append ldflags " $tmp"
353 if {[regexp "^libs=" $i]} {
354 regsub "^libs=" $i "" tmp
357 if {[regexp "^incdir=" $i]} {
358 regsub "^incdir=" $i "-I" tmp
359 append add_flags " $tmp"
361 if {[regexp "^libdir=" $i]} {
362 regsub "^libdir=" $i "-L" tmp
363 append add_flags " $tmp"
365 if {[regexp "^ldscript=" $i]} {
366 regsub "^ldscript=" $i "" ldscript
368 if {[regexp "^redirect=" $i]} {
369 regsub "^redirect=" $i "" redirect
371 if {[regexp "^optimize=" $i]} {
372 regsub "^optimize=" $i "" optimize
374 if {[regexp "^timeout=" $i]} {
375 regsub "^timeout=" $i "" timeout
379 if {[board_info $host exists cflags_for_target]} {
380 append add_flags " [board_info $host cflags_for_target]"
384 global CXX_FOR_TARGET
386 global F77_FOR_TARGET
387 global F90_FOR_TARGET
388 global GNATMAKE_FOR_TARGET
390 global GO_LD_FOR_TARGET
391 global RUSTC_FOR_TARGET
392 global HIPCC_FOR_TARGET
394 if {[info exists GNATMAKE_FOR_TARGET]} {
395 if { $compiler_type == "ada" } {
396 set compiler $GNATMAKE_FOR_TARGET
400 if {[info exists CC_FOR_TARGET]} {
401 if { $compiler == "" } {
402 set compiler $CC_FOR_TARGET
406 if {[info exists CXX_FOR_TARGET]} {
407 if { $compiler_type == "c++" } {
408 set compiler $CXX_FOR_TARGET
412 if {[info exists D_FOR_TARGET]} {
413 if { $compiler_type == "d" } {
414 set compiler $D_FOR_TARGET
418 if {[info exists F77_FOR_TARGET]} {
419 if { $compiler_type == "f77" } {
420 set compiler $F77_FOR_TARGET
424 if {[info exists F90_FOR_TARGET]} {
425 if { $compiler_type == "f90" } {
426 set compiler $F90_FOR_TARGET
430 if { $compiler_type == "go" } {
431 if {[info exists GO_FOR_TARGET]} {
432 set compiler $GO_FOR_TARGET
434 if {[info exists GO_LD_FOR_TARGET]} {
435 set linker $GO_LD_FOR_TARGET
439 if {[info exists RUSTC_FOR_TARGET]} {
440 if {$compiler_type == "rust"} {
441 set compiler $RUSTC_FOR_TARGET
445 if {[info exists HIPCC_FOR_TARGET]} {
446 if {$compiler_type == "hip"} {
447 set compiler $HIPCC_FOR_TARGET
451 if { $type == "executable" && $linker != "" } {
455 if { $compiler == "" } {
456 set compiler [board_info $dest compiler]
457 if { $compiler == "" } {
458 return "default_target_compile: No compiler to compile with"
462 if {![is_remote host]} {
463 if { [which $compiler] == 0 } {
464 return "default_target_compile: Can't find $compiler."
468 if {$type == "object"} {
469 if {$compiler_type == "rust"} {
470 append add_flags "--emit obj"
472 append add_flags " -c"
476 if { $type == "preprocess" } {
477 append add_flags " -E"
480 if { $type == "assembly" } {
481 append add_flags " -S"
484 if {[board_info $dest exists cflags]} {
485 append add_flags " [board_info $dest cflags]"
488 if { $type == "executable" } {
489 if {[board_info $dest exists ldflags]} {
490 append add_flags " [board_info $dest ldflags]"
492 if { $compiler_type == "c++" } {
493 append add_flags " [g++_link_flags]"
497 catch "glob -nocomplain $tool_root_dir/libstdc++/libstdc++.so* $tool_root_dir/libstdc++/libstdc++.sl" tmp
498 if { ${tmp} != "" } {
499 if {[regexp ".*solaris2.*" $target_triplet]} {
501 append add_flags " -R$tool_root_dir/libstdc++"
502 } elseif {[regexp ".*(osf|irix5|linux).*" $target_triplet]} {
504 append add_flags " -Wl,-rpath,$tool_root_dir/libstdc++"
510 if {![info exists ldscript]} {
511 set ldscript [board_info $dest ldscript]
515 if { $i == "debug" } {
516 if {[board_info $dest exists debug_flags]} {
517 append add_flags " [board_info $dest debug_flags]"
519 append add_flags " -g"
524 if {[info exists optimize]} {
525 append add_flags " $optimize"
528 if { $type == "executable" } {
529 append add_flags " $ldflags"
531 if {[file exists $x]} {
534 append add_flags " $x"
538 if {[board_info $dest exists libs]} {
539 append add_flags " [board_info $dest libs]"
542 # This probably isn't such a good idea, but it avoids nasty
543 # hackiness in the testsuites.
544 # The math library must be linked in before the C library. The C
545 # library is linked in by the linker script, so this must be before
547 if {[board_info $dest exists mathlib]} {
548 append add_flags " [board_info $dest mathlib]"
550 append add_flags " -lm"
553 # This must be added here.
554 append add_flags " $ldscript"
556 if {[board_info $dest exists remote_link]} {
558 append add_flags " -Wl,-r"
560 if {[board_info $dest exists output_format]} {
561 append add_flags " -Wl,-oformat,[board_info $dest output_format]"
565 if {[board_info $dest exists multilib_flags]} {
566 append add_flags " [board_info $dest multilib_flags]"
569 verbose "doing compile"
572 if {[is_remote host]} {
574 set file [remote_download host $x]
576 warning "Unable to download $x to host."
577 return "Unable to download $x to host."
579 append sources " $file"
586 if {[is_remote host]} {
587 append add_flags " -o " [file tail $destfile]
588 remote_file host delete [file tail $destfile]
590 if { $destfile != "" } {
591 append add_flags " -o $destfile"
595 # This is obscure: we put SOURCES at the end when building an
596 # object, because otherwise, in some situations, libtool will
597 # become confused about the name of the actual source file.
600 set opts "$early_flags $add_flags $sources"
603 switch $linker_opts_order {
604 "flags-then-sources" {
605 set opts "$early_flags $add_flags $sources"
607 "sources-then-flags" {
608 set opts "$early_flags $sources $add_flags"
611 error "Invalid value for board_info linker_opts_order"
616 set opts "$early_flags $sources $add_flags"
620 if {[is_remote host]} {
621 if {[host_info exists use_at]} {
622 set fid [open "atfile" "w"]
625 set opts "@[remote_download host atfile]"
626 remote_file build delete atfile
630 verbose "Invoking the compiler as $compiler $opts" 2
632 if {[info exists redirect]} {
633 verbose "Redirecting output to $redirect" 2
634 set status [remote_exec host "$compiler $opts" "" "" $redirect]
636 if {[info exists timeout]} {
637 verbose "Setting timeout to $timeout" 2
638 set status [remote_exec host "$compiler $opts" "" "" "" $timeout]
640 set status [remote_exec host "$compiler $opts"]
644 set compiler_flags $opts
645 if {[is_remote host]} {
646 remote_upload host [file tail $destfile] $destfile
647 remote_file host delete [file tail $destfile]
649 set comp_output [prune_warnings [lindex $status 1]]
650 regsub "^\[\r\n\]+" $comp_output "" comp_output
651 if { [lindex $status 0] != 0 } {
652 verbose -log "compiler exited with status [lindex $status 0]"
654 if { [lindex $status 1] != "" } {
655 verbose -log "output is:\n[lindex $status 1]" 2
657 if { [lindex $status 0] != 0 && "${comp_output}" == "" } {
658 set comp_output "exit status is [lindex $status 0]"
660 return ${comp_output}
663 # See if the version of dejaGNU being used to run the testsuite is
664 # recent enough to contain support for building Ada programs or not.
665 # If not, then use the functions above in place of the ones provided
666 # by dejaGNU. This is only temporary (brobecker/2004-03-31).
668 set use_gdb_compile 0
669 if {[info procs find_gnatmake] == ""} {
670 rename gdb_find_gnatmake find_gnatmake
671 set use_gdb_compile 1
674 if {[info procs find_gfortran] == ""} {
675 rename gdb_find_gfortran find_gfortran
676 set use_gdb_compile 1
679 if {[info procs find_go_linker] == ""} {
680 rename gdb_find_go find_go
681 rename gdb_find_go_linker find_go_linker
682 set use_gdb_compile 1
685 if {[info procs find_gdc] == ""} {
686 rename gdb_find_gdc find_gdc
687 set use_gdb_compile 1
690 if {[info procs find_rustc] == ""} {
691 rename gdb_find_rustc find_rustc
692 set use_gdb_compile 1
695 if {[info procs find_hipcc] == ""} {
696 rename gdb_find_hipcc find_hipcc
697 set use_gdb_compile 1
700 if {$use_gdb_compile} {
701 catch {rename default_target_compile {}}
702 rename gdb_default_target_compile default_target_compile
706 # Provide 'lreverse' missing in Tcl before 7.5.
708 if {[info procs lreverse] == ""} {
709 proc lreverse { arg } {
711 while { [llength $retval] < [llength $arg] } {
712 lappend retval [lindex $arg end-[llength $retval]]
718 # Various ccache versions provide incorrect debug info such as ignoring
719 # different current directory, breaking GDB testsuite.
720 set env(CCACHE_DISABLE) 1
721 unset -nocomplain env(CCACHE_NODISABLE)