-
Notifications
You must be signed in to change notification settings - Fork 2
/
mkpkg
executable file
·795 lines (715 loc) · 23 KB
/
mkpkg
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# Copyright (c) 2015, Mattia Penati <mattia.penati@gmail.com>
# Copyright (c) 2020-2024, Pasquale Claudio Africa <pasqualeclaudio.africa@polimi.it>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. 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.
#
# 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.
# the following environment variables can change the behaviour of the command
#
# mkPrefix = [path] (mandatory)
# The installation prefix of mk.
# mkUseColor = [auto|yes|no] (default: auto)
# If set to 'no' than the command does not use the colors for the output.
# mkSandboxDir = [path] (default: none)
# In this directory where the build script is executed, this to ensure enough
# space to build the package.
# mkKeepBuildDir = [yes|no] (default: no)
# If set to 'yes' then the build directory is not removed if an error occours.
# mkParallelBuild = [yes|no] (default: yes)
# If set to 'no' then the build process ignore the flags --jobs.
# mkDebug = [yes|no] (default: no)
# If set to 'yes' then bash print all the commands.
# setup the environment
# =============================================================================
# checking if the environment is setup correctly (variable mkPrefix)
if test -z "$mkPrefix"; then
echo "The environment is not set properly, 'mkPrefix' variable is not set" >&2
exit 1
fi
if test ! -d $mkPrefix; then
echo "Variable 'mkPrefix' is not pointing to an existing directory" >&2
echo " mkPrefix='$mkPrefix'" >&2
exit 1
fi
if test ! -r "$mkPrefix/etc/mk.config"; then
echo "Unable to read the configuration file '$mkPrefix/etc/mk.config'" >&2
exit 1
fi
# reset the environment, but the toolchain is not discarded
if [[ -n "$mkToolchain" ]]; then
mkLoadedToolchain="$mkToolchain"
module purge
fi
export PATH=$mkPrefix/bin:/usr/bin:/bin
export LD_LIBRARY_PATH=
export PKG_CONFIG_PATH=
if [[ -n "$mkLoadedToolchain" ]]; then
module load "$mkLoadedToolchain"
fi
# some aliases to increase the verbosity
# Busybox is not used because it breaks some package build process.
alias cp="command cp -v"
alias install="command install -v"
alias ln="command ln -v"
alias mv="command mv -v"
alias rm="command rm -v"
alias rmdir="command rmdir -v"
# load the configuration file and detect the current system configuration
# PREFIX installation prefix
# PKGVERSION used to customize the version string of some packages
# VENDOR the vendor name
# BUGURL the url used to report the bugs
# BUILD the triple of build system
# TARGET the triple of target system (computed by VENDOR)
# ARCH the architecture of the system
# OS the operating system
# CFLAGS the common flags for C compiler
# CXXFLAGS the common flags for C++ compiler
# FCFLAGS the common flags for Fortran compiler
# TM the name of job scheduler
# TMPREFIX the installation prefix of job scheduler
# INFINIBAND the list of the required Infiniband drivers
source $mkPrefix/etc/mk.config
if [[ ! $(realpath "$mkPrefix") = $(realpath "$PREFIX") ]]; then
echo "Internal error: $mkPrefix does not point to a proper installation."
exit 1
fi
BUILD=$(cc -dumpmachine 2> /dev/null)
if [[ -z "$BUILD" ]]; then
echo "Unable to detect the build system"
exit 1
fi
ARCH=$(uname -m 2> /dev/null)
if [[ -z "$ARCH" ]]; then
echo "Unable to detect the architecture of the system"
exit 1
fi
[[ -z "$VENDOR" ]] && VENDOR="unknown"
case "$(uname -s 2> /dev/null)" in
"Linux") OS="linux";;
*) echo "Unsupport operating system"; exit 1
esac
TARGET=$ARCH-$VENDOR-$OS
# fix the configuration depending on the architecture
case "$ARCH" in
"x86_64")
CFLAGS="$CFLAGS -fPIC"
CXXFLAGS="$CXXFLAGS -fPIC"
FCFLAGS="$FCFLAGS -fPIC"
;;
"ppc64")
;;
*)
echo "Unsupported architecture '$ARCH'"
exit 1
esac
# exporting the environment variables
export CFLAGS
export CXXFLAGS
export FCFLAGS
# Check the environment variables used by mkpkg command
[[ -z "$mkUseColor" ]] && mkUseColor=auto
[[ -z "$mkKeepBuildDir" ]] && mkKeepBuildDir=no
[[ -z "$mkParallelBuild" ]] && mkParallelBuild=yes
[[ -z "$mkDebug" ]] && mkDebug=no
if [[ $mkUseColor = "auto" ]]; then
mkUseColor=$([[ $(tput colors 2> /dev/null) -ge 8 ]] && echo yes || echo no)
fi
if [[ ! "$mkUseColor" = "no" && ! "$mkUseColor" = "yes" ]]; then
echo "Invalid value '$mkUseColor' for variable 'mkUseColor'" >&2
exit 1
fi
if [[ -n "$mkSandboxDir" && ! -d $mkSandboxDir ]]; then
echo "Variable 'mkSandboxDir' does not point to a valid directory: '$mkSandboxDir'" >&2
exit 1
fi
if [[ ! "$mkKeepBuildDir" = "no" && ! "$mkKeepBuildDir" = "yes" ]]; then
echo "Invalid value '$mkKeepBuildDir' for variable 'mkKeepBuildDir'" >&2
exit 1
fi
if [[ ! "$mkParallelBuild" = "no" && ! "$mkParallelBuild" = "yes" ]]; then
echo "Invalid value '$mkParallelBuild' for variable 'mkParallelBuild'" >&2
exit 1
fi
if [[ ! "$mkDebug" = "no" && ! "$mkDebug" = "yes" ]]; then
echo "Invalid value '$mkDebug' for variable 'mkDebug'" >&2
exit 1
fi
# defined the variables and the function used for the output
# =============================================================================
# create the special file descriptors, this command use the file descriptors as
# follows:
# 1 for the standard output (if $mkVerbose = "no" then point to $mkLogFile)
# 2 for the standard error (if $mkVerbose = "no" then point to $mkLogFile)
# 3 the real standard output (internal usage)
# 4 the real standard error (internal usage)
exec 3>&1
exec 4>&2
# colors
unset RESET BOLD BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
if [[ "$mkUseColor" != "no" && $(tput colors 2> /dev/null) -ge 8 ]]; then
if tput setaf 0 &>/dev/null; then
RESET="$(tput sgr0)"
BOLD="$(tput bold)"
BLACK="$(tput setaf 0)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
WHITE="$(tput setaf 7)"
else
RESET="\e[0m"
BOLD="\e[1m"
BLACK="\e[30m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
CYAN="\e[36m"
WHITE="\e[37m"
fi
fi
readonly RESET BOLD BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
# error <msg>
# Print an error message to the standard error stream
error() {
echo -e "${BOLD}${RED}>> Error${RESET}${BOLD} $1${RESET}" | tee -a "$mkLogFile" >&4
}
# warning <msg>
# Print a warning message to the standard error stream
warning() {
echo -e "${BOLD}${YELLOW}>> Warning${RESET}${BOLD} $1${RESET}" | tee -a "$mkLogFile" >&4
}
# message <msg>
# Print a message to the standard output stream
message() {
echo -e "${BOLD}>> $1${RESET}" | tee -a "$mkLogFile" >&3
}
# plain <msg>
# Print a plain text to the standard output stream
plain() {
echo -e " ${RESET}$1${RESET}" | tee -a "$mkLogFile" >&3
}
# log <msg>
# Print a messago only in the log file
log() {
echo -e "${BOLD}>> $1${RESET}"
}
# parse the command line arguments
# =============================================================================
# usage
# Print the command help.
usage() {
# redirect to the correct file
echo "Install the select package or the package contained in the current directory"
echo ""
echo "Usage:"
echo " mkpkg [options] [pkg]"
echo " mkpkg --configuration"
echo " mkpkg -h | --help"
echo ""
echo "Options:"
echo " --options=<opts> Pass the given options to the build script"
echo " --jobs=<jobs> Execute the parallel building"
echo " -v | --verbose Print all messages to the screen (very verbose)"
echo " --log=<file> Write all messages in the given file"
echo " --test=<path> Build the package and install in the given path"
echo " --configuration Print the current configuration"
echo " -h | --help Show this help message"
echo ""
return 0
}
# default options
pkgOptions=
pkgJobs=1
pkgScript=
mkConfiguration=no
mkVerbose=no
mkLogFile=/dev/null
mkTest=no
# parse the command line arguments
while true; do
case "$1" in
--options=*) pkgOptions=${1#*=};;
--jobs=*) pkgJobs=${1#*=};;
--configuration) mkConfiguration=yes;;
-v|--verbose) mkVerbose=yes;;
--log=*) mkLogFile=${1#*=};;
--test=*) mkTest=${1#*=};;
-h|--help) usage; exit 0;;
-*) error "Unknown option '$1'"; exit 1;;
*) break;;
esac
shift
done
pkgScript="$1"; shift
# there are some unkown arguments
if [[ -n "$@" ]]; then
error "Unknown arguments '$@'"
exit 1
fi
# check value of variables
mkLogFile=$(readlink -f "$mkLogFile")
if [[ ! $pkgJobs -ge 1 ]]; then
error "Invalid value for the options --jobs: '$mkJobs'"
exit 1
fi
# print the current configuration
# =============================================================================
if [[ "$mkConfiguration" = "yes" ]]; then
message "Current mkpkg configuration"
plain "PREFIX $PREFIX"
plain "BUILD $BUILD"
plain "TARGET $TARGET"
plain "CFLAGS $CFLAGS"
plain "CXXFLAGS $CXXFLAGS"
plain "FCFLAGS $FCFLAGS"
plain "PATH $PATH"
plain "LD_LIBRARY_PATH $LD_LIBRARY_PATH"
plain "PKG_CONFIG_PATH $PKG_CONFIG_PATH"
if [[ -n "$TM" ]]; then
plain "TM $TM"
plain "TMPREFIX $TMPREFIX"
fi
if [[ -n "$INFINIBAND" ]]; then
plain "INFINIBAND ${INFINIBAND[*]}"
fi
exit 0
fi
# check the given build script
# =============================================================================
if [[ -z "$pkgScript" ]]; then
pkgScript=$(pwd)
fi
if [[ -d $pkgScript ]]; then
pkgScript="$pkgScript"/PKGBUILD
fi
if [[ ! -r $pkgScript ]]; then
error "Unable to read the build script '$pkgScript'"
exit 1
fi
pkgScript=$(readlink -f "$pkgScript")
# functions used to write the build script
# =============================================================================
# require_function <fun>
# Check if the given function exists, return true if it exists
require_function() {
local fun=$1; declare -f $fun &> /dev/null; return $?
}
# mktmpdir
# Create a temporary directory. The behaviour of this command depends on the
# value of the variable $mkSandboxDir
mktmpdir() {
if [[ -z "$mkSandboxDir" ]]; then
echo "$(mktemp -d)"
else
echo "$(mktemp -d -p "$mkSandboxDir")"
fi
}
# download <url> [<output>]
# Download the resource from the <url> and save it to the <output> file.
# If the output is not given then the file is saved in the $srcdir.
download() {
# arguments and variables
local url=$1
local output=$2
local cmd=""
# create the command
case $url in
http:*) cmd="wget $url";;
https:*) cmd="wget --no-check-certificate $url";;
ftp*) cmd="wget $url";;
*) error "Unknown protocol for url '$url'"; exit 1;;
esac
# manage the output
[[ -z "$output" ]] && output=$srcdir
if [[ -d $output ]]; then
cmd="$cmd -P $output"
else
cmd="$cmd -O $output"
fi
if [[ -f $output || -f "$output"/$(basename $url) ]]; then
message "File '$url' already downloaded"
else
message "Downloading '$url'"
$mkPrefix/bin/busybox $cmd
fi
}
# check <file> <checksum>
# Verify the <checksum> for the given <file>. If the file does not exist then
# the function looks in $srcdir.
check() {
local file=$1
local sum=$2
local cmd=
# if the file does not exists, then it tries to look in source directory
[[ ! -f $file && -f $srcdir/$file ]] && file="$srcdir/$file"
case ${#sum} in
0) error "No checksum is provided for file '$file'"; exit 1;;
32) cmd=md5sum
message "Checking MD5 checksum for '$(basename "$file")'";;
40) cmd=sha1sum
message "Checking SHA1 checksum for '$(basename "$file")'";;
*) error "Unknown hash function for '$file' ($sum)"; exit 1
esac
local csum=$($mkPrefix/bin/busybox $cmd $file | cut -f 1 -d " ")
if [[ ! $sum = $csum ]]; then
error "Checksum for '$file' does not agree"
plain "$sum != $csum"
exit 1
fi
}
# extract <archive> [<output>]
# Extract the given <archive> in the given <output> directory. If the file does
# not exist then the function looks in $srcdir.
extract() {
# arguments and variables
local archive=$1
local output=$2
local cmd=""
# if the file does not exists, then it tries to look in source directory
[[ ! -f $archive && -f $srcdir/$archive ]] && archive="$srcdir/$archive"
# create the command
case $archive in
*.tgz | *.tar.gz)
cmd="tar -vxzf $archive"
[[ -n "$output" ]] && cmd="$cmd --strip 1 -C $output"
;;
*.tar.bz2)
cmd="tar -vxjf $archive"
[[ -n "$output" ]] && cmd="$cmd --strip 1 -C $output"
;;
*.tar.xz)
cmd="tar -vxJf $archive"
[[ -n "$output" ]] && cmd="$cmd --strip 1 -C $output"
;;
*.zip)
cmd="unzip $archive"
[[ -n "$output" ]] && cmd="$cmd -d $output"
;;
*)
error "Unknown type of archive for file '$archive'"
exit 1
esac
# run the command
if [[ -z "$output" ]]; then
message "Extracting '$(basename "$archive")'"
$mkPrefix/bin/busybox $cmd
else
message "Extracting '$(basename "$archive")' in '$output'"
mkdir "$output"
eval $cmd
# zip has no strip option
case $archive in
*.zip)
local f=$(ls -A "$output")
for dir in $f; do
if [[ ! -d "$output/$dir" ]]; then
error "Unable to strip the first component from the zip file '$archive'"
exit 1
fi
mv "$output/$dir"/{*,.[^.]?*} "$output"
rmdir "$output/$dir"
done
;;
esac
fi
}
# make
# A function that replace the command make. It is used to manage automatically
# the parallel build. The behaviour of this function depends on the value of
# the variable $mkParallelBuild.
make() {
local cmd="command make"
if [[ ! "$mkParallelBuild" = "no" && $pkgJobs -ge 2 ]]; then
cmd="$cmd -j $pkgJobs"
fi
eval $cmd $@
}
# compress_doc <file>
# Compress the documentation pages (both man and info). It accept arguments of
# two different types: directory or files. If a regular file or a link is
# given, then it is compressed and eventually the link recreated. If a
# directory is given then if it is of the form <prefix>/share/man then the
# function run on contained directories, otherwise if it is of the form
# <prefix>/share/info then the function run on the contained files.
compress_doc() {
local file=
for file in $@; do
if [[ ! $file = *.gz ]]; then
if [[ -f $file ]]; then
log "Compressing '$file'"
"$mkPrefix"/bin/busybox gzip -f --verbose -9 "$file"
elif [[ -h $file ]]; then
log "Compressing '$file'"
local realfile=$(readlink -f "$file")
local reallink=$(readlink "$file")
if [[ -e "$realfile" ]]; then
"$mkPrefix"/bin/busybox gzip -f --verbose 9 "$realfile"
fi
rm -vf "$file"
ln -vfs "$reallink.gz" "$file.gz"
elif [[ -d $file ]]; then
case $file in
*/share/man)
for manpagedir in $(find "$file" -maxdepth 1 -mindepth 1 -type d); do
for eachpage in $(ls -A "$manpagedir") ; do
compress_doc "$manpagedir/$eachpage"
done
done;;
*/share/info)
for eachpage in $(ls -A "$file") ; do
case $eachpage in
dir) ;;
*) compress_doc "$file/$eachpage";;
esac
done;;
esac
fi
fi
done
}
# get_filetype <file>
# Return a string describing the current filetype
get_filetype() {
local file=$1
local filetype=none
if [[ -n "$(file -b "$file" | grep "executable" | grep "ELF" | grep "dynamically")" ]]; then
filetype=executable
elif [[ -n "$(file -b "$file" | grep "shared object" | grep "ELF")" ]]; then
filetype=library
fi
echo "$filetype"
}
# strip_
# Strip a binary or a library, removing the unneeded and debug symbols.
strip_() {
local file=
for file in $@; do
local filetype=$(get_filetype "$file")
# We need to manage the case when strip command strips itself.
if [[ $filetype = "executable" && "$(basename "$file")" = "strip" ]]; then
cp "$file" "$file.new"
chmod 0755 "$file.new"
command strip -v --strip-unneeded "$file.new"
rm "$file"
mv "$file.new" "$file"
else
case $filetype in
executable) log "Stripping '$(readlink -f $file)'"
chmod 0755 "$file"
command strip -v --strip-unneeded "$file"
;;
library)
# We need to manage the cases when strip command strips
# ld*.so* or libc.*.so*/libdl*.so*, otherwise we get a
# "Text file busy" or "Bus error" error, respectively.
if [[ "$(basename "$file")" == "ld"*".so"* ||
"$(basename "$file")" == "libc"*".so"* ||
"$(basename "$file")" == "libdl"*".so"* ]]; then
cp "$file" "$file.new"
chmod 0755 "$file.new"
command strip -v --strip-debug "$file.new"
rm "$file"
mv "$file.new" "$file"
else
log "Stripping '$(readlink -f $file)'"
chmod 0755 "$file"
command strip -v --strip-debug "$file"
fi
;;
none) ;;
*) error "Internal error"; exit 1
esac
fi
done
}
# strip <file>
# Strip a file. If the argument is a directory, then all the contained files
# are stripped recursively.
strip() {
local file=
for file in $@; do
if [[ -d $file ]]; then
find $file -type f -print0 | while IFS= read -r -d '' file; do strip_ $file; done
else
strip_ $file
fi
done
}
# reset_rpath_ <file>
# Reset the RPATH of the file.
reset_rpath_() {
local file=
for file in $@; do
local filetype=$(get_filetype $file)
case $filetype in
executable | library )
log "Reset the RPATH of '$file'"
# we need to manage the case when find command find itself:
# open: Text file busy
if [[ "$(basename "$file")" = "find" ]]; then
cp "$file" "$file.new"
"$mkPrefix"/bin/chrpath --delete "$file.new"
rm "$file"
mv "$file.new" "$file"
else
"$mkPrefix"/bin/chrpath --delete $file
fi
;;
none) ;;
*) error "Internal error"; exit 1
esac
done
}
# reset_rpath <file>
# Reset the RPATH of the file. If the argument is a directory, then all the
# contained files are resetted recursively.
reset_rpath() {
local file=
for file in $@; do
if [[ -d $file ]]; then
find "$file" -type f -print0 | while IFS= read -r -d '' file; do reset_rpath_ "$file"; done
else
reset_rpath_ "$file"
fi
done
}
# update_linker_cache
# Update the linker cache if it necessary.
update_linker_cache() {
if [[ ! "$mkTest" = "no" ]]; then
return 0
fi
if [[ -n "$mkToolchainPrefix" ]]; then
# Glibc case
if [[ -e "$mkToolchainPrefix"/sbin/ldconfig ]]; then
log "Updating the dynamic linker cache"
"$mkToolchainPrefix"/sbin/ldconfig
fi
fi
}
# error management
# =============================================================================
# die
# Called when the program is interrupted by an external signal.
die() {
local signal=$1
trap - "$signal"
message "Removing '$tmpdir'"
error "Aborting ($signal)"
rm -rf "$tmpdir" &> /dev/null || true
kill "-$signal" "$$"
}
# error_exit
# Called when an error occours (when a function has a return status different
# from 0).
error_exit() {
trap - ERR
error "Build script '$pkgScript' failed"
if [[ "$mkKeepBuildDir" = "yes" ]]; then
message "You can inspect the build directory '$tmpdir' for more information"
else
message "Removing '$tmpdir'"
rm -rf "$tmpdir"
fi
exit 1
}
# Setting the traps
for signal in TERM HUP QUIT SIGINT; do
trap "die $signal" "$signal"
done
trap 'error_exit' ERR
# default operations
# =============================================================================
# build and package functions are empty by default
build() {
return 0
}
package() {
return 0
}
# run
# This is the default implementation of run function. First it runs the build
# function, then it runs the package function. If $mkTest is set to yes then
# the package step is executed in the given directory.
run() {
# if test, then fix the dstdir
if [[ ! "$mkTest" = "no" ]]; then
mkTest=$(readlink -f "$mkTest")
message "Creating the test directory '$mkTest'"
install -dm 0700 "$mkTest"
dstdir="$mkTest"
fi
# the toolchain base dir cannot be cleared (otherwise it fails)
if [[ -d "$dstdir" && ! "$dstdir" = "$mkToolchainBase" ]]; then
rm -rf "${dstdir:?}/*"
fi
message "Building the package"
build $@
message "Installing in '$dstdir'"
package $@
update_linker_cache
}
# execute the build script
# =============================================================================
# set a sane umask
umask 0022
# turn off bahs hash function
set +h
# ERR trap is inherited by functions
set -E
# reset log file
> "$mkLogFile"
# managing verbosity
if [[ "$mkVerbose" = "yes" ]]; then
# 1 and 2 are redirected to tee, tee writes in $mkLogFile then to the real output
exec 1> >(tee -a "$mkLogFile" >&3)
exec 2> >(tee -a "$mkLogFile" >&4)
else
# 1 and 2 are redirected directly to log file
exec 1>>"$mkLogFile" 2>&1
fi
# debugging
[[ "$mkDebug" = "yes" ]] && set -x
# These variables contains the path to the common directories used for the
# build and package processes:
# srcdir The directory containing the build script and related files
# tmpdir The temporary directory where the build process acts
srcdir=$(dirname "$pkgScript")
tmpdir=$(mktmpdir)
# read the build script
message "Running the build script '$pkgScript'"
[[ -n "$pkgOptions" ]] && plain "options > $pkgOptions"
message "Running in '$tmpdir'"
source "$pkgScript"
pushd "$tmpdir"
run "$pkgOptions"
popd
# cleanup
message "Removing '$tmpdir'"
rm -rf "$tmpdir"
# exit without problems
exit 0