summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 0460b30cff3db86b6e6be49ff4c6073caaecc327 (plain)
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
#shellcheck shell=bash
# Maintainer: shadichy <shadichy@blisslabs.org>

### BUILD OPTIONS
# Set these variables to ANYTHING that is not null or choose proper variable to enable them

# Kernel branch
: "${_default_branch:=6.18}"
: "${_BRANCH:=6.18}"

# Build a debug package with non-stripped vmlinux
: "${_build_debug:=no}"

# Use android defconfig
: "${_use_android_defconfig:=no}"

# Use sccache over ccache
: "${_use_sccache:=no}"

### LLVM
TARGET_CLANG_PATH=/opt/android/clang/bin
export \
	LLVM=1 \
	CC=${TARGET_CLANG_PATH}/clang \
	LD=${TARGET_CLANG_PATH}/ld.lld \
	AR=${TARGET_CLANG_PATH}/llvm-ar \
	NM=${TARGET_CLANG_PATH}/llvm-nm \
	OBJCOPY=${TARGET_CLANG_PATH}/llvm-objcopy \
	OBJDUMP=${TARGET_CLANG_PATH}/llvm-objdump \
	READELF=${TARGET_CLANG_PATH}/llvm-readelf \
	OBJSIZE=${TARGET_CLANG_PATH}/llvm-size \
	STRIP=${TARGET_CLANG_PATH}/llvm-strip \
	HOSTCC=${TARGET_CLANG_PATH}/clang \
	HOSTCXX=${TARGET_CLANG_PATH}/clang++ \
	HOSTLD=${TARGET_CLANG_PATH}/ld.lld \
	HOSTLDFLAGS=-fuse-ld=lld \
	HOSTAR=${TARGET_CLANG_PATH}/llvm-ar \
	LIBCLANG_PATH=$(realpath ${TARGET_CLANG_PATH}/../lib)

CCACHE=$(command -v ccache)
if [ "$CCACHE" ]; then
	export \
		CC="$CCACHE $CC"
fi

SCCACHE=$(command -v sccache)
if [ "$SCCACHE" ]; then
	if [ "$_use_sccache" = "yes" ] || [ ! "$CCACHE" ]; then
		export \
			CC="$SCCACHE $CC"
	fi
fi

### Toolchain
export \
	M4="$(realpath "$(command -v m4)")" \
	BISON="$(realpath "$(command -v bison)")" \
	LEX="$(realpath "$(command -v lex)")" \
	DEPMOD="$(realpath "$(command -v depmod)")" \
	PERL="$(realpath "$(command -v perl)")"

### Rust
if [ -d "/opt/android/rust" ]; then
	: "${RUST_BIN_DIR:=/opt/android/rust/bin}"
elif [ "$(command -v rustup)" ]; then
	: "${RUST_BIN_DIR:=~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin}"
else
	: "${RUST_BIN_DIR:=/usr/bin}"
fi

export \
	RUSTC=${RUST_BIN_DIR}/rustc \
	HOSTRUSTC=${RUST_BIN_DIR}/rustc \
	RUSTFMT=${RUST_BIN_DIR}/rustfmt \
	CLIPPY=${RUST_BIN_DIR}/clippy-driver

if [ "$SCCACHE" ]; then
	# Since we can only use rust with sccache
	export \
		RUSTC="$SCCACHE $RUSTC"
fi

# Workaround for android modules
export KBUILD_MODPOST_WARN=1

OLD_PATH=$PATH
export PATH=$TARGET_CLANG_PATH:$RUST_BIN_DIR:$PATH

if [ "$_default_branch" != "$_BRANCH" ]; then
	_pkgsuffix=-$_BRANCH
fi

_kernel_name=zenith
pkgbase=linux-$_kernel_name
_pkgname="${pkgbase}${_pkgsuffix}-git"

_ver=${_BRANCH%-*}
_ver=${_BRANCH%_*}
pkgver=6.18.21
pkgdesc="Androidâ„¢ Generic Project's Zenith linux kernel - ${_BRANCH} branch"
pkgrel=1
_kernver="$pkgver-$pkgrel"
_kernuname="${pkgbase}-${_pkgsuffix}"
arch=('x86' 'x86_64')
url="https://github.com/android-generic/kernel-${_kernel_name}.git"
license=('GPL-2.0-only')
options=('!strip' '!debug' '!lto')
optdepends=(
	'modloader: for loading Android kernel modules'
)
makedepends=(
	bc
	clang-android
	cpio
	gettext
	git
	libelf
	lld
	llvm
	pahole
	perl
	python
	rust
	rust-bindgen
	rust-src
	tar
	xz
	zstd
)
makeoptdepends=(
	rust-android-bin
	ccache
	sccache
)

# Using custom download agent to shallow clone the repo
cat <<'EOF' >DLAGENTS
#!/bin/sh

PWD=$(pwd)

ORIGIN=${1#shallowclone+}
ORG_URL=${ORIGIN%%'?'*}
ORG_ARGS=${ORIGIN#*'?'}

DEST=${2}
REAL_DEST=${DEST%.part}

### Parse url parameters

arg_parser() {
  local args=$1
  shift

  IFS='&'
  set -- ${args}
  unset IFS

  BRANCH=
  COMMIT=
  TAG=
  RECURSE_SUBMODULES=
  DEPTH=1

  while [ $# -gt 0 ]; do
    case $1 in
      branch=*) BRANCH=${1#branch=} ;;
      commit=*) COMMIT=${1#commit=} ;;
      tag=*) TAG=${1#tag=} ;;
      recurse=true) RECURSE_SUBMODULES=1 ;;
      depth=*) DEPTH=${1#depth=} ;;
      *) : ;;
    esac
    shift
  done

  export BRANCH COMMIT TAG RECURSE_SUBMODULES DEPTH
}

arg_parser "${ORG_ARGS}"

update_src() {
  git fetch \
    --depth 1 \
    ${RECURSE_SUBMODULES:+'--recurse-submodules'} \
    origin "${COMMIT:-${BRANCH:-${TAG}}}"
}

### Verify if destination already exists and is a valid git repository with the correct remote URL

verify_dest() {
  local dest=$1 current_url
  [ -d "${dest}/.git" ] || return
  echo "Source dest exists, updating..."

  cd "${dest}"
  git remote set-url origin "${ORG_URL}"
  
  { # Abort any in-progress tasks
    git merge --abort ||
      git rebase --abort ||
      git cherry-pick --abort || :
  } 2>/dev/null

  # Update the existing shallow clone
  update_src
  git reset --hard FETCH_HEAD
  cd "${PWD}"

  ln -s "../${dest}" "../src/${dest}"
  echo ${dest}
  exit 0
}

verify_dest "${DEST}"
verify_dest "${REAL_DEST}"

### If not, perform a fresh shallow clone

rm -rf "${DEST}"
mkdir -p "${DEST}"

cd "${DEST}"
git init --quiet
git remote add origin "${ORG_URL}"

update_src
git reset --hard FETCH_HEAD

cd "${PWD}"

ln -s "../${REAL_DEST}" "../src/${REAL_DEST}"
echo ${REAL_DEST}
EOF
chmod +x DLAGENTS
export DLAGENTS="shallowclone::$(realpath "./DLAGENTS") %u %o"

source=(
	"${pkgbase}::shallowclone+${url}?branch=${_BRANCH}&depth=1"
	"zenith_linux-x86_64_defconfig"
)

sha256sums=(
	'SKIP'
	'159f072734e2624a395e45dace1cd2052b27eaff07a6a98d24833d0c633db9da'
)

export KBUILD_BUILD_HOST=blisslabs
export KBUILD_BUILD_USER="$pkgbase"

export BUILD_FLAGS=(
	ARCH="$CARCH"
	LLVM=1
	CC="$CC"
	LD="$LD"
	AR="$AR"
	NM="$NM"
	OBJCOPY="$OBJCOPY"
	OBJDUMP="$OBJDUMP"
	READELF="$READELF"
	OBJSIZE="$OBJSIZE"
	STRIP="$STRIP"
	HOSTCC="$HOSTCC"
	HOSTCXX="$HOSTCXX"
	HOSTLD="$HOSTLD"
	HOSTLDFLAGS="$HOSTLDFLAGS"
	HOSTAR="$HOSTAR"
	CROSS_COMPILE="$CROSS_COMPILE"
	YACC="$BISON"
	LEX="$LEX"
	M4="$M4"
	DEPMOD="$DEPMOD"
	PERL="$PERL"
	KBUILD_BUILD_HOST="$KBUILD_BUILD_HOST"
	KBUILD_BUILD_USER="$KBUILD_BUILD_USER"
	KBUILD_BUILD_TIMESTAMP="$KBUILD_BUILD_TIMESTAMP"
)

export KERNEL_TARGET=bzImage
export KERNEL_CONFIG_DIR=arch/x86/configs
export TARGET_KERNEL_CONFIG=android-${CARCH}_defconfig

KBUILD_OUTPUT="."
KBUILD_JOBS=$(nproc)
export MAKE_CMD=(
	make
	-j"$KBUILD_JOBS"
	-l"$((KBUILD_JOBS + 2))"
	-C .
	O="$KBUILD_OUTPUT"
	"${BUILD_FLAGS[@]}"
)

_die() {
	error "$@"
	exit 1
}

pkgver() {
	cd "${srcdir}/../${pkgbase}"

	# get VERSION, PATCHLEVEL and SUBLEVEL from makefile
	local ver pat sub
	read -r ver pat sub < <(head -6 Makefile | sed -nE 's/^(VERSION|PATCHLEVEL|SUBLEVEL) = ([0-9]+)$/\2/p' | xargs)

	printf '%s.%s.%s' "$ver" "$pat" "$sub"
}

prepare() {
	# SELINUX_DIFFCONFIG=$(realpath "${srcdir}/selinux_diffconfig")
	# NFTABLES_DIFFCONFIG=$(realpath "${srcdir}/nftables_diffconfig")
	# KCONFIG="${KERNEL_CONFIG_DIR}/${TARGET_KERNEL_CONFIG}"

	cd "${srcdir}/../${pkgbase}"

	git submodule update --init --recursive ||
		_die "Failed to update git submodules"

	mkdir -p "$KBUILD_OUTPUT"

	if [ "$_use_android_defconfig" = yes ]; then
		CONFIG=arch/x86/configs/android-x86_64_defconfig
	else
		CONFIG=${srcdir}/zenith_linux-x86_64_defconfig
	fi

	mv "$CONFIG" "$KBUILD_OUTPUT"/.config
	"${MAKE_CMD[@]}" olddefconfig

	### Prepared version
	"${MAKE_CMD[@]}" -s kernelrelease >"$KBUILD_OUTPUT/version"
	echo "Prepared $pkgbase version $(<"$KBUILD_OUTPUT/version")"

	echo "Setting version..."
	echo "" >"$KBUILD_OUTPUT/localversion.10-pkgrel"
	echo "" >"$KBUILD_OUTPUT/localversion.20-pkgname"

}

clean() {
	cd "${srcdir}/../${pkgbase}"
	"${MAKE_CMD[@]}" clean
	"${MAKE_CMD[@]}" mrproper
}

_sign_modules() {
	msg2 "Signing modules in $1"
	local sign_script="${pkgbase}/scripts/sign-file"
	local sign_key="$(grep -Po 'CONFIG_MODULE_SIG_KEY="\K[^"]*' "${pkgbase}/.config")"
	if [[ ! "$sign_key" =~ ^/ ]]; then
		sign_key="${pkgbase}/${sign_key}"
	fi
	local sign_cert="${pkgbase}/certs/signing_key.x509"
	local hash_algo="$(grep -Po 'CONFIG_MODULE_SIG_HASH="\K[^"]*' "${pkgbase}/.config")"

	find "$1" -type f -name '*.ko' -print -exec \
		"${sign_script}" "${hash_algo}" "${sign_key}" "${sign_cert}" '{}' \;
}

build() {
	cd "${srcdir}/../${pkgbase}"

	"${MAKE_CMD[@]}" $KERNEL_TARGET modules
}

_package() {
	pkgdesc="The $pkgdesc kernel and modules"
	depends=('coreutils' 'kmod' 'initramfs')
	optdepends=('wireless-regdb: to set the correct wireless channels of your country'
		'linux-firmware: firmware images needed for some devices'
		'modprobed-db: Keeps track of EVERY kernel module that has ever been probed - useful for those of us who make localmodconfig'
		'scx-scheds: to use sched-ext schedulers')
	provides=(VIRTUALBOX-GUEST-MODULES WIREGUARD-MODULE KSMBD-MODULE V4L2LOOPBACK-MODULE NTSYNC-MODULE VHBA-MODULE ADIOS-MODULE)

	cd "${srcdir}/../${pkgbase}"
	local modulesdir="$pkgdir/usr/lib/modules/$(<"$KBUILD_OUTPUT/version")"

	echo "Installing boot image..."
	# systemd expects to find the kernel here to allow hibernation
	# https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344
	install -Dm644 "${KBUILD_OUTPUT}/arch/$CARCH/boot/$KERNEL_TARGET" "$modulesdir/vmlinuz"

	# Used by mkinitcpio to name the kernel
	echo "$_pkgname" | install -Dm644 /dev/stdin "$modulesdir/pkgbase"

	echo "Installing modules..."
	ZSTD_CLEVEL=19 "${MAKE_CMD[@]}" INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 \
		DEPMOD=/bin/true modules_install # Suppress depmod

	# remove build links
	rm "$modulesdir"/build || :
}

_package-headers() {
	pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
	depends=('pahole' "${_pkgname}")

	cd "${srcdir}/../${pkgbase}"
	local builddir="$pkgdir/usr/lib/modules/$(<"$KBUILD_OUTPUT/version")/build"

	echo "Installing build files..."

	cd "$KBUILD_OUTPUT"
	install -Dt "$builddir" -m644 .config Makefile Module.symvers System.map \
		localversion.* version vmlinux # tools/bpf/bpftool/vmlinux.h

	cd "${srcdir}/../${pkgbase}"
	install -Dt "$builddir/kernel" -m644 tools/perf/util/bpf_skel/vmlinux/vmlinux.h
	install -Dt "$builddir/kernel" -m644 kernel/Makefile
	install -Dt "$builddir/arch/x86" -m644 arch/x86/Makefile
	cp -t "$builddir" -a scripts
	ln -srt "$builddir" "$builddir/scripts/gdb/vmlinux-gdb.py"

	# required when STACK_VALIDATION is enabled
	cd "$KBUILD_OUTPUT"
	install -Dt "$builddir/tools/objtool" tools/objtool/objtool

	# required when DEBUG_INFO_BTF_MODULES is enabled
	cd "${srcdir}/../${pkgbase}"
	if [ -f tools/bpf/resolve_btfids/resolve_btfids ]; then
		install -Dt "$builddir/tools/bpf/resolve_btfids" tools/bpf/resolve_btfids/resolve_btfids
	fi

	echo "Installing headers..."
	cp -t "$builddir" -a include
	cp -t "$builddir/arch/x86" -a arch/x86/include

	cd "$KBUILD_OUTPUT"
	install -Dt "$builddir/arch/x86/kernel" -m644 arch/x86/kernel/asm-offsets.s

	cd "${srcdir}/../${pkgbase}"
	install -Dt "$builddir/drivers/md" -m644 drivers/md/*.h
	install -Dt "$builddir/net/mac80211" -m644 net/mac80211/*.h

	# https://bugs.archlinux.org/task/13146
	install -Dt "$builddir/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h

	# https://bugs.archlinux.org/task/20402
	install -Dt "$builddir/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
	install -Dt "$builddir/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
	install -Dt "$builddir/drivers/media/tuners" -m644 drivers/media/tuners/*.h

	# https://bugs.archlinux.org/task/71392
	install -Dt "$builddir/drivers/iio/common/hid-sensors" -m644 drivers/iio/common/hid-sensors/*.h

	# KernelSU headers
	# install -Dt "$builddir/drivers/kernelsu" -m644 drivers/kernelsu/*.h

	# Selinux headers
	# install -Dt "$builddir/security/selinux" -m644 $KBUILD_OUTPUT/security/selinux/*.h
	# install -Dt "$builddir/security/selinux/include" -m644 security/selinux/include/*.h
	# install -Dt "$builddir/security/selinux/ss" -m644 security/selinux/ss/*.h

	echo "Installing KConfig files..."
	find . -name 'Kconfig*' -exec install -Dm644 {} "$builddir/{}" \;

	# Install .rmeta files if they exist
	if compgen -G "rust/*.rmeta" 1>/dev/null; then
		install -Dt "$builddir/rust" -m644 rust/*.rmeta
	fi

	# Install .so files if they exist
	if compgen -G "rust/*.so" 1>/dev/null; then
		install -Dt "$builddir/rust" rust/*.so
	fi

	echo "Installing unstripped VDSO..."
	"${MAKE_CMD[@]}" INSTALL_MOD_PATH="$pkgdir/usr" vdso_install \
		link= # Suppress build-id symlinks

	echo "Removing unneeded architectures..."
	local arch
	for arch in "$builddir"/arch/*/; do
		[[ $arch = */x86/ ]] && continue
		echo "Removing $(basename "$arch")"
		rm -r "$arch"
	done

	echo "Removing documentation..."
	rm -r "$builddir/Documentation"

	echo "Removing broken symlinks..."
	find -L "$builddir" -type l -printf 'Removing %P\n' -delete

	echo "Removing loose objects..."
	find "$builddir" -type f -name '*.o' -printf 'Removing %P\n' -delete

	echo "Stripping build tools..."
	local file
	while read -rd '' file; do
		case "$(file -Sib "$file")" in
		application/x-sharedlib\;*) # Libraries (.so)
			strip -v $STRIP_SHARED "$file" ;;
		application/x-archive\;*) # Libraries (.a)
			strip -v $STRIP_STATIC "$file" ;;
		application/x-executable\;*) # Binaries
			strip -v $STRIP_BINARIES "$file" ;;
		application/x-pie-executable\;*) # Relocatable binaries
			strip -v $STRIP_SHARED "$file" ;;
		esac
	done < <(find "$builddir" -type f -perm -u+x ! -name vmlinux -print0)

	echo "Stripping vmlinux..."
	strip -v $STRIP_STATIC "$builddir/vmlinux"

	echo "Adding symlink..."
	mkdir -p "$pkgdir/usr/src"
	ln -sr "$builddir" "$pkgdir/usr/src/$_pkgname"
}

_package-dbg() {
	pkgdesc="Non-stripped vmlinux file for the $pkgdesc kernel"
	depends=("${_pkgname}-headers")

	cd "${srcdir}/../${pkgbase}"
	mkdir -p "$pkgdir/usr/src/debug/${_pkgname}"
	install -Dt "$pkgdir/usr/src/debug/${_pkgname}" -m644 vmlinux
}

pkgname=("$_pkgname")
[ "$_build_debug" = "yes" ] && pkgname+=("$_pkgname-dbg")
pkgname+=("$_pkgname-headers")
for _p in "${pkgname[@]}"; do
	eval "package_$_p() {
    $(declare -f "_package${_p#$_pkgname}")
    _package${_p#$_pkgname}
    }"
done