forked from cubicdaiya/ngx_dynamic_upstream
-
Notifications
You must be signed in to change notification settings - Fork 11
/
build.sh
executable file
·512 lines (428 loc) · 11.8 KB
/
build.sh
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
#!/bin/bash
if [ "$1" == "" ]; then
echo "build.sh <clean/clean_all> <download/download_all> <build>"
exit 0
fi
download=0
download_only=0
download_all=0
build_deps=0
clean_all=0
compile=0
build_only=0
build_debug=0
make_clean=0
DIR="$(pwd)"
VERSION="1.17.6"
PCRE_VERSION="8.40"
ZLIB_VERSION="1.2.11"
SUFFIX=""
if [ "$BUILD_DIR" == "" ]; then
BUILD_DIR="$DIR/build"
fi
if [ "$INSTALL_DIR" == "" ]; then
INSTALL_DIR="$DIR/install"
fi
if [ "$ERR_LOG" == "" ]; then
ERR_LOG=/dev/null
else
ERR_LOG=$BUILD_DIR/$ERR_LOG
fi
if [ "$BUILD_LOG" == "" ]; then
BUILD_LOG=/dev/null
else
BUILD_LOG=$BUILD_DIR/$BUILD_LOG
fi
[ -e "$BUILD_DIR" ] || mkdir -p $BUILD_DIR
export JIT_PREFIX="$BUILD_DIR/deps/luajit"
export ZLIB_PREFIX="$BUILD_DIR/deps/zlib"
export PCRE_PREFIX="$BUILD_DIR/deps/pcre"
export LUAJIT_INC="$JIT_PREFIX/usr/local/include/luajit-2.1"
export LUAJIT_LIB="$JIT_PREFIX/usr/local/lib"
export LUAJIT_BIN="$JIT_PREFIX/usr/local/bin/luajit-2.1.0-beta3"
export LD_LIBRARY_PATH="$PCRE_PREFIX/lib:$LUAJIT_LIB:$ZLIB_PREFIX/lib"
export DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH
export PATH=/usr/local/bin:/bin:/usr/bin:$PATH
export LUA_PATH="$JIT_PREFIX/usr/local/share/luajit-2.1.0-beta3/?.lua;;"
ADDITIONAL_INCLUDES="-I$PCRE_PREFIX/include -I$ZLIB_PREFIX/include"
ADDITIONAL_LIBS="-L$PCRE_PREFIX/lib -L$ZLIB_PREFIX/lib"
function clean() {
rm -rf install 2>>$ERR_LOG
if [ $clean_all -eq 1 ]; then
rm -rf $BUILD_DIR 2>>$ERR_LOG
else
rm -rf $(ls -1d $BUILD_DIR/* 2>>$ERR_LOG | grep -v deps) 2>>$ERR_LOG
fi
if [ $download_all -eq 1 ]; then
rm -rf download 2>>$ERR_LOG
fi
}
doclean=0
dobuild=0
for i in "$@"
do
if [ "$i" == "download" ]; then
download=1
fi
if [ "$i" == "download_all" ]; then
download=1
download_all=1
fi
if [ "$i" == "clean_all" ]; then
clean_all=1
doclean=1
fi
if [ "$i" == "build" ]; then
dobuild=1
fi
if [ "$i" == "build_only" ]; then
dobuild=1
build_only=1
fi
if [ "$i" == "build_debug" ]; then
dobuild=1
build_debug=1
fi
if [ "$i" == "build_debug_clean" ]; then
dobuild=1
build_debug=1
make_clean=1
fi
if [ "$i" == "clean" ]; then
doclean=1
fi
if [ "$i" == "compile" ]; then
compile=1
fi
done
if [ $doclean -eq 1 ]; then
clean
fi
if [ $download -eq 1 ] && [ $dobuild -eq 0 ]; then
download_only=1
fi
current_os=`uname`
if [ "$current_os" = "Linux" ]; then
platform="linux"
arch=`uname -p`
shared="so"
if [ -e /etc/redhat-release ]; then
vendor='redhat'
ver=`cat /etc/redhat-release | awk 'match($0, /[0-9]+/){print substr($0, RSTART, RLENGTH)}'`
fi
if [ -e /etc/centos-release ]; then
vendor='centos'
ver=`cat /etc/centos-release | awk 'match($0, /[0-9]+/){print substr($0, RSTART, RLENGTH)}'`
fi
if [ $vendor = "redhat" ] || [ $vendor = "centos" ]; then
os_release=$ver".0"
if [ $ver -lt 7 ]; then
GDB_VERSION="7.12"
else
GDB_VERSION="8.2"
fi
if [ "$arch" != "x86_64" ]; then
arch='i686'
fi
DISTR_NAME=$vendor-$platform-$os_release-$arch
else
vendor=$(uname -r)
DISTR_NAME=$vendor-$platform-$arch
fi
OPENSSL_FLAGS="linux-x86_64"
fi
if [ "$current_os" = "Darwin" ]; then
platform="macos"
arch=`uname -m`
vendor="apple"
shared="dylib"
OPENSSL_FLAGS="darwin64-x86_64-cc enable-ec_nistp_64_gcc_128 no-ssl2 no-ssl3"
CFLAGS="-arch x86_64"
DISTR_NAME=$vendor-$platform-$arch
fi
echo "name: "$DISTR_NAME
case $platform in
linux)
# platform has been recognized
;;
macos)
# platform has been recognized
;;
*)
echo "I do not recognize the platform '$platform'." | tee -a $BUILD_LOG
exit 1;;
esac
if [ -z "$BUILD_VERSION" ]; then
BUILD_VERSION="develop"
fi
function build_pcre() {
echo "Build PCRE" | tee -a $BUILD_LOG
cd pcre-$PCRE_VERSION
./configure --prefix="$PCRE_PREFIX" --libdir="$PCRE_PREFIX/lib" --enable-utf --enable-jit >> $BUILD_LOG 2>>$ERR_LOG
make -j 8 >> $BUILD_LOG 2>>$ERR_LOG
r=$?
if [ $r -ne 0 ]; then
exit $r
fi
make install >> $BUILD_LOG 2>>$ERR_LOG
cd ..
}
function build_zlib() {
echo "Build ZLIB" | tee -a $BUILD_LOG
cd zlib-$ZLIB_VERSION
./configure --prefix="$ZLIB_PREFIX" --libdir="$ZLIB_PREFIX/lib" >> $BUILD_LOG 2>>$ERR_LOG
make -j 8 >> $BUILD_LOG 2>>$ERR_LOG
r=$?
if [ $r -ne 0 ]; then
exit $r
fi
make install >> $BUILD_LOG 2>>$ERR_LOG
cd ..
}
function build_luajit() {
echo "Build luajit" | tee -a $BUILD_LOG
cd luajit2
make >> $BUILD_LOG 2>>$ERR_LOG
r=$?
if [ $r -ne 0 ]; then
exit $r
fi
DESTDIR="$JIT_PREFIX" make install >> $BUILD_LOG 2>>$ERR_LOG
cd ..
}
function build_release() {
olddir=$PWD
cd nginx-$VERSION
make clean >> $BUILD_LOG 2>>$ERR_LOG
echo "Configuring release nginx-$VERSION" | tee -a $BUILD_LOG
./configure --prefix="$INSTALL_DIR/nginx-$VERSION" \
$EMBEDDED_OPTS \
--with-poll_module \
--with-threads \
--with-pcre-jit \
--with-stream \
--with-cc-opt="$ADDITIONAL_INCLUDES" \
--with-ld-opt="$ADDITIONAL_LIBS" \
--add-module=../ngx_devel_kit \
--add-module=../lua-nginx-module \
--add-module=../stream-lua-nginx-module \
--add-module=../echo-nginx-module \
--add-module=../.. >> $BUILD_LOG 2>>$ERR_LOG
r=$?
if [ $r -ne 0 ]; then
exit $r
fi
echo "Build release nginx-$VERSION" | tee -a $BUILD_LOG
make -j 8 >> $BUILD_LOG 2>>$ERR_LOG
r=$?
if [ $r -ne 0 ]; then
exit $r
fi
make install >> $BUILD_LOG 2>>$ERR_LOG
cd $INSTALL_DIR/nginx-$VERSION
cd $olddir
}
function gitclone() {
LD_LIBRARY_PATH="" git clone $1 >> $BUILD_LOG 2> /tmp/err
if [ $? -ne 0 ]; then
cat /tmp/err
exit 1
fi
}
function gitcheckout() {
git checkout $1 >> $BUILD_LOG 2> /tmp/err
if [ $? -ne 0 ]; then
cat /tmp/err
exit 1
fi
}
function download_module() {
if [ $download -eq 1 ] || [ ! -e $3.tar.gz ]; then
if [ $download_all -eq 1 ] || [ ! -e $3.tar.gz ]; then
echo "Download $1/$2/$3.git from=$4" | tee -a $BUILD_LOG
gitclone $1/$2/$3.git
echo "$1/$2/$3.git" > $3.log
echo >> $3.log
cd $3
gitcheckout $4
echo $4" : "$(git log -1 --oneline | awk '{print $1}') >> ../$3.log
echo >> ../$3.log
git log -1 | grep -E "(^[Cc]ommit)|(^[Aa]uthor)|(^[Dd]ate)" >> ../$3.log
cd ..
tar zcf $3.tar.gz $3
rm -rf $3
else
echo "Get $3" | tee -a $BUILD_LOG
fi
else
echo "Get $3" | tee -a $BUILD_LOG
fi
}
function download_dep() {
if [ $download -eq 1 ] || [ ! -e $2-$3.tar.gz ]; then
if [ $download_all -eq 1 ] || [ ! -e $2-$3.tar.gz ]; then
echo "Download $2-$3.$4" | tee -a $BUILD_LOG
LD_LIBRARY_PATH="" curl -s -L -o $2-$3.tar.gz $1/$2-$3.$4
echo "$1/$2-$3.$4" > $2.log
else
echo "Get $2-$3.tar.gz" | tee -a $BUILD_LOG
fi
else
echo "Get $2-$3.tar.gz" | tee -a $BUILD_LOG
fi
}
function extract_downloads() {
cd download
for d in $(ls -1 *.tar.gz)
do
echo "Extracting $d" | tee -a $BUILD_LOG
tar zxf $d -C $BUILD_DIR --keep-old-files 2>>$ERR_LOG
done
cd ..
}
function download() {
mkdir -p $BUILD_DIR 2>>$ERR_LOG
mkdir $BUILD_DIR/debug 2>>$ERR_LOG
mkdir $BUILD_DIR/deps 2>>$ERR_LOG
mkdir download 2>>$ERR_LOG
mkdir download/lua_modules 2>>$ERR_LOG
cd download
download_dep http://nginx.org/download nginx $VERSION tar.gz
download_dep http://ftp.cs.stanford.edu/pub/exim/pcre pcre $PCRE_VERSION tar.gz
download_dep http://zlib.net zlib $ZLIB_VERSION tar.gz
download_module https://github.com simpl ngx_devel_kit master
download_module https://github.com openresty lua-nginx-module v0.10.15
download_module https://github.com openresty stream-lua-nginx-module v0.0.7
download_module https://github.com openresty echo-nginx-module v0.61
download_module https://github.com openresty luajit2 v2.1-agentzh
cd ..
}
function install_file() {
echo "Install $1" | tee -a $BUILD_LOG
if [ ! -e "$INSTALL_DIR/nginx-$VERSION/$2" ]; then
mkdir -p "$INSTALL_DIR/nginx-$VERSION/$2"
fi
if [ "$4" == "" ]; then
if [ "$3" == "" ]; then
cp -r $1 "$INSTALL_DIR/nginx-$VERSION/$2/"
else
cp -r $1 "$INSTALL_DIR/nginx-$VERSION/$2/$3"
fi
else
echo $4 > "$INSTALL_DIR/nginx-$VERSION/$2/$3"
fi
}
function install_gzip() {
echo "Install $1" | tee -a $BUILD_LOG
if [ ! -e "$INSTALL_DIR/nginx-$VERSION/$2" ]; then
mkdir -p "$INSTALL_DIR/nginx-$VERSION/$2"
fi
if [ "$4" == "" ]; then
if [ "$3" == "" ]; then
tar zxf $1 -C "$INSTALL_DIR/nginx-$VERSION/$2/"
else
tar zxf $1 -C "$INSTALL_DIR/nginx-$VERSION/$2/$3"
fi
else
echo $4 > "$INSTALL_DIR/nginx-$VERSION/$2/$3"
fi
}
function install_files() {
for f in $(ls $1)
do
install_file $f $2
done
}
function build() {
cd $BUILD_DIR
if [ $build_deps -eq 1 ] || [ ! -e deps/luajit ]; then
build_luajit
fi
if [ $build_deps -eq 1 ] || [ ! -e deps/zlib ]; then
build_zlib
fi
if [ $build_deps -eq 1 ] || [ ! -e deps/pcre ]; then
build_pcre
fi
build_release
install_file "$JIT_PREFIX/usr/local/lib/*.$shared*" lib
install_files "$ZLIB_PREFIX/lib/libz.$shared*" lib
install_file "$PCRE_PREFIX/bin" .
install_files "$PCRE_PREFIX/lib/libpcre.$shared*" lib
install_files "$PCRE_PREFIX/lib/libpcreposix.$shared*" lib
chmod 755 $(find $INSTALL_DIR/nginx-$VERSION/lib -name "*.$shared*")
chmod 755 $INSTALL_DIR/nginx-$VERSION/bin/*
cd $DIR
}
if [ $build_only -eq 0 ] && [ $build_debug -eq 0 ]; then
clean
fi
download
if [ $download_only -eq 0 ]; then
if [ $build_only -eq 0 ] && [ $build_debug -eq 0 ]; then
extract_downloads
fi
build
fi
function install_resty_module() {
if [ $7 -eq 1 ] || [ ! -e $3.tar.gz ]; then
if [ $8 -eq 1 ] || [ ! -e $3.tar.gz ]; then
echo "Download $1/$2/$3.git from=$6" | tee -a $BUILD_LOG
gitclone $1/$2/$3.git
echo "$1/$2/$3.git" > $3.log
echo >> $3.log
cd $3
gitcheckout $6
echo $6" : "$(git log -1 --oneline | awk '{print $1}') >> ../$3.log
echo >> ../$3.log
git log -1 | grep -E "(^[Cc]ommit)|(^[Aa]uthor)|(^[Dd]ate)" >> ../$3.log
cd ..
tar zcf $3.tar.gz $3
rm -rf $3
else
echo "Get $3-$6" | tee -a $BUILD_LOG
fi
else
echo "Get $3-$6" | tee -a $BUILD_LOG
fi
if [ $9 -eq 0 ]; then
echo "Install $3/$4" | tee -a $BUILD_LOG
if [ ! -e "$INSTALL_DIR/nginx-$VERSION/$5" ]; then
mkdir -p "$INSTALL_DIR/nginx-$VERSION/$5"
fi
if [ -e $3.tar.gz ]; then
tar zxf $3.tar.gz
cp -r $3/$4 "$INSTALL_DIR/nginx-$VERSION/$5/"
rm -rf $3
fi
fi
}
uninstall_file() {
rm -rf $INSTALL_DIR/nginx-$VERSION/$1
}
make_dir() {
mkdir $INSTALL_DIR/nginx-$VERSION/$1
}
function install_lua_modules() {
if [ $download_all -eq 1 ]; then
rm -rf download/lua_modules/* 2>>$ERR_LOG
fi
cd download/lua_modules
install_resty_module https://github.com openresty lua-resty-core lib . v0.1.17 $download $download_all $download_only
install_resty_module https://github.com openresty lua-resty-lrucache lib . v0.09 $download $download_all $download_only
cd ../..
}
if [ $build_debug -eq 1 ]; then
exit 0
fi
install_lua_modules
if [ $download_only -eq 1 ]; then
exit 0
fi
cd "$DIR"
cd $INSTALL_DIR
kernel_name=$(uname -s)
kernel_version=$(uname -r)
tar zcvf nginx-$VERSION$SUFFIX-$kernel_name-$kernel_version.tar.gz nginx-$VERSION$SUFFIX
rm -rf nginx-$VERSION$SUFFIX
exit $r