OSDN Git Service

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