OSDN Git Service

2005-09-01 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / lib / libstdc++.exp
1 # libstdc++ "tool init file" for DejaGNU
2
3 # Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
18
19
20 # Define callbacks and load other libraries.  This file is loaded relatively
21 # early, and before any other file we write ourselves.  "load_lib" will
22 # find anything in the DejaGNU installation tree, or in our lib directory.
23 # "load_gcc_lib" will search the core compiler's .exp collection instead.
24 #
25 # The naming rule is that dg.exp looks for "tool-" and runtest.exp looks
26 # for "tool_" when finding callbacks.  Utility routines we define for
27 # our callbacks begin with "v3-".
28 #
29 # libstdc++_* callbacks we don't define, but could:
30 # ..._option_help           prints additional --help output
31 # ..._option_proc (--foo)   process our own options
32 # ..._init (normal.exp)     called once per test file
33 # ..._finish                bracketing function for libstdc++_init
34 # ...-dg-prune              removing output text, see top of system dg.exp
35 #
36 # Useful hook:  if ${hostname}_init exists, it will be called, almost
37 # the last thing before testing begins.  This can be defined in, e.g.,
38 # ~/.dejagnurc or $DEJAGNU.
39
40 proc load_gcc_lib { filename } {
41     global srcdir
42     load_file $srcdir/../../gcc/testsuite/lib/$filename
43 }
44
45 # system routines
46 load_lib dg.exp
47 load_lib libgloss.exp
48 # compiler routines, then ours
49 load_gcc_lib target-supports.exp
50 load_gcc_lib target-supports-dg.exp
51 load_lib prune.exp
52 load_lib dg-options.exp
53 load_gcc_lib target-libpath.exp
54 load_gcc_lib wrapper.exp
55
56 # Useful for debugging.  Pass the name of a variable and the verbosity
57 # threshold (number of -v's on the command line).
58 proc v3track { var n } {
59     upvar $var val
60     verbose "++ $var is $val" $n
61 }
62
63 # Called by v3-init below.  "Static" to this file.
64 proc v3-copy-files {srcfiles} {
65     foreach f $srcfiles {
66         if { [catch { set symlink [file readlink $f] } x] } then {
67             remote_download target $f
68         } else {
69             if { [regexp "^/" "$symlink"] } then {
70                 remote_download target $symlink
71             } else {
72                 set dirname [file dirname $f]
73                 remote_download target $dirname/$symlink
74             }
75         }
76     }
77 }
78
79 # Called once, during runtest.exp setup.
80 proc libstdc++_init { testfile } {
81     global env
82     global srcdir blddir objdir tool_root_dir
83     global cxx cxxflags
84     global includes
85     global gluefile wrap_flags
86     global ld_library_path
87     global target_triplet
88
89     # We set LC_ALL and LANG to C so that we get the same error
90     # messages as expected.
91     setenv LC_ALL C
92     setenv LANG C
93
94     set blddir [lookfor_file [get_multilibs] libstdc++-v3]
95     set flags_file "${blddir}/scripts/testsuite_flags"
96     v3track flags_file 2
97
98     # If a test doesn't have special options, use DEFAULT_CXXFLAGS.
99     # Use this variable if the behavior
100     #   1) only applies to libstdc++ testing
101     #   2) might need to be negated 
102     # In particular, some tests have to be run without precompiled
103     # headers, or without assertions.
104     global DEFAULT_CXXFLAGS
105     if ![info exists DEFAULT_CXXFLAGS] then {
106         set DEFAULT_CXXFLAGS ""
107         # Host specific goo here.
108         if { [string match "powerpc-*-darwin*" $target_triplet] } {
109             append DEFAULT_CXXFLAGS " -multiply_defined suppress"
110         } 
111     }
112     v3track DEFAULT_CXXFLAGS 2
113
114     # By default, we assume we want to run program images.
115     global dg-do-what-default
116     set dg-do-what-default run
117
118     # Copy any required data files.
119     v3-copy-files [glob -nocomplain "$srcdir/data/*.tst"]
120     v3-copy-files [glob -nocomplain "$srcdir/data/*.txt"]
121
122     # Locate libgcc.a so we don't need to account for different values of
123     # SHLIB_EXT on different platforms
124     set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
125     if {$gccdir != ""} {
126         set gccdir [file dirname $gccdir]
127     }
128     v3track gccdir 3
129
130     # Compute what needs to be added to the existing LD_LIBRARY_PATH.
131     if {$gccdir != ""} {
132         set ld_library_path ""
133         append ld_library_path ":${gccdir}"
134         set compiler ${gccdir}/g++
135         append ld_library_path ":${blddir}/src/.libs"
136
137         if { [is_remote host] == 0 && [which $compiler] != 0 } {
138           foreach i "[exec $compiler --print-multi-lib]" {
139             set mldir ""
140             regexp -- "\[a-z0-9=/\.-\]*;" $i mldir
141             set mldir [string trimright $mldir "\;@"]
142             if { "$mldir" == "." } {
143               continue
144             }
145             if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
146               append ld_library_path ":${gccdir}/${mldir}"
147             }
148           }
149         }
150
151         set_ld_library_path_env_vars
152         if [info exists env(LD_LIBRARY_PATH)] {
153           verbose -log "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
154         }
155     } else {
156         set compiler [transform "g++"]
157     }
158
159     # Do a bunch of handstands and backflips for cross compiling and
160     # finding simulators...
161     if [is_remote host] {
162         set header [remote_download host ${blddir}/testsuite/testsuite_hooks.h]
163         if { $header == "" } {
164             verbose -log "Unable to download ${blddir}/testsuite/testsuite_hooks.h to host."
165             return "untested"
166         }
167       set cxx [transform "g++"]
168       set cxxflags "-ggdb3"
169       set includes "-I./"
170     } else {
171         # If we find a testsuite_flags file, we're testing in the build dir.
172         if { [file exists $flags_file] } {
173             set cxx [exec sh $flags_file --build-cxx]
174             set cxxflags [exec sh $flags_file --cxxflags]
175             set includes [exec sh $flags_file --build-includes]
176         } else {
177             set cxx [transform "g++"]
178             set cxxflags "-g -O2 -D_GLIBCXX_ASSERT -fmessage-length=0" 
179             set includes "-I${srcdir}"
180             # Guess at the location of the installed locale files.
181             # (It would be nice if "gcc --print-file-name" could find
182             # message files, but it cannot.)
183             set absolute_cxx [which $cxx]
184             if { $absolute_cxx != "" } {
185                 set localedir "[file dirname $absolute_cxx]/../share/locale"
186             }
187             set cxxflags "$cxxflags -DLOCALEDIR=\"$localedir\""
188         }
189     }
190
191     # If a PCH file is available, use it.  We must delay performing
192     # this check until $cxx and such have been initialized because we
193     # perform a test compilation.  (Ideally, gcc --print-file-name would
194     # list PCH files, but it does not.)
195     global PCH_CXXFLAGS
196     if ![info exists PCH_CXXFLAGS] then {
197         set src "config[pid].cc"
198         set f [open $src "w"]
199         puts $f "int main () {}"
200         close $f
201
202         set lines [v3_target_compile $src "config[pid].o" object \
203                    "additional_flags=-include additional_flags=bits/stdc++.h"]
204         if {$lines == "" } {
205             set PCH_CXXFLAGS "-include bits/stdc++.h"
206         } else {
207             set PCH_CXXFLAGS ""
208         }
209         file delete $src
210         v3track PCH_CXXFLAGS 2
211     }
212
213     libstdc++_maybe_build_wrapper "${objdir}/testglue.o"
214 }
215
216 # Callback for cleanup routines.
217 proc libstdc++_exit { } {
218     global gluefile;
219
220     if [info exists gluefile] {
221         file_on_build delete $gluefile;
222         unset gluefile;
223     }
224 }
225
226 # Callback from system dg-test.
227 proc libstdc++-dg-test { prog do_what extra_tool_flags } {
228     # Set up the compiler flags, based on what we're going to do.
229     switch $do_what {
230         "preprocess" {
231             set compile_type "preprocess"
232             set output_file "[file rootname [file tail $prog]].i"
233         }
234         "compile" {
235             set compile_type "assembly"
236             set output_file "[file rootname [file tail $prog]].s"
237         }
238         "assemble" {
239             set compile_type "object"
240             set output_file "[file rootname [file tail $prog]].o"
241         }
242         "link" {
243             set compile_type "executable"
244             set output_file "./[file rootname [file tail $prog]].exe"
245         }
246         "run" {
247             set compile_type "executable"
248             # FIXME: "./" is to cope with "." not being in $PATH.
249             # Should this be handled elsewhere?
250             # YES.
251             set output_file "./[file rootname [file tail $prog]].exe"
252             # This is the only place where we care if an executable was
253             # created or not.  If it was, dg.exp will try to run it.
254             remote_file build delete $output_file;
255         }
256         default {
257             perror "$do_what: not a valid dg-do keyword"
258             return ""
259         }
260     }
261     set options ""
262     if { $extra_tool_flags != "" } {
263         lappend options "additional_flags=$extra_tool_flags"
264     }
265
266     # There is a libstdc++_compile made for us by default (via the tool-
267     # and-target file), but the defaults are lacking in goodness.
268     set comp_output [v3_target_compile "$prog" "$output_file" "$compile_type" $options];
269     set comp_output [ prune_g++_output $comp_output ];
270
271     return [list $comp_output $output_file]
272 }
273
274 # True if the library supports wchar_t.
275 set v3-wchar_t 0
276
277 # True if the library supports threads.
278 set v3-threads 0
279
280 # True if the library supports symbol versioning.
281 set v3-symver 0
282
283 # A string naming object files to be linked into all tests.
284 set v3-test_objs ""
285
286 # Called from libstdc++-dg-test above.  Calls back into system's
287 # target_compile to actually do the work.
288 proc v3_target_compile { source dest type options } {
289     global gluefile 
290     global wrap_flags
291     global cxx
292     global cxxflags
293     global includes
294     global blddir
295     global v3-test_objs
296
297     if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
298         lappend options "libs=${gluefile}"
299         lappend options "ldflags=${wrap_flags}"
300     }
301
302     set cxx_final $cxx
303     set cxxlibglossflags  [libgloss_link_flags]
304     set cxx_final [concat $cxx_final $cxxlibglossflags]
305     set cxx_final [concat $cxx_final $cxxflags]
306     set cxx_final [concat $cxx_final $includes]
307     # Link the support objects into executables.
308     if { $type == "executable" } {
309         set cxx_final [concat $cxx_final ${v3-test_objs}]
310     }
311
312     lappend options "compiler=$cxx_final"
313
314     return [target_compile $source $dest $type $options]
315 }
316
317
318 # Build the support objects linked in with the libstdc++ tests.  In
319 # addition, set v3-wchar_t, v3-threads, v3-test_objs, and v3-symver
320 # appropriately.
321 proc v3-build_support {} {
322     global srcdir
323     global v3-wchar_t
324     global v3-threads
325     global v3-test_objs
326     global v3-symver
327
328     # Figure out whether or not the library supports certain features.
329     set v3-wchar_t 0
330     set v3-threads 0
331     set v3-symver 0
332     set v3-test_objs ""
333
334     set config_src "config.cc"
335     set f [open $config_src "w"]
336     puts $f "#include <bits/c++config.h>"
337     puts $f "#include <bits/gthr.h>"
338     close $f
339     set preprocessed [v3_target_compile $config_src "" \
340                        preprocess "additional_flags=-dN"]
341     if { [string first "_GLIBCXX_USE_WCHAR_T" $preprocessed] != -1 } {
342         verbose -log "wchar_t support detected"
343         set v3-wchar_t 1
344     }
345     if { [string first "_GLIBCXX_SYMVER" $preprocessed] != -1 } {
346         verbose -log "symbol versioning support detected"
347         set v3-symver 1
348     }
349     if { [string first "__GTHREADS" $preprocessed] != -1 } {
350         verbose -log "thread support detected"
351         set v3-threads 1
352     }
353
354     # Build the support objects.
355     set source_files \
356         [list testsuite_abi.cc testsuite_allocator.cc testsuite_character.cc testsuite_hooks.cc]
357     foreach f $source_files {
358         set object_file [file rootname $f].o
359         # Compile with "-w" so that warnings issued by the compiler
360         # do not prevent compilation.
361         if { [v3_target_compile $srcdir/$f $object_file "object" \
362                   [list "incdir=$srcdir" "additional_flags=-w"]]
363              != "" } {
364             error "could not compile $f"
365         }
366         append v3-test_objs "$object_file "
367     }
368 }
369
370 proc check_v3_target_namedlocale { } {
371     global et_namedlocale_saved
372     global et_namedlocale_target_name
373     global tool 
374
375     if { ![info exists et_namedlocale_target_name] } {
376         set et_namedlocale_target_name ""
377     }
378
379     # If the target has changed since we set the cached value, clear it.
380     set current_target [current_target_name]
381     if { $current_target != $et_namedlocale_target_name } {
382         verbose "check_v3_target_namedlocale: `$et_namedlocale_target_name'" 2
383         set et_namedlocale_target_name $current_target
384         if [info exists et_namedlocale_saved] {
385             verbose "check_v3_target_namedlocale: removing cached result" 2
386             unset et_namedlocale_saved
387         }
388     }
389
390     if [info exists et_namedlocale_saved] {
391         verbose "check_v3_target_namedlocale: using cached result" 2
392     } else {
393         set et_namedlocale_saved 0
394
395         # Set up, compile, and execute a C++ test program that tries to use
396         # all the required named locales.
397         set src nlocale[pid].cc
398         set exe nlocale[pid].x
399
400         set f [open $src "w"]
401         puts $f "#include <locale>"
402         puts $f "using namespace std;"  
403         puts $f "int main ()"
404         puts $f "{"
405         puts $f "  try" 
406         puts $f "  {" 
407         puts $f "     locale(\"\");"
408         puts $f "     locale(\"de_DE\");"
409         puts $f "     locale(\"de_DE.ISO-8859-15@euro\");"
410         puts $f "     locale(\"de_DE@euro\");"
411         puts $f "     locale(\"en_HK\");"
412         puts $f "     locale(\"en_PH\");"
413         puts $f "     locale(\"en_US\");"
414         puts $f "     locale(\"en_US.ISO-8859-1\");"
415         puts $f "     locale(\"en_US.ISO-8859-15\");"
416         puts $f "     locale(\"en_US.UTF-8\");"
417         puts $f "     locale(\"es_ES\");"
418         puts $f "     locale(\"es_MX\");"
419         puts $f "     locale(\"fr_FR\");"
420         puts $f "     locale(\"fr_FR@euro\");"
421         puts $f "     locale(\"is_IS\");"
422         puts $f "     locale(\"is_IS.UTF-8\");"
423         puts $f "     locale(\"it_IT\");"
424         puts $f "     locale(\"ja_JP.eucjp\");"
425         puts $f "     locale(\"se_NO.UTF-8\");"
426         puts $f "     locale(\"ta_IN\");"
427         puts $f "     locale(\"zh_TW\");"
428         puts $f "     return 0;"
429         puts $f "  }"
430         puts $f "  catch(...)"
431         puts $f "  {"
432         puts $f "     return 1;"
433         puts $f "  }"   
434         puts $f "}"
435         close $f
436
437         set lines [v3_target_compile $src $exe executable ""]
438         file delete $src
439
440         if [string match "" $lines] {
441             # No error message, compilation succeeded.
442             set result [${tool}_load "./$exe" "" ""]
443             set status [lindex $result 0]
444             remote_file build delete $exe
445
446             verbose "check_v3_target_namedlocale: status is <$status>" 2
447
448             if { $status == "pass" } {
449                 set et_namedlocale_saved 1
450             }
451         } else {
452             verbose "check_v3_target_namedlocale: compilation failed" 2
453         }
454     }
455     return $et_namedlocale_saved
456 }
457
458 proc check_v3_target_cxa_atexit { } {
459     global et_cxa_atexit
460     global et_cxa_atexit_target_name
461     global tool 
462
463     if { ![info exists et_cxa_atexit_target_name] } {
464         set et_cxa_atexit_target_name ""
465     }
466
467     # If the target has changed since we set the cached value, clear it.
468     set current_target [current_target_name]
469     if { $current_target != $et_cxa_atexit_target_name } {
470         verbose "check_v3_target_cxa_atexit: `$et_cxa_atexit_target_name'" 2
471         set et_cxa_atexit_target_name $current_target
472         if [info exists et_cxa_atexit] {
473             verbose "check_v3_target_cxa_atexit: removing cached result" 2
474             unset et_cxa_atexit
475         }
476     }
477
478     if [info exists et_cxa_atexit] {
479         verbose "check_v3_target_cxa_atexit: using cached result" 2
480     } else {
481         set et_cxa_atexit 0
482
483         # Set up, compile, and execute a C++ test program that depends
484         # on correct ordering of static object destructors. This is
485         # indicative of the presence and use of __cxa_atexit.
486         set src cxaatexit[pid].cc
487         set exe cxaatexit[pid].x
488
489         set f [open $src "w"]
490         puts $f "#include <stdlib.h>"
491         puts $f "static unsigned int count;"
492         puts $f "struct X"
493         puts $f "{"
494         puts $f "X() { count = 1; }"
495         puts $f "~X()"
496         puts $f "{"
497         puts $f "  if (count != 3)"
498         puts $f "    exit(1);"
499         puts $f "  count = 4;"
500         puts $f "}"
501         puts $f "};"
502         puts $f "void f()"
503         puts $f "{"
504         puts $f "static X x;"
505         puts $f "}"
506         puts $f "struct Y"
507         puts $f "{"
508         puts $f "Y() { f(); count = 2; }"
509         puts $f "~Y()"
510         puts $f "{"
511         puts $f "if (count != 2)"
512         puts $f "  exit(1);"
513         puts $f "count = 3;"
514         puts $f "}"
515         puts $f "};"
516         puts $f "Y y;"
517         puts $f "int main()"
518         puts $f "{ return 0; }"
519         close $f
520
521         set lines [v3_target_compile $src $exe executable ""]
522         file delete $src
523
524         if [string match "" $lines] {
525             # No error message, compilation succeeded.
526             set result [${tool}_load "./$exe" "" ""]
527             set status [lindex $result 0]
528             remote_file build delete $exe
529
530             verbose "check_v3_target_cxa_atexit: status is <$status>" 2
531
532             if { $status == "pass" } {
533                 set et_cxa_atexit 1
534             }
535         } else {
536             verbose "check_v3_target_cxa_atexit: compilation failed" 2
537         }
538     }
539     return $et_cxa_atexit
540 }