OSDN Git Service

2005-09-15 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
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] } {
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/testsuite_hooks.h]
176         if { $header == "" } {
177             verbose -log "Unable to download ${blddir}/testsuite/testsuite_hooks.h to host."
178             return "untested"
179         }
180       set cxx [transform "g++"]
181       set cxxflags "-ggdb3"
182       set includes "-I./"
183     } else {
184         # If we find a testsuite_flags file, we're testing in the build dir.
185         if { [file exists $flags_file] } {
186             set cxx [exec sh $flags_file --build-cxx]
187             set cxxflags [exec sh $flags_file --cxxflags]
188             set includes [exec sh $flags_file --build-includes]
189         } else {
190             set cxx [transform "g++"]
191             set cxxflags "-g -O2 -D_GLIBCXX_ASSERT -fmessage-length=0" 
192             set includes "-I${srcdir}"
193             # Guess at the location of the installed locale files.
194             # (It would be nice if "gcc --print-file-name" could find
195             # message files, but it cannot.)
196             set absolute_cxx [which $cxx]
197             if { $absolute_cxx != "" } {
198                 set localedir "[file dirname $absolute_cxx]/../share/locale"
199             }
200             set cxxflags "$cxxflags -DLOCALEDIR=\"$localedir\""
201         }
202     }
203
204     # If a PCH file is available, use it.  We must delay performing
205     # this check until $cxx and such have been initialized because we
206     # perform a test compilation.  (Ideally, gcc --print-file-name would
207     # list PCH files, but it does not.)
208     global PCH_CXXFLAGS
209     if ![info exists PCH_CXXFLAGS] then {
210         set src "config[pid].cc"
211         set f [open $src "w"]
212         puts $f "int main () {}"
213         close $f
214
215         set lines [v3_target_compile $src "config[pid].o" object \
216                    "additional_flags=-include additional_flags=bits/stdc++.h"]
217         if {$lines == "" } {
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             remote_file build delete $output_file;
268         }
269         default {
270             perror "$do_what: not a valid dg-do keyword"
271             return ""
272         }
273     }
274     set options ""
275     if { $extra_tool_flags != "" } {
276         lappend options "additional_flags=$extra_tool_flags"
277     }
278
279     # There is a libstdc++_compile made for us by default (via the tool-
280     # and-target file), but the defaults are lacking in goodness.
281     set comp_output [v3_target_compile "$prog" "$output_file" "$compile_type" $options];
282     set comp_output [ prune_g++_output $comp_output ];
283
284     return [list $comp_output $output_file]
285 }
286
287 # True if the library supports wchar_t.
288 set v3-wchar_t 0
289
290 # True if the library supports threads.
291 set v3-threads 0
292
293 # True if the library supports symbol versioning.
294 set v3-symver 0
295
296 # A string naming object files to be linked into all tests.
297 set v3-test_objs ""
298
299 # Called from libstdc++-dg-test above.  Calls back into system's
300 # target_compile to actually do the work.
301 proc v3_target_compile { source dest type options } {
302     global gluefile 
303     global wrap_flags
304     global cxx
305     global cxxflags
306     global includes
307     global blddir
308     global v3-test_objs
309
310     if { [target_info needs_status_wrapper] != "" && [info exists gluefile] } {
311         lappend options "libs=${gluefile}"
312         lappend options "ldflags=${wrap_flags}"
313     }
314
315     set cxx_final $cxx
316     set cxxlibglossflags  [libgloss_link_flags]
317     set cxx_final [concat $cxx_final $cxxlibglossflags]
318     set cxx_final [concat $cxx_final $cxxflags]
319     set cxx_final [concat $cxx_final $includes]
320
321     # Flag setting based on type argument.
322     if { $type == "executable" } {
323         # Link the support objects into executables.
324         set cxx_final [concat $cxx_final ${v3-test_objs}]
325     } else {
326         if { $type == "sharedlib" } {
327             # Don't link in anything.
328             set type "executable"
329         }
330     }
331
332     lappend options "compiler=$cxx_final"
333
334     return [target_compile $source $dest $type $options]
335 }
336
337
338 # Build the support objects linked in with the libstdc++ tests.  In
339 # addition, set v3-wchar_t, v3-threads, v3-test_objs, and v3-symver
340 # appropriately.
341 proc v3-build_support { } {
342     global srcdir
343     global v3-wchar_t
344     global v3-threads
345     global v3-test_objs
346     global v3-symver
347     global v3-sharedlib
348
349     # Figure out whether or not the library supports certain features.
350     set v3-wchar_t 0
351     set v3-threads 0
352     set v3-symver 0
353     set v3-test_objs ""
354
355     set config_src "config.cc"
356     set f [open $config_src "w"]
357     puts $f "#include <bits/c++config.h>"
358     puts $f "#include <bits/gthr.h>"
359     close $f
360     set preprocessed [v3_target_compile $config_src "" \
361                        preprocess "additional_flags=-dN"]
362     if { [string first "_GLIBCXX_USE_WCHAR_T" $preprocessed] != -1 } {
363         verbose -log "wchar_t support detected"
364         set v3-wchar_t 1
365     }
366     if { [string first "_GLIBCXX_SYMVER" $preprocessed] != -1 } {
367         verbose -log "symbol versioning support detected"
368         set v3-symver 1
369     }
370     if { [string first "__GTHREADS" $preprocessed] != -1 } {
371         verbose -log "thread support detected"
372         set v3-threads 1
373     }
374
375     # Build the support objects.
376     set source_files \
377         [list testsuite_abi.cc testsuite_allocator.cc testsuite_character.cc testsuite_hooks.cc ]
378     foreach f $source_files {
379         set object_file [file rootname $f].o
380         # Compile with "-w" so that warnings issued by the compiler
381         # do not prevent compilation.
382         if { [v3_target_compile $srcdir/$f $object_file "object" \
383                   [list "incdir=$srcdir" "additional_flags=-w"]]
384              != "" } {
385             error "could not compile $f"
386         }
387         append v3-test_objs "$object_file "
388     }
389
390     # Build the shared support objects.
391     if { ${v3-sharedlib} == 1 } {
392         set source_files \
393             [list testsuite_shared.cc]
394         foreach f $source_files {
395             set object_file [file rootname $f].so
396             # Compile with "-w" so that warnings issued by the compiler
397             # do not prevent compilation.
398             if { [v3_target_compile $srcdir/$f $object_file "sharedlib" \
399              [list "incdir=$srcdir" "additional_flags=-w -shared -fPIC -DPIC"]]
400                  != "" } {
401                 error "could not compile $f"
402             }
403         }
404     }
405 }
406
407 proc check_v3_target_sharedlib { } {
408     global v3-sharedlib
409     return ${v3-sharedlib}
410 }
411
412 proc check_v3_target_namedlocale { } {
413     global et_namedlocale_saved
414     global et_namedlocale_target_name
415     global tool 
416
417     if { ![info exists et_namedlocale_target_name] } {
418         set et_namedlocale_target_name ""
419     }
420
421     # If the target has changed since we set the cached value, clear it.
422     set current_target [current_target_name]
423     if { $current_target != $et_namedlocale_target_name } {
424         verbose "check_v3_target_namedlocale: `$et_namedlocale_target_name'" 2
425         set et_namedlocale_target_name $current_target
426         if [info exists et_namedlocale_saved] {
427             verbose "check_v3_target_namedlocale: removing cached result" 2
428             unset et_namedlocale_saved
429         }
430     }
431
432     if [info exists et_namedlocale_saved] {
433         verbose "check_v3_target_namedlocale: using cached result" 2
434     } else {
435         set et_namedlocale_saved 0
436
437         # Set up, compile, and execute a C++ test program that tries to use
438         # all the required named locales.
439         set src nlocale[pid].cc
440         set exe nlocale[pid].x
441
442         set f [open $src "w"]
443         puts $f "#include <locale>"
444         puts $f "using namespace std;"  
445         puts $f "int main ()"
446         puts $f "{"
447         puts $f "  try" 
448         puts $f "  {" 
449         puts $f "     locale(\"\");"
450         puts $f "     locale(\"de_DE\");"
451         puts $f "     locale(\"de_DE.ISO-8859-15@euro\");"
452         puts $f "     locale(\"de_DE@euro\");"
453         puts $f "     locale(\"en_HK\");"
454         puts $f "     locale(\"en_PH\");"
455         puts $f "     locale(\"en_US\");"
456         puts $f "     locale(\"en_US.ISO-8859-1\");"
457         puts $f "     locale(\"en_US.ISO-8859-15\");"
458         puts $f "     locale(\"en_US.UTF-8\");"
459         puts $f "     locale(\"es_ES\");"
460         puts $f "     locale(\"es_MX\");"
461         puts $f "     locale(\"fr_FR\");"
462         puts $f "     locale(\"fr_FR@euro\");"
463         puts $f "     locale(\"is_IS\");"
464         puts $f "     locale(\"is_IS.UTF-8\");"
465         puts $f "     locale(\"it_IT\");"
466         puts $f "     locale(\"ja_JP.eucjp\");"
467         puts $f "     locale(\"se_NO.UTF-8\");"
468         puts $f "     locale(\"ta_IN\");"
469         puts $f "     locale(\"zh_TW\");"
470         puts $f "     return 0;"
471         puts $f "  }"
472         puts $f "  catch(...)"
473         puts $f "  {"
474         puts $f "     return 1;"
475         puts $f "  }"   
476         puts $f "}"
477         close $f
478
479         set lines [v3_target_compile $src $exe executable ""]
480         file delete $src
481
482         if [string match "" $lines] {
483             # No error message, compilation succeeded.
484             set result [${tool}_load "./$exe" "" ""]
485             set status [lindex $result 0]
486             remote_file build delete $exe
487
488             verbose "check_v3_target_namedlocale: status is <$status>" 2
489
490             if { $status == "pass" } {
491                 set et_namedlocale_saved 1
492             }
493         } else {
494             verbose "check_v3_target_namedlocale: compilation failed" 2
495         }
496     }
497     return $et_namedlocale_saved
498 }
499
500 proc check_v3_target_cxa_atexit { } {
501     global et_cxa_atexit
502     global et_cxa_atexit_target_name
503     global tool 
504
505     if { ![info exists et_cxa_atexit_target_name] } {
506         set et_cxa_atexit_target_name ""
507     }
508
509     # If the target has changed since we set the cached value, clear it.
510     set current_target [current_target_name]
511     if { $current_target != $et_cxa_atexit_target_name } {
512         verbose "check_v3_target_cxa_atexit: `$et_cxa_atexit_target_name'" 2
513         set et_cxa_atexit_target_name $current_target
514         if [info exists et_cxa_atexit] {
515             verbose "check_v3_target_cxa_atexit: removing cached result" 2
516             unset et_cxa_atexit
517         }
518     }
519
520     if [info exists et_cxa_atexit] {
521         verbose "check_v3_target_cxa_atexit: using cached result" 2
522     } else {
523         set et_cxa_atexit 0
524
525         # Set up, compile, and execute a C++ test program that depends
526         # on correct ordering of static object destructors. This is
527         # indicative of the presence and use of __cxa_atexit.
528         set src cxaatexit[pid].cc
529         set exe cxaatexit[pid].x
530
531         set f [open $src "w"]
532         puts $f "#include <stdlib.h>"
533         puts $f "static unsigned int count;"
534         puts $f "struct X"
535         puts $f "{"
536         puts $f "  X() { count = 1; }"
537         puts $f "  ~X()"
538         puts $f "  {"
539         puts $f "    if (count != 3)"
540         puts $f "      exit(1);"
541         puts $f "    count = 4;"
542         puts $f "  }"
543         puts $f "};"
544         puts $f "void f()"
545         puts $f "{"
546         puts $f "  static X x;"
547         puts $f "}"
548         puts $f "struct Y"
549         puts $f "{"
550         puts $f "  Y() { f(); count = 2; }"
551         puts $f "  ~Y()"
552         puts $f "  {"
553         puts $f "    if (count != 2)"
554         puts $f "      exit(1);"
555         puts $f "    count = 3;"
556         puts $f "  }"
557         puts $f "};"
558         puts $f "Y y;"
559         puts $f "int main()"
560         puts $f "{ return 0; }"
561         close $f
562
563         set lines [v3_target_compile $src $exe executable ""]
564         file delete $src
565
566         if [string match "" $lines] {
567             # No error message, compilation succeeded.
568             set result [${tool}_load "./$exe" "" ""]
569             set status [lindex $result 0]
570             remote_file build delete $exe
571
572             verbose "check_v3_target_cxa_atexit: status is <$status>" 2
573
574             if { $status == "pass" } {
575                 set et_cxa_atexit 1
576             }
577         } else {
578             verbose "check_v3_target_cxa_atexit: compilation failed" 2
579         }
580     }
581     return $et_cxa_atexit
582 }
583
584 proc check_v3_target_debug_mode { } {
585     global cxxflags
586     global et_debug_mode
587
588     global tool 
589
590     if { ![info exists et_debug_mode_target_name] } {
591         set et_debug_mode_target_name ""
592     }
593
594     # If the target has changed since we set the cached value, clear it.
595     set current_target [current_target_name]
596     if { $current_target != $et_debug_mode_target_name } {
597         verbose "check_v3_target_debug_mode: `$et_debug_mode_target_name'" 2
598         set et_debug_mode_target_name $current_target
599         if [info exists et_debug_mode] {
600             verbose "check_v3_target_debug_mode: removing cached result" 2
601             unset et_debug_mode
602         }
603     }
604
605     if [info exists et_debug_mode] {
606         verbose "check_v3_target_debug_mode: using cached result" 2
607     } else {
608         set et_debug_mode 0
609
610         # Set up, compile, and execute a C++ test program that depends
611         # on correct ordering of static object destructors. This is
612         # indicative of the presence and use of __cxa_atexit.
613         set src debug_mode[pid].cc
614         set exe debug_mode[pid].x
615
616         set f [open $src "w"]
617         puts $f "#include <string>"
618         puts $f "using namespace std;"  
619         puts $f "int main()"
620         puts $f "{ return 0; }"
621         close $f
622
623         set cxxflags_saved $cxxflags
624         set cxxflags "$cxxflags -Werror -O0 -D_GLIBCXX_DEBUG"
625         set lines [v3_target_compile $src $exe executable ""]
626         set cxxflags $cxxflags_saved
627         file delete $src
628
629         if [string match "" $lines] {
630             # No error message, compilation succeeded.
631             set et_debug_mode 1
632         }
633     }
634     verbose "check_v3_target_debug_mode: $et_debug_mode" 2
635     return $et_debug_mode
636 }