forked from Joshua-Riek/ubuntu-rockchip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·244 lines (221 loc) · 6.37 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
#!/bin/bash
set -eE
trap 'echo "Error: in $0 on line $LINENO"' ERR
# Change to the script's directory only once
cd "$(dirname -- "$(readlink -f -- "$0")")"
#######################################
# Display script usage.
#######################################
usage() {
cat << HEREDOC
Usage: $0 --board=[orangepi-5] --suite=[jammy|noble] --flavor=[server|desktop]
Required arguments:
-b, --board=BOARD Target board
-s, --suite=SUITE Ubuntu suite
-f, --flavor=FLAVOR Ubuntu flavor
Optional arguments:
-h, --help Show this help message and exit
-c, --clean Clean the build directory
-ko, --kernel-only Only compile the kernel
-uo, --uboot-only Only compile u-boot
-ro, --rootfs-only Only build the root filesystem
-l, --launchpad Use kernel and u-boot from Launchpad repository
-v, --verbose Increase verbosity of the bash script
HEREDOC
}
#######################################
# Helper function to load suite, flavor, or board
# configurations and handle "help".
# Globals:
# None
# Arguments:
# 1) config_type: (suite|flavor|board)
# 2) config_value: e.g. jammy, server, orangepi-5
#######################################
load_config() {
local config_type="$1"
local config_value="$2"
local config_path=""
case "$config_type" in
suite) config_path="config/suites" ;;
flavor) config_path="config/flavors" ;;
board) config_path="config/boards" ;;
*) echo "Internal Error: Unknown config_type $config_type" >&2; exit 1 ;;
esac
if [[ "$config_value" == "help" ]]; then
for file in "$config_path"/*; do
basename "${file%.sh}"
done
exit 0
fi
if [[ -n "$config_value" ]]; then
while :; do
for file in "$config_path"/*; do
if [[ "$config_value" == "$(basename "${file%.sh}")" ]]; then
# shellcheck source=/dev/null
source "$file"
break 2
fi
done
echo "Error: \"${config_value}\" is an unsupported ${config_type}"
exit 1
done
fi
}
#######################################
# Check for root privileges.
#######################################
if [[ "$(id -u)" -ne 0 ]]; then
echo "Please run as root"
exit 1
fi
#######################################
# Parse command line arguments.
#######################################
while [[ "$#" -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-b=*|--board=*)
export BOARD="${1#*=}"
shift
;;
-b|--board)
export BOARD="$2"
shift 2
;;
-s=*|--suite=*)
export SUITE="${1#*=}"
shift
;;
-s|--suite)
export SUITE="$2"
shift 2
;;
-f=*|--flavor=*)
export FLAVOR="${1#*=}"
shift
;;
-f|--flavor)
export FLAVOR="$2"
shift 2
;;
-ko|--kernel-only)
export KERNEL_ONLY=Y
shift
;;
-uo|--uboot-only)
export UBOOT_ONLY=Y
shift
;;
-ro|--rootfs-only)
export ROOTFS_ONLY=Y
shift
;;
-l|--launchpad)
export LAUNCHPAD=Y
shift
;;
-c|--clean)
export CLEAN=Y
shift
;;
-v|--verbose)
set -x
shift
;;
-*)
echo "Error: unknown argument \"${1}\""
exit 1
;;
*)
shift
;;
esac
done
#######################################
# Load suite, flavor, and board configs
# (handles the "help" subcommands)
#######################################
load_config "suite" "${SUITE}"
load_config "flavor" "${FLAVOR}"
load_config "board" "${BOARD}"
#######################################
# Clean build directory, if requested.
#######################################
if [[ "${CLEAN}" == "Y" ]]; then
if [[ -d build/rootfs ]]; then
umount -lf build/rootfs/dev/pts 2>/dev/null || true
umount -lf build/rootfs/* 2>/dev/null || true
fi
rm -rf build
fi
#######################################
# Redirect all script output to a log file.
#######################################
mkdir -p build/logs
exec > >(tee "build/logs/build-$(date +"%Y%m%d%H%M%S").log") 2>&1
#######################################
# Early-exit scenarios for partial builds.
#######################################
if [[ "${KERNEL_ONLY}" == "Y" ]]; then
if [[ -z "${SUITE}" ]]; then
usage
exit 1
fi
./scripts/build-kernel.sh
exit 0
fi
if [[ "${ROOTFS_ONLY}" == "Y" ]]; then
if [[ -z "${SUITE}" || -z "${FLAVOR}" ]]; then
usage
exit 1
fi
./scripts/build-rootfs.sh
exit 0
fi
if [[ "${UBOOT_ONLY}" == "Y" ]]; then
if [[ -z "${BOARD}" ]]; then
usage
exit 1
fi
./scripts/build-u-boot.sh
exit 0
fi
#######################################
# Require board, suite, and flavor for full build.
#######################################
if [[ -z "${BOARD}" || -z "${SUITE}" || -z "${FLAVOR}" ]]; then
usage
exit 1
fi
#######################################
# Build Kernel if not found (and if not using Launchpad).
#######################################
if [[ "${LAUNCHPAD}" != "Y" ]]; then
# Check if we already have kernel .deb files
if [[ ! -e "$(find build/linux-image-*.deb 2>/dev/null | sort | tail -n1)" ||
! -e "$(find build/linux-headers-*.deb 2>/dev/null | sort | tail -n1)" ]]; then
./scripts/build-kernel.sh
fi
fi
#######################################
# Build U-Boot if not found (and if not using Launchpad).
#######################################
if [[ "${LAUNCHPAD}" != "Y" ]]; then
# Check if we already have the correct U-Boot .deb
if [[ ! -e "$(find build/u-boot-${BOARD}_*.deb 2>/dev/null | sort | tail -n1)" ]]; then
./scripts/build-u-boot.sh
fi
fi
#######################################
# Create the root filesystem.
#######################################
./scripts/build-rootfs.sh
#######################################
# Create the disk image.
#######################################
./scripts/config-image.sh
exit 0