This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build_env.sh
executable file
·149 lines (119 loc) · 3.52 KB
/
build_env.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
#!/usr/bin/env bash
##################################################
# Check if the build environment is already loaded
##################################################
if [ "$BUILD_ENV_LOADED" = "true" ]; then
return 0
fi
# exit if a command fails
set -e
###########
# Variables
###########
# the variables are stored in an .env file that is created by 0_prepare.sh
if [ ! -f ".env" ]; then
echo "Please run 0_prepare.sh first!" >&2
return 1
fi
# source .env file
. .env
###########
# Functions
###########
# this function should be called when entering the environment
function _enter() {
pushd "$BASE_DIR"
# mark the entering process as complete
export BUILD_ENV_ENTERED="true"
}
# this function should be called when leaving the environment
function _leave() {
# remove the temporary directory
rm -rf "$TMP_DIR"
# the following steps should only be taken if _enter was called before
if [ "$BUILD_ENV_ENTERED" = "true" ]; then
# equivalent to the pushd in the _enter function
popd
unset BUILD_ENV_ENTERED
fi
}
# try to call the 'build' function
function _build() {
_enter
declare -f -F "build" > /dev/null && build
# collect all relevant artifacts, that were produced during the build process
declare -f -F "collect_artifacts" > /dev/null && collect_artifacts
_leave
}
# try to call the 'clean' function
function _clean() {
_enter
declare -f -F "clean" > /dev/null && clean
_leave
}
function apply_patches() {
for patch_file in "$1"/*.patch; do
[ -f "$patch_file" ] || break
echo "Applying patch $patch_file"
# check if it's a git patch or not
if grep -q -- "--git" "$patch_file"; then
# ignore a or b path prefix in the patch file
out=$(patch -N -d "$2" -p1 < "$patch_file") || echo "${out}" | grep "Skipping patch" -q || (echo "$out" && false)
else
out=$(patch -N -d "$2" < "$patch_file") || echo "${out}" | grep "Skipping patch" -q || (echo "$out" && false)
fi
done
}
function get_loopdev() {
if [ ! -f "$1" ]; then
echo "No image specified!" 1>&2
return 1
fi
# create the loop device for editing the image
loop_dev=$(sudo losetup -Pf --show "$1")
# workaround if losetup can't create 'p1' and 'p2' nods
# drop the first line, as this is our LOOPDEV itself, but we only want the child partitions
loop_partitions=$(lsblk --raw --output "MAJ:MIN" --noheadings ${loop_dev} | tail -n +2)
loop_partitions_counter=1
for i in $loop_partitions; do
maj=$(echo $i | cut -d: -f1)
min=$(echo $i | cut -d: -f2)
if [ ! -e "${loop_dev}p${loop_partitions_counter}" ]; then
sudo mknod ${loop_dev}p${loop_partitions_counter} b $maj $min
fi
loop_partitions_counter=$((loop_partitions_counter + 1))
done
# return the loop device
echo "$loop_dev"
}
# override of the pusd and popd functions to suppress output
function pushd() {
command pushd "$@" > /dev/null
}
function popd() {
command popd "$@" > /dev/null
}
# this is the entering point for each script
function entry_point() {
case "$1" in
"build")
_build
;;
"clean")
_clean
;;
*)
_build
;;
esac
}
# override of the exit function
function exit() {
# leave the environment before exiting
_leave
command exit "$@"
}
######################################
# mark the build environment as loaded
######################################
BUILD_ENV_LOADED="true"