MAKEALL: Fix case substitution for old bash
[platform/kernel/u-boot.git] / MAKEALL
1 #!/bin/bash
2 # Tool mainly for U-Boot Quality Assurance: build one or more board
3 # configurations with minimal verbosity, showing only warnings and
4 # errors.
5
6 usage()
7 {
8         # if exiting with 0, write to stdout, else write to stderr
9         local ret=${1:-0}
10         [ "${ret}" -eq 1 ] && exec 1>&2
11         cat <<-EOF
12         Usage: MAKEALL [options] [--] [boards-to-build]
13
14         Options:
15           -a ARCH,   --arch ARCH       Build all boards with arch ARCH
16           -c CPU,    --cpu CPU         Build all boards with cpu CPU
17           -v VENDOR, --vendor VENDOR   Build all boards with vendor VENDOR
18           -s SOC,    --soc SOC         Build all boards with soc SOC
19           -l,        --list            List all targets to be built
20           -m,        --maintainers     List all targets and maintainer email
21           -M,        --mails           List all targets and all affilated emails
22           -C,        --check           Enable build checking
23           -n,        --continue        Continue (skip boards already built)
24           -r,        --rebuild-errors  Rebuild any boards that errored
25           -h,        --help            This help output
26
27         Selections by these options are logically ANDed; if the same option
28         is used repeatedly, such selections are ORed.  So "-v FOO -v BAR"
29         will select all configurations where the vendor is either FOO or
30         BAR.  Any additional arguments specified on the command line are
31         always build additionally.  See the boards.cfg file for more info.
32
33         If no boards are specified, then the default is "powerpc".
34
35         Environment variables:
36           BUILD_NCPUS      number of parallel make jobs (default: auto)
37           CROSS_COMPILE    cross-compiler toolchain prefix (default: "")
38           CROSS_COMPILE_<ARCH> cross-compiler toolchain prefix for
39                            architecture "ARCH".  Substitute "ARCH" for any
40                            supported architecture (default: "")
41           MAKEALL_LOGDIR   output all logs to here (default: ./LOG/)
42           BUILD_DIR        output build directory (default: ./)
43           BUILD_NBUILDS    number of parallel targets (default: 1)
44
45         Examples:
46           - build all Power Architecture boards:
47               MAKEALL -a powerpc
48               MAKEALL --arch powerpc
49               MAKEALL powerpc
50           - build all PowerPC boards manufactured by vendor "esd":
51               MAKEALL -a powerpc -v esd
52           - build all PowerPC boards manufactured either by "keymile" or "siemens":
53               MAKEALL -a powerpc -v keymile -v siemens
54           - build all Freescale boards with MPC83xx CPUs, plus all 4xx boards:
55               MAKEALL -c mpc83xx -v freescale 4xx
56         EOF
57         exit ${ret}
58 }
59
60 SHORT_OPTS="ha:c:v:s:lmMCnr"
61 LONG_OPTS="help,arch:,cpu:,vendor:,soc:,list,maintainers,mails,check,continue,rebuild-errors"
62
63 # Option processing based on util-linux-2.13/getopt-parse.bash
64
65 # Note that we use `"$@"' to let each command-line parameter expand to a
66 # separate word. The quotes around `$@' are essential!
67 # We need TEMP as the `eval set --' would nuke the return value of
68 # getopt.
69 TEMP=`getopt -o ${SHORT_OPTS} --long ${LONG_OPTS} \
70      -n 'MAKEALL' -- "$@"`
71
72 [ $? != 0 ] && usage 1
73
74 # Note the quotes around `$TEMP': they are essential!
75 eval set -- "$TEMP"
76
77 SELECTED=''
78 ONLY_LIST=''
79 PRINT_MAINTS=''
80 MAINTAINERS_ONLY=''
81 CONTINUE=''
82 REBUILD_ERRORS=''
83
84 while true ; do
85         case "$1" in
86         -a|--arch)
87                 # echo "Option ARCH: argument \`$2'"
88                 if [ "$opt_a" ] ; then
89                         opt_a="${opt_a%)} || \$2 == \"$2\")"
90                 else
91                         opt_a="(\$2 == \"$2\")"
92                 fi
93                 SELECTED='y'
94                 shift 2 ;;
95         -c|--cpu)
96                 # echo "Option CPU: argument \`$2'"
97                 if [ "$opt_c" ] ; then
98                         opt_c="${opt_c%)} || \$3 == \"$2\" || \$3 ~ /$2:/)"
99                 else
100                         opt_c="(\$3 == \"$2\" || \$3 ~ /$2:/)"
101                 fi
102                 SELECTED='y'
103                 shift 2 ;;
104         -s|--soc)
105                 # echo "Option SoC: argument \`$2'"
106                 if [ "$opt_s" ] ; then
107                         opt_s="${opt_s%)} || \$6 == \"$2\" || \$6 ~ /$2/)"
108                 else
109                         opt_s="(\$6 == \"$2\" || \$6 ~ /$2/)"
110                 fi
111                 SELECTED='y'
112                 shift 2 ;;
113         -v|--vendor)
114                 # echo "Option VENDOR: argument \`$2'"
115                 if [ "$opt_v" ] ; then
116                         opt_v="${opt_v%)} || \$5 == \"$2\")"
117                 else
118                         opt_v="(\$5 == \"$2\")"
119                 fi
120                 SELECTED='y'
121                 shift 2 ;;
122         -C|--check)
123                 CHECK='C=1'
124                 shift ;;
125         -n|--continue)
126                 CONTINUE='y'
127                 shift ;;
128         -r|--rebuild-errors)
129                 REBUILD_ERRORS='y'
130                 shift ;;
131         -l|--list)
132                 ONLY_LIST='y'
133                 shift ;;
134         -m|--maintainers)
135                 ONLY_LIST='y'
136                 PRINT_MAINTS='y'
137                 MAINTAINERS_ONLY='y'
138                 shift ;;
139         -M|--mails)
140                 ONLY_LIST='y'
141                 PRINT_MAINTS='y'
142                 shift ;;
143         -h|--help)
144                 usage ;;
145         --)
146                 shift ; break ;;
147         *)
148                 echo "Internal error!" >&2 ; exit 1 ;;
149         esac
150 done
151 # echo "Remaining arguments:"
152 # for arg do echo '--> '"\`$arg'" ; done
153
154 FILTER="\$1 !~ /^#/"
155 [ "$opt_a" ] && FILTER="${FILTER} && $opt_a"
156 [ "$opt_c" ] && FILTER="${FILTER} && $opt_c"
157 [ "$opt_s" ] && FILTER="${FILTER} && $opt_s"
158 [ "$opt_v" ] && FILTER="${FILTER} && $opt_v"
159
160 if [ "$SELECTED" ] ; then
161         SELECTED=$(awk '('"$FILTER"') { print $1 }' boards.cfg)
162
163         # Make sure some boards from boards.cfg are actually found
164         if [ -z "$SELECTED" ] ; then
165                 echo "Error: No boards selected, invalid arguments"
166                 exit 1
167         fi
168 fi
169
170 #########################################################################
171
172 # Print statistics when we exit
173 trap exit 1 2 3 15
174 trap print_stats 0
175
176 # Determine number of CPU cores if no default was set
177 : ${BUILD_NCPUS:="`getconf _NPROCESSORS_ONLN`"}
178
179 if [ "$BUILD_NCPUS" -gt 1 ]
180 then
181         JOBS="-j $((BUILD_NCPUS + 1))"
182 else
183         JOBS=""
184 fi
185
186 if [ "${MAKEALL_LOGDIR}" ] ; then
187         LOG_DIR=${MAKEALL_LOGDIR}
188 else
189         LOG_DIR="LOG"
190 fi
191
192 : ${BUILD_NBUILDS:=1}
193 BUILD_MANY=0
194
195 if [ "${BUILD_NBUILDS}" -gt 1 ] ; then
196         BUILD_MANY=1
197         : ${BUILD_DIR:=./build}
198         mkdir -p "${BUILD_DIR}/ERR"
199         find "${BUILD_DIR}/ERR/" -type f -exec rm -f {} +
200 fi
201
202 : ${BUILD_DIR:=.}
203
204 OUTPUT_PREFIX="${BUILD_DIR}"
205
206 [ -d ${LOG_DIR} ] || mkdir "${LOG_DIR}" || exit 1
207 if [ "$CONTINUE" != 'y' -a "$REBUILD_ERRORS" != 'y' ] ; then
208         find "${LOG_DIR}/" -type f -exec rm -f {} +
209 fi
210
211 LIST=""
212
213 # Keep track of the number of builds and errors
214 ERR_CNT=0
215 ERR_LIST=""
216 WRN_CNT=0
217 WRN_LIST=""
218 TOTAL_CNT=0
219 SKIP_CNT=0
220 CURRENT_CNT=0
221 OLDEST_IDX=1
222 RC=0
223
224 # Helper funcs for parsing boards.cfg
225 boards_by_field()
226 {
227         FS="[ \t]+"
228         [ -n "$3" ] && FS="$3"
229         awk \
230                 -v field="$1" \
231                 -v select="$2" \
232                 -F "$FS" \
233                 '($1 !~ /^#/ && $field == select) { print $1 }' \
234                 boards.cfg
235 }
236 boards_by_arch() { boards_by_field 2 "$@" ; }
237 boards_by_cpu()  { boards_by_field 3 "$@" "[: \t]+" ; }
238 boards_by_soc()  { boards_by_field 6 "$@" ; }
239
240 #########################################################################
241 ## MPC5xx Systems
242 #########################################################################
243
244 LIST_5xx="$(boards_by_cpu mpc5xx)"
245
246 #########################################################################
247 ## MPC5xxx Systems
248 #########################################################################
249
250 LIST_5xxx="$(boards_by_cpu mpc5xxx)"
251
252 #########################################################################
253 ## MPC512x Systems
254 #########################################################################
255
256 LIST_512x="$(boards_by_cpu mpc512x)"
257
258 #########################################################################
259 ## MPC8xx Systems
260 #########################################################################
261
262 LIST_8xx="$(boards_by_cpu mpc8xx)"
263
264 #########################################################################
265 ## PPC4xx Systems
266 #########################################################################
267
268 LIST_4xx="$(boards_by_cpu ppc4xx)"
269
270 #########################################################################
271 ## MPC8220 Systems
272 #########################################################################
273
274 LIST_8220="$(boards_by_cpu mpc8220)"
275
276 #########################################################################
277 ## MPC824x Systems
278 #########################################################################
279
280 LIST_824x="$(boards_by_cpu mpc824x)"
281
282 #########################################################################
283 ## MPC8260 Systems (includes 8250, 8255 etc.)
284 #########################################################################
285
286 LIST_8260="$(boards_by_cpu mpc8260)"
287
288 #########################################################################
289 ## MPC83xx Systems (includes 8349, etc.)
290 #########################################################################
291
292 LIST_83xx="$(boards_by_cpu mpc83xx)"
293
294 #########################################################################
295 ## MPC85xx Systems (includes 8540, 8560 etc.)
296 #########################################################################
297
298 LIST_85xx="$(boards_by_cpu mpc85xx)"
299
300 #########################################################################
301 ## MPC86xx Systems
302 #########################################################################
303
304 LIST_86xx="$(boards_by_cpu mpc86xx)"
305
306 #########################################################################
307 ## 74xx/7xx Systems
308 #########################################################################
309
310 LIST_74xx_7xx="$(boards_by_cpu 74xx_7xx)"
311
312 #########################################################################
313 ## PowerPC groups
314 #########################################################################
315
316 LIST_TSEC="             \
317         ${LIST_83xx}    \
318         ${LIST_85xx}    \
319         ${LIST_86xx}    \
320 "
321
322 LIST_powerpc="          \
323         ${LIST_5xx}     \
324         ${LIST_512x}    \
325         ${LIST_5xxx}    \
326         ${LIST_8xx}     \
327         ${LIST_8220}    \
328         ${LIST_824x}    \
329         ${LIST_8260}    \
330         ${LIST_83xx}    \
331         ${LIST_85xx}    \
332         ${LIST_86xx}    \
333         ${LIST_4xx}     \
334         ${LIST_74xx_7xx}\
335 "
336
337 # Alias "ppc" -> "powerpc" to not break compatibility with older scripts
338 # still using "ppc" instead of "powerpc"
339 LIST_ppc="              \
340         ${LIST_powerpc} \
341 "
342
343 #########################################################################
344 ## StrongARM Systems
345 #########################################################################
346
347 LIST_SA="$(boards_by_cpu sa1100)"
348
349 #########################################################################
350 ## ARM7 Systems
351 #########################################################################
352
353 LIST_ARM7="$(boards_by_cpu arm720t)"
354
355 #########################################################################
356 ## ARM9 Systems
357 #########################################################################
358
359 LIST_ARM9="$(boards_by_cpu arm920t)     \
360         $(boards_by_cpu arm926ejs)      \
361         $(boards_by_cpu arm925t)        \
362         $(boards_by_cpu arm946es)       \
363 "
364
365 #########################################################################
366 ## ARM11 Systems
367 #########################################################################
368 LIST_ARM11="$(boards_by_cpu arm1136)    \
369         $(boards_by_cpu arm1176)        \
370 "
371
372 #########################################################################
373 ## ARMV7 Systems
374 #########################################################################
375
376 LIST_ARMV7="$(boards_by_cpu armv7)"
377
378 #########################################################################
379 ## AT91 Systems
380 #########################################################################
381
382 LIST_at91="$(boards_by_soc at91)"
383
384 #########################################################################
385 ## Xscale Systems
386 #########################################################################
387
388 LIST_pxa="$(boards_by_cpu pxa)"
389
390 LIST_ixp="$(boards_by_cpu ixp)"
391
392 #########################################################################
393 ## SPEAr Systems
394 #########################################################################
395
396 LIST_spear="$(boards_by_soc spear)"
397
398 #########################################################################
399 ## ARM groups
400 #########################################################################
401
402 LIST_arm="$(boards_by_arch arm)"
403
404 #########################################################################
405 ## MIPS Systems         (default = big endian)
406 #########################################################################
407
408 LIST_mips4kc="          \
409         incaip          \
410         incaip_100MHz   \
411         incaip_133MHz   \
412         incaip_150MHz   \
413         qemu_mips       \
414         vct_platinum    \
415         vct_platinum_small      \
416         vct_platinum_onenand    \
417         vct_platinum_onenand_small      \
418         vct_platinumavc \
419         vct_platinumavc_small   \
420         vct_platinumavc_onenand \
421         vct_platinumavc_onenand_small   \
422         vct_premium     \
423         vct_premium_small       \
424         vct_premium_onenand     \
425         vct_premium_onenand_small       \
426 "
427
428 LIST_au1xx0="           \
429         dbau1000        \
430         dbau1100        \
431         dbau1500        \
432         dbau1550        \
433 "
434
435 LIST_mips="             \
436         ${LIST_mips4kc} \
437         ${LIST_mips5kc} \
438         ${LIST_au1xx0}  \
439 "
440
441 #########################################################################
442 ## MIPS Systems         (little endian)
443 #########################################################################
444
445 LIST_xburst_el="        \
446         qi_lb60         \
447 "
448
449 LIST_au1xx0_el="        \
450         dbau1550_el     \
451         pb1000          \
452 "
453 LIST_mips_el="                  \
454         ${LIST_xburst_el}       \
455         ${LIST_au1xx0_el}       \
456 "
457 #########################################################################
458 ## OpenRISC Systems
459 #########################################################################
460
461 LIST_openrisc="$(boards_by_arch openrisc)"
462
463 #########################################################################
464 ## x86 Systems
465 #########################################################################
466
467 LIST_x86="$(boards_by_arch x86)"
468
469 #########################################################################
470 ## Nios-II Systems
471 #########################################################################
472
473 LIST_nios2="$(boards_by_arch nios2)"
474
475 #########################################################################
476 ## MicroBlaze Systems
477 #########################################################################
478
479 LIST_microblaze="$(boards_by_arch microblaze)"
480
481 #########################################################################
482 ## ColdFire Systems
483 #########################################################################
484
485 LIST_m68k="$(boards_by_arch m68k)"
486 LIST_coldfire=${LIST_m68k}
487
488 #########################################################################
489 ## AVR32 Systems
490 #########################################################################
491
492 LIST_avr32="$(boards_by_arch avr32)"
493
494 #########################################################################
495 ## Blackfin Systems
496 #########################################################################
497
498 LIST_blackfin="$(boards_by_arch blackfin)"
499
500 #########################################################################
501 ## SH Systems
502 #########################################################################
503
504 LIST_sh2="$(boards_by_cpu sh2)"
505 LIST_sh3="$(boards_by_cpu sh3)"
506 LIST_sh4="$(boards_by_cpu sh4)"
507
508 LIST_sh="$(boards_by_arch sh)"
509
510 #########################################################################
511 ## SPARC Systems
512 #########################################################################
513
514 LIST_sparc="$(boards_by_arch sparc)"
515
516 #########################################################################
517 ## NDS32 Systems
518 #########################################################################
519
520 LIST_nds32="$(boards_by_arch nds32)"
521
522 #-----------------------------------------------------------------------
523
524 get_target_location() {
525         local target=$1
526         local BOARD_NAME=""
527         local CONFIG_NAME=""
528         local board=""
529         local vendor=""
530
531         # Automatic mode
532         local line=`egrep -i "^[[:space:]]*${target}[[:space:]]" boards.cfg`
533
534         if [ -z "${line}" ] ; then echo "" ; return ; fi
535
536         set ${line}
537
538         # add default board name if needed
539         [ $# = 3 ] && set ${line} ${1}
540
541         CONFIG_NAME="${1%_config}"
542
543         [ "${BOARD_NAME}" ] || BOARD_NAME="${1%_config}"
544
545         if [ "$4" = "-" ] ; then
546                 board=${BOARD_NAME}
547         else
548                 board="$4"
549         fi
550
551         [ $# -gt 4 ] && [ "$5" != "-" ] && vendor="$5"
552         [ $# -gt 6 ] && [ "$7" != "-" ] && {
553                 tmp="${7%:*}"
554                 if [ "$tmp" ] ; then
555                         CONFIG_NAME="$tmp"
556                 fi
557         }
558
559         # Assign board directory to BOARDIR variable
560         if [ -z "${vendor}" ] ; then
561             BOARDDIR=${board}
562         else
563             BOARDDIR=${vendor}/${board}
564         fi
565
566         echo "${CONFIG_NAME}:${BOARDDIR}"
567 }
568
569 get_target_maintainers() {
570         local name=`echo $1 | cut -d : -f 1`
571
572         if ! grep -qsi "[[:blank:]]${name}[[:blank:]]" MAINTAINERS ; then
573                 echo ""
574                 return ;
575         fi
576
577         local line=`tac MAINTAINERS | grep -ni "[[:blank:]]${name}[[:blank:]]" | cut -d : -f 1`
578         local mail=`tac MAINTAINERS | tail -n +${line} | \
579                 sed -n ":start /.*@.*/ { b mail } ; n ; b start ; :mail /.*@.*/ { p ; n ; b mail } ; q" | \
580                 sed "s/^.*<//;s/>.*$//"`
581         echo "$mail"
582 }
583
584 get_target_arch() {
585         local target=$1
586
587         # Automatic mode
588         local line=`egrep -i "^[[:space:]]*${target}[[:space:]]" boards.cfg`
589
590         if [ -z "${line}" ] ; then echo "" ; return ; fi
591
592         set ${line}
593         echo "$2"
594 }
595
596 list_target() {
597         if [ "$PRINT_MAINTS" != 'y' ] ; then
598                 echo "$1"
599                 return
600         fi
601
602         echo -n "$1:"
603
604         local loc=`get_target_location $1`
605
606         if [ -z "${loc}" ] ; then echo "ERROR" ; return ; fi
607
608         local maintainers_result=`get_target_maintainers ${loc} | tr " " "\n"`
609
610         if [ "$MAINTAINERS_ONLY" != 'y' ] ; then
611
612                 local dir=`echo ${loc} | cut -d ":" -f 2`
613                 local cfg=`echo ${loc} | cut -d ":" -f 1`
614                 local git_result=`git log --format=%aE board/${dir} \
615                                 include/configs/${cfg}.h | grep "@"`
616                 local git_result_recent=`echo ${git_result} | tr " " "\n" | \
617                                                 head -n 3`
618                 local git_result_top=`echo ${git_result} | tr " " "\n" | \
619                         sort | uniq -c | sort -nr | head -n 3 | \
620                         sed "s/^ \+[0-9]\+ \+//"`
621
622                 echo -e "$git_result_recent\n$git_result_top\n$maintainers_result" | \
623                         sort -u | tr "\n" " " | sed "s/ $//" ;
624         else
625                 echo -e "$maintainers_result" | sort -u | tr "\n" " " | \
626                                                 sed "s/ $//" ;
627         fi
628
629         echo ""
630 }
631
632 # Each finished build will have a file called ${donep}${n},
633 # where n is the index of the build. Each build
634 # we've already noted as finished will have ${skipp}${n}.
635 # The code managing the build process will use this information
636 # to ensure that only BUILD_NBUILDS builds are in flight at once
637 donep="${LOG_DIR}/._done_"
638 skipp="${LOG_DIR}/._skip_"
639
640 build_target_killed() {
641         echo "Aborted $target build."
642         # Remove the logs for this board since it was aborted
643         rm -f ${LOG_DIR}/$target.MAKELOG ${LOG_DIR}/$target.ERR
644         exit
645 }
646
647 build_target() {
648         target=$1
649         build_idx=$2
650
651         if [ "$ONLY_LIST" == 'y' ] ; then
652                 list_target ${target}
653                 return
654         fi
655
656         if [ $BUILD_MANY == 1 ] ; then
657                 output_dir="${OUTPUT_PREFIX}/${target}"
658                 mkdir -p "${output_dir}"
659                 trap build_target_killed TERM
660         else
661                 output_dir="${OUTPUT_PREFIX}"
662         fi
663
664         export BUILD_DIR="${output_dir}"
665
666         target_arch=$(get_target_arch ${target})
667         eval cross_toolchain=\$CROSS_COMPILE_`echo $target_arch | tr '[:lower:]' '[:upper:]'`
668         if [ "${cross_toolchain}" ] ; then
669             MAKE="make CROSS_COMPILE=${cross_toolchain}"
670         elif [ "${CROSS_COMPILE}" ] ; then
671             MAKE="make CROSS_COMPILE=${CROSS_COMPILE}"
672         else
673             MAKE=make
674         fi
675
676         ${MAKE} distclean >/dev/null
677         ${MAKE} -s ${target}_config
678
679         ${MAKE} ${JOBS} ${CHECK} all \
680                 >${LOG_DIR}/$target.MAKELOG 2> ${LOG_DIR}/$target.ERR
681
682         # Check for 'make' errors
683         if [ ${PIPESTATUS[0]} -ne 0 ] ; then
684                 RC=1
685         fi
686
687         if [ $BUILD_MANY == 1 ] ; then
688                 trap - TERM
689
690                 ${MAKE} -s tidy
691
692                 if [ -s ${LOG_DIR}/${target}.ERR ] ; then
693                         cp ${LOG_DIR}/${target}.ERR ${OUTPUT_PREFIX}/ERR/${target}
694                 else
695                         rm ${LOG_DIR}/${target}.ERR
696                 fi
697         else
698                 if [ -s ${LOG_DIR}/${target}.ERR ] ; then
699                         if grep -iw error ${LOG_DIR}/${target}.ERR ; then
700                                 : $(( ERR_CNT += 1 ))
701                                 ERR_LIST="${ERR_LIST} $target"
702                         else
703                                 : $(( WRN_CNT += 1 ))
704                                 WRN_LIST="${WRN_LIST} $target"
705                         fi
706                 else
707                         rm ${LOG_DIR}/${target}.ERR
708                 fi
709         fi
710
711         OBJS=${output_dir}/u-boot
712         if [ -e ${output_dir}/spl/u-boot-spl ]; then
713                 OBJS="${OBJS} ${output_dir}/spl/u-boot-spl"
714         fi
715
716         ${CROSS_COMPILE}size ${OBJS} | tee -a ${LOG_DIR}/$target.MAKELOG
717
718         [ -e "${LOG_DIR}/${target}.ERR" ] && cat "${LOG_DIR}/${target}.ERR"
719
720         touch "${donep}${build_idx}"
721 }
722
723 manage_builds() {
724         search_idx=${OLDEST_IDX}
725         if [ "$ONLY_LIST" == 'y' ] ; then return ; fi
726
727         while true; do
728                 if [ -e "${donep}${search_idx}" ] ; then
729                         : $(( CURRENT_CNT-- ))
730                         [ ${OLDEST_IDX} -eq ${search_idx} ] &&
731                                 : $(( OLDEST_IDX++ ))
732
733                         # Only want to count it once
734                         rm -f "${donep}${search_idx}"
735                         touch "${skipp}${search_idx}"
736                 elif [ -e "${skipp}${search_idx}" ] ; then
737                         [ ${OLDEST_IDX} -eq ${search_idx} ] &&
738                                 : $(( OLDEST_IDX++ ))
739                 fi
740                 : $(( search_idx++ ))
741                 if [ ${search_idx} -gt ${TOTAL_CNT} ] ; then
742                         if [ ${CURRENT_CNT} -ge ${BUILD_NBUILDS} ] ; then
743                                 search_idx=${OLDEST_IDX}
744                                 sleep 1
745                         else
746                                 break
747                         fi
748                 fi
749         done
750 }
751
752 build_targets() {
753         for t in "$@" ; do
754                 # If a LIST_xxx var exists, use it.  But avoid variable
755                 # expansion in the eval when a board name contains certain
756                 # characters that the shell interprets.
757                 case ${t} in
758                         *[-+=]*) list= ;;
759                         *)       list=$(eval echo '${LIST_'$t'}') ;;
760                 esac
761                 if [ -n "${list}" ] ; then
762                         build_targets ${list}
763                 else
764                         : $((TOTAL_CNT += 1))
765                         : $((CURRENT_CNT += 1))
766                         rm -f "${donep}${TOTAL_CNT}"
767                         rm -f "${skipp}${TOTAL_CNT}"
768                         if [ "$CONTINUE" = 'y' -a -e ${LOG_DIR}/$t.MAKELOG ] ; then
769                                 : $((SKIP_CNT += 1))
770                                 touch "${donep}${TOTAL_CNT}"
771                         elif [ "$REBUILD_ERRORS" = 'y' -a ! -e ${LOG_DIR}/$t.ERR ] ; then
772                                 : $((SKIP_CNT += 1))
773                                 touch "${donep}${TOTAL_CNT}"
774                         else
775                                 if [ $BUILD_MANY == 1 ] ; then
776                                         build_target ${t} ${TOTAL_CNT} &
777                                 else
778                                         CUR_TGT="${t}"
779                                         build_target ${t} ${TOTAL_CNT}
780                                         CUR_TGT=''
781                                 fi
782                         fi
783                 fi
784
785                 # We maintain a running count of all the builds we have done.
786                 # Each finished build will have a file called ${donep}${n},
787                 # where n is the index of the build. Each build
788                 # we've already noted as finished will have ${skipp}${n}.
789                 # We track the current index via TOTAL_CNT, and the oldest
790                 # index. When we exceed the maximum number of parallel builds,
791                 # We look from oldest to current for builds that have completed,
792                 # and update the current count and oldest index as appropriate.
793                 # If we've gone through the entire list, wait a second, and
794                 # reprocess the entire list until we find a build that has
795                 # completed
796                 if [ ${CURRENT_CNT} -ge ${BUILD_NBUILDS} ] ; then
797                         manage_builds
798                 fi
799         done
800 }
801
802 #-----------------------------------------------------------------------
803
804 kill_children() {
805         local OS=$(uname -s)
806         local children=""
807         case "${OS}" in
808                 "Darwin")
809                         # Mac OS X is known to have BSD style ps
810                         local pgid=$(ps -p $$ -o pgid | sed -e "/PGID/d")
811                         children=$(ps -g $pgid -o pid | sed -e "/PID\|$$\|$pgid/d")
812                         ;;
813                 *)
814                         # everything else tries the GNU style
815                         local pgid=$(ps -p $$ --no-headers -o "%r" | tr -d ' ')
816                         children=$(pgrep -g $pgid | sed -e "/$$\|$pgid/d")
817                         ;;
818         esac
819
820         kill $children 2> /dev/null
821         wait $children 2> /dev/null
822
823         exit
824 }
825
826 print_stats() {
827         if [ "$ONLY_LIST" == 'y' ] ; then return ; fi
828
829         # Only count boards that completed
830         : $((TOTAL_CNT = `find ${skipp}* 2> /dev/null | wc -l`))
831
832         rm -f ${donep}* ${skipp}*
833
834         if [ $BUILD_MANY == 1 ] && [ -e "${OUTPUT_PREFIX}/ERR" ] ; then
835                 ERR_LIST=`grep -riwl error ${OUTPUT_PREFIX}/ERR/`
836                 ERR_LIST=`for f in $ERR_LIST ; do echo -n " $(basename $f)" ; done`
837                 ERR_CNT=`echo $ERR_LIST | wc -w | awk '{print $1}'`
838                 WRN_LIST=`grep -riwL error ${OUTPUT_PREFIX}/ERR/`
839                 WRN_LIST=`for f in $WRN_LIST ; do echo -n " $(basename $f)" ; done`
840                 WRN_CNT=`echo $WRN_LIST | wc -w | awk '{print $1}'`
841         else
842                 # Remove the logs for any board that was interrupted
843                 rm -f ${LOG_DIR}/${CUR_TGT}.MAKELOG ${LOG_DIR}/${CUR_TGT}.ERR
844         fi
845
846         : $((TOTAL_CNT -= ${SKIP_CNT}))
847         echo ""
848         echo "--------------------- SUMMARY ----------------------------"
849         if [ "$CONTINUE" = 'y' -o "$REBUILD_ERRORS" = 'y' ] ; then
850                 echo "Boards skipped: ${SKIP_CNT}"
851         fi
852         echo "Boards compiled: ${TOTAL_CNT}"
853         if [ ${ERR_CNT} -gt 0 ] ; then
854                 echo "Boards with errors: ${ERR_CNT} (${ERR_LIST} )"
855         fi
856         if [ ${WRN_CNT} -gt 0 ] ; then
857                 echo "Boards with warnings but no errors: ${WRN_CNT} (${WRN_LIST} )"
858         fi
859         echo "----------------------------------------------------------"
860
861         if [ $BUILD_MANY == 1 ] ; then
862                 kill_children
863         fi
864
865         exit $RC
866 }
867
868 #-----------------------------------------------------------------------
869
870 # Build target groups selected by options, plus any command line args
871 set -- ${SELECTED} "$@"
872 # run PowerPC by default
873 [ $# = 0 ] && set -- powerpc
874 build_targets "$@"
875 wait