# Maintainer: Casey Connolly <kcxt@postmarketos.org>
# Kernel config based on: arch/arm64/configs/defconfig

pkgname=linux-next
pkgver=6.16_git20250723
pkgrel=0
_nextver="${pkgver#*_git}"
pkgdesc="Linux next kernel"
arch="armv7 aarch64"
case "$CARCH" in
	aarch64) _carch="arm64";_defconfig="defconfig";;
	# FIXME: We should not be using qcom_defconfig. Instead we should be using multi_v7_defconfig.
	# The problem is two-fold:
	# - We need to reduce the amount of mainline kernels.
	# - We need to find a way to not duplicate upstream defconfigs into our repo.
	# Please see: https://gitlab.postmarketos.org/postmarketOS/pmaports/-/merge_requests/6133#note_468011
	armv7) _carch="arm";_defconfig="qcom_defconfig";;
esac
_flavor="generic-trailblazer"
url="https://kernel.org"
license="GPL-2.0-only"
# FIXME: kconfigcheck isn't supported since we use fragments
options="!strip !check !tracedeps pmb:cross-native"
source="
	https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/snapshot/linux-next-next-$_nextver.tar.gz
	devices.config
	pmos.config
"
makedepends="
	bash
	bc
	bison
	clang
	clang-libclang
	devicepkg-dev
	elfutils-dev
	findutils
	flex
	git
	lld
	llvm
	openssl-dev
	perl
	postmarketos-installkernel
	python3
	rust
	rust-bindgen
	rust-src
	rustfmt
	zstd
"
builddir="$srcdir/linux-next-next-$_nextver"

prepare() {
	default_prepare
	cp "$srcdir/pmos.config" "$builddir"/arch/"$_carch"/configs/
	cp "$srcdir/devices.config" "$builddir"/arch/"$_carch"/configs/

	# Generate .config
	unset LDFLAGS
	make LLVM=1 ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-next-$_nextver" \
		"$_defconfig" pmos.config devices.config
}

build() {
	unset LDFLAGS
	make LLVM=1 O="$_outdir" ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS"
}

package() {
	if [ "$CARCH" != "armv7" ]; then
		install -Dm644 "$builddir/arch/$_carch/boot/vmlinuz.efi" \
			"$pkgdir/boot/linux.efi"
	fi

	mkdir -p "$pkgdir"/boot

	make LLVM=1 zinstall modules_install dtbs_install \
		ARCH="$_carch" \
		INSTALL_PATH="$pkgdir"/boot/ \
		INSTALL_MOD_PATH="$pkgdir" \
		INSTALL_MOD_STRIP=1 \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs

	rm -f "$pkgdir"/lib/modules/*/build "$pkgdir"/lib/modules/*/source

	install -D "$builddir"/include/config/kernel.release \
		"$pkgdir"/usr/share/kernel/next-"$_nextver"/kernel.release
}

sha512sums="
d0092f5b5ee2c931258710470c85a75ce1e0bbec4af6c25637aabdebf724e7b7c7d893c9dad6c2eea5b84f5e673860385ba4d97071f395d49534519ff210082f  linux-next-next-20250723.tar.gz
def9df5035ad91aab0032acacc50456bdbe39daafa4a1a0b4fb699ae9e89b9c16bf40bb6043b52fad0b6d1755729b332209f24de4d75233f167cf584edc6e9ba  devices.config
dc83b61179ab579528d191aacd36c416ba821dd7d0577234724ebdbc78cb157363892933e784a2d9eaa2c08e31f94b84be9694ec57b7812c6c2a862d90dfd113  pmos.config
"
