-
Notifications
You must be signed in to change notification settings - Fork 35
/
build.sh
executable file
·402 lines (382 loc) · 12 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
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
#!/bin/bash
source ./dependencies.sh
[[ -z $KERNEL_VERSION ]] && KERNEL_VERSION='6.6.49'
[[ -z $BUILDROOT_VERSION ]] && BUILDROOT_VERSION='2024.02.5'
declare -ar ARCHITECTURES=("x64" "x86" "arm64")
PIPE_JOINED_ARCHITECTURES=$(IFS="|"; echo "${ARCHITECTURES[@]}"; unset IFS)
Usage() {
echo -e "Usage: $0 [-knfvh?] [-a x64]"
echo -e "\t\t-a --arch [$PIPE_JOINED_ARCHITECTURES] (optional) pick the architecture to build. Default is to build for all."
echo -e "\t\t-f --filesystem-only (optional) Build the FOG filesystem but not the kernel."
echo -e "\t\t-k --kernel-only (optional) Build the FOG kernel but not the filesystem."
echo -e "\t\t-p --path (optional) Specify a path to download and build the sources."
echo -e "\t\t-n --noconfirm (optional) Build systems without confirmation."
echo -e "\t\t-i --install-dep (optional) Attempt to install dependencies."
echo -e "\t\t-h --help -? Display this message."
exit 0
}
[[ -n $arch ]] && unset $arch
shortopts="?hkfnia:p:"
longopts="help,kernel-only,filesystem-only,noconfirm,install-dep,arch:,path:"
optargs=$(getopt -o $shortopts -l $longopts -n "$0" -- "$@")
[[ $? -ne 0 ]] && Usage
eval set -- "$optargs"
while :; do
case $1 in
-\? | -h | --help)
Usage
exit 0
;;
-k | --kernel-only)
buildKernelOnly="y"
shift
;;
-f | --filesystem-only)
buildFSOnly="y"
shift
;;
-n | --noconfirm)
confirm="n"
shift
;;
-i | --install-dep)
installDep="y"
shift
;;
-a | --arch)
arch=$2
if ! echo ${ARCHITECTURES[@]} | grep -w $arch >/dev/null; then
echo "Error: Invalid architecture specified. Valid options are: $PIPE_JOINED_ARCHITECTURES"
Usage
exit 1
fi
shift 2
;;
-p | --path)
buildPath=$2
shift 2
;;
--)
shift
break
;;
*)
echo "Error: Invalid option."
Usage
exit 1
;;
esac
done
[[ -z $arch ]] && arch="${ARCHITECTURES[@]}"
[[ -z $buildPath ]] && buildPath=$(dirname $(readlink -f $0))
[[ -z $confirm ]] && confirm="y"
[[ -z $installDep ]] && installDep="n"
checkDependencies
installDependencies $installDep
cd $buildPath || exit 1
function buildFilesystem() {
local arch="$1"
brURL="https://buildroot.org/downloads/buildroot-$BUILDROOT_VERSION.tar.xz"
echo "Preparing buildroot $BUILDROOT_VERSION on $arch build:"
if [[ ! -d fssource$arch ]]; then
if [[ ! -f buildroot-$BUILDROOT_VERSION.tar.xz ]]; then
dots "Downloading buildroot source package"
wget -q $brURL && echo "Done"
if [[ $? -ne 0 ]]; then
echo "Failed"
exit 1
fi
fi
dots "Extracting buildroot sources"
tar xJf buildroot-$BUILDROOT_VERSION.tar.xz
mv buildroot-$BUILDROOT_VERSION fssource$arch
echo "Done"
fi
cd fssource$arch
if [[ -f ../patch/filesystem/fs.patch ]]; then
dots " * Applying filesystem patch"
echo
patch -p1 < ../patch/filesystem/fs.patch
if [[ $? -ne 0 ]]; then
echo "Failed"
exit 1
fi
echo "Done"
else
echo " * WARNING: Did not find any patch file(s), building filesystem without patches!"
fi
dots "Preparing code"
if [[ ! -f .packConfDone ]]; then
cat ../Buildroot/package/newConf.in >> package/Config.in
touch .packConfDone
fi
rsync -avPrI ../Buildroot/ . > /dev/null
sed -i "s/^export initversion=[0-9][0-9]*$/export initversion=$(date +%Y%m%d)/" board/FOG/FOS/rootfs_overlay/usr/share/fog/lib/funcs.sh
if [[ ! -f .config ]]; then
cp ../configs/fs$arch.config .config
case "${arch}" in
x64)
make oldconfig
;;
x86)
make ARCH=i486 oldconfig
;;
arm64)
make ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig
;;
*)
make oldconfig
;;
esac
fi
echo "Done"
if [[ $confirm != n ]]; then
read -p "We are ready to build. Would you like to edit the config file [y|n]?" config
if [[ $config == y ]]; then
case "${arch}" in
x64)
make menuconfig
;;
x86)
make ARCH=i486 menuconfig
;;
arm64)
make ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
;;
*)
make menuconfig
;;
esac
else
echo "Ok, running make oldconfig instead to ensure the config is clean."
case "${arch}" in
x64)
make oldconfig
;;
x86)
make ARCH=i486 oldconfig
;;
arm64)
make ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig
;;
*)
make oldconfig
;;
esac
fi
read -p "We are ready to build are you [y|n]?" ready
if [[ $ready == n ]]; then
echo "Nothing to build!? Skipping."
cd ..
return
fi
fi
bash -c "while true; do echo \$(date) - building ...; sleep 30s; done" &
PING_LOOP_PID=$!
case "${arch}" in
x64)
make >buildroot$arch.log 2>&1
status=$?
;;
x86)
make ARCH=i486 >buildroot$arch.log 2>&1
status=$?
;;
arm64)
make ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- >buildroot$arch.log 2>&1
status=$?
;;
*)
make >buildroot$arch.log 2>&1
status=$?
;;
esac
kill $PING_LOOP_PID
[[ $status -gt 0 ]] && tail buildroot$arch.log && exit $status
cd ..
[[ ! -d dist ]] && mkdir dist
cd dist
case "${arch}" in
x64)
compiledfile="../fssource$arch/output/images/rootfs.ext2.xz"
initfile='init.xz'
;;
x86)
compiledfile="../fssource$arch/output/images/rootfs.ext2.xz"
initfile='init_32.xz'
;;
arm64)
compiledfile="../fssource$arch/output/images/rootfs.cpio.gz"
initfile='arm_init.cpio.gz'
;;
esac
[[ ! -f $compiledfile ]] && echo 'File not found.' || cp $compiledfile $initfile && sha256sum $initfile > ${initfile}.sha256
cd ..
}
function buildKernel() {
local arch="$1"
kernelURL="https://www.kernel.org/pub/linux/kernel/v${KERNEL_VERSION:0:1}.x/linux-$KERNEL_VERSION.tar.xz"
echo "Preparing kernel $KERNEL_VERSION on $arch build:"
[[ -d kernelsource$arch ]] && rm -rf kernelsource$arch
if [[ ! -f linux-$KERNEL_VERSION.tar.xz ]]; then
dots "Downloading kernel source"
wget -q $kernelURL && echo "Done"
if [[ $? -ne 0 ]]; then
echo "Failed"
exit 1
fi
fi
dots "Extracting kernel source"
tar xJf linux-$KERNEL_VERSION.tar.xz
mv linux-$KERNEL_VERSION kernelsource$arch
echo "Done"
if [[ ! -d linux-firmware ]]; then
dots "Cloning Linux firmware repository"
git clone git://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git >/dev/null 2>&1
echo "Done"
else
dots "Updating Linux firmware repository"
cd linux-firmware
git pull --rebase >/dev/null 2>&1
cd ..
echo "Done"
fi
dots "Copying firmware files"
cp -r linux-firmware kernelsource$arch/
echo "Done"
dots "Preparing kernel source"
cd kernelsource$arch
make mrproper
cp ../configs/kernel$arch.config .config
echo "Done"
if [[ -f ../patch/kernel/linux.patch ]]; then
dots " * Applying patch"
echo
patch -p1 < ../patch/kernel/linux.patch
if [[ $? -ne 0 ]]; then
echo "Failed"
exit 1
fi
else
echo " * WARNING: Did not find a patch file building vanilla kernel without patches!"
fi
if [[ $confirm != n ]]; then
read -p "We are ready to build. Would you like to edit the config file [y|n]?" config
if [[ $config == y ]]; then
case "${arch}" in
x64)
make menuconfig
;;
x86)
make ARCH=i386 menuconfig
;;
arm64)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
;;
*)
make menuconfig
;;
esac
else
echo "Ok, running make oldconfig instead to ensure the config is clean."
case "${arch}" in
x64)
make oldconfig
;;
x86)
make ARCH=i386 oldconfig
;;
arm64)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig
;;
*)
make oldconfig
;;
esac
fi
read -p "We are ready to build are you [y|n]?" ready
if [[ $ready == y ]]; then
echo "This make take a long time. Get some coffee, you'll be here a while!"
case "${arch}" in
x64)
make -j $(nproc) bzImage
status=$?
;;
x86)
make ARCH=i386 -j $(nproc) bzImage
status=$?
;;
arm64)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j $(nproc) Image
status=$?
;;
*)
make -j $(nproc) bzImage
status=$?
;;
esac
else
echo "Nothing to build!? Skipping."
cd ..
return
fi
[[ $status -gt 0 ]] && exit $status
else
case "${arch}" in
x64)
make oldconfig
make -j $(nproc) bzImage
status=$?
;;
x86)
make ARCH=i386 oldconfig
make ARCH=i386 -j $(nproc) bzImage
status=$?
;;
arm64)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j $(nproc) Image
status=$?
;;
*)
make oldconfig
make -j $(nproc) bzImage
status=$?
;;
esac
fi
[[ $status -gt 0 ]] && exit $status
cd ..
mkdir -p dist
cd dist
case "$arch" in
x64)
compiledfile="../kernelsource$arch/arch/x86/boot/bzImage"
kernelfile='bzImage'
;;
x86)
compiledfile="../kernelsource$arch/arch/x86/boot/bzImage"
kernelfile='bzImage32'
;;
arm64)
compiledfile="../kernelsource$arch/arch/$arch/boot/Image"
kernelfile='arm_Image'
;;
esac
[[ ! -f $compiledfile ]] && echo 'File not found.' || cp $compiledfile $kernelfile && sha256sum $kernelfile > ${kernelfile}.sha256
cd ..
}
dots() {
local pad=$(printf "%0.1s" "."{1..60})
printf " * %s%*.*s" "$1" 0 $((60-${#1})) "$pad"
return 0
}
for buildArch in $arch
do
if [[ -z $buildKernelOnly ]]; then
buildFilesystem $buildArch
fi
if [[ -z $buildFSOnly ]]; then
buildKernel $buildArch
fi
done