OSDN Git Service

2000-11-15 David Billinghurst <David.Billinghurst@riotinto.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / mkcheck.in
1 #!/usr/bin/env bash
2
3
4 # Script to do automated testing and data collection for various test
5 # files, so that we don't have to do this by hand on every test file.
6 # It attempts to collect some diagnostic info about size and speed that
7 # should be useful in the future as the library gets tuned for size
8 # and speed.  In addition, it tests static and shared linkage, iff each
9 # has been enabled.
10
11 # Invocation 
12 # mkcheck [012] (path to build) (path to src) (path to install)
13
14 ### XXX There are a lot of tests in here for OS-specific stuff.  If we
15 ###     move to a 'configure.target' method of determining those extra
16 ###     flags and whatnot, we can take out all those things and source
17 ###     that file from here.  (Write that file with this in mind...)
18
19 ### XXX Note that breaking out of this with ^C will not work.  Dunno why.
20
21
22 # This has been true all along.  Found out about it the hard way...
23 case $BASH_VERSION in
24     1*)  echo 'You need bash 2.x to run mkcheck.  Exiting.'; exit 1 ;;
25     *)   ;;   # ??
26 esac
27
28
29 #
30 # 1: variables
31 #
32 # WHICH determines if you are
33 # (0) testing the build binary and headers, or
34 # (1) testing the installed binary and headers, or
35 # (2) testing under dejagnu (just print the standard flags needed).
36 WHICH=$1
37 if [ "$WHICH"x = 0x ] && [ $# -eq 3 ]; then
38   echo "running mkcheck"
39   echo "$0: testing the build directory"
40 elif [ "$WHICH"x = 1x ] && [ $# -eq 4 ]; then
41   echo "running mkcheck"
42   echo "$0: testing the install directory $4"
43 elif [ "$WHICH"x = 2x ] && [ $# -eq 3 ]; then
44   true
45 else
46   echo 'Usage: mkcheck 0 (path to build) (path to src)'
47   echo '       mkcheck 1 (path to build) (path to src) (path to install)'
48   echo '       mkcheck 2 (path to build) (path to src)'
49   exit 1;
50 fi
51   
52 BUILD_DIR=$2
53 if [ ! -d "$BUILD_DIR" ]; then
54   echo "build directory $BUILD_DIR not found, exiting."
55   exit 1
56 fi
57
58 SRC_DIR=$3
59 if [ ! -d "$SRC_DIR" ]; then
60   echo "source directory $SRC_DIR not found, exiting."
61   exit 1
62 fi
63
64 if [ $WHICH -eq 1 ]; then 
65     PREFIX_DIR=$4
66     if [ ! -d "$PREFIX_DIR" ]; then
67     echo "install directory $PREFIX_DIR not found, exiting."
68     exit 1
69     fi
70 fi
71
72 # This is LIBTOOL=@LIBTOOL@ piped through a bit of sanity that we can
73 # assume for this script (by the time we run this).
74 LIBTOOL="$BUILD_DIR/libtool"
75 chmod u+x $LIBTOOL
76
77 # INC_PATH == include path to new headers for use on gcc command-line
78 top_srcdir=@top_srcdir@
79 C_DIR="`basename @C_INCLUDE_DIR@`"
80 if [ $WHICH != "1" ]; then
81   INC_PATH="-nostdinc++ @CSHADOW_FLAGS@ -I$BUILD_DIR/include \
82     -I$SRC_DIR/include/std  -I$SRC_DIR/include/$C_DIR \
83     -I$SRC_DIR/include -I$SRC_DIR/libsupc++ -I$SRC_DIR/libio \
84     -I$SRC_DIR/testsuite"
85 elif [ $WHICH -eq 1 ]; then
86   INC_PATH="-I$SRC_DIR/testsuite"
87 fi
88
89 if [ $WHICH -eq 2 ]; then
90   echo $INC_PATH -I$SRC_DIR/include/backward -I$SRC_DIR/include/ext
91   exit 0;
92 fi
93
94 # It's not dejagnu; we need to do things ourselves.  Pick up any extra
95 # settings for this target.
96 . ${top_srcdir}/configure.target
97
98 # LIB_PATH == where to find the build libraries for libtool's use
99 # CXX == how to call the compiler
100 if [ $WHICH -eq 0 ]; then
101   LIB_PATH="$BUILD_DIR/src"
102   CXX="$BUILD_DIR/../../gcc/g++ -B$BUILD_DIR/../../gcc/"
103 elif [ $WHICH -eq 1 ]; then
104   LIB_PATH="$PREFIX_DIR/lib"
105   CXX="$PREFIX_DIR/bin/g++"
106 fi
107
108 # gcc compiler flags (maybe use glibcpp_cxxflags from configure.target,
109 # but thst's really meant for building the library itself, not using it)
110 CXX_FLAG="-ggdb3 -DDEBUG_ASSERT @SECTION_FLAGS@ @SECTION_LDFLAGS@"
111
112 # specific libtool flag(s) to force the use of shared libraries, if any
113 SH_FLAG=""
114
115 # specific libtool flag(s) to force the use of static libraries, if any
116 #ST_FLAG="-static"
117 ST_FLAG="-all-static"
118
119 # LTCXX == how to call libtool when creating an executable
120 # LTEXE == how to call libtool when running an executable
121 # LIBS == any extra needed -l switches, etc (may need more libs, lose lose)
122 if [ $WHICH -eq 0 ]; then
123   LTCXX="$LIBTOOL --tag=CXX --mode=link \
124           $CXX $CXX_FLAG $INC_PATH   \
125           $LIB_PATH/../libsupc++/libsupc++.la  $LIB_PATH/libstdc++.la   \
126           -no-install"
127   LTEXE="$LIBTOOL --mode=execute"
128   LIBS="-nodefaultlibs -lc -lgcc -lc"
129 elif [ $WHICH -eq 1 ]; then
130   # For the installed version, we really only need to use libtool and
131   # the .la file to get correct rpaths.
132   LTCXX="$LIBTOOL --tag=CXX --mode=link \
133           $CXX $CXX_FLAG $INC_PATH -L$LIB_PATH   \
134           $LIB_PATH/libstdc++.la -no-install -rpath $LIB_PATH"
135   LTEXE="$LIBTOOL --mode=execute"
136   LIBS=
137 fi
138
139 # Set up the testing directory, which should be in a directory called
140 # "testsuite" in the root level of the build directory.
141 TEST_DIR="`pwd`/testsuite"
142 if [ ! -d "$TEST_DIR" ]; then
143     echo "making directory $TEST_DIR"
144     mkdir $TEST_DIR
145     mkdir $TEST_DIR/.libs    # help libtool keep quiet
146     chmod u+w $TEST_DIR
147 fi
148
149 # the name of the file that will collect and hold all this useful data:
150 RESULTS_FILE="$TEST_DIR/$(date +%Y%m%d)-mkcheck.txt"
151
152 # the name of the log file that will append compiler diagnostics:
153 LOG_FILE="$TEST_DIR/$(date +%Y%m%d)-mkchecklog.txt"
154
155 # the names of the specific test files to be run
156 TESTS_FILE="$TEST_DIR/$(date +%Y%m%d)-mkcheckfiles.txt"
157
158 # the heap size limit for testsuite binaries; start with a 2MB limit as per
159 # http://sources.redhat.com/ml/libstdc++/2000-10/msg00029.html
160 MAX_MEM_USAGE=3072
161
162 #
163 # 2: clean, make files, append general test info
164 #
165 if [ -f $RESULTS_FILE ]; then
166     rm $RESULTS_FILE
167 fi
168 if [ -f $LOG_FILE ]; then
169     rm $LOG_FILE
170 fi
171
172 # Make a list of the files we're going to run, or use an old one if it exists.
173 if [ ! -f "$TESTS_FILE" ]; then
174     echo "making file $TESTS_FILE"
175     for LONG_NAME in $SRC_DIR/testsuite/*/*.cc
176     do
177         DIR_NAME=$(dirname $LONG_NAME)
178         SHORT_NAME="`basename $DIR_NAME`/`basename $LONG_NAME`"
179         echo "$SHORT_NAME" >> $TESTS_FILE
180     done
181 fi
182
183 # Nasty solution to replace GNU date(1)'s %s time_t output function.
184 if [ ! -x "$TEST_DIR/printnow" ]; then
185     echo "making utility $TEST_DIR/printnow"
186     gcc -o "$TEST_DIR/printnow" "$SRC_DIR/testsuite/printnow.c"
187     strip "$TEST_DIR/printnow"
188 fi
189
190 # Remove old executables.
191 rm -rf "$TEST_DIR"/*exe
192
193 # Remove old core files (which now get left in cwd, not $TEST_DIR).
194 rm -rf ./*core*
195
196 # Copy over the data files for filebufs
197 cp $SRC_DIR/testsuite/27_io/*.txt $TEST_DIR
198 cp $SRC_DIR/testsuite/27_io/*.tst $TEST_DIR
199 chmod u+w $TEST_DIR/*.txt
200 chmod u+w $TEST_DIR/*.tst
201
202 # Emit useful info about compiler and platform
203 echo "host: $(uname -mrsv)" >> $RESULTS_FILE
204 echo "compiler: $($CXX -v 2>&1 | tail -1)" >> $RESULTS_FILE
205 echo "$($CXX -v 2>&1 | grep ^Configured)" >> $RESULTS_FILE
206 echo "compiler flags: $CXX_FLAG" >> $RESULTS_FILE
207 echo "date: $(date +%Y%m%d)" >> $RESULTS_FILE
208 echo "" >> $RESULTS_FILE
209
210 explanation='+: pass, -b: build failure, -r: run failure, x: disabled'
211 printf "%s\n     %s\n"  'p == pass/fail execution test'  "$explanation"  \
212        >> $RESULTS_FILE
213 echo "ctime == time to compile and link" >> $RESULTS_FILE
214 echo "etime == time for executable to run" >> $RESULTS_FILE
215 echo "text == size of the executable text section" >> $RESULTS_FILE
216 echo "data == size of the executable data section" >> $RESULTS_FILE
217 echo "total == size of the executable" >> $RESULTS_FILE
218 echo "" >> $RESULTS_FILE
219 echo "(First static, then shared.)" >> $RESULTS_FILE
220
221 echo "p" | awk '{printf("%s ", $1)}' >> $RESULTS_FILE
222 echo "ctime" "etime" | awk '{printf("%s\t%s\t", $1, $2)}' >> $RESULTS_FILE
223 echo "text" "data" | awk '{printf("%s\t%s\t", $1, $2)}' >> $RESULTS_FILE
224 echo "total" "name" | awk '{printf("%s\t%s\t", $1, $2)}' >> $RESULTS_FILE
225 echo "" >> $RESULTS_FILE
226
227
228 #
229 # 2.5:  support functions
230 #
231
232 # Figure out how to extract size information from binaries.  We take
233 # the text of the value we want as an argument, and leave the size in
234 # the appropriate variable.
235 #
236 # We discover what kind of size(1) we are using *once* and build a shell
237 # function named 'size_command' to wrap it.  (The "function" keyword is
238 # redundant here, but helps me read it, so there.)  Previously we were
239 # re-discovering the size(1) arguments three times for each test; sloooow.
240 #
241 # It is VERY IMPORTANT not to compare these numbers across platforms.
242 # Different size(1)'s extract section information differently.  For
243 # example, using the native Sun size(1) and GNU size(1) built for Suns
244 # on the exact same binary will give very different numbers, due to all
245 # the variance in command-line options and arbitrary names of ELF sections.
246 #
247 # and suddenly we go to 2-space indentations...
248 setup_size_command()
249 {
250   if size --version 2> /dev/null | grep -c GNU > /dev/null;
251   then    # Then we're using a GNU size(1) built for this platform.
252     # We lose .rodata and .data1 and who knows what else... kludge.
253     function size_command()
254     {
255       case $1 in
256         TEXT)  TEXT=$(size -A $EXENAME | grep ^.text | awk '{print $2}')  ;;
257         DATA)  DATA=$(size -A $EXENAME | grep -w ^.data | awk '{print $2}')  ;;
258         SIZE)  SIZE=$(size -A $EXENAME | grep otal | awk '{print $2}')  ;;
259       esac
260     }
261   else
262     # Not using GNU size; check for platform.  These numbers seem to match
263     # up to text/data/total, although their meanings seem to be different.
264     # THIS TABLE IS SORTED.  KEEP IT THAT WAY.
265     case @host_os@ in
266       *aix*)
267         function size_command()
268         {
269           case $1 in
270             TEXT)  TEXT=$(size -X32_64 $EXENAME | awk '{print $2}')  ;;
271             DATA)  DATA=$(size -X32_64 $EXENAME | awk '{print $4}')  ;;
272             SIZE)  SIZE=$(size -X32_64 $EXENAME | awk '{print $12}')  ;;
273           esac
274         }
275         ;;
276       *irix*)
277         function size_command()
278         {
279           case $1 in
280             TEXT)  TEXT=$(size -4 $EXENAME | awk '{print $1}')  ;;
281             DATA)  DATA=$(size -4 $EXENAME | awk '{print $3}')  ;;
282             SIZE)  SIZE=$(size -4 $EXENAME | awk '{print $7}')  ;;
283           esac
284         }
285         ;;
286       *solaris*)
287         function size_command()
288         {
289           case $1 in
290             TEXT)  TEXT=$(size $EXENAME | awk '{print $1}')  ;;
291             DATA)  DATA=$(size $EXENAME | awk '{print $3}')  ;;
292             SIZE)  SIZE=$(size $EXENAME | awk '{print $7}')  ;;
293           esac
294         }
295         ;;
296       *)
297         echo ' * Warning!  Skipping section sizes!' 1>&2
298         function size_command()
299         {
300         case $1 in
301           TEXT)  TEXT=0 ;;
302           DATA)  DATA=0 ;;
303           SIZE)  SIZE=0 ;;
304         esac
305         }
306         ;;
307     esac
308   fi
309 }
310
311 # Test for file output
312 test_for_output()
313 {
314     # This checks for emitted output files, which is useful when
315     # testing file-related output.  The rules for this working are as
316     # follows: the emitted file must have the ".txt" extension, and be
317     # based on the actual *.cc file's name.  For example, 27/filbuf.cc
318     # currently outputs files named 27/filebuf-2.txt and 27/filebuf-3.txt.
319     # Also, the first emitted file must be in the form $NAME-1.txt.
320     # The control file must follow the same constraints, but have a
321     # ".tst" extension.  Thus, you have 27/filebuf-2.tst, etc.
322
323     # NAME contains the source name, like 27/filebuf.cc
324     # From that NAME, we want to generate some possible names, using
325     # ls on MATCH, a pattern description generated with sed.
326
327     # this is the name of the resulting diff file, if any
328     DIFF_FILE="`echo $TEST_NAME | sed 's/cc$/diff/'`"
329     # construct wildcard names, ie for $NAME=filebuf.cc, makes "filebuf*.tst"
330     DATA_FILES="`echo $NAME | sed 's/\.cc/\*\.tst/g'`"
331     # make sure there is at least one, then go
332     ST_E="`echo $NAME | sed 's/\.cc/\-1\.tst/g'`"
333     if [ -f $ST_E ]; then
334         # list of actual files that match the wildcard above, ie
335         # "filebuf-1.tst"
336         ST_MATCH_LIST="`ls $DATA_FILES`"
337         for i in $ST_MATCH_LIST; do
338             # ST_OUT_FILE is generated in the build directory.
339             PRE_NAME2="$TEST_DIR/`basename $i`"
340             ST_OUT_FILE="`echo $PRE_NAME2 | sed 's/tst$/txt/'`"
341             diff $ST_OUT_FILE $i > $DIFF_FILE
342             if [ -s $DIFF_FILE ]; then
343                 RESULT="-r"
344                 echo "$ST_OUT_FILE has some problems, dude"
345             else
346                 RESULT="+"
347             fi
348             rm $DIFF_FILE
349         done
350     else
351         # the file does no output, and didn't abnormally
352         # terminate, so assume passed.
353         RESULT="+"
354     fi
355 }
356     
357
358 #
359 # 3: compile, link, execute, time
360 #
361 # Abstract out the common code for compiling, linking, executing and printing.
362 test_file()
363 {
364     # NB: S_FLAG has to be last argument because it may be null, and
365     # error checking hasn't been invented yet.
366     NAME=$1
367     EXENAME=$2
368     S_FLAG=$3
369
370     SRC_NAME="$SRC_DIR/testsuite/$1"
371     TEST_NAME="$TEST_DIR/`basename $NAME`"
372
373     # This would be deliciously easy if GNU date's %s were always around.
374     # There are three ways to do this:  1) use the builtin 'time' like we
375     # do later; then getting compiler errors into LOG_FILE is a nightmare.
376     # 2) Grab the output of a formatted date(1) and do the math; harder
377     # and harder as we try compiling at, say, top of the hour; we would
378     # eventually have to calculate time_t anyhow.  Or 3) just grab two
379     # time_t's (no more overhead than grabbing two date(1)'s).
380     compiler_invocation="$LTCXX $S_FLAG $SRC_NAME -o $EXENAME $LIBS"
381     # Why the leading weird character, like a colon?  That way you can
382     # 'grep -v ^: *mkchecklog.txt' and just see the errors.
383     #printf "\n: " >> $LOG_FILE
384     printf "\n" >> $LOG_FILE
385     COMP_TIME_START=$($TEST_DIR/printnow)
386     $compiler_invocation >> $LOG_FILE 2>&1
387     COMP_TIME_END=$($TEST_DIR/printnow)
388
389     if [ $COMP_TIME_START -lt $COMP_TIME_END ]; then
390         C_TIME=$[ $COMP_TIME_END - $COMP_TIME_START ]
391     else
392         C_TIME="0"
393     fi
394
395     if [ -f $EXENAME ]; then
396         size_command TEXT
397         size_command DATA
398         size_command SIZE
399
400         # Actually run the executable and time it.  Note that output
401         # printed by the executable will be lost and cannot be redirected,
402         # because we need to capture the output of 'time'.  Bummer.
403         TIMEFORMAT='timemark %R'
404         E_TIME_TEXT="$(exec 2>&1; ulimit -d $MAX_MEM_USAGE; \
405                      time $LTEXE $EXENAME)"
406         E_ABNORMAL_TERMINATION=$?
407         E_TIME="$(echo $E_TIME_TEXT | awk '{print $2}')"
408         # joining those two commands does not work due to quoting problems:
409         #E_TIME="$(exec 2>&1; time $EXENAME | awk '{print $2}')"
410         # this will work as a fallback on certain systems...?
411         #E_TIME=$(exec 2>&1; time $EXENAME | cut -d ' ' -f 2)
412  
413         if [ "$E_ABNORMAL_TERMINATION" -ne 0 ]; then
414             RESULT='-r'
415             rm -f ./*core
416             # sometimes you want to save all core files for review:
417             #mv ./core $EXENAME.core
418             # sometimes the OS allows you to name core files yourself:
419             #mv ./*core $EXENAME.core
420             #mv ./core* $EXENAME.core
421         else
422             test_for_output
423         fi
424
425         # sometimes you want to save all failing exe files for review:
426         if [ "$RESULT" = "+" ]; then
427             rm "$EXENAME"
428         fi
429     else
430         # the file did not compile/link.
431         RESULT="-b"
432         TEXT="0"
433         DATA="0"
434         SIZE="0"
435     fi
436
437     printf "%s\t" "$RESULT"
438     printf "%-2s %d\t%.3f\t%s\t%s\t%s\t%s\n"   \
439         "$RESULT" $C_TIME $E_TIME $TEXT $DATA $SIZE $NAME >> $RESULTS_FILE
440 }
441
442 setup_size_command
443 echo ""
444 echo "Detailed test results in .${RESULTS_FILE/$BUILD_DIR}"
445 echo $explanation
446 echo "------------------------------------------------------------------------"
447 printf "static\tshared\ttest\n"
448 echo "------------------------------------------------------------------------"
449
450 TEST_TIME_START=$($TEST_DIR/printnow)
451 for NAME in `cat $TESTS_FILE`
452 do
453     PRE_NAME="$TEST_DIR/`basename $NAME`"
454     ST_NAME="`echo $PRE_NAME | sed 's/cc$/st-exe/'`"
455     SH_NAME="`echo $PRE_NAME | sed 's/cc$/sh-exe/'`"
456
457     if test @enable_static@ = yes; then
458         test_file $NAME $ST_NAME $ST_FLAG 
459     else
460         printf "x\t"
461         printf "static skipped\n" >> $RESULTS_FILE
462     fi
463     if test @enable_shared@ = yes; then
464         test_file $NAME $SH_NAME $SH_FLAG 
465     else
466         printf "x\t"
467         printf "shared skipped\n" >> $RESULTS_FILE
468     fi
469     printf "%s\n" "$NAME"
470
471     echo "" >> $RESULTS_FILE
472 done
473 TEST_TIME_END=$($TEST_DIR/printnow)
474
475
476 #
477 # 4: summary
478 #
479 # grep can count faster than we can...
480 total_failures=$(egrep -c "^\-" $RESULTS_FILE)
481 total_successes=$(egrep -c "^\+" $RESULTS_FILE)
482 resultstext="pass/fail results:  ${total_successes}/${total_failures}"
483 if [ $total_failures -eq 0 ]; then
484     resultstext="${resultstext}, WIN WIN"
485 fi
486 sed -e "/^date:/a\\
487 $resultstext" $RESULTS_FILE > ${RESULTS_FILE}.tmp
488 mv ${RESULTS_FILE}.tmp $RESULTS_FILE
489
490 if [ $TEST_TIME_START -lt $TEST_TIME_END ]; then
491     TEST_TIME=$[ $TEST_TIME_END - $TEST_TIME_START ]
492     echo "testrun == $TEST_TIME seconds"
493     echo "testrun == $TEST_TIME seconds" >> $RESULTS_FILE
494 fi
495
496 exit 0
497
498