forked from dellelce/mkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkit.build.sh
More file actions
executable file
·455 lines (363 loc) · 9.69 KB
/
mkit.build.sh
File metadata and controls
executable file
·455 lines (363 loc) · 9.69 KB
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
#!/bin/bash
#
# mkit build library
#
# prepare_build: only for those that do not support explicitly a build directory
# different than source
# This function expects to be in the build directory
prepare_build()
{
typeset dir="$1"
[ ! -d "$dir" ] && return 1
dirList=$(find $dir -type d)
#make directores
for bad in $dirList
do
baseDir=${bad#${dir}/} #remove "base" directory
mkdir -p "$baseDir" || return "$?"
done
# link files
find $dir -type f | awk -vdir=$dir '
{
fn=$0
bad=$0
sub(dir"/", "", fn);
printf("ln -sf %c%s%c %c%s%c\n", 34, bad, 34, 34, fn, 34)
}
' | $SHELL
}
# logger_file: return a full file name to be used for the specified id
logger_file()
{
typeset logid="$1"
typeset LAST_LOG="${LOGSDIR}/${TIMESTAMP}_${logid}.log"
echo $LAST_LOG
}
#
#
build_sanity_gnuconf()
{
[ -z "$1" ] && { echo "build_sanity_gnuconf srcdirectory"; return 1; }
[ ! -d "$1" ] && { echo "build_sanity_gnuconf: invalid srcdirectory: $1"; return 1; }
[ ! -f "$1/configure" -a -f "$1/configure.ac" ] &&
{
typeset cwd="$PWD"
cd "$1"
# Allow mixing aclocals......
[ -d "/usr/share/aclocal" ] &&
{
export ACLOCAL_PATH="/usr/share/aclocal:${prefix}/share/aclocal"
}
autoreconf -vif >/dev/null 2>&1; ar_rc=$?
cd "$cwd"
[ $ar_rc -ne 0 ] && { echo "autoreconf failed with rc = $ar_rc"; return $ar_rc; }
build_sanity_gnuconf $1
return $?
}
[ ! -f "$1/configure" -a -f "$1/buildconf.sh" ] &&
{
echo "build_sanity_gnuconf: no configure file in: $1 but buildconf.sh is present"
$dir/buildconf.sh; bc_rc=$?
[ $bc_rc -ne 0 ] && return $bc_rc
build_sanity_gnuconf $dir
return $?
}
[ ! -f "$1/configure" ] && { echo "build_sanity_gnuconf: no configure file in: $1"; return 1; }
return 0
}
#
# logging function to be used by build functions
build_logger()
{
export LAST_LOG=$(logger_file "$1")
cat >> "${LAST_LOG}"
}
# GNU Configure wrapper with layers to generate "configure" file if needed
#
build_gnuconf()
{
typeset rc=0
export rc_conf=0 rc_make=0 rc_makeinstall=0
typeset id="$1"; shift # build id
typeset dir="$1"; shift # src directory
typeset pkgbuilddir="$BUILDDIR/$id"
[ -z "$dir" ] && { echo "usage: build_gnuconf id source_dir_path"; return 1; }
build_sanity_gnuconf $dir
rc=$?
[ $rc -ne 0 ] && { echo "build_gnuconf: build sanity tests failed for $dir"; return $rc; }
[ ! -d "$pkgbuilddir" ] &&
{ mkdir -p "$pkgbuilddir"; } ||
{ pkgbuilddir="$BUILDDIR/${id}.${RANDOM}"; mkdir -p "$pkgbuilddir"; }
cd "$pkgbuilddir" ||
{
echo "build_gnuconf: Failed to change to build directory: " $pkgbuilddir;
return 1;
}
# some "configure"s do not support building in a directory different than the source directory
# TODO: cwd to "$dir"
[ "$opt" == "BADCONFIGURE" ] &&
{
prepare_build "$dir"
}
echo "Building $id [${BOLD}$(getbasename $id)${RESET}] at $(date)"
echo
time_start # setup timer
export CFLAGS="${BASE_CFLAGS} -I${prefix}/include"
export LDFLAGS="${BASE_LDFLAGS} -L${prefix}/lib -Wl,-rpath=${prefix}/lib"
echo "Configuring..."
logFile=$(logger_file ${id}_configure)
$dir/configure --prefix="${prefix}" $* > ${logFile} 2>&1; rc_conf=$?
[ "$rc_conf" -ne 0 ] && { cat "${logFile}"; return $rc_conf; }
echo "Running make..."
logFile=$(logger_file ${id}_make)
make > ${logFile} 2>&1; rc_make=$?
[ "$rc_make" -ne 0 ] && { cat "${logFile}"; return $rc_make; }
echo "Running make install..."
logFile=$(logger_file ${id}_makeinstall)
make install > ${logFile} 2>&1; rc_makeinstall=$?
cd "$WORKDIR"
[ "$rc_makeinstall" -ne 0 ] && { cat "${logFile}"; }
time_end
return $rc_makeinstall
}
# add_run_dep: runtime dependencies
#
add_run_dep()
{
typeset id_list="$*" item=""
[ -z "$id_list" ] && return 1 # sanitycheck
[ -z "$RUNTIME_LIST" ] && { export RUNTIME_LIST="$id_list"; } || { export RUNTIME_LIST="$RUNTIME_LIST $id_list"; }
# remove possible duplicates
RUNTIME_LIST=$(
for item in $RUNTIME_LIST
do
echo $item
done | awk '!x[$0]++'
)
}
# add_build_dep: buildtime dependencies
#
add_build_dep()
{
typeset id_list="$*" item=""
[ -z "$id_list" ] && return 1 # sanitycheck
[ -z "$BUILDTIME_LIST" ] && { export BUILDTIME_LIST="$id_list"; } || { export BUILDTIME_LIST="$BUILDTIME_LIST $id_list"; }
# remove possible duplicates
BUILDTIME_LIST=$(
for item in $BUILDTIME_LIST
do
echo $item
done | awk '!x[$0]++'
)
}
#
# run_build: build all
# globals used:
# RUNTIME_LIST
# BUILDTIME_LIST
# DOWNLOAD_MAP
# BASE_CFLAGS
# BASE_LDFLAGS
# all variables set by add_options if any
run_build()
{
typeset pkg=""
typeset rc=0
typeset buildprefix=""
[ ! -z "$CFLAGS" ] && { export BASE_CFLAGS="$CFLAGS"; unset CFLAGS; }
[ ! -z "$LDFLAGS" ] && { export BASE_LDFLAGS="$CFLAGS"; unset LDFLAGS; }
# download latest archives / builds name mapping
download || { echo "Download failed for one of the components"; exit 1; }
# print download map ==== do we really need to print this?
[ ! -z "$DOWNLOAD_MAP" ] &&
{
echo "Downloaded software:"
echo
for item in $DOWNLOAD_MAP
do
echo $item| awk -F: '
{
cnt=split($2,bn_a,"/");
bn=bn_a[cnt]
printf("%-12s %s\n", $1, bn);
}
'
done
}
# if there is any build-time dep prepare a custom prefix for them.
[ ! -z "$BUILDTIME_LIST" ] &&
{
export buildprefix="$TMP/build_prefix_${RANDOM}${RANDOM}"
# no checks?
mkdir -p "$buildprefix/bin"
# build prefix files to be found before everything else
export PATH="$buildprefix/bin:$PATH"
# use underscore to mark package as "build-dep" (to simplify following loop)
BUILDTIME_LIST=$(
for _pkg in $BUILDTIME_LIST
do
echo "_"${_pkg}
done
)
}
for pkg in $BUILDTIME_LIST $RUNTIME_LIST
do
build=0
[ ${pkg} != "${pkg#_}" ] && { build=1; pkg="${pkg#_}"; }
fname=$(getbasename $pkg)
[ "$fname" == "installed" ] && continue
eval options="\$options_${pkg}"
echo
func="build_${pkg}"
type $func >/dev/null 2>&1
func_rc=$?
[ $func_rc -ne 0 ] &&
{
# module was not found built-in: try as module
module_func="$MKIT/modules/build/${pkg}.sh"
[ -f "$module_func" ] &&
{
. "$module_func"
type $func >/dev/null 2>&1
func_rc=$?
} ||
{
echo "Build function for $pkg is invalid or does not exist"
return 1
}
}
# uncompress
do_uncompress ${pkg} || return $?
[ "$build" -eq 1 ] &&
{
prefix="$buildprefix" $func $options; rc=$?
} ||
{
$func $options; rc=$?
}
[ "$rc" -ne 0 ] &&
{
echo "Failed build of $pkg with return code: $rc"
return $rc
}
done
[ -d "$buildprefix" ] && rm -rf "$buildprefix"
return 0 # we hate bash bugs so we add a return 0 here
}
#
# timing functions
#
time_start()
{
export start=$(date +%s)
}
time_end()
{
export end=$(date +%s)
let elapsed="(( $end - $start ))"
echo "Elapsed: ${elapsed}secs"
}
#
#
build_raw_core()
{
typeset rc=0 cwd=""
export rc_conf=0 rc_make=0 rc_makeinstall=0
typeset id="$1"; shift # build id
typeset dir="$1"; shift # src directory
typeset pkgbuilddir="$BUILDDIR/$id"
[ ! -d "$pkgbuilddir" ] &&
{ mkdir -p "$pkgbuilddir"; } ||
{ pkgbuilddir="$BUILDDIR/${id}.${RANDOM}"; mkdir -p "$pkgbuilddir"; }
cd "$pkgbuilddir" ||
{
echo "build: Failed to change to build directory: " $pkgbuilddir;
return 1;
}
# we create a build directory different than source directory for them.
prepare_build "$dir"
echo "Building $id [${BOLD}$(getbasename $id)${RESET}] at $(date)"
echo
time_start
# make
{
logFile=$(logger_file ${id}_make)
echo "Running make: logging at ${logFile}"
cwd="$PWD"; cd "$dir"
make > ${logFile} 2>&1; rc_make="$?"
cd "$cwd"
}
[ "$rc_make" -ne 0 ] && { cat ${logFile}; return "$rc_make"; }
# make install
{
logFile=$(logger_file ${id}_makeinstall)
echo "Running make install: logging at ${logFile}"
cwd="$PWD"; cd "$dir"
make install PREFIX="${prefix}" > ${logFile} 2>&1
rc_makeinstall="$?"
cd "$cwd"
}
[ "$rc_makeinstall" -ne 0 ] && { cat "${logFile}"; return "$rc_makeinstall"; }
time_end
return 0
}
#
#
build_perlmodule()
{
typeset rc=0 cwd=""
export rc_conf=0 rc_make=0 rc_makeinstall=0
typeset id="$1"; shift # build id
typeset dir="$1"; shift # src directory
typeset pkgbuilddir="$BUILDDIR/$id"
[ ! -d "$pkgbuilddir" ] &&
{ mkdir -p "$pkgbuilddir"; } ||
{ pkgbuilddir="$BUILDDIR/${id}.${RANDOM}"; mkdir -p "$pkgbuilddir"; }
cd "$pkgbuilddir" ||
{
echo "build: Failed to change to build directory: " $pkgbuilddir;
return 1;
}
# redis & many others does not have a GNU configure but just a raw makefile
# or some other sometimes fancy buil systems.
# we create a build directory different than source directory for them.
prepare_build "$dir"
echo "Building $id [${BOLD}$(getbasename $id)${RESET}] at $(date)"
echo
time_start
# configurepl
logFile=$(logger_file ${id}_configurepl)
echo "Running Makefile.PL"
cwd="$PWD"; cd "$dir"
PERL5LIB=$prefix/share/perl5 \
$prefix/bin/perl Makefile.PL PREFIX=$prefix > ${logFile} 2>&1; rc_configurepl="$?"
cd "$cwd"
[ "$rc_configurepl" -ne 0 ] && { cat ${logFile}; return "$rc_configurepl"; }
# make
logFile=$(logger_file ${id}_make)
echo "Running make"
cwd="$PWD"; cd "$dir"
PERL5LIB=$prefix/share/perl5 \
make > ${logFile} 2>&1; rc_make="$?"
cd "$cwd"
[ "$rc_make" -ne 0 ] && { cat ${logFile}; return "$rc_make"; }
# make install
logFile=$(logger_file ${id}_makeinstall)
echo "Running make install"
cwd="$PWD"; cd "$dir"
PERL5LIB=$prefix/share/perl5 \
make install > ${logFile} 2>&1
rc_makeinstall="$?"
cd "$cwd"
[ "$rc_makeinstall" -ne 0 ] && { cat "${logFile}"; return "$rc_makeinstall"; }
time_end
return 0
}
# add_options: allow to pass custom options from profiles to build functions
add_options()
{
typeset pkg="$1"; shift
typeset options="$*"
eval "export options_${pkg}=\"${options}\""
}
### EOF ###