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