OSDN Git Service

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