1# Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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 2 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, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17# Define libmudflap callbacks for dg.exp.
18# This file is a copy of libstdc++-v3's dejagnu driver, with minor changes.
19
20# Useful hook:  if ${hostname}_init exists, it will be called, almost
21# the last thing before testing begins.  This can be defined in, e.g.,
22# ~/.dejagnurc or $DEJAGNU.
23
24proc load_gcc_lib { filename } {
25    global srcdir
26    load_file $srcdir/../../gcc/testsuite/lib/$filename
27}
28
29load_lib mfdg.exp
30load_lib libgloss.exp
31load_gcc_lib target-libpath.exp
32
33proc libmudflap-init { language } {
34    global env
35    global srcdir outdir blddir objdir tool_root_dir
36    global cxx cxxflags
37    global includes
38    global libs
39    global gluefile wrap_flags
40    global ld_library_path
41
42    switch $language {
43        "c" { set cxx [find_gcc] }
44        "c++" { set cxx [find_g++] }
45        default { error "bad language code $language"; return }
46    }
47
48    verbose -log "libmudflap-init $cxx"
49
50    set blddir [lookfor_file [get_multilibs] libmudflap]
51    set cxxblddir [lookfor_file [get_multilibs] libstdc++-v3]
52    set cxxflags_file "${cxxblddir}/scripts/testsuite_flags"
53
54    # By default, we assume we want to run program images.
55    global dg-do-what-default
56    set dg-do-what-default run
57
58    # set LD_LIBRARY_PATH so that libgcc_s, libstdc++ binaries can be found.
59    # locate libgcc.a so we don't need to account for different values of
60    # SHLIB_EXT on different platforms
61    set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
62    if {$gccdir != ""} {
63	set gccdir [file dirname $gccdir]
64    }
65
66    set ld_library_path "."
67    append ld_library_path ":${gccdir}"
68    append ld_library_path ":${cxxblddir}/src/.libs"
69    if {[is_remote host] == 0} {
70	foreach i "[exec ${gccdir}/xgcc --print-multi-lib]" {
71	    set mldir ""
72	    regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
73	    set mldir [string trimright $mldir "\;@"]
74	    if { "$mldir" == "." } {
75		continue
76	    }
77	    if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
78		append ld_library_path ":${gccdir}/${mldir}"
79	    }
80	}
81    }
82    append ld_library_path ":${blddir}/.libs"
83
84    set libs "-L${blddir}/.libs"
85    set cxxflags "-ggdb3 -DDEBUG_ASSERT"
86    set includes "-I${srcdir} -I${srcdir}/.. -I.."
87
88    if {$language == "c++"} {
89        if {[file exists $cxxflags_file]} then {
90            set includes "${includes} [exec sh $cxxflags_file --build-includes]"
91            set cxxflags "${cxxflags} [exec sh $cxxflags_file --cxxflags]"
92            # c++ libs are included by --build-cxx below
93            set cxx "[exec sh $cxxflags_file --build-cxx]"
94        } else {
95            lappend libs "-L${cxxblddir}src/.libs"
96            lappend includes "-I../../libstdc++-v3/include"
97        }
98    }
99
100    global mfconfig_libs
101    global add_flags
102    append add_flags " $mfconfig_libs"
103
104    set_ld_library_path_env_vars
105    if [info exists env(LD_LIBRARY_PATH)] {
106	verbose -log "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
107    }
108
109    if { [target_info needs_status_wrapper]!=""} {
110      file delete ${objdir}/testglue.o;
111      set gluefile ${objdir}/testglue.o;
112      set result [build_wrapper $gluefile];
113      if { $result != "" } {
114          set gluefile [lindex $result 0];
115          set wrap_flags [lindex $result 1];
116      } else {
117          unset gluefile
118      }
119    }
120
121    # If there is no static library then don't run tests with -static.
122    global tool
123    set opts "additional_flags=-static"
124    lappend opts "additional_flags=-fmudflap"
125    lappend opts "additional_flags=-lmudflap"
126    set src stlm[pid].c
127    set exe stlm[pid].x
128
129    set f [open $src "w"]
130    puts $f "int main () { }"
131    close $f
132    set lines [${tool}_target_compile $src $exe executable "$opts"]
133    file delete $src
134    remote_file build delete $exe
135
136    if { ![string match "" $lines] } {
137	# Compilation failed; assume static library is not available.
138	global MUDFLAP_FLAGS
139	set i [lsearch $MUDFLAP_FLAGS "*static*"]
140	set MUDFLAP_FLAGS [lreplace $MUDFLAP_FLAGS $i $i]
141    }
142}
143
144proc libmudflap-dg-test { prog do_what extra_tool_flags } {
145    # Set up the compiler flags, based on what we're going to do.
146
147    switch $do_what {
148	"preprocess" {
149	    set compile_type "preprocess"
150	    set output_file "[file rootname [file tail $prog]].i"
151	}
152	"compile" {
153	    set compile_type "assembly"
154	    set output_file "[file rootname [file tail $prog]].s"
155	}
156	"assemble" {
157	    set compile_type "object"
158	    set output_file "[file rootname [file tail $prog]].o"
159	}
160	"link" {
161	    set compile_type "executable"
162	    set output_file "./[file rootname [file tail $prog]].exe"
163	}
164	"run" {
165	    set compile_type "executable"
166	    # FIXME: "./" is to cope with "." not being in $PATH.
167	    # Should this be handled elsewhere?
168	    # YES.
169	    set output_file "./[file rootname [file tail $prog]].exe"
170	    # This is the only place where we care if an executable was
171	    # created or not.  If it was, dg.exp will try to run it.
172	    remote_file build delete $output_file;
173	}
174	default {
175	    perror "$do_what: not a valid dg-do keyword"
176	    return ""
177	}
178    }
179    set options ""
180    if { $extra_tool_flags != "" } {
181	lappend options "additional_flags=$extra_tool_flags"
182    }
183
184    global mfconfig_libs
185    lappend options "libs=$mfconfig_libs"
186
187    set comp_output [libmudflap_target_compile "$prog" "$output_file" "$compile_type" $options];
188    set comp_output [prune_gcc_output $comp_output ];
189
190    return [list $comp_output $output_file]
191}
192
193
194proc libmudflap_target_compile { source dest type options } {
195    global gluefile
196    global wrap_flags
197    global cxx
198    global cxxflags
199    global includes
200    global libs
201    global blddir
202
203    if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
204	lappend options "libs=${gluefile}"
205	lappend options "ldflags=${wrap_flags}"
206    }
207
208    set cxx_final $cxx
209    set cxxlibglossflags  [libgloss_link_flags]
210    set cxx_final [concat $cxx_final $cxxlibglossflags]
211    set cxx_final [concat $cxx_final $cxxflags]
212    set cxx_final [concat $cxx_final $includes]
213    set cxx_final [concat $cxx_final $libs]
214
215    lappend options "compiler=$cxx_final"
216
217    # Picks up the freshly-built testsuite library corresponding to the
218    # multilib under test.
219    lappend options "ldflags=-L${blddir}/testsuite"
220
221    return [target_compile $source $dest $type $options]
222}
223
224
225# A bit sloppy...  Returns a list of source files (full pathnames) to
226# compile.  We mimic the mkcheck script in that the first time this is run,
227# all existing files are listed in "testsuite_files" in the output
228# directory.  Subsequent runs pull the list from that file, allowing users
229# to trim the list down to problematic tests.
230### This is supposed to be done via RUNTESTFLAGS, but that doesn't work.
231proc libmudflap-list-sourcefiles { } {
232    global srcdir
233    global outdir
234
235    set files_file "${outdir}/testsuite_files"
236    set sfiles ""
237    if { [file exists $files_file] } {
238	set f [open $files_file]
239	while { ! [eof $f] } {
240	    set t [gets $f]
241	    if { [string length "$t"] != 0 } {
242		lappend sfiles ${srcdir}/${t}
243	    }
244	}
245    } else {
246	set f [open $files_file "w"]
247	set where_we_were [pwd]
248	cd $srcdir
249	foreach s [lsort [glob -nocomplain "*/*.cc" "*/*/*.cc" "{,*/}*/*/*/*.cc" ]] {
250	    lappend sfiles ${srcdir}/${s}
251	    puts $f $s
252	}
253	cd $where_we_were
254    }
255    close $f
256
257    # Disable wchar_t tests if library not configured to support
258    # wchar_t testing.
259    set wchar_file "${outdir}/testsuite_wchar_t"
260    if { [file exists $wchar_file] } {
261	return $sfiles
262    } else {
263	# Remove wchar_t tests files from list.
264	set res {}
265	foreach w $sfiles {
266	    if [regexp "wchar_t" $w] {
267		verbose -log "element out list is $w"
268	    } else {
269		verbose -log "element in list is $w"
270		lappend res $w
271	    }
272	}
273	return $res
274    }
275}
276
277
278proc prune_gcc_output { text } {
279    regsub -all {(^|\n)[^\n]*ld: warning: libgcc_s[^\n]*not found[^\n]*try using[^\n]*} $text "" text
280    regsub -all {(^|\n)[^\n]*In function.*pthread_create[^\n]*} $text "" text
281    regsub -all {(^|\n)[^\n]*the use of .pthread.*is deprecated[^\n]*} $text "" text
282    regsub -all {(^|\n)[^\n]*Dwarf Error:.*FORM value: 14[^\n]*} $text "" text
283    regsub -all {(^|\n)[^\n]*In function[^\n]*} $text "" text
284    regsub -all {(^|\n)[^\n]*Using.*in statically linked applications requires[^\n]*} $text "" text
285
286    return $text
287}
288