# Reference: <https://postmarketos.org/vendorkernel>
# Co-Maintainer: Stefan Hansson <newbyte@postmarketos.org>
maintainer="Alistair Francis <alistair@alistair23.me>"
pkgname=linux-purism-librem5
pkgver=6.12.94
pkgrel=0
# NOTE: Don't forget to rebase the config! See prepare() for instructions.
_purismrel=1
_purismver=${pkgver}pureos$_purismrel
_commit=db350fb91e60d12fde6bc7b1ece03836b215e444
pkgdesc="Purism Librem 5 phone kernel fork"
arch="aarch64"
_carch="arm64"
_flavor="purism-librem5"
url="https://source.puri.sm/Librem5/linux"
license="GPL-2.0-only"
options="!strip !check !tracedeps
	pmb:cross-native
	pmb:kconfigcheck-community
	pmb:kconfigcheck-librem5
	"
makedepends="
	bison
	clang
	elfutils-dev
	findutils
	flex
	linux-headers
	lld
	llvm
	openssl-dev
	pahole
	perl
	postmarketos-installkernel
	python3
	zstd
	"
install="$pkgname.post-upgrade"

# kconfig generated with: ARCH=arm64 make defconfig KBUILD_DEFCONFIG=librem5_defconfig
_config="config-$_flavor.$arch"


source="
	$pkgname-$_purismver.tar.gz::https://source.puri.sm/Librem5/linux/-/archive/pureos/$_purismver/linux-pureos$_purismver.tar.gz
	$_config
"
builddir="$srcdir/linux-pureos-$_purismver-$_commit"

prepare() {
	default_prepare
	cp "$srcdir/config-$_flavor.$arch" .config

	####### Rebase config
	# The kernel config needs to be periodically rebased to enable new
	# modules/features that Purism has enabled in their defconfig. This is done
	# by: 1) generate .config using librem5_defconfig, 2) diff that .config
	# with the config in this package, 3) carefully going over the diff and
	# selecting new things enabled in their config.
	#
	# This can be uncommented to help generate the defconfig using "pmbootstrap
	# build linux-purism-librem5". The build will fail (exit 1) to let you grab
	# the config before it starts building the kernel:
	#    make ARCH="$_carch" LLVM=1 \
	#       defconfig KBUILD_DEFCONFIG=librem5_defconfig
	#    exit 1
	#
	# After that run the following
	#    cp <chroot_native/home/pmos/build/src/linux-pureos-6.6.83pureos2_cri1/.config> \
	#       device/community/linux-purism-librem5/config-purism-librem5.aarch64
	#    pmbootstrap kconfig edit linux-purism-librem5
	#    pmbootstrap kconfig check linux-purism-librem5
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" LLVM=1 \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-$_flavor"
}

package() {
	make dtbs_install install modules_install \
		ARCH="$_carch" \
		LLVM=1 \
		INSTALL_PATH="$pkgdir"/boot \
		INSTALL_MOD_PATH="$pkgdir"/usr \
		INSTALL_MOD_STRIP=1 \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs

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

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


}

sha512sums="
94050892d45a94c9e518d380e16523ada1b4af6c321cfccd42255297b06ad891eb6b3fe30235b59cd7f45401b7d7d50a11eb93d130c2f4bba38af3bd85c92d7e  linux-purism-librem5-6.12.94pureos1.tar.gz
29d63830ef8ff73f5d76615622580a019f48e8a3f4a8a404721020ae4ff3efd8b230f61b6e9ce124134164dfe28b93a85052f2b841466d244013d0d1e02e8a24  config-purism-librem5.aarch64
"
