OSDN Git Service

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