# Archived: -dev packages x86_64 binaries in aarch64 package which makes akms part of device-clockworkpi-uconsole-radxa-cm5 fail when built on aarch64
# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm64/configs/rockchip_linux_defconfig
maintainer="Antoine Martin (ayakael) <dev@ayakael.net>"
pkgname=linux-radxa
pkgver=6.1.115
pkgrel=1
pkgdesc="Radxa kernel fork"
arch="aarch64"
_carch="arm64"
_flavor="radxa"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps pmb:cross-native"
subpackages="$pkgname-dev:_dev:$CBUILD_ARCH"
makedepends="
	bash
	bc
	bison
	devicepkg-dev
	dtc
	flex
	findutils
	installkernel
	linux-firmware
	linux-headers
	openssl-dev
	perl
	xz
"

# Source
_repository="kernel"
_commit="74729d7c051026f99138b6e0e60d580c0bf226db"
_config="config-$_flavor.$arch"
source="
	$pkgname-$_commit.tar.gz::https://github.com/radxa/$_repository/archive/$_commit.tar.gz
	$_config
	postmarketos-defconfig.patch
"
builddir="$srcdir/$_repository-$_commit"
_builddir="$srcdir"/build-$_flavor.$CARCH

prepare() {
	default_prepare

	mkdir -p "$_builddir"
	cp "$srcdir/config-$_flavor.$CARCH" "$_builddir"/.config

	# copies missing library
	mkdir -p "$_builddir"/drivers/gpu/arm/bifrost/
	cp "$builddir"/drivers/gpu/arm/bifrost/mali_csffw.bin "$_builddir"/drivers/gpu/arm/bifrost/mali_csffw.bin

	# remove and regenerate localversion
	rm -f localversion*
	echo "-$pkgrel-$_flavor" > "$_builddir"/localversion-postmarketos

	make -C "$builddir" \
		O="$_builddir" \
		ARCH="$_carch" \
		olddefconfig
}

build() {
	unset LDFLAGS
	cd "$_builddir"
	local _kver=$(make kernelversion)
	if [ "$_kver" != "$pkgver" ]; then
		error "Version in Makefile ($_kver) does not correspond with pkgver ($pkgver)"
		return 1
	fi
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-postmarketOS"
}

package() {
	downstreamkernel_package "$_builddir" "$pkgdir" "$_carch" "$_flavor"

	cd "$_builddir"
	mkdir -p "$pkgdir"/boot
	make 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
}

_dev() {
	cd "$_builddir"
	local _abi_release="$(make -s kernelrelease)"
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for $_flavor kernel"
	depends="$_depends_dev"

	local dir="$subpkgdir"/usr/src/linux-headers-$_abi_release

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$_builddir"/.config "$dir"/.config
	echo "-$pkgrel-$_flavor" > "$dir"/localversion-postmarketos

	make -j1 -C "$builddir" ARCH="$_carch" O="$dir" \
		syncconfig prepare modules_prepare scripts

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the sources
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk
	cd "$builddir"
	find .  -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
		-print | cpio -pdm "$dir"

	cp -a scripts include "$dir"
	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$_builddir"/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/$_abi_release
	ln -sf /usr/src/linux-headers-$_abi_release \
		"$subpkgdir"/lib/modules/$_abi_release/build
}

sha512sums="
3c268ca3eaaa6bbffce9759f963ec6030a4a4d2c3b8ed5e130ca5c29d25bad78e43faa575fbc6c1f90ffdbffea39db6a569f073d1917547a4bfbe7b8bf506578  linux-radxa-74729d7c051026f99138b6e0e60d580c0bf226db.tar.gz
e23a8fb644bca3e18c399112b1f0f569ddcda37d1cceb2fdef82ef97993071ee085c16c35898a22222988876af5456bc422bb247d501ff6725aa7c6cc7367522  config-radxa.aarch64
a9ec1018a798ee26cb864933dd3aa1d460e6e40a30c55cb0fdd56806031f3b2755d3823d6c30f7bcc1d2bb71d3cabe7a409f46a2e5ff78dc9ff5a31108709758  postmarketos-defconfig.patch
"
