This repository was archived by the owner on Feb 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild
More file actions
executable file
·422 lines (357 loc) · 9.4 KB
/
build
File metadata and controls
executable file
·422 lines (357 loc) · 9.4 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
#!/bin/bash
bindmount() {
mount --bind $1 $2
}
unmount() {
while true; do
mountpoint -q $1 || break
umount $1 2>/dev/null
done
}
# Download packages from the source link
fetch_source() {
filename=$(basename $1)
url=$1
wgetcmd="wget --passive-ftp --tries=3 --waitretry=3 --output-document=$SRCDIR/$filename.part"
if [ ! -f $SRCDIR/$filename ]; then
[[ -f $SRCDIR/$filename.part ]] && resume="-c"
$wgetcmd $resume $url && \
mv $SRCDIR/$filename.part $SRCDIR/$filename
fi
}
extract() {
rm -fr "$BUILDDIR/$name"
mkdir -p "$BUILDDIR/$name"
for src in $source; do
filename=$(basename $src)
case $src in
*.tar|*.tar.gz|*.tar.Z|*.tgz|*.tar.bz2|*.tbz2|*.tar.xz|*.txz|*.tar.lzma|*.zip)
msg "extracting $filename..."
tar -xf "$SRCDIR/$filename" -C "$BUILDDIR/$name" || {
msgerr "failed extracting $filename"
exit 1
};;
esac
done
}
ports() {
[ "$1" ] || return 1
[ -f "$ROOTFS/var/lib/scratchpkg/index/$1/.pkginfo" ] && return 0
mkdir -p $ROOTFS/var/lib/scratchpkg/index
[ -f "$PORTSDIR/$1/spkgbuild" ] || {
msgerr "Ports '$1' not exist."
return 1
}
cd $PORTSDIR/$1
settermtitle "Building '$1'..."
pkgbuild --config=$PKGBUILDCONF --root=$ROOTFS -i --no-preinstall --no-postinstall || {
settermtitle "Building '$1' failed."
exit 1
}
cd - >/dev/null
}
# build cross_compiler from source
cross_compiler() {
[ "$1" ] || return 1
[ -f "$TCDIR/$1" ] && return 0
source $TCSCRIPTDIR/$1
if [ "$source" ]; then
for src in $source; do
fetch_source $src
done
fi
extract || {
msgerr "Failed to extract '$(basename $source)'."
return 1
}
cd "$BUILDDIR/$name"
if [ "$(type -t build)" = "function" ]; then
settermtitle "Building '$1'..."
(set -e -x; build 2>&1)
if [ "$?" != 0 ]; then
settermtitle "Building '$1' failed"
msgerr "'build' failed"
exit 1
else
settermtitle "Building '$1' completed"
msg "Cross toolchain build for '$name-$version' completed."
touch "$TCDIR/$1"
rm -fr "$BUILDDIR/$name"
fi
fi
cd - >/dev/null
}
settermtitle() {
echo -en "\033]0;$*\a"
}
msg() {
echo "=> $*"
}
msg2() {
echo "-> $*"
}
msgerr() {
echo "!> $*"
}
setup_pkgbuildconf() {
cat << EOF > pkgbuild.conf
export HOST="$HOST"
export TARGET="$TARGET"
export CARCH="$CARCH"
export CC="$TARGET-gcc"
export CXX="$TARGET-g++"
export AR="$TARGET-ar"
export AS="$TARGET-as"
export RANLIB="$TARGET-ranlib"
export LD="$TARGET-ld"
export STRIP="$TARGET-strip"
export TCDIR="$TCDIR"
export PATH=$PATH
export ROOTFS="$ROOTFS"
export PKG_CONFIG_PATH="$ROOTFS/usr/lib/pkgconfig:$ROOTFS/usr/share/pkgconfig"
export PKG_CONFIG_SYSROOT_DIR="$ROOTFS"
BOOTSTRAP=1
MAKEFLAGS="$MAKEFLAGS"
CFLAGS="$CFLAGS"
CXXFLAGS="$CXXFLAGS"
SOURCE_DIR="$SRCDIR"
PACKAGE_DIR="$PKGDIR"
WORK_DIR="$BUILDDIR"
EOF
}
# will create livecd and iso
build_livecd() {
msg "Building liveiso..."
set -e
make_liveworkdir
make_initramfs
make_squashfs
make_efiboot
make_iso
cleaning
set +e
}
# building base package from scratch using scratchpkg
build_base() {
for b in $base_pkg; do
case $b in
cross-*) cross_compiler $b || exit $?;;
*) ports $b || exit $?;;
esac
done
}
chroot_run() {
xchroot $ROOTFS $@
return $?
}
# create efiboot
make_efiboot() {
msg "Setup efiboot..."
mkdir -p "$LIVEWDIR"/boot/{grub/{fonts,x86_64-efi},EFI}
if [ -f /usr/share/grub/unicode.pf2 ];then
cp /usr/share/grub/unicode.pf2 "$LIVEWDIR"/boot/grub/fonts
fi
if [ -f "$FILESDIR"/isolinux/splash.png ]; then
cp "$FILESDIR"/isolinux/splash.png "$LIVEWDIR"/boot/grub/
fi
echo "set prefix=/boot/grub" > "$LIVEWDIR"/boot/grub-early.cfg
cp -a /usr/lib/grub/x86_64-efi/*.{mod,lst} "$LIVEWDIR"/boot/grub/x86_64-efi
cp "$FILESDIR"/grub.cfg "$LIVEWDIR"/boot/grub/
grub-mkimage \
-c "$LIVEWDIR"/boot/grub-early.cfg \
-o "$LIVEWDIR"/boot/EFI/bootx64.efi \
-O x86_64-efi \
-p "" iso9660 normal search search_fs_file
modprobe loop
dd if=/dev/zero of="$LIVEWDIR"/boot/efiboot.img count=4096
mkdosfs -n KOOMPILIVE-UEFI "$LIVEWDIR"/boot/efiboot.img
mkdir -p "$EFIBOOTDIR"
mount -o loop "$LIVEWDIR"/boot/efiboot.img "$EFIBOOTDIR"
mkdir -p "$EFIBOOTDIR"/EFI/boot
cp "$LIVEWDIR"/boot/EFI/bootx64.efi "$EFIBOOTDIR"/EFI/boot
unmount "$EFIBOOTDIR"
rm -fr "$EFIBOOTDIR"
}
make_iso() {
msg "Making iso..."
rm -f $OUTPUT
xorriso -as mkisofs \
-isohybrid-mbr "$FILESDIR"/isohdpfx.bin \
-c isolinux/boot.cat \
-b isolinux/isolinux.bin \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
-eltorito-alt-boot \
-e boot/efiboot.img \
-no-emul-boot \
-isohybrid-gpt-basdat \
-volid $ISOLABEL \
-o $OUTPUT $LIVEWDIR
}
make_squashfs() {
msg "Squashing filesystem..."
mksquashfs $ROOTFS "$LIVEWDIR"/koompi/rootfs.sfs \
-b 1048576 -comp zstd \
-e $ROOTFS/var/cache/scratchpkg/sources/* \
-e $ROOTFS/var/cache/scratchpkg/packages/* \
-e $ROOTFS/var/cache/scratchpkg/work/* \
-e $ROOTFS/tmp/* 2>/dev/null
}
make_initramfs() {
msg "Preparing initramfs..."
cp $ROOTFS/boot/vmlinuz-koompi "$LIVEWDIR"/boot/vmlinuz
sed "s/@ISOLABEL@/$ISOLABEL/g" "$FILESDIR"/livecd.hook > $ROOTFS/usr/share/mkinitramfs/hooks/livecd.hook
kernver=$(file $ROOTFS/boot/vmlinuz-koompi | cut -d ' ' -f9)
chroot_run mkinitramfs -k $kernver -a livecd -o /boot/initrd-koompi.img
mv $ROOTFS/boot/initrd-koompi.img "$LIVEWDIR"/boot/initrd
}
make_liveworkdir() {
isolinux_files="chain.c32 isolinux.bin ldlinux.c32 libutil.c32 reboot.c32 menu.c32 libcom32.c32 poweroff.c32"
msg "Preparing iso workdir..."
rm -fr "$LIVEWDIR"
mkdir -p "$LIVEWDIR"/{koompi,isolinux,boot}
for file in $isolinux_files; do
cp "$FILESDIR"/$file "$LIVEWDIR"/isolinux
done
cp "$FILESDIR"/isolinux.cfg "$LIVEWDIR"/isolinux
[ -d liverootfs ] && cp -Ra liverootfs "$LIVEWDIR"
}
cleaning() {
msg "Cleaning workdir..."
rm -fr "$LIVEWDIR"
}
build_rootfs() {
rm -f "$BUILDROOT"/koompilinux-rootfs.tar.xz
pushd "$ROOTFS" >/dev/null
msg "Building rootfs image..."
tar -cvJpf "$BUILDROOT"/koompilinux-rootfs.tar.xz *
popd >/dev/null
}
chrootbuild() {
builderror=0
PKG="$(echo $PKG | tr ',' ' ')"
mount_all
chroot_run scratch install $PKG -y || builderror=1
unmount_all
return $builderror
}
checkroot() {
[ "$(id -u)" = 0 ] || {
msg "Need root access!"
exit 1
}
}
umount_any_mounted() {
for m in $(findmnt --list | grep $ROOTFS | awk '{print $1}' | sort | tac); do
unmount $m
done
}
# mount all ports
mount_all() {
umount_any_mounted
mkdir -p $ROOTFS/usr/ports/core
bindmount $PORTSDIR $ROOTFS/usr/ports/core
bindmount $SRCDIR $ROOTFS/var/cache/scratchpkg/sources
bindmount $PKGDIR $ROOTFS/var/cache/scratchpkg/packages
}
# unmount it after finish
unmount_all() {
unmount $ROOTFS/var/cache/scratchpkg/packages
unmount $ROOTFS/var/cache/scratchpkg/sources
unmount $ROOTFS/usr/ports/core
}
rebase_rootfs() {
rebaseerror=0
mount_all
chroot_run xbase || rebaseerror=1
unmount_all
return $rebaseerror
}
enter_chroot() {
exitstatus=0
mount_all
chroot_run /bin/sh || exitstatus=1
unmount_all
return $exitstatus
}
sysup_rootfs() {
exitstatus=0
mount_all
chroot_run scratch upgrade scratchpkg -y || exitstatus=1
chroot_run scratch sysup -y || exitstatus=1
unmount_all
return $exitstatus
}
# argument
parser() {
while [ "$1" ]; do
case $1 in
-jobs=*) JOBS=${1#*=};;
-root=*) ROOTFS=${1#*=};;
-pkgdir=*) PKGDIR=${1#*=};;
-srcdir=*) SRCDIR=${1#*=};;
-pkgbuildconf=*) PKGBUILDCONF=${1#*=};;
-iso) LIVE=1;;
-rootfs) RFS=1;;
-pkg=*) PKG=${1#*=};;
-rebase) REBASE=1;;
-chroot) CHROOT=1;;
-umount) UMOUNT=1;;
-sysup) SYSUP=1;;
*) msgerr "invalid options: $1"; exit 1;;
esac
shift
done
}
main() {
checkroot
mkdir -p "$ROOTFS" "$BUILDDIR" "$SRCDIR" "$PKGDIR" "$TCDIR"
#umount_any_mounted
setup_pkgbuildconf
build_base
[ "$UMOUNT" = 1 ] && umount_any_mounted
[ "$CHROOT" = 1 ] && enter_chroot
[ "$SYSUP" = 1 ] && sysup_rootfs
[ "$REBASE" = 1 ] && rebase_rootfs
[ "$RFS" = 1 ] && build_rootfs
[ "$PKG" ] && {
chrootbuild || exit 1
}
[ "$LIVE" = 1 ] && build_livecd
}
parser "$@"
BUILDROOT=$(pwd)
TCDIR="$BUILDROOT/toolchain"
SRCDIR="${SRCDIR:-$BUILDROOT/sources}"
PKGDIR="${PKGDIR:-$BUILDROOT/packages}"
PORTSDIR="$BUILDROOT/ports"
BUILDDIR="$BUILDROOT/work"
ROOTFS="${ROOTFS:-$BUILDROOT/rootfs}"
TCSCRIPTDIR="$BUILDROOT/tcscripts"
PKGBUILDCONF="${PKGBUILDCONF:-$BUILDROOT/pkgbuild.conf}"
JOBS="${JOBS:-$(nproc)}"
# live iso
ISOLABEL="KOOMPILIVE"
OUTPUT="koompilinux-$(date +"%Y%m%d").iso"
LIVEWDIR="$BUILDDIR/live"
FILESDIR="$BUILDROOT/files"
EFIBOOTDIR="$BUILDDIR/efiboot"
export PATH="$TCDIR/bin:$PATH"
export CARCH="$(uname -m)"
export TARGET="$CARCH-linux-musl"
export HOST="$(echo ${MACHTYPE} | sed "s/-[^-]*/-cross/")"
export MAKEFLAGS="-j$JOBS"
export CFLAGS="-O2 -march=x86-64 -pipe"
export CXXFLAGS="${CFLAGS}"
base_pkg="cross-scratchpkg linux-headers cross-binutils cross-gcc-pass1 cross-musl cross-gcc-pass2
filesystem man-pages musl tzdata zlib bzip2 xz file ncurses readline m4 bc binutils gmp mpfr libmpc attr acl shadow
gcc pkgconf libcap sed psmisc iana-etc bison flex grep bash libtool gdbm gperf expat inetutils perl
autoconf automake kmod gettext libuargp musl-fts elfutils libressl coreutils diffutils gawk findutils
less gzip libmnl iproute2 kbd libpipeline make patch man-db tar texinfo vim procps-ng pcre2 util-linux e2fsprogs
sysklogd eudev dhcpcd libarchive curl httpup scratchpkg runit runit-bootscripts ca-certificates base"
[ "$LIVE" = 1 ] && {
base_pkg="$base_pkg linux-firmware fuse2 grub mkinitramfs python3 linux efivar popt efibootmgr grub-efi links nnn lzo lz4 zstd squashfs-tools"
}
main
exit 0