summaryrefslogtreecommitdiffstats
path: root/rtems-coverage/do_coverage
blob: 75352b5f9ecce623794b4bae9d978a7810fef8dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#! /bin/sh
#
#  Script to perform a single coverage test run on a single BSP
#  in a specific configuration.  This script automates all steps
#  of building RTEMS, running the tests, and generating the reports.
#

vfile=`dirname $0`/VERSIONS-COVERAGE
if [ ! -r ${vfile} ] ; then
  echo VERSIONS-COVERAGE file not found
  exit 1
fi

source ${vfile}

if [ ! -d ${BASEDIR} ] ; then
  echo Have you set the BASEDIR in VERSIONS correctly?
  exit 1
fi

progname=${0##*/}        # fast basename hack for ksh, bash

USAGE="
usage: $progname [ -opts ]
        -v        -- verbose (default=no)
        -d        -- analyze Core Configuration (default=yes)
        -D        -- analyze Developmental Configuration (default=yes)
        -A        -- Execute all steps (default=no)
        -B BSP    -- Specify BSP to test
        -P        -- Enable POSIX API (default=yes)
        -S        -- Optimize for size (default=no)
        -m        -- Update and build covMerge (default=no)
        -u        -- Do CVS update on RTEMS (default=no)
        -c        -- Configure RTEMS (default=no)
        -b        -- Build RTEMS (default=no)
        -C        -- get tests from build to test execution point (default=no)
        -L        -- do not link files to test execution point (default=link)
        -r        -- run the tests (default=no)
        -R        -- generate reports (default=no)
        -g        -- generate GCOV reports (default=no)
        -f        -- publish the results to ftp site (default=no)
        -t        -- save the results locally (default=no)
        -O        -- output directory (default=BSP-CONF-YYYYMMDD-HHMM)
        -p        -- project name (default empty)

Notes:  + There are currently NO checks at each step to determine if
          the previous steps were performed!!!
        + Use of each option toggles the setting.  For example, \"-v -v -A -u\"
          results in verbose=no and all steps done except CVS update on RTEMS."

#   log an error to stderr
prerr()
{
    echo "$*" >&2
}

fatal() {
    prerr "$USAGE"
    [ "$1" ] && (prerr ; prerr $*);
    exit 1
}

check_status()
{
  if [ $1 -ne 0 ] ; then
    shift
    echo "FAILED: " "$*" >&2
    exit 1
  fi
}

toggle()
{
  case $1 in
    no)  echo "yes" ;;
    yes) echo "no" ;;
    *)   fatal "Unknown value to toggle ($1)" ;;
  esac
}

# parse arguments for these
verbose="no"
do_all="no"
do_posix="yes"
do_optimize_size="no"
do_covoar="no"
do_rtems_update="no"
do_rtems_configure="no"
do_rtems_build="no"
do_copy_tests="no"
do_link_tests="yes"
do_run_tests="no"
do_generate_gcov="no"
do_reports="no"
do_publish="no"
do_save_tarballs="yes"
do_core="yes"
do_developmental="yes"
outputDir=""
BSP="not_set"

while getopts vB:PSmAucbCLrgRftO:dD OPT
do
    case "$OPT" in
        A)
          do_all="yes"
          do_covoar="yes"
          do_posix="yes"
          do_rtems_update="yes"
          do_rtems_configure="yes"
          do_rtems_build="yes"
          do_copy_tests="yes"
          do_run_tests="yes"
          do_reports="yes"
          ;;
        d) do_core=`toggle ${do_core}` ;;
        D) do_developmental=`toggle ${do_developmental}` ;;
        B) BSP="${OPTARG}";;
        O) outputDir="${OPTARG}";;
        v) verbose=`toggle ${verbose}` ;;
        m) do_covoar=`toggle ${do_covoar}` ;;
        P) do_posix=`toggle ${do_posix}` ;;
        S) do_optimize_size=`toggle ${do_optimize_size}` ;;
        u) do_rtems_update=`toggle ${do_rtems_update}` ;;
        c) do_rtems_configure=`toggle ${do_rtems_configure}` ;;
        b) do_rtems_build=`toggle ${do_rtems_build}` ;;
        C) do_copy_tests=`toggle ${do_copy_tests}` ;;
        L) do_link_tests=`toggle ${do_link_tests}` ;;
        r) do_run_tests=`toggle ${do_run_tests}` ;;
        g) do_generate_gcov=`toggle ${do_generate_gcov}` ;;
        R) do_reports=`toggle ${do_reports}` ;;
        f) do_publish=`toggle ${do_publish}` ;;
        t) do_save_tarballs=`toggle ${do_save_tarballs}` ;;
        *) fatal;;
    esac
done

#  If we are to update or configure RTEMS, then we need to clean the
#  RTEMS build tree up.
do_clean="no"
if [ ${do_rtems_update} = "yes" -o \
     ${do_rtems_configure} = "yes" ] ; then
  do_clean="yes"
fi

# allow bsp override
if [ ${BSP} == "not_set" ] ; then
  fatal "No BSP selected"
fi

# you can't publish unless ftp directory is accessible
if [ ${do_publish} = "yes" ] ; then
  if [ ! -d ${FTPDIR} ] ; then
    fatal "Publishing results requested and ${FTPDIR} does not exist"
  fi
fi

# For most BSPs, there is a script with the same name as the BSP.
RUNNER=${BSP}

RTEMSEXT=${DEFAULT_EXEEXT}
COVEXT=cov
case ${BSP} in
  pc386)
    CPU=i386
    COVERAGE_FORMAT=QEMU
    RTEMS_EXTRA_ARGS="USE_COM1_AS_CONSOLE=1"
    RTEMS_EXTRA_ARGS="${RTEMS_EXTRA_ARGS} BSP_PRESS_KEY_FOR_RESET=0"
    RTEMSEXT=exe
    ;; 
  uC5282)
    CPU=m68k
    COVERAGE_FORMAT=QEMU
    RTEMSEXT=exe
    COVEXT=tra
    ;;
  qemuppc)
    RTEMSEXT=ralf
    CPU=powerpc
    COVERAGE_FORMAT=QEMU
    ;;
  lm32_evr)
    CPU=lm32
    COVERAGE_FORMAT=QEMU
    RTEMSEXT=exe
    COVEXT=tra
    RUNNER=qemu-lm32_evr
    RTEMS_EXTRA_ARGS="ON_SIMULATOR=1"
    ;;
  erc32|leon2|leon3)
    CPU=sparc
    COVERAGE_FORMAT=TSIM
    ;;
  edb7312|rtl22xx|gumstix|smdk2410)
    CPU=arm
    COVERAGE_FORMAT=Skyeye
    RTEMS_EXTRA_ARGS="ON_SKYEYE=1"
    ;;
  *)
    fatal "Unsupported BSP (${BSP}) for coverage analysis."
    ;;
esac

case ${RTEMS_VERSION} in
  4.5*)  TARGET=${CPU}-rtems ;;
  4.6*)  TARGET=${CPU}-rtems ;;
  4.7*)  TARGET=${CPU}-rtems4.7 ;;
  4.8*)  TARGET=${CPU}-rtems4.8 ;;
  4.9*)  TARGET=${CPU}-rtems4.9 ;;
  4.10*) TARGET=${CPU}-rtems4.10 ;;
  4.11*) TARGET=${CPU}-rtems4.11 ;;
  *)
    fatal "Unknown RTEMS Version (${RTEMS_VERSION})" ;;
esac

##### VERBOSE
print_verbose()
{
  echo "BSP:                  " ${BSP}
  echo "Target:               " ${TARGET}
  echo "Compiler version:     " `${TARGET}-gcc --version  | grep gcc`
  echo "do_optimize_size:     " ${do_optimize_size}
  echo "Optimization Level:   " ${c_opt}
  echo "POSIX Enabled:        " ${do_posix}
  echo "Core Configuration    " ${do_core}
  echo "Developmental Code:   " ${do_developmental}
  echo "do_all:               " ${do_all}
  echo "do_covoar             " ${do_covoar}
  echo "do_rtems_update:      " ${do_rtems_update}
  echo "do_rtems_configure:   " ${do_rtems_configure}
  echo "do_rtems_build:       " ${do_rtems_build}
  echo "do_copy_tests:        " ${do_copy_tests}
  echo "do_link_tests:        " ${do_link_tests}
  echo "do_run_tests:         " ${do_run_tests}
  echo "do_generate_gcov:     " ${do_generate_gcov}
  echo "do_reports:           " ${do_reports}
  echo "do_publish:           " ${do_publish}
  echo "do_save_tarballs:     " ${do_save_tarballs}
  echo "Clean Before:         " ${do_clean}
  echo "Executable Extension: " ${RTEMSEXT}
  echo "Coverage Extension:   " ${COVEXT}
}

if [ ${do_optimize_size} = yes ] ; then
 c_opt=-Os
else
 c_opt=-O2
fi

if [ ${do_core} = "no" -a ${do_developmental} = "no" ]; then
  fatal "No symbol configurations to analyze selected"
fi

if [ ${verbose} = "yes" ] ; then
  print_verbose
fi

# Basic error checking and sanity checking on the directory structure
# and PATH
if [ ! -d ${COVBASE} ] ; then
  echo "The directory you specified for the base of the coverage run is "
  echo "not present. (${COVBASE})"
  exit 1
fi

if [ ! -d ${RTEMSDIR} ] ; then
  echo "${RTEMSDIR} not found!!!"
  exit 1
fi

type ${TARGET}-gcc
check_status $? "Path appears to be broken"

# Start real action
NM=${TARGET}-nm
type ${NM}
check_status $? "Cannot find ${NM} on PATH"

# Locate the custom file
custom=${RTEMSDIR}/make/custom/${BSP}.cfg
if [ ! -r ${custom} ] ; then
  custom=`ls -1 \
     ${RTEMSDIR}/c/src/lib/libbsp/${CPU}/*/make/custom/${BSP}.cfg 2>/dev/null`
  check_status $? "unable to find a custom file"
  if [ x${custom} = x -o ! -r ${custom} ] ; then
    echo "Unable to read ${custom}"
    exit 1
  fi
fi

grep "^CFLAGS_OPTIMIZE_V.*=.*-O[s01234].*" ${custom} >/dev/null
if [ $? -ne 0 ] ; then
  echo "Unable to find CFLAGS_OPTIMIZE_V in ${custom}"
  exit 1
fi

EXTENSION=${c_opt}
if [ ${do_posix} = yes ] ; then
  EXTENSION=${EXTENSION}P
else
  EXTENSION=${EXTENSION}p
fi

year=`date +%Y`
day=`date +%Y%m%d`
hour=`date +%H`
minute=`date +%M`

echo "Testing ${TARGET}/${BSP} at optimization (${c_opt})"

# print a data element in table
print_element()
{
  echo "  <TD>"${*}"</TD>"
}

# generate table row of results in html
generate_html()
{
  summary=$1
  echo "<TR>"
    print_element ${day}
    print_element ${hour}:${minute}
    print_element ${c_opt}
    print_element ${do_posix}
    print_element ${analyze_developmental}
    print_element `grep "Uncovered range" ${summary} | cut -d ':' -f2`
    print_element `grep "age Not Exec" ${summary} | cut -d ':' -f2`
    print_element `grep "age Exec" ${summary} | cut -d ':' -f2`
    print_element `grep "Bytes Not Exec" ${summary} | cut -d ':' -f2`
    print_element `grep "Bytes Analyzed" ${summary} | cut -d ':' -f2`
    print_element "<A HREF=\"${results_dir}\">untarred</A> " \
                  "<A HREF=\"${results_dir}.tar.bz2\">tarball</A> "
  echo "</TR>"
}

add_to_summary()
{
  unref=${results_dir}/sizes_unreferenced.txt 
  echo
  unreffed=`cat ${unref} | wc -l` 
  echo "Symbols unreferenced: " ${unreffed}
  if [ ${unreffed} = 0 ] ; then
    size_of=0
  else
    size_of=`cut -f1 ${unref} | sed -e '2,$s/$/ +/' -e '$,$s/$/ p/' | dc`
  fi
  echo "Bytes in unreferenced symbols : " ${size_of}
}

generate_size_per_file()
{
  total=0
  pfile=X
  cut -d':' -f1 ${results_dir}/sizes_all.txt | sort  -k3 | while read line
  do
    size=`echo $line | cut -d' ' -f1`
    file=`echo $line | cut -d' ' -f3`
    #echo $size XXX $file
    if [ $file != $pfile ] ; then
      if [ $pfile != X ]; then
	echo $total $pfile
	total=0
      fi
      pfile=$file
    fi
    #echo ADD $file $total $size `expr $total + $size`
    total=`expr $total + $size`
  done | sort -n -r
}

# generate the coverage report set
generate_reports()
{
  if [ $# -ne 3 ] ; then
    echo Usage: ${0} TARGET BSP COVERAGE_FORMAT
    exit 1
  fi

  TARGET=${1}
  BSP=${2}
  COVERAGE_FORMAT=${3}

  cd ${BASEDIR}/${BSP}-tests/
  check_status $? "cd ${BSP}-tests"

  sed -e "s/@FORMAT@/${COVERAGE_FORMAT}/" \
      -e "s/@TARGET@/${TARGET}/" \
      -e "s,@EXPLANATIONS@,${COVBASE}/Explanations.txt," \
      -e "s,@SYMBOLS_FILE@,${BASEDIR}/${BSP}-tests/rtems.syms," \
      -e "s,@OUTPUT_DIRECTORY@,${results_dir}," \
      -e "s/@EXECUTABLE_EXTENSION@/exe/" \
      -e "s/@COVERAGE_EXTENSION@/${RTEMSEXT}.${COVEXT}/" \
      -e "s,@GCNOS_LIST@,${rtems_gcnos_file}," \
      -e "s/@PROJECT_NAME@/RTEMS ${RTEMS_VERSION}/" \
      <${COVBASE}/rtems_config.in \
      >${BASEDIR}/${BSP}-tests/config
  check_status $? "Unable to generate COVOAR config file"

  rm -rf ${results_dir}
  mkdir  ${results_dir}
  covoar -C ${BASEDIR}/${BSP}-tests/config \
    *.exe
  check_status $? "covoar failed"

  # This should be made obsolete by sortable tables. 
  #(echo "====== Largest Range Sizes (Size and Count)======" ; \
  #  grep ^Size uncovered.txt | cut -d':' -f2 | sort -n | uniq -c | \
  #     tail -15 | sed -e 's/ *\([0-9]*\) *\([0-9]*\)/\2 \1/' | sort -n -r | \
  #     while read l; do printf "%6d %6d\n" $l; done ; \
  #  echo "=====================================" ) >>summary.txt

  check_endof >${results_dir}/testsWithNoEndOfTest.txt

  # Generate the configuration settings file
  (
    echo "#"
    echo "#  Settings for this coverage test run"
    echo "#"
    echo
    print_verbose
  ) >${results_dir}/configuration.txt

  RTEMSLIB=${BASEDIR}/b-${BSP}/${TARGET}/${BSP}/lib/librtemscpu.a

  if [ -r ${RTEMSLIB} ] ; then
    while read symbol
    do
      line=`${TARGET}-nm --format=sysv \
	${BASEDIR}/b-${BSP}/${TARGET}/${BSP}/lib/librtemscpu.a | \
	grep FUNC | sed -e's/|/ | /g' | grep ${symbol}`
      size=`echo ${line} | cut -d'|' -f5 | tr "[:lower:]" "[:upper:]"`
      size=`echo 16 i ${size} p | dc`
      echo "${size}	${symbol}	unknown"
    done <${results_dir}/no_range_uncovered.txt | sort -n -r \
         >${results_dir}/sizes_unreferenced.txt
  fi

  cat ${results_dir}/sizes_unreferenced.txt ${results_dir}/sizes.txt | \
    sort -n -r >${results_dir}/sizes_all.txt

  generate_size_per_file >${results_dir}/size_per_file.txt

  generate_html ${results_dir}/summary.txt >${results_dir}/row.html
  add_to_summary >>${results_dir}/summary.txt

  cat ${results_dir}/summary.txt ${results_dir}/testsWithNoEndOfTest.txt

  mv ${results_dir}/index.html ${results_dir}/index.html.tmp
  sed -f ${COVBASE}/rtems_items.sed  \
    <${results_dir}/index.html.tmp \
    >${results_dir}/index.html 

  # Now create the archive of information
  cp ${COVOARBASE}/covoar.css ${COVOARBASE}/*gif ${COVOARBASE}/table.js ${results_dir}
  echo "Results saved in ${results_dir}.tar.bz2"
  tar cjf ${results_dir}.tar.bz2 ${results_dir}
}

# This grabs the list of methods from a library or object file
filter_nm()
{
  for l in $*
  do
    if [ -r ${l} ] ; then
      test ${verbose} = "yes" && echo Generating symbols from ${l} >&2
      # echo "========= START $l"
      ${TARGET}-nm --format=sysv $l | grep "FUNC|" | \
        cut -d'|' -f1 | sed -e 's/ *$//' -e "s,$, ${l},"

      # echo "========= END $l"

    # no else
      # We keep a complete CURRENT list and some are not in old versions
    fi
  done
}

# generate the list of symbols to analyze
generate_symbols()
{
  case ${RTEMS_VERSION} in
    4.5*|4.6*)
      cd ${BASEDIR}/b-${BSP}/${TARGET}/c/${BSP}/exec
      check_status $? "could not cd to object directory"
      subd=o-optimize/
      ;;
    *)
      cd ${BASEDIR}/b-${BSP}/${TARGET}/c/${BSP}/cpukit
      check_status $? "could not cd to object directory"
      subd=
      ;;
  esac

  case ${RTEMS_VERSION} in
    4.5*)
      filter_nm wrapup/rtems/o-optimize/librtems.a
      if [ ${do_posix} = "yes" ] ; then
	filter_nm wrapup/posix/o-optimize/libposix.a
      fi

      if [ ${analyze_developmental} = "yes" ] ; then
	filter_nm ../lib/libc/${subd}libcsupport.a
	filter_nm ../lib/libmisc/wrapup/${subd}libmisc.a
      fi
      ;;
    *) # 4.6 or newer
      # Base set of libraries to analyse.  Do NOT include libscorecpu.a!!!
      filter_nm score/${subd}libscore.a
      filter_nm sapi/${subd}libsapi.a
      filter_nm rtems/${subd}librtems.a

      # Now add the POSIX API onto that
      if [ ${do_posix} = "yes" ] ; then
        filter_nm posix/${subd}libposix.a
      fi

      # Now add developmental libraries
      # NOTE: all non-network libraries are listed here.  --joel 5 March 2010
      if [ ${analyze_developmental} = "yes" ] ; then
        filter_nm libfs/${subd}librfs.a
        filter_nm libfs/${subd}libdosfs.a
        filter_nm libfs/${subd}libdevfs.a
        filter_nm libfs/${subd}libimfs.a
        filter_nm libcsupport/${subd}libcsupport.a
        # filter_nm libi2c/${subd}libi2c.a
        # filter_nm zlib/${subd}libz.a
        # filter_nm libmd/${subd}libmd.a
        # filter_nm libmisc/${subd}libshell.a
        filter_nm libmisc/${subd}libbspcmdline.a
        filter_nm libmisc/${subd}libcpuuse.a
        # filter_nm libmisc/${subd}libuuid.a
        filter_nm libmisc/${subd}libstackchk.a
        # filter_nm libmisc/${subd}libmw-fb.a
        # NEVER INCLUDE libdummy.a!!!
        # filter_nm libmisc/${subd}libdummy.a
        filter_nm libmisc/${subd}libfsmount.a
        filter_nm libmisc/${subd}libstringto.a
        filter_nm libmisc/${subd}libdevnull.a
        # filter_nm libmisc/${subd}libcapture.a
        filter_nm libmisc/${subd}libdumpbuf.a
        # filter_nm libmisc/${subd}libmonitor.a
        # filter_nm libmisc/${subd}libserdbg.a
        # filter_nm libmisc/${subd}libuntar.a
        filter_nm libblock/${subd}libblock.a
      fi
      ;;
    esac
}

# remove symbols we do not want coverage on.
remove_unwanted_symbols()
{
#  cat
#  sed -e '/IMFS_print_node/d' \
#      -e '/IMFS_dump_directory/d' \
#      -e '/IMFS_dump/d' \
#      -e '/IMFS_memfile_maximum_size/d'
   sed -e '/rtems_shell_debugrfs/d' \
       -e '/rtems_rfs_shell_/d'
}

# Now we are ready to start doing real work
start=`date`

# If necessary, clean up the RTEMS build and test run directories
if [ ${do_clean} = "yes" ] ; then
  echo "Cleaning before building"
  rm -rf ${BASEDIR}/b-${BSP}
  rm -rf ${BASEDIR}/${BSP}-tests
else
  echo "Skipping clean before building"
fi

# If they don't exist, create the RTEMS build and test run directories
test -d ${BASEDIR}/b-${BSP}     || mkdir ${BASEDIR}/b-${BSP}
test -d ${BASEDIR}/${BSP}-tests || mkdir ${BASEDIR}/${BSP}-tests

# If requested, update and build the coverage support tools
if [ ${do_covoar} = "yes" ] ; then
  echo "Updating and building covoar..."

  cd ${COVBASE}/../covoar
  check_status $? "cd covoar"

  cvs up -Pd 2>&1 | grep -v ^cvs
  make clean all
  check_status $? "build covoar"

  make
  check_status $? "make covoar"
else
  echo "Skipping Updating and building covoar..."
fi

# If requested, update the RTEMS tree
if [ ${do_rtems_update} = "yes" ] ; then
  echo "Updating RTEMS ..."
  cd ${RTEMSDIR}
  check_status $? "cd rtems"

  cvs up -Pd 2>&1 | grep -v ^cvs
  ./bootstrap -c
  ./bootstrap
else
  echo "Skipping Updating RTEMS ..."
fi

# If requested, configure RTEMS
if [ ${do_rtems_configure} = "yes" ] ; then
  # Now let's patch the make/custom file
  cat ${custom} | 
  sed -e '/^CFLAGS_OPTIMIZE_V.*=.*-ftest-coverage -finline-functions -g/d' \
      -e "s/-O[0123s]/${c_opt}/" | 
  if [ ${do_generate_gcov} = "yes" ] ; then
    sed -e \
      '/^CFLAGS_OPTIMIZE_V/a CFLAGS_OPTIMIZE_V += -ftest-coverage -finline-functions -g' 
  else
    cat
  fi >${custom}.tmp
  mv ${custom}.tmp ${custom}

  echo "Configuring RTEMS..."
  rm -rf ${BASEDIR}/b-${BSP}/
  mkdir ${BASEDIR}/b-${BSP}/
  cd ${BASEDIR}/b-${BSP}/
  check_status $? "cd b-${BSP}"

  ##################
  ################## WARNING!!!!!!
  ##################
  ################## BE CAREFUL ABOUT THIS CONFIGURE COMMAND.  IT IS
  ################## VERY SPECIFIC TO COVERAGE TESTING
  ##################
  if [ ${do_posix} = "yes" ] ; then
    posix_en=en
  else
    posix_en=dis
  fi

  if [ ${RTEMS_VERSION} = 4.5 ] ; then
    ${RTEMSDIR}/configure \
      --target=${TARGET} --enable-rtemsbsp=${BSP} \
      --enable-maintainer-mode \
      --disable-itron --${posix_en}able-posix --enable-tests \
      --disable-networking --disable-ada --disable-cxx \
      --prefix=${BASEDIR}/coverage/install >c.log 2>&1
  else
    ${RTEMSDIR}/configure NDEBUG=1 \
      RTEMS_DO_NOT_INLINE_THREAD_ENABLE_DISPATCH=1 \
      RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE=1 \
      RTEMS_DO_NOT_UNROLL_THREADQ_ENQUEUE_PRIORITY=1 \
      ${RTEMS_EXTRA_ARGS} \
      --target=${TARGET} --enable-rtemsbsp=${BSP} \
      --enable-maintainer-mode \
      --disable-itron --${posix_en}able-posix --enable-tests \
      --disable-networking --disable-ada --disable-cxx \
      --prefix=${BASEDIR}/coverage/install >c.log 2>&1
  fi
  check_status $? "configuring RTEMS for ${BSP}"
else
  echo "Skipping Configuring RTEMS ..."
fi

# If requested, build RTEMS
if [ ${do_rtems_build} = "yes" ] ; then
  echo "Building RTEMS..."

  cd ${BASEDIR}/b-${BSP}/
  check_status $? "cd b-${BSP}"

  case ${RTEMS_VERSION} in
    4.5*|4.6*)
      sleep 5  # 4.5 on chroot'ed env needs this for some reason
      make >b.log 2>&1
      ;;
    *)
      cpus=`/usr/bin/getconf _NPROCESSORS_ONLN`
      make -j${cpus} >b.log 2>&1
      ;;
  esac
  check_status $? "Building RTEMS for ${BSP}"
else
  echo "Skipping Building RTEMS ..."
fi

# If requested, copy the tests from the build tree to the run tree
if [ ${do_copy_tests} = "yes" ] ; then
  echo "Copying tests..."

  # clean destination
  rm -rf ${BASEDIR}/${BSP}-tests/*
  check_status $? "clean test directory"

  cd ${BASEDIR}/b-${BSP}/
  check_status $? "cd b-${BSP}"

  find `pwd` -name "*.ralf" -o -name "*.exe" | while read f
  do
    rm -f ../${BSP}-tests/`basename ${f}`
    if [ ${do_link_tests} = "yes" ] ; then 
      ln -s ${f} ../${BSP}-tests
    else
      cp ${f} ../${BSP}-tests
    fi
  done
  
else
  echo "Skipping copying tests..."
fi

# If requested prepare data to create gcov reports 
if [ ${do_generate_gcov} = "yes" ] ; then
  echo "Preparing data for gcov reports..."

  cd ${BASEDIR}/b-${BSP}/
  check_status $? "cd b-${BSP}"

  rtems_gcnos_file="${BASEDIR}/${BSP}-tests/rtems.gcnos"
  find `pwd`  -name \*.gcno >${rtems_gcnos_file}

  check_status $? "Preparing data for gcov reports for for ${BSP}"
else
  rtems_gcnos_file="/dev/null"
  echo "Skipping creating gcov reports ..."
fi

# If requested, run the tests with coverage reporting enabled
if [ ${do_run_tests} = "yes" ] ; then
  echo "Running tests..."
  cd ${BASEDIR}/${BSP}-tests/
  check_status $? "cd ${BSP}-tests"

  time ${RUNNER} -c *.${RTEMSEXT}
else
  echo "Skipping Running tests..."
fi

# If requested, generate the coverage reports
if [ ${do_reports} = "yes" ] ; then
  echo "Generating reports..."

  cd ${BASEDIR}/${BSP}-tests/
  check_status $? "cd ${BSP}-tests"

  to_analyze=""
  if [ ${do_core} = "yes" ] ; then
    to_analyze="${to_analyze} no"
  fi
  if [ ${do_developmental} = "yes" ]; then
    to_analyze="${to_analyze} yes"
  fi

  for analyze_developmental in ${to_analyze}
  do
    if [ ${analyze_developmental} = yes ] ; then
      echo "Processing coverage for developmental configuration ..."
      EXT=${EXTENSION}D
    else
      echo "Processing coverage for core source configuration ..."
      EXT=${EXTENSION}d
    fi
    if [ "X${outputDir}" = "X" ] ; then
      results_dir=${BSP}${EXT}-${day}-${hour}${minute}
    else
      results_dir=${outputDir}
    fi

    generate_symbols | sort -u | remove_unwanted_symbols \
      >${BASEDIR}/${BSP}-tests/rtems.syms
    syms=`cat ${BASEDIR}/${BSP}-tests/rtems.syms | wc -l`
    if [ ${syms} -eq 0 ] ; then
      fatal "no symbols found"
    fi

    generate_reports ${TARGET} ${BSP} ${COVERAGE_FORMAT}
  done
else
  echo "Skipping Generating reports..."
fi

# If requested, publish the coverage reports
if [ ${do_publish} = "yes" -o ${do_save_tarballs} = "yes" ] ; then
  if [ -d ${BASEDIR}/${BSP}-tests ] ; then
    cd ${BASEDIR}/${BSP}-tests/
    if [ ${do_publish} = "yes" ] ; then
      cp ${BSP}${EXTENSION}[dD]-${day}-${hour}${minute}.tar.bz2 ${FTPDIR}
      check_status $? "cp ${BSP} results to ftp site failed"
      ${COVBASE}/generate_coverage_html ${FTPDIR}
    fi
    if [ ${do_save_tarballs} = "yes" ] ; then
      if [ ! -d ${TARDIR} ] ; then
         echo "Creating ${TARDIR}"
         mkdir -p ${TARDIR}
         check_status $? "Failed to mkdir ${TARDIR}"
      fi
      cp ${BSP}${EXTENSION}[dD]-${day}-${hour}${minute}.tar.bz2 ${TARDIR}
      check_status $? "cp ${BSP} results to save directory"
    fi
  else
    echo "No results to publish or save"
  fi
else
  echo "Skipping Publishing reports..."
fi
stop=`date`

echo "Started: " ${start}
echo "Stopped: " ${stop}

exit 0