# Reference: <https://postmarketos.org/vendorkernel>
# Co-Maintainer: Stefan Hansson <newbyte@postmarketos.org>
# Contributor: Clayton Craft <clayton@craftyguy.net>
maintainer="Alistair Francis <alistair@alistair23.me>"
pkgname=linux-purism-librem5
pkgver=6.6.139
pkgrel=0
# NOTE: Don't forget to rebase the config! See prepare() for instructions.
_purismrel=1
_purismver=${pkgver}pureos$_purismrel
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
	findutils
	flex
	linux-headers
	lld
	llvm
	openssl-dev
	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
	treewide-remove-unnecessary-linux-version.h-inclusion.patch
"
builddir="$srcdir/linux-pureos-$_purismver"

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="
d51709ee85ac8ebe271fc57ba60e2ab5f90225bd045b813a281553b0f76ae4c227317201fbb509e59af9813ea3e49947e83ac7d82b97cede15212cadd90fc372  linux-purism-librem5-6.6.139pureos1.tar.gz
92837e9d725d2f6e6aebfc10e79007dc671cbc57ddb10c9af91b0b666131275f0897e355c77705f2b57c4c87472f4f8264d32060765582878fde04891c49f997  config-purism-librem5.aarch64
76c1e07b5479fe7be0d1e4a74a8f4a2cd8fc25fbeb3a4efcaf8300e27817a320c0c1340ea6c00ad1c3dd86cc2cb192280de8a91360bad80665115701ba11632d  treewide-remove-unnecessary-linux-version.h-inclusion.patch
"
