-
Notifications
You must be signed in to change notification settings - Fork 16
/
helpers_functions.sh
456 lines (407 loc) · 11.9 KB
/
helpers_functions.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
#
# The BSD 3-Clause License. http://www.opensource.org/licenses/BSD-3-Clause
#
# This file is part of 'Qt-builds' project.
# Copyright (c) 2013 by Alexpux (alexpux@gmail.com)
# All rights reserved.
#
# Project: Qt-builds ( https://github.com/Alexpux/Qt-builds )
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the distribution.
# - Neither the name of the 'Qt-builds' nor the names of its contributors may
# be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# **************************************************************************
die() {
echo $@
exit 1
}
# **************************************************************************
get_filename_extension() {
local _fileext=
local _filename=$1
while [[ $_filename = ?*.* &&
( ${_filename##*.} = [A-Za-z]* || ${_filename##*.} = 7z ) ]]; do
_fileext=${_filename##*.}.$_fileext
_filename=${_filename%.*}
done
_fileext=${_fileext%.}
echo "$_fileext"
}
# **************************************************************************
# install toolchains
toolchains_prepare() {
local _file_mingw32=$(basename $URL_MINGW32)
local _ext_mingw32=$(get_filename_extension $_file_mingw32)
local _file_mingw64=$(basename $URL_MINGW64)
local _ext_mingw64=$(get_filename_extension $_file_mingw64)
pushd $TOOLCHAINS_DIR > /dev/null
[[ ! -f $TOOLCHAINS_DIR/${_file_mingw32%.$_ext_mingw32}-unpack.marker ]] && {
echo "-> Prepare 32-bit toolchain..."
[[ -d $TOOLCHAINS_DIR/mingw32 ]] && {
echo -n "--> Remove previous toolchain..."
rm -rf $TOOLCHAINS_DIR/mingw32
echo " done"
}
func_download ${_file_mingw32%.$_ext_mingw32} ".$_ext_mingw32" $URL_MINGW32
func_uncompress ${_file_mingw32%.$_ext_mingw32} ".$_ext_mingw32" $TOOLCHAINS_DIR
echo "--> Preparing 32-bit toolchain done"
} || {
echo "-> 32-bit toolchain prepared"
}
[[ ! -f $TOOLCHAINS_DIR/${_file_mingw64%.$_ext_mingw64}-unpack.marker ]] && {
echo "-> Prepare 64-bit toolchain..."
[[ -d $TOOLCHAINS_DIR/mingw64 ]] && {
echo -n "--> Remove previous toolchain..."
rm -rf $TOOLCHAINS_DIR/mingw64
echo " done"
}
func_download ${_file_mingw64%.$_ext_mingw64} ".$_ext_mingw64" $URL_MINGW64
func_uncompress ${_file_mingw64%.$_ext_mingw64} ".$_ext_mingw64" $TOOLCHAINS_DIR
echo "--> Preparing 64-bit toolchain done"
} || {
echo "-> 64-bit toolchain prepared"
}
popd > /dev/null
}
# **************************************************************************
function func_absolute_to_relative {
# $1 - first path
# $2 - second path
local _common=$1
local _target=$2
local _back=""
while [[ "${_target#$_common}" == "${_target}" ]]; do
_common=$(dirname $_common)
_back="../${_back}"
done
[[ -z $_back ]] && {
_back="./"
echo "${_back}"
} || {
echo "${_back}${_target#$_common/}"
}
}
# **************************************************************************
# download the sources
function func_download {
# $1 - package name
# $2 - sources type: .tar.gz, .tar.bz2 e.t.c...
# if sources get from a repository, choose it's type: cvs, svn, hg, git
# $3 - URL
# $4 - revision
[[ -z $3 ]] && {
die "URL is empty. terminate."
}
local _WGET_TIMEOUT=5
local _WGET_TRIES=10
local _WGET_WAIT=2
local _result=0
local _log_name=$MARKERS_DIR/${1//\//_}-download.log
local _marker_name=$MARKERS_DIR/${1//\//_}-download.marker
local _repo_update=no
local _filename=$(basename $3)
[[ "$2" == "cvs" || "$2" == "svn" || "$2" == "hg" || "$2" == "git" ]] && {
local _lib_name=$UNPACK_DIR/$1
if [[ $UPDATE_SOURCES == yes ]]
then
_repo_update=yes
fi
} || {
local _lib_name=$SRC_DIR/$1
}
[[ ! -f $_marker_name || "$_repo_update" == "yes" ]] && {
[[ -f $SRC_DIR/$_filename ]] && {
echo -n "---> Delete corrupted download..."
rm -f $SRC_DIR/$_filename
echo " done"
}
pushd $SRC_DIR > /dev/null
echo -n "---> download $1..."
case $2 in
cvs)
#local _prev_dir=$PWD
#cd $1
[[ -n $4 ]] && {
cvs -z9 -d $3 co -D$4 $1 > $_log_name 2>&1
} || {
cvs -z9 -d $3 co $1 > $_log_name 2>&1
}
#cd $_prev_dir
_result=$?
;;
svn)
[[ -d $_lib_name/.svn ]] && {
pushd $_lib_name > /dev/null
svn-clean -f > $_log_name 2>&1
svn revert -R ./ >> $_log_name 2>&1
svn up >> $_log_name 2>&1
popd > /dev/null
} || {
[[ -n $4 ]] && {
svn co -r $4 $3 $_lib_name > $_log_name 2>&1
} || {
svn co $3 $_lib_name > $_log_name 2>&1
}
}
_result=$?
;;
hg)
hg clone $3 $_lib_name > $_log_name 2>&1
_result=$?
;;
git)
[[ -d $_lib_name/.git ]] && {
pushd $_lib_name > /dev/null
git clean -f > $_log_name 2>&1
git reset --hard >> $_log_name 2>&1
git pull >> $_log_name 2>&1
popd > /dev/null
} || {
[[ -n $4 ]] && {
git clone --branch $4 $3 $_lib_name > $_log_name 2>&1
} || {
git clone $3 $_lib_name > $_log_name 2>&1
}
}
_result=$?
;;
*)
[[ ! -f $_marker_name && -f $_lib_name ]] && rm -rf $_lib_name
wget \
--tries=$_WGET_TRIES \
--timeout=$_WGET_TIMEOUT \
--wait=$_WGET_WAIT \
--no-check-certificate \
$3 > $_log_name 2>&1
_result=$?
;;
esac
popd > /dev/null
[[ $_result == 0 ]] && {
echo " done"
touch $_marker_name
} || {
[[ $SHOW_LOG_ON_ERROR == yes ]] && $LOGVIEWER $_log_name &
die " error $_result!"
}
} || {
echo "---> downloaded"
}
}
# uncompress sources
function func_uncompress {
# $1 - name
# $2 - ext
# $3 - src dir name
# $4 - ignore unpack errors
local _src_dir=$UNPACK_DIR
local _ignore_error=0
local _marker_location=$MARKERS_DIR
[[ "x$3" != "x" && "x$3" == "x--ignore" ]] && {
_ignore_error=1
}
[[ "x$4" != "x" && "x$4" == "x--ignore" ]] && {
_ignore_error=1
}
[[ "x$3" != "x" && "x$3" != "x--ignore" ]] && {
_src_dir=$3
_marker_location=$3
}
local _result=0
local _unpack_cmd
local _marker_name=$_marker_location/$1-unpack.marker
local _log_name=$_marker_location/$1-unpack.log
[[ $2 == .tar.gz || $2 == .tgz || $2 == .tar.bz2 || $2 == .tar.lzma \
|| $2 == .tar.xz || $2 == .tar.7z || $2 == .7z || $2 == .zip ]] && {
[[ ! -f $_marker_name ]] && {
echo -n "---> unpack..."
case $2 in
.tar.gz|.tgz) _unpack_cmd="tar xvf $SRC_DIR/$1$2 -C $_src_dir > $_log_name 2>&1" ;;
.tar.bz2) _unpack_cmd="tar xvjf $SRC_DIR/$1$2 -C $_src_dir > $_log_name 2>&1" ;;
.tar.lzma) _unpack_cmd="tar xvJf $SRC_DIR/$1$2 -C $_src_dir > $_log_name 2>&1" ;;
.tar.xz) _unpack_cmd="tar -xv --xz -f $SRC_DIR/$1$2 -C $_src_dir > $_log_name 2>&1" ;;
.tar.7z) die "unimplemented. terminate." ;;
.7z) _unpack_cmd="7za x -y $SRC_DIR/$1$2 -o$_src_dir > $_log_name 2>&1" ;;
.zip) _unpack_cmd="unzip $SRC_DIR/$1$2 -d $_src_dir > $_log_name 2>&1" ;;
*) die " error. bad archive type: $2" ;;
esac
eval ${_unpack_cmd}
_result=$?
[[ $_result == 0 || $_ignore_error == 1 ]] && {
echo " done"
touch $_marker_name
} || {
[[ $SHOW_LOG_ON_ERROR == yes ]] && $LOGVIEWER $_log_name &
die " error $_result!"
}
} || {
echo "---> unpacked"
}
}
}
# **************************************************************************
# create copy of sources in build directory
function lndirs() {
local _src_dir=
local _dest_dir=
[[ $# -eq 0 ]] && {
_src_dir=$UNPACK_DIR/$P_V
_dest_dir=$BUILD_DIR/$P_V
} || {
_src_dir=$UNPACK_DIR/$1
_dest_dir=$BUILD_DIR/$2
}
[[ ! -f $_dest_dir/lndirs.marker ]] && {
echo -n "---> Copy sources to build directory..."
mkdir -p $_dest_dir
lndir $_src_dir $_dest_dir > $LOG_DIR/${P_V}-lndirs.log 2>&1 || die "Fail lndir sources"
touch $_dest_dir/lndirs.marker
echo " done"
} || {
echo "---> Sources already copied"
}
}
# **************************************************************************
# apply list of patches
function func_apply_patches {
# $1 - src dir name
# $2 - patches list
# $3 - sources directory
local _src_dir=$UNPACK_DIR
[[ "x$3" != "x" ]] && {
_src_dir=$3
}
local _result=0
local _index=0
local -a _list=( "${!2}" )
[[ ${#_list[@]} == 0 ]] && {
echo "---> No patches for $1"
return 0
}
_index=$((${#_list[@]}-1))
[[ -f $_src_dir/$1/_patch-$_index.marker ]] && {
echo "---> patched"
return 0
}
_index=0
[[ ${#_list[@]} > 0 ]] && {
echo -n "---> patching..."
}
local it=
local applevel=
pushd $_src_dir/$1 > /dev/null
for it in ${_list[@]} ; do
local _patch_marker_name=$_src_dir/$1/_patch-$_index.marker
[[ ! -f $_patch_marker_name ]] && {
[[ -f $PATCH_DIR/${it} ]] || die "Patch $PATCH_DIR/${it} not found!"
local level=
local found=no
for level in 0 1 2 3 4
do
applevel=$level
if patch -p$level --dry-run -i $PATCH_DIR/${it} > $_src_dir/$1/patch-$_index.log 2>&1
then
found=yes
break
fi
done
[[ $found == "yes" ]] && {
patch -p$applevel -i $PATCH_DIR/${it} > $_src_dir/$1/patch-$_index.log 2>&1
touch $_patch_marker_name
} || {
_result=1
break
}
}
_index=$(($_index+1))
done
popd > /dev/null
[[ $_result == 0 ]] && {
echo " done"
} || {
[[ $SHOW_LOG_ON_ERROR == yes ]] && $LOGVIEWER $_src_dir/$1/patch-$_index.log &
die "Failed to apply patch ${it} at level $applevel"
}
}
# **************************************************************************
# configure
function func_configure {
# $1 - build dir name
# $2 - src dir name
# $3 - flags
# $4 - parent source directory (set if it not $SRC_DIR)
local _src_dir=$UNPACK_DIR/$2
[[ ! -z $4 ]] && {
_src_dir=$4/$2
}
local _marker=$BUILD_DIR/$1/_configure.marker
local _result=0
local _log_name=$LOG_DIR/${2//\//_}-configure.log
[[ ! -f $_marker ]] && {
echo -n "---> configure..."
mkdir -p $BUILD_DIR/$1
local _rell=$( func_absolute_to_relative $BUILD_DIR/$1 $_src_dir )
pushd $BUILD_DIR/$1 > /dev/null
eval ${_rell}/configure "${3}" > $_log_name 2>&1
_result=$?
[[ $_result == 0 ]] && {
echo " done"
touch $_marker
} || {
[[ $SHOW_LOG_ON_ERROR == yes ]] && $LOGVIEWER $_log_name &
die " error $_result!"
}
popd > /dev/null
} || {
echo "---> configured"
}
}
# **************************************************************************
# make
function func_make {
# $1 - build dir name
# $2 - make prog name
# $3 - make flags
# $4 - text
# $5 - text if completed
# $6 - index
local _marker=$BUILD_DIR/$1/_$5$6.marker
local _result=0
local _log_name=$LOG_DIR/${1//\//_}-$5$6.log
local _make_cmd="$2 $3"
[[ ! -f $_marker ]] && {
echo -n "---> $4..."
( cd $BUILD_DIR/$1 && eval ${_make_cmd} > $_log_name 2>&1 )
_result=$?
[[ $_result == 0 ]] && {
echo " done"
touch $_marker
} || {
[[ $SHOW_LOG_ON_ERROR == yes ]] && $LOGVIEWER $_log_name &
die " error $_result!"
}
} || {
echo "---> $5"
}
}
# **************************************************************************