# Template file for 'rpi-kernel'
#
# We track the latest Raspberry Pi LTS kernel as that is what is used in the
# official Raspberry Pi OS distribution. This is currently 5.15:
#
# https://forums.raspberrypi.com/viewtopic.php?t=322879
#
# Commit hash is picked from latest tag [1], if appropriate, or from latest
# "Merge remote-tracking branch 'stable/linux-5.x.y' into rpi-5.x.y" commit.
#
# [1] https://github.com/raspberrypi/linux/tags
#
# Upstream documentation: https://www.raspberrypi.com/documentation/computers/linux_kernel.html

pkgname=rpi-kernel
version=5.15.72
revision=1
_githash="3b98eb7a4aeaecd5274108dc1be7a5df94253500"
archs="armv6l* armv7l* aarch64*"
hostmakedepends="perl kmod uboot-mkimage openssl-devel bc bison flex xz"
makedepends="ncurses-devel"
maintainer="Piraty <mail@piraty.dev>"
homepage="http://www.kernel.org"
license="GPL-2.0-only"
short_desc="Linux kernel for Raspberry Pi (${version%.*} series [git ${_githash:0:7}])"
distfiles="https://github.com/raspberrypi/linux/archive/${_githash}.tar.gz"
checksum=5cad1fd22f309ddd7b996df748aa21ced72f88d47fdbb8d4bfd9ef3a8a9b6ce9
python_version=3

_kernver="${version}_${revision}"

nodebug=yes
nostrip=yes
noverifyrdeps=yes
noshlibprovides=yes

triggers="kernel-hooks"
# These files could be modified when an external module is built.
mutable_files="
 /usr/lib/modules/${_kernver}/modules.dep
 /usr/lib/modules/${_kernver}/modules.dep.bin
 /usr/lib/modules/${_kernver}/modules.symbols
 /usr/lib/modules/${_kernver}/modules.symbols.bin
 /usr/lib/modules/${_kernver}/modules.alias
 /usr/lib/modules/${_kernver}/modules.alias.bin
 /usr/lib/modules/${_kernver}/modules.devname"

_arch=
_image_target=
_image_name=
case "$XBPS_TARGET_MACHINE" in
	armv6l*) _arch=arm; _image_target=zImage; _image_name=kernel.img ;;
	armv7l*) _arch=arm; _image_target=zImage; _image_name=kernel7.img ;;
	aarch64*) _arch=arm64; _image_target=Image.gz; _image_name=kernel8.img ;;
esac

_cross=
if [ "$CROSS_BUILD" ]; then
	_cross="CROSS_COMPILE=${XBPS_CROSS_TRIPLET}-"
fi

do_configure() {
	local target defconfig

	case "$XBPS_TARGET_MACHINE" in
		# for zero, 1
		armv6l*) target=bcmrpi_defconfig ;;
		# for 2
		armv7l*) target=bcm2709_defconfig ;;
		# for 3, 4, zero 2
		aarch64*) target=bcm2711_defconfig ;;
	esac

	defconfig="arch/${_arch}/configs/${target}"
	echo "CONFIG_CONNECTOR=y" >> "$defconfig"
	echo "CONFIG_PROC_EVENTS=y" >> "$defconfig"

	# HID Controllers
	echo "CONFIG_HID_STEAM=y" >> "$defconfig"

	# Disable GCC plugins
	echo "# CONFIG_GCC_PLUGINS is not set" >> "$defconfig"

	# LXD 4.2+ support
	echo "CONFIG_BRIDGE_VLAN_FILTERING=y" >> "$defconfig"

	make ${makejobs} ${_cross} ARCH=${_arch} ${target}

	# Always use our revision to CONFIG_LOCALVERSION to match our pkg version.
	vsed -i -e "s|^\(CONFIG_LOCALVERSION=\).*|\1\"_${revision}\"|" .config
}

do_build() {
	make ${makejobs} ${_cross} ARCH=${_arch} prepare
	make ${makejobs} ${_cross} ARCH=${_arch} ${_image_target} modules dtbs
}

do_install() {
	local hdrdest="${DESTDIR}/usr/src/${sourcepkg}-headers-${_kernver}"

	# Run depmod after compressing modules.
	vsed -i -e '2iexit 0' scripts/depmod.sh

	# Install kernel, firmware and modules
	make ${makejobs} ARCH=${_arch} INSTALL_MOD_PATH=${DESTDIR} modules_install

	# Install device tree blobs
	make ${makejobs} ARCH=${_arch} INSTALL_DTBS_PATH=${DESTDIR}/boot dtbs_install

	# move dtbs that ended up in /boot/broadcom
	if [ -d "${DESTDIR}/boot/broadcom" ]; then
		mv ${DESTDIR}/boot/broadcom/*dtb ${DESTDIR}/boot
	fi

	# Install kernel image
	vinstall arch/${_arch}/boot/${_image_target} 644 boot ${_image_name}

	# Switch to /usr.
	vmkdir usr
	mv ${DESTDIR}/lib ${DESTDIR}/usr

	cd ${DESTDIR}/usr/lib/modules/${_kernver}
	rm -f source build
	ln -sf ../../../src/${sourcepkg}-headers-${_kernver} build

	cd ${wrksrc}
	# Install required headers to build external modules
	install -Dm644 Makefile ${hdrdest}/Makefile
	install -Dm644 kernel/Makefile ${hdrdest}/kernel/Makefile
	install -Dm644 .config ${hdrdest}/.config
	for file in $(find . -name Kconfig\*); do
		mkdir -p ${hdrdest}/$(dirname $file)
		install -Dm644 $file ${hdrdest}/${file}
	done
	for file in $(find arch/${_arch} scripts -name module.lds -o -name Kbuild.platforms -o -name Platform); do
		mkdir -p ${hdrdest}/$(dirname $file)
		install -Dm644 $file ${hdrdest}/${file}
	done
	mkdir -p ${hdrdest}/include

	# Remove firmware stuff provided by the "linux-firmware" pkg.
	rm -rf ${DESTDIR}/usr/lib/firmware

	for i in acpi asm-generic clocksource config crypto drm dt-bindings generated linux \
		math-emu media net pcmcia scsi sound trace uapi vdso video xen; do
		[ -d include/$i ] && cp -a include/$i ${hdrdest}/include
	done

	cd ${wrksrc}
	# Remove helper binaries built for host,
	# if generated files from the scripts/ directory need to be included,
	# they need to be copied to ${hdrdest} before this step
	if [ "$CROSS_BUILD" ]; then
		make ${makejobs} ARCH=${_arch} _mrproper_scripts
		# remove host specific objects as well
		find scripts -name '*.o' -delete
	fi

	# Copy files necessary for later builds.
	cp Module.symvers ${hdrdest}
	cp -a scripts ${hdrdest}
	mkdir -p ${hdrdest}/security/selinux
	cp -a security/selinux/include ${hdrdest}/security/selinux
	mkdir -p ${hdrdest}/tools/include
	cp -a tools/include/tools ${hdrdest}/tools/include
	if [ -d "arch/${_arch}/tools" ]; then
		cp -a arch/${_arch}/tools ${hdrdest}/arch/${_arch}
	fi

	# copy arch includes for external modules
	mkdir -p ${hdrdest}/arch/${_arch}
	cp -a arch/${_arch}/include ${hdrdest}/arch/${_arch}

	mkdir -p ${hdrdest}/arch/${_arch}/kernel
	cp arch/${_arch}/Makefile ${hdrdest}/arch/${_arch}
	cp arch/${_arch}/kernel/asm-offsets.s ${hdrdest}/arch/${_arch}/kernel
	if [ "$_arch" = "arm64" ] ; then
		cp -a arch/${_arch}/kernel/vdso ${hdrdest}/arch/${_arch}/kernel/
	fi

	# Add md headers
	mkdir -p ${hdrdest}/drivers/md
	cp drivers/md/*.h ${hdrdest}/drivers/md

	# Add inotify.h
	mkdir -p ${hdrdest}/include/linux
	cp include/linux/inotify.h ${hdrdest}/include/linux

	# Add wireless headers
	mkdir -p ${hdrdest}/net/mac80211/
	cp net/mac80211/*.h ${hdrdest}/net/mac80211

	# Remove unneeded architectures
	# (save the correct one + Kconfig and delete all others)
	mkdir -p arch-backup
	cp -r ${hdrdest}/arch/${_arch} ${hdrdest}/arch/Kconfig arch-backup/
	rm -rf ${hdrdest}/arch
	mv arch-backup ${hdrdest}/arch
	# Keep arch/x86/ras/Kconfig as it is needed by drivers/ras/Kconfig
	mkdir -p ${hdrdest}/arch/x86/ras
	cp -a arch/x86/ras/Kconfig ${hdrdest}/arch/x86/ras/Kconfig

	# Compress all modules with xz to save a few MBs.
	msg_normal "$pkgver: compressing kernel modules with gzip, please wait...\n"
	find ${DESTDIR} -name '*.ko' | xargs -n1 -P0 gzip -9

	# ... and run depmod again.
	depmod -b ${DESTDIR}/usr -F System.map ${_kernver}
}

subpackages="rpi-kernel-headers"

case "$XBPS_TARGET_MACHINE" in
	armv7l*) subpackages+=" rpi2-kernel rpi2-kernel-headers" ;;
	aarch64*) subpackages+=" rpi3-kernel rpi3-kernel-headers rpi4-kernel rpi4-kernel-headers" ;;
esac

rpi-kernel-headers_package() {
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	short_desc="${short_desc/kernel/kernel headers}"
	pkg_install() {
		vmove usr/src
		vmove usr/lib/modules/${_kernver}/build
	}
}

# hard-code the minimum version to prevent possible issues with partial upgrades
# of legacy (split) systems
rpi2-kernel_package() {
	build_style=meta
	short_desc+=" (transitional dummy package)"
	depends="rpi-kernel>=5.15.68_1"
}

rpi2-kernel-headers_package() {
	build_style=meta
	short_desc="${short_desc/kernel/kernel headers} (transitional dummy package)"
	depends="rpi-kernel-headers>=5.15.68_1"
}

rpi3-kernel_package() {
	build_style=meta
	short_desc+=" (transitional dummy package)"
	depends="rpi-kernel>=5.15.68_1"
}

rpi3-kernel-headers_package() {
	build_style=meta
	short_desc="${short_desc/kernel/kernel headers} (transitional dummy package)"
	depends="rpi-kernel-headers>=5.15.68_1"
}

rpi4-kernel_package() {
	build_style=meta
	short_desc+=" (transitional dummy package)"
	depends="rpi-kernel>=5.15.68_1"
}

rpi4-kernel-headers_package() {
	build_style=meta
	short_desc="${short_desc/kernel/kernel headers} (transitional dummy package)"
	depends="rpi-kernel-headers>=5.15.68_1"
}
