-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-all
executable file
·374 lines (329 loc) · 8.48 KB
/
build-all
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
ld-all [ --? ]
# [ --all "..." ]
# [ --check "..." ]
# [ --configure "..." ]
# [ --environment "..." ]
# [ --help ]
# [ --logdirectory dir ]
# [ --on "[user@]host[:dir][,envfile] ..." ]
# [ --source "dir..." ]
# [ --userhosts "file(s)" ]
# [ --version ]
# package(s)
#
# 可选用的初始化文件:
# $HOME/.build/directories list of source directories
# $HOME/.build/userhosts list of [user@]host[:dir][,envfile]
IFS='
'
PATH=/usr/local/bin:/bin:/usr/bin
export PATH
UMASK=002
umask $UMASK
build_one(){
#语法:
# build_one [user@]host[:build-directory][,envfile]
arg="`eval echo $1`"
userhost="`echo $arg | sed -e 's/:.*$//'`"
user="`echo $userhost | sed -e s'/@.*$//'`"
test "$user" = "$userhost" && user=$USER
host="`echo $userhost | sed -e s'/^[^@]@//'`"
envfile="`echo $arg | sed -e 's/^[^,]*,//'`"
test "$envfile" = "$arg" && envfile=/dev/null
builddir="`echo $arg | sed -e s'/^[^,]*,//'`"
test "$builddir" = "$arg" && builddir=/tmp
parbase=`basename $PARFILE`
#NB:如果这些模式被更换过,则更新find_package()
package="`echo $parbase | \
sed -e 's/[.]jar$//' \
-e 's/[.]tar[.]bz2$//' \
-e 's/[.]tar[.]gz$//' \
-e 's/[.]tar[.]Z$//' \
-e 's/[.]tar$//' \
-e 's/[.]taz$//' \
-e 's/[.]zip$//'`"
#如果我们在远程主机上看不到包文件,则复制过去
echo $SSH $SSHFLAGS $userhost "test -f $PARFILE"
if $SSH $SSHFLAGS $userhost "test -f $PARFILE"
then
parbaselocal=$PARFLE
else
parbaselocal=$parbase
echo $SCP $PARFILE $userhost:$builddir
$SCP $PARFILE $userhost:$builddir
fi
#在远程主机上解开存档文件、构建,以及后台执行方式检查它
sleep 1 #为了保证唯一的日志文件名
now="`date $DATEFLAGS`"
logfile="$package.$host.$now.log"
nice $SSH $SSHFLAGS $userhost "
echo '==================================================' ;
test -f $BUILDBEGIN && . $BUILDBEGIN || \
test -f $BUILDBEGIN && source $BUILDBEGIN || \
true ;
echo 'Package: $package' ;
echo 'Archive: $PARFILE' ;
echo 'Date: $now' ;
echo 'Local user: $USER' ;
echo 'Local host: `hostname`' ;
echo 'Local log directory: $LOGDIR' ;
echo 'Local log file: $logfile' ;
echo 'Remote user: $user' ;
echo 'Remote host: $host' ;
echo 'Remote directory: $builddir' ;
printf 'Remote date: ' ;
date $DATEFLAGS ;
printf 'Remote uname: ' ;
uname -a || true ;
printf 'Remote gcc version: ' ;
gcc --version | head -n 1 || echo ;
printf 'Remote g++ version: ' ;
g++ --version | head -n 1 || echo ;
echo 'Configure environment: `$STRIPCOMMENTS $envfile | \
$JOINLINES`' ;
echo 'Extra environment: $EXTRAENVIRONMENT' ;
echo 'Configure directory: $CONFIGUREDIR' ;
echo 'Configure flags: $CONFIGUREFLAGS' ;
echo 'Make all targets: $ALLTARGETS' ;
echo 'Make check targets: $CHECKTARGETS' ;
echo 'Disk free report for $builddir/$package:' ;
df $builddir | $INDENT ;
echo 'Environment:' ;
env | env LC_ALL=C sort | $INDENT ;
echo '==============================================' ;
umask $UMASK ;
cd $builddir || exit 1 ;
/bin/rm -rf $builddir/$package ;
$PAR $parbaselocal ;
test "$parbase" = "$parbaselocal" && /bin/rm -f $parbase ;
cd $package/$CONFIGUREDIR || exit 1 ;
test -f configure && \
chmod a+x configure && \
env `$STRIPCOMMENTS $envfile | $JOINLINES` \
$EXTRAENVIRONMENT \
nice time ./configure $CONFIGUREFLAGS ;
nice time make $ALLTARGETS && nice time make $CHECKTARGETS ;
echo '===============================================' ;
echo 'Disk free report for $builddir/$package:' ;
df $builddir | $INDENT ;
printf 'Remote date: ' ;
date $DATEFLAGS ;
cd ;
test -f $BUILDEND && . $BUILDEND || \
test -f $BUILDEND && source $BUILDEND || \
true;
echo '===============================================' ;
" < /dev/null > "$LOGDIR/$logfile" 2>&1 &
}
error(){
echo "$@" 1>&2
usage_and_exit 1
}
find_file(){
#语法:
# find_file file program-and-args
#如果找到,返回0,如果找不到返回1
if test -r "$1"
then
PAR="$2"
PARFILE="$1"
return 0
else
return 1
fi
}
find_package(){
#语法:
# find_package package-x.y.z
base=`echo "$1" | sed -e 's/[-_][.]*[0-9].*$//'`
PAR=
PARFILE=
for srcdir in $SRCDIRS
do
test "$srcdir" = "." && srcdir="`pwd`"
for subdir in "$base" ""
do
#如果此列表有改变,则更新build_one()内的包设置
find_file $srcdir/$subdir/$1.tar.gz "tar xfz" && return
find_file $srcdir/$subdir/$1.tar.Z "tar xfz" && return
find_file $srcdir/$subdir/$1.tar "tar xf" && return
find_file $srcdir/$subdir/$1.tar.bz2 "tar xfj" && return
find_file $srcdir/$subdir/$1.tar.tgz "tar xfz" && return
find_file $srcdir/$subdir/$1.tar.zip "unzip -q" && return
find_file $srcdir/$subdir/$1.jar "jar xf" && return
done
done
}
set_userhosts(){
#语法:
# set_userhosts file(s)
for u in "$@"
do
if test -r "$u"
then
ALTUSERHOSTS="$ALTUSERHOSTS $u"
elif test -r "$BUILDHOME/$u"
then
ALTUSERHOSTS="$ALTUSERHOSTS $BUn
PAR="$2"
PARFILE="$1"
return 0
else
re fi
done
}
usage(){
cat <<EOF
Usage:
$PROGRAM [ --? ]
[ --all "..." ]
[ --check "..." ]
[ --configure "..." ]
[ --environment "..." ]
[ --help ]
[ --logdirectory dir ]
[ --on "[user@]host[:dir][,envfile] ..." ]
[ --source "dir ..." ]
[ --userhosts "file(s)" ]
[ --version ]
package(s)
EOF
}
usage_and_exit(){
usage
exit $1
}
version(){
echo "$PROGRAM version $VERSION"
}
warning(){
echo "$@" 1>&2
EXITCODE=`expr $EXITCODE + 1 `
}
ALLTARGETS=
altlogdir=
altsrcdirs=
ALTUSERHOSTS=
BUILDBEGIN=./.build/begin
BUILDEND=./.build/end
BUILDHOME=$HOME/.build
CHECKTARGETS=check
CONFIGUREDIR=.
CONFIGUREFLAGS=
DATEFLAGS="+%Y.%m.%d.%H.%M.%S"
EXITCODE=0
EXTRAENVIRONMENT=
INDENT="awk '{ print \"\t\t\t\" \$0 }'"
JOINLINES="tr '\n' '\040'"
LOGDIR=
PROGRAM=`basename $0`
SCP=scp
SSH=ssh
SSHFLAGS=${SSHFLAGS--x}
STRIPCOMMENTS='sed -e s/#.*$//'
userhosts=
VERSION=1.0
#默认的初始化文件
defaultdirectories=$BUILDHOME/directories
defaultuserhosts=$BUILDHOME/userhosts
#要寻找包分发的位置列表,如果用户未提供个人化列表,则使用默认列表:
SRCDIRS="`$STRIPCOMMENTS $defaultdirectories 2> /dev/null`"
test -z "$SRCDIRS" && \
SRCDIRS=".
/usr/local/src
/usr/local/gnu/src
$HOME/src
$HOME/gnu/src
/tmp
/usr/tmp
/var/tmp"
while test $# -gt 0
do
case $1 in
--all | --al | --a | -all | -al | -a )
shift
ALLTARGETS="$1"
--cd | -cd )
shift
CONFIGUREDIR="$1"
--check | --chec | --che | --ch | -check | -chec | -che | -ch )
shift
CHECKTARGETS="$1"
--configure | --conf | --co | -configure | -conf | -co )
shift
CONFIGUREFLAGS="$1"
--environment | --environ | -- envir | --e | -environment | \
-environ | -envir | -e )
shift
EXTRAENVIRONMENT="$1"
--help | --h | '--?' | -help | -h | '-?' )
usage_and_exit 0
--logdirectory | --log | --l | -logdirectory | -log | -l )
shift
altlogdir="$1"
--on | --o | -on | -o )
shift
userhosts="$userhosts $1"
--source | --s | -source | -s )
shift
altsrcdirs="$altsrcdirs $1"
--userhosts | --u | -userhosts | -u )
shift
set_userhosts $1
--version | --v | -version | -v )
version
exit 0
-* )
error "Unrecognized option: $1"
* )
break
esac
shift
done
#寻找适当的邮件客户端程序
for MAIL in /bin/mailx /usr/bin/mailx /usr/sbin/mailx /usr/ucb/mailx \
/bin/mail /usr/bin/mail
do
test -x $MAIL && break
done
test -x $MAIL || error "Cannot find mail client"
#命令行来源目录优先于默认值
SRCDIRS="$altsrcdirs $SRCDIRS"
if test -n "$userhosts"
then
test -n "$ALTUSERHOSTS" &&
userhosts="$userhosts `$STRIPCOMMENTS $ALTUSERHOSTS 2> /dev/null`"
else
test -z "$ALTUSERHOSTS" && ALTUSERHOSTS="$defaultuserhosts"
userhosts="`$STRIPCOMMENTS $ALTUSERHOSTS 2> /dev/null`"
fi
#检查是否要执行某些操作
test -z "$userhosts" && usage_and_exit 1
for p in "$@"
do
find_package "$p"
if test -z "$PARFILE"
then
warning "Cannot find package file $p"
fi
LOGDIR="$altlogdir"
if test -z "$LOGDIR" -o ! -d "$LOGDIR" -o ! -w "$LOGDIR"
then
for LOGDIR in "`dirname $PARFILE`/logs/$p" \
$BUILDHOME/logs/$p /usr/tmp /var/tmp /tmp
do
test -d "$LOGDIR" || mkdir -p "LOGDIR" 2> /dev/null
test -d "$LOGDIR" -a -w "$LOGDIR" && break
done
fi
msg="Check build logs for $p in `hostname`:$LOGDIR"
echo "$msg"
echo "$msg" | $MAIL -s "$msg" $USER 2> /dev/null
for u in $userhosts
do
build_one $u
done
done
#将退出状态限制为一般unix实际做法
test $EXITCODE -gt 125 && EXITCODE=125
exit $EXITCODE