OSDN Git Service

2006-10-09 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
18 # 02110-1301, USA.
19
20
21 # Define callbacks and load other libraries.  This file is loaded relatively
22 # early, and before any other file we write ourselves.  "load_lib" will
23 # find anything in the DejaGNU installation tree, or in our lib directory.
24 # "load_gcc_lib" will search the core compiler's .exp collection instead.
25 #
26 # The naming rule is that dg.exp looks for "tool-" and runtest.exp looks
27 # for "tool_" when finding callbacks.  Utility routines we define for
28 # our callbacks begin with "v3-".
29 #
30 # libstdc++_* callbacks we don't define, but could:
31 # ..._option_help           prints additional --help output
32 # ..._option_proc (--foo)   process our own options
33 # ..._init (normal.exp)     called once per test file
34 # ..._finish                bracketing function for libstdc++_init
35 # ...-dg-prune              removing output text, see top of system dg.exp
36 #
37 # Useful hook:  if ${hostname}_init exists, it will be called, almost
38 # the last thing before testing begins.  This can be defined in, e.g.,
39 # ~/.dejagnurc or $DEJAGNU.
40
41 proc load_gcc_lib { filename } {
42     global srcdir
43     load_file $srcdir/../../gcc/testsuite/lib/$filename
44 }
45
46 # system routines
47 load_lib dg.exp
48 load_lib libgloss.exp
49 # compiler routines, then ours
50 load_gcc_lib target-supports.exp
51 load_gcc_lib target-supports-dg.exp
52 load_lib prune.exp
53 load_lib dg-options.exp
54 load_gcc_lib target-libpath.exp
55 load_gcc_lib wrapper.exp
56
57 # Useful for debugging.  Pass the name of a variable and the verbosity
58 # threshold (number of -v's on the command line).
59 proc v3track { var n } {
60     upvar $var val
61     verbose "++ $var is $val" $n
62 }
63
64 # Called by v3-init below.  "Static" to this file.
65 proc v3-copy-files {srcfiles} {
66     foreach f $srcfiles {
67         if { [catch { set symlink [file readlink $f] } x] } then {
68             remote_download target $f
69         } else {
70             if { [regexp "^/" "$symlink"] } then {
71                 remote_download target $symlink
72             } else {
73                 set dirname [file dirname $f]
74                 remote_download target $dirname/$symlink
75             }
76         }
77     }
78 }
79
80 # Called once, during runtest.exp setup.
81 proc libstdc++_init { testfile } {
82     global env
83     global v3-sharedlib 
84     global srcdir blddir objdir tool_root_dir
85     global cxx cxxflags cxxldflags
86     global includes
87     global gluefile wrap_flags
88     global ld_library_path
89     global target_triplet
90
91     # We set LC_ALL and LANG to C so that we get the same error
92     # messages as expected.
93     setenv LC_ALL C
94     setenv LANG C
95
96     set blddir [lookfor_file [get_multilibs] libstdc++-v3]
97     set flags_file "${blddir}/scripts/testsuite_flags"
98     v3track flags_file 2
99
100     # If a test doesn't have special options, use DEFAULT_CXXFLAGS.
101     # Use this variable if the behavior
102     #   1) only applies to libstdc++ testing
103     #   2) might need to be negated 
104     # In particular, some tests have to be run without precompiled
105     # headers, or without assertions.
106     global DEFAULT_CXXFLAGS
107     if ![info exists DEFAULT_CXXFLAGS] then {
108         set DEFAULT_CXXFLAGS ""
109         # Host specific goo here.
110         if { [string match "powerpc-*-darwin*" $target_triplet] } {
111             append DEFAULT_CXXFLAGS " -multiply_defined suppress"
112         } 
113     }
114     v3track DEFAULT_CXXFLAGS 2
115
116     # By default, we assume we want to run program images.
117     global dg-do-what-default
118     set dg-do-what-default run
119
120     # Copy any required data files.
121     v3-copy-files [glob -nocomplain "$srcdir/data/*.tst"]
122     v3-copy-files [glob -nocomplain "$srcdir/data/*.txt"]
123
124     # Locate libgcc.a so we don't need to account for different values of
125     # SHLIB_EXT on different platforms
126     set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
127     if {$gccdir != ""} {
128         set gccdir [file dirname $gccdir]
129     }
130     v3track gccdir 3
131
132     # Look for shared library. (ie libstdc++.so.)
133     set v3-sharedlib 0
134     set sharedlibdir [lookfor_file $blddir src/.libs/libstdc++.so]
135     if {$sharedlibdir != ""} {
136         if { [string match "*-*-linux*" $target_triplet] && [isnative] } {
137             set v3-sharedlib 1
138             verbose -log "shared library support detected"
139         } 
140     }
141     v3track v3-sharedlib 3
142
143     # Compute what needs to be added to the existing LD_LIBRARY_PATH.
144     if {$gccdir != ""} {
145         set ld_library_path ""
146         append ld_library_path ":${gccdir}"
147         set compiler ${gccdir}/g++
148         append ld_library_path ":${blddir}/src/.libs"
149
150         if { [is_remote host] == 0 && [which $compiler] != 0 } {
151           foreach i "[exec $compiler --print-multi-lib]" {
152             set mldir ""
153             regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
154             set mldir [string trimright $mldir "\;@"]
155             if { "$mldir" == "." } {
156               continue
157             }
158             if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
159               append ld_library_path ":${gccdir}/${mldir}"
160             }
161           }
162         }
163
164         set_ld_library_path_env_vars
165         if [info exists env(LD_LIBRARY_PATH)] {
166           verbose -log "LD_LIBRARY_PATH = $env(LD_LIBRARY_PATH)"
167         }
168     } else {
169         set compiler [transform "g++"]
170     }
171
172     # Do a bunch of handstands and backflips for cross compiling and
173     # finding simulators...
174     if [is_remote host] {
175         set header [remote_download host ${blddir}/testsuite/util/testsuite_hooks.h]
176         if { $header == "" } {
177             verbose -log "Unable to download ${blddir}/testsuite/util/testsuite_hooks.h to host."
178             return "untested"
179         }
180       set cxx [transform "g++"]
181       set cxxflags "-ggdb3"
182       set cxxldflags ""
183       set includes "-I./"
184     } else {
185         # If we find a testsuite_flags file, we're testing in the build dir.
186         if { [file exists $flags_file] } {
187             set cxx [exec sh $flags_file --build-cxx]
188             set cxxflags [exec sh $flags_file --cxxflags]
189             set cxxldflags [exec sh $flags_file --cxxldflags]
190             set includes [exec sh $flags_file --build-includes]
191         } else {
192             set cxx [transform "g++"]
193             set includes "-I${srcdir}/util"
194             set cxxldflags ""
195             set cxxflags "-g -O2 -D_GLIBCXX_ASSERT -fmessage-length=0" 
196         }
197     }
198
199     # Always use MO files built by this test harness.
200     set cxxflags "$cxxflags -DLOCALEDIR=\".\""
201
202     # If a PCH file is available, use it.  We must delay performing
203     # this check until $cxx and such have been initialized because we
204     # perform a test compilation.  (Ideally, gcc --print-file-name would
205     # list PCH files, but it does not.)
206     global PCH_CXXFLAGS
207     if ![info exists PCH_CXXFLAGS] then {
208         set src "config[pid].cc"
209         set f [open $src "w"]
210         puts $f "int main () {}"
211         close $f
212
213         set lines [v3_target_compile $src "config[pid].o" object \
214                  "additional_flags=-include additional_flags=bits/stdtr1c++.h"]
215         if {$lines == "" } {
216 #           set PCH_CXXFLAGS "-include bits/extc++.h"
217 #           set PCH_CXXFLAGS "-include bits/stdtr1c++.h"
218             set PCH_CXXFLAGS "-include bits/stdc++.h"
219         } else {
220             set PCH_CXXFLAGS ""
221         }
222         file delete $src
223         v3track PCH_CXXFLAGS 2
224     }
225
226     libstdc++_maybe_build_wrapper "${objdir}/testglue.o"
227 }
228
229 # Callback for cleanup routines.
230 proc libstdc++_exit { } {
231     global gluefile;
232
233     if [info exists gluefile] {
234         file_on_build delete $gluefile;
235         unset gluefile;
236     }
237 }
238
239 # Callback from system dg-test.
240 proc libstdc++-dg-test { prog do_what extra_tool_flags } {
241     # Set up the compiler flags, based on what we're going to do.
242     switch $do_what {
243         "preprocess" {
244             set compile_type "preprocess"
245             set output_file "[file rootname [file tail $prog]].i"
246         }
247         "compile" {
248             set compile_type "assembly"
249             set output_file "[file rootname [file tail $prog]].s"
250         }
251         "assemble" {
252             set compile_type "object"
253             set output_file "[file rootname [file tail $prog]].o"
254         }
255         "link" {
256             set compile_type "executable"
257             set output_file "./[file rootname [file tail $prog]].exe"
258         }
259         "run" {
260             set compile_type "executable"
261             # FIXME: "./" is to cope with "." not being in $PATH.
262             # Should this be handled elsewhere?
263             # YES.
264             set output_file "./[file rootname [file tail $prog]].exe"
265             # This is the only place where we care if an executable was
266             # created or not.  If it was, dg.exp will try to run it.
267             catch { remote_file build delete $output_file }
268         }
269         default {
270             perror "$do_what: not a valid dg-do keyword"
271             return ""
272         }
273     }
274
275     set select_compile "v3_target_compile"
276     set options ""
277     if { $extra_tool_flags != "" } {
278         verbose -log "extra_tool_flags are:"
279         verbose -log $extra_tool_flags
280         if { [string first "-x c" $extra_tool_flags ] != -1 } {
281             # Short-circut a bunch of complicated goo here for the
282             # special case of compiling a test file as a "C" file, not
283             # as C++. Why? So -nostdc++ doesn't trip us up. So all the
284             # extra object files don't trip us up. So automatically
285             # linking in libstdc++ doesn't happen. So CXXFLAGS don't
286             # error.
287             verbose -log "compiling and executing as C, not C++"
288
289             set edit_tool_flags $extra_tool_flags
290             regsub -all ".x c" $edit_tool_flags "" edit_tool_flags
291             lappend options "additional_flags=$edit_tool_flags"
292
293             set select_compile "v3_target_compile_as_c"
294         } else {
295             lappend options "additional_flags=$extra_tool_flags"
296         }
297     }
298
299     # There is a libstdc++_compile made for us by default (via the tool-
300     # and-target file), but the defaults are lacking in goodness.
301     set comp_output [$select_compile "$prog" "$output_file" "$compile_type" $options];
302     set comp_output [ prune_g++_output $comp_output ];
303
304     return [list $comp_output $output_file]
305 }
306
307 # True if the library supports wchar_t.
308 set v3-wchar_t 0
309
310 # True if the library supports threads.
311 set v3-threads 0
312
313 # True if the library supports symbol versioning.
314 set v3-symver 0
315
316 # A string naming object files to be linked into all tests.
317 set v3-test_objs ""
318
319 # Called from libstdc++-dg-test above.  Calls back into system's
320 # target_compile to actually do the work.
321 proc v3_target_compile { source dest type options } {
322     global gluefile 
323     global wrap_flags
324     global cxx
325     global cxxflags
326     global cxxldflags
327     global includes
328     global v3-test_objs
329
330     if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
331         lappend options "libs=${gluefile}"
332         lappend options "ldflags=${wrap_flags}"
333     }
334
335     set cxx_final $cxx
336     set cxxlibglossflags  [libgloss_link_flags]
337     set cxx_final [concat $cxx_final $cxxlibglossflags]
338     set cxx_final [concat $cxx_final $cxxflags]
339     set cxx_final [concat $cxx_final $includes]
340
341     # Flag setting based on type argument.
342     if { $type == "executable" } {
343         # Link the support objects into executables.
344         set cxx_final [concat $cxx_final $cxxldflags]
345         # lappend options "additional_flags=./libtestc++.a"
346         set cxx_final [concat $cxx_final ${v3-test_objs}]
347     } else {
348         if { $type == "sharedlib" } {
349             # Don't link in anything.
350             set type "executable"
351         }
352     }
353
354     lappend options "compiler=$cxx_final"
355     lappend options "timeout=600"
356
357     return [target_compile $source $dest $type $options]
358 }
359
360
361 # Called from libstdc++-dg-test above, but only for "C" compilation.
362 # Calls back into system's target_compile to actually do the work.
363 proc v3_target_compile_as_c { source dest type options } {
364     global gluefile 
365     global wrap_flags
366     global includes
367
368     if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
369         lappend options "libs=${gluefile}"
370         lappend options "ldflags=${wrap_flags}"
371     }
372
373     set tname [target_info name]
374     set cc_final [board_info $tname compiler]
375     set cxxlibglossflags  [libgloss_link_flags]
376     set cc_final [concat $cc_final $cxxlibglossflags]
377     set cc_final [concat $cc_final $includes]
378     regsub -all ".nostdinc.." $cc_final "" cc_final
379
380     lappend options "compiler=$cc_final"
381     lappend options "timeout=600"
382
383     return [target_compile $source $dest $type $options]
384 }
385
386 # Build the support objects linked in with the libstdc++ tests.  In
387 # addition, set v3-wchar_t, v3-threads, v3-test_objs, and v3-symver
388 # appropriately.
389 proc v3-build_support { } {
390     global srcdir
391     global v3-wchar_t
392     global v3-threads
393     global v3-test_objs
394     global v3-symver
395     global v3-sharedlib
396
397     # Figure out whether or not the library supports certain features.
398     set v3-wchar_t 0
399     set v3-threads 0
400     set v3-symver 0
401     set v3-test_objs ""
402
403     set config_src "config.cc"
404     set f [open $config_src "w"]
405     puts $f "#include <bits/c++config.h>"
406     puts $f "#include <bits/gthr.h>"
407     close $f
408     set preprocessed [v3_target_compile $config_src "" \
409                        preprocess "additional_flags=-dN"]
410     if { [string first "_GLIBCXX_USE_WCHAR_T" $preprocessed] != -1 } {
411         verbose -log "wchar_t support detected"
412         set v3-wchar_t 1
413     }
414     if { [string first "_GLIBCXX_SYMVER" $preprocessed] != -1 } {
415         verbose -log "symbol versioning support detected"
416         set v3-symver 1
417     }
418     if { [string first "__GTHREADS" $preprocessed] != -1 } {
419         verbose -log "thread support detected"
420         set v3-threads 1
421     }
422
423     # Try to build the MO files that are used by some of the locale
424     # tests.  If we can't build them, that's OK; it just means that
425     # those tests will fail.
426     foreach lang [list "fr" "de"] {
427         catch {
428             file mkdir "$lang/LC_MESSAGES"
429             remote_exec "build" "msgfmt" "-o $lang/LC_MESSAGES/libstdc++.mo $srcdir/../po/$lang.po"
430             if [is_remote host] {
431                 remote_exec "host" "mkdir" "-p $lang/LC_MESSAGES"
432                 remote_download "host" "$lang/LC_MESSAGES/libstdc++.mo" "$lang/LC_MESSAGES/libstdc++.mo"
433             }
434         }
435     }
436
437     # Build the support objects.
438     set source_files [list testsuite_abi.cc testsuite_allocator.cc \
439                           testsuite_character.cc testsuite_hooks.cc \
440                  rng/twister_rand_gen.cc io/verified_cmd_line_input.cc \
441                           io/prog_bar.cc performance/time/elapsed_timer.cc ]
442     foreach f $source_files {
443         set obj [file rootname $f].o
444         set object_file [file tail $obj]
445         # Compile with "-w" so that warnings issued by the compiler
446         # do not prevent compilation.
447         if { [v3_target_compile $srcdir/util/$f $object_file "object" \
448                   [list "incdir=$srcdir" "additional_flags=-w"]]
449              != "" } {
450             error "could not compile $f"
451         }
452         append v3-test_objs "$object_file "
453     }
454
455     # Collect into libtestc++.a
456     set arcommand "ar -rc ./libtestc++.a ${v3-test_objs}"
457     set result [lindex [local_exec "$arcommand" "" "" 300] 0]
458     verbose "link result is $result"
459     if { $result == 0 } {
460         set ranlibcommand "ranlib ./libtestc++.a"
461         set result [lindex [local_exec "$ranlibcommand" "" "" 300] 0]
462         if { $result != 0 } {
463             error "could not link libtestc++.a"
464             
465             # We cannot actually use libtestc++.a because it's hard to
466             # position this library after the test file being compiled
467             # on the constructed compile line. However, we use this as
468             # a convenience for performance testing.  
469             #set v3-test_objs "./libtestc++.a"
470         }
471     }
472
473     # Build any shared objects needed for regression testing.
474     if { ${v3-sharedlib} == 1 } {
475         set source_files [list testsuite_shared.cc]
476         foreach f $source_files {
477             set object_file [file rootname $f].so
478             # Compile with "-w" so that warnings issued by the compiler
479             # do not prevent compilation.
480             if { [v3_target_compile $srcdir/util/$f $object_file "sharedlib" \
481              [list "incdir=$srcdir" "additional_flags=-w -shared -fPIC -DPIC"]]
482                  != "" } {
483                 error "could not compile $f"
484             }
485         }
486     }
487 }
488
489 proc check_v3_target_sharedlib { } {
490     global v3-sharedlib
491     return ${v3-sharedlib}
492 }
493
494 proc check_v3_target_namedlocale { } {
495     global et_namedlocale_saved
496     global et_namedlocale_target_name
497     global tool 
498
499     if { ![info exists et_namedlocale_target_name] } {
500         set et_namedlocale_target_name ""
501     }
502
503     # If the target has changed since we set the cached value, clear it.
504     set current_target [current_target_name]
505     if { $current_target != $et_namedlocale_target_name } {
506         verbose "check_v3_target_namedlocale: `$et_namedlocale_target_name'" 2
507         set et_namedlocale_target_name $current_target
508         if [info exists et_namedlocale_saved] {
509             verbose "check_v3_target_namedlocale: removing cached result" 2
510             unset et_namedlocale_saved
511         }
512     }
513
514     if [info exists et_namedlocale_saved] {
515         verbose "check_v3_target_namedlocale: using cached result" 2
516     } else {
517         set et_namedlocale_saved 0
518
519         # Set up, compile, and execute a C++ test program that tries to use
520         # all the required named locales.
521         set src nlocale[pid].cc
522         set exe nlocale[pid].x
523
524         set f [open $src "w"]
525         puts $f "#include <locale>"
526         puts $f "using namespace std;"  
527         puts $f "int main ()"
528         puts $f "{"
529         puts $f "  try" 
530         puts $f "  {" 
531         puts $f "     locale(\"\");"
532         puts $f "     locale(\"de_DE\");"
533         puts $f "     locale(\"de_DE.ISO-8859-15@euro\");"
534         puts $f "     locale(\"de_DE@euro\");"
535         puts $f "     locale(\"en_HK\");"
536         puts $f "     locale(\"en_PH\");"
537         puts $f "     locale(\"en_US\");"
538         puts $f "     locale(\"en_US.ISO-8859-1\");"
539         puts $f "     locale(\"en_US.ISO-8859-15\");"
540         puts $f "     locale(\"en_US.UTF-8\");"
541         puts $f "     locale(\"es_ES\");"
542         puts $f "     locale(\"es_MX\");"
543         puts $f "     locale(\"fr_FR\");"
544         puts $f "     locale(\"fr_FR@euro\");"
545         puts $f "     locale(\"is_IS\");"
546         puts $f "     locale(\"is_IS.UTF-8\");"
547         puts $f "     locale(\"it_IT\");"
548         puts $f "     locale(\"ja_JP.eucjp\");"
549         puts $f "     locale(\"se_NO.UTF-8\");"
550         puts $f "     locale(\"ta_IN\");"
551         puts $f "     locale(\"zh_TW\");"
552         puts $f "     return 0;"
553         puts $f "  }"
554         puts $f "  catch(...)"
555         puts $f "  {"
556         puts $f "     return 1;"
557         puts $f "  }"   
558         puts $f "}"
559         close $f
560
561         set lines [v3_target_compile $src $exe executable ""]
562         file delete $src
563
564         if [string match "" $lines] {
565             # No error message, compilation succeeded.
566             set result [${tool}_load "./$exe" "" ""]
567             set status [lindex $result 0]
568             remote_file build delete $exe
569
570             verbose "check_v3_target_namedlocale: status is <$status>" 2
571
572             if { $status == "pass" } {
573                 set et_namedlocale_saved 1
574             }
575         } else {
576             verbose "check_v3_target_namedlocale: compilation failed" 2
577         }
578     }
579     return $et_namedlocale_saved
580 }
581
582 proc check_v3_target_debug_mode { } {
583     global cxxflags
584     global et_debug_mode
585
586     global tool 
587
588     if { ![info exists et_debug_mode_target_name] } {
589         set et_debug_mode_target_name ""
590     }
591
592     # If the target has changed since we set the cached value, clear it.
593     set current_target [current_target_name]
594     if { $current_target != $et_debug_mode_target_name } {
595         verbose "check_v3_target_debug_mode: `$et_debug_mode_target_name'" 2
596         set et_debug_mode_target_name $current_target
597         if [info exists et_debug_mode] {
598             verbose "check_v3_target_debug_mode: removing cached result" 2
599             unset et_debug_mode
600         }
601     }
602
603     if [info exists et_debug_mode] {
604         verbose "check_v3_target_debug_mode: using cached result" 2
605     } else {
606         set et_debug_mode 0
607
608         # Set up, compile, and execute a C++ test program that depends
609         # on debug mode working.
610         set src debug_mode[pid].cc
611         set exe debug_mode[pid].exe
612
613         set f [open $src "w"]
614         puts $f "#include <string>"
615         puts $f "using namespace std;"  
616         puts $f "int main()"
617         puts $f "{ return 0; }"
618         close $f
619
620         set cxxflags_saved $cxxflags
621         set cxxflags "$cxxflags -Werror -O0 -D_GLIBCXX_DEBUG"
622         set lines [v3_target_compile $src $exe executable ""]
623         set cxxflags $cxxflags_saved
624         file delete $src
625
626         if [string match "" $lines] {
627             # No error message, compilation succeeded.
628             set et_debug_mode 1
629         }
630     }
631     verbose "check_v3_target_debug_mode: $et_debug_mode" 2
632     return $et_debug_mode
633 }