Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / releasetools / build
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         build
5 # Description:  Perform building in parallel
6 # Author:       Andrew@DeFaria.com
7 # Created:      Mon Jun  2 11:19:46 PDT 2003
8 # Language:     bash
9 # Modification: 6/2/2003 AD: Added code to handle new cards (if present)
10 #               6/11/2003 AD: Added -nomonitor option to pop up monitor windows 
11 #               (default). Added -sequential option to build sequentially 
12 #               instead of parallel per card. Default off. Added -wait option
13 #               to wait for all jobs to complete. Default off.
14 #               6/17/2003 AD: Changed 2310_onu to onu2310 as the card directory
15 #               has changed.
16 #
17 # (c) Copyright 2001-2003, Andrew@DeFaria.com, all rights reserved.
18 #
19 ################################################################################
20 # Set me to command name
21 me=$(basename $0)
22
23 # Source /etc/site_parms
24 if [ -f /etc/site_parms ]; then
25   . /etc/site_parms
26 else
27   echo "$me: WARNING: /etc/site_parms does not exist!"
28 fi
29
30 # Set adm_base
31 adm_base="$SITE_TOOLS_PATH/adm"
32
33 # Set adm_fpath
34 adm_fpath=${adm_fpath:-$adm_base/functions}
35
36 # Source functions
37 . $adm_fpath/common
38
39 # Commands used
40 make="$SITE_TOOLS_PATH/bin/smake"
41
42 # Get cards definition
43 . $SITE_TOOLS_PATH/adm/etc/cards
44
45 # Positioning for geometry parameter
46 declare -i xpos=10
47 declare -i ypos=10
48 declare -i yinc=175
49 declare -i index=1
50
51 # Because of a problem with the make system we must stagger the parallel
52 # makes to avoid collisions with .d dependency files. Here we set the
53 # sleep interval between builds.
54 declare -i sleep_interval=${sleep_interval:-35}
55
56 function usage {
57   if [ ! -z "$1" ]; then
58     error "$1\n"
59   fi
60   display "Usage: $me: [-cle|an | -clo|bber] [-n|omonitor] [-w|ait] [-s|equential]"
61   display "\t      [-q|uiet] [-t|ee <filename>] [-v|erbose] [-d|ebug] [-u|sage]"
62   display "\t      [card...]"
63   display
64   display "Where:"
65   display "\t-clean:\t\tPerform make clean before building"
66   display "\t-clobber:\tPerform make clobber before building"
67   display "\t-nomonitor:\tDo not put up monitor windows"
68   display "\t-wait:\t\tWait for all builds to complete before"
69   display "\t\t\treturning to the shell"
70   display "\t-sequential:\tPerform builds sequentially (not parallel by card)"
71   display "\t-quiet:\t\tBe quiet"
72   display "\t-tee:\t\tAppend build.log to <filename>"
73   display "\t-verbose:\tTurn on verbose mode"
74   display "\t-debug:\t\tTurn on debug mode"
75   display "\t-usage:\t\tDisplay usage"
76   display "\t<card>\t\tBuild only for these individual card(s)"
77   exit 1
78 } # usage
79
80 # Get parameters
81 clean="no"
82 clobber="no"
83 monitor="yes"
84 wait="no"
85 sequential="no"
86 quiet="no"
87 teefile=""
88 cards_passed_in=""
89
90 while [ $# -ge 1 ]; do
91   case "$1" in
92     -u|-usage)
93       usage
94     ;;
95
96     -cle|-clean)
97       clean="yes"
98     ;;
99
100     -clo|-clobber)
101       clobber="yes"
102     ;;
103
104     -n|-nomonitor)
105       monitor="no"
106     ;;
107
108     -s|-sequential)
109       sequential="yes"
110     ;;
111
112     -w|-wait)
113       wait="yes"
114     ;;
115
116     -q|-quiet)
117       quiet="yes"
118     ;;
119
120     -v|-verbose)
121       verbose="yes"
122     ;;
123
124     -d|-debug)
125       debug="yes"
126     ;;
127
128     -t|-tee)
129       if [ $# -le 1 ]; then
130         usage "Tee filename is mmissing"
131       else
132         shift
133         teefile="$1"
134       fi
135     ;;
136
137     *)
138       cards_passed_in="$cards_passed_in $(echo $1 | tr [:upper:] [:lower:])"
139       ;;
140   esac
141   shift
142 done
143
144 # Check to see if they specified both clean AND clobber!
145 if [ $clean = "yes" -a $clobber = "yes" ]; then
146   usage "Cannot specify both clean and clobber!"
147 fi
148
149 # If cards were passed in, make sure that they are all on the list
150 # and replace the standard definition of cards:
151 if [ ! -z "$cards_passed_in" ]; then
152   for card_passed_in in $cards_passed_in; do
153     match="no"
154
155     for card in $cards; do
156       if [[ $card_passed_in = $card ]]; then
157         # If we match then set flag and break out of this for loop
158         match="yes"
159         break
160       fi
161     done
162
163     # If we've fallen through with no match then we error out
164     if [[ $match = "no" ]]; then
165       error "The card specified, $card_passed_in, is an unknown card!" 10
166     fi
167   done
168
169   # Now replace $cards    
170   cards="$cards_passed_in"
171 fi
172
173 # Check if we are in a view and if we are in the build area
174 view=$(cleartool pwv -short)
175
176 if [[ $view = "** NONE **" ]]; then
177   error "Not in a view!" 1
178 fi
179
180 # Check to see if we are in the proper build directory
181 current_dir=$(basename $(pwd))
182
183 if [ "$current_dir" != "build" ]; then
184   error "Not in $view's build directory!" 2
185 fi
186
187 # Clean if requested
188 if [ $clean = "yes" ]; then
189   verbose "Cleaning up..."
190   if [ $quiet = "yes" ]; then
191     if [ -z "$teefile" ]; then
192       $make clean > /dev/null 2>&1
193     else
194       $make clean >> $teefile
195     fi
196   else
197     if [ -z "$teefile" ]; then
198       $make clean
199     else
200       $make clean 2>&1 | tee -a $teefile
201     fi
202   fi
203 fi
204       
205 # Clobber if requested
206 if [ $clobber = "yes" ]; then
207   verbose "Clobbering old build..."
208   if [ $quiet = "yes" ]; then
209     if [ -z "$teefile" ]; then
210       $make clobber > /dev/null 2>&1
211     else
212       $make clobber >> $teefile
213     fi
214   else
215     if [ -z "$teefile" ]; then
216       $make clobber
217     else
218       $make clobber 2>&1 | tee -a $teefile
219     fi
220   fi
221 fi
222       
223 declare -i build_status=0
224 declare -i overall_status=0
225
226 # Now perform a build for each card
227 for card in $cards; do
228   # Check to see if card's build directory is present
229   if [ "$card" = "scc" ]; then
230     card_dirname="sc"
231   else
232     card_dirname=$card
233   fi
234
235   if [ -d "$card_dirname" ]; then
236     if [ "$quiet" = "no" -a -z "$teefile" ]; then
237       echo -n "Starting build job for $card..."
238     fi
239
240     # Clear out build log file
241     rm -f $card.build.log
242
243     # Start make
244     if [ "$sequential" = "yes" ]; then
245       if [ ! -z "$teefile" ]; then
246         $make $card.sf 2>&1 | tee -a $teefile > $card.build.log
247         # Note: Need to grab the status of make from the PIPESTATUS
248         #       array, otherwise the return status of a pipe is the
249         #       same as the status of the last command in the pipe,
250         #       in this case the tee! For a pipe, ${PIPESTATUS[0]}
251         #       is the return status of the first command in the pipe,
252         #       ${PIPESTATUS[1]} is the status of the second command
253         #       and so on.
254         build_status=${PIPESTATUS[0]}
255         let overall_status=overall_status+build_status
256       else
257         $make $card.sf > $card.build.log 2>&1
258         build_status=$?
259         let overall_status=overall_status+build_status
260       fi
261     else 
262       $make $card.sf > $card.build.log 2>&1 &
263
264       if [ "$monitor" = "yes" ]; then
265         # Put up a monitor window
266         rxvt \
267           -title "Monitoring build of $card - type Control-C to close window" \
268           -geometry 80x10+$xpos+$ypos \
269           -e tail -f $card.build.log &
270
271         # Adjust positions and index
272         let ypos=ypos+yinc
273         let index=index+1
274       fi
275
276       # Sleep to avoid .d dependency file collisions
277       if [ "$quiet" = "no" ]; then
278         echo " Sleeping for $sleep_interval seconds..."
279       fi
280       sleep $sleep_interval
281
282       # This disown is required or else when this script exits it will send
283       # SIGHUPs to all jobs stated from this script.
284       disown -h
285     fi
286   fi
287 done
288
289 if [ "$wait" = "yes" ]; then
290   wait
291   overall_status=$?
292 fi
293
294 exit $overall_status