# Template file for 'odroid-c2-kernel'
pkgname=odroid-c2-kernel
version=3.14.79
revision=1
_githash=558eb86c1abe7dc321b8457c5edf80bfe72492ec
wrksrc="linux-${_githash}"
maintainer="Christian Neukirchen <chneukirchen@gmail.com>"
homepage="https://github.com/hardkernel/linux/tree/odroidc2-3.14.y"
license="GPL-2"
short_desc="The Linux kernel for ODROID-C2 (${version%.*} series)"
distfiles="https://github.com/hardkernel/linux/archive/${_githash}.tar.gz"
checksum=13e1833f930c246f05bac50bbf29dc142eb6c70520eeb60c1ab148a800798782

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

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

only_for_archs="aarch64 aarch64-musl"
hostmakedepends="perl kmod libressl bc"
makedepends="ncurses-devel"
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"

do_configure() {
	if [ "$CROSS_BUILD" ]; then
		_args="CROSS_COMPILE=${XBPS_CROSS_TRIPLET}-"
	fi
	cp ${FILESDIR}/config .config
	make ${makejobs} ${_args} ARCH=arm64 oldconfig
	# Always use our revision to CONFIG_LOCALVERSION to match our pkg version.
	sed -i -e "s|^\(CONFIG_LOCALVERSION=\).*|\1\"_${revision}\"|" .config
}
do_build() {
	if [ "$CROSS_BUILD" ]; then
		_args="CROSS_COMPILE=${XBPS_CROSS_TRIPLET}-"
	fi
	export LDFLAGS=
	make ${makejobs} ${_args} ARCH=arm64 prepare
	make ${makejobs} ${_args} ARCH=arm64 Image modules dtbs
}
do_install() {
	local hdrdest

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

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

	vmkdir boot
	vmkdir boot/dtbs
	vinstall arch/arm64/boot/Image 644 boot
	vinstall arch/arm64/boot/dts/meson64_odroidc2.dtb 644 boot/dtbs
#   install -D -m644 vmlinux "${pkgdir}/usr/lib/modules/${_kernver}/build/vmlinux"

	hdrdest=${DESTDIR}/usr/src/${sourcepkg}-headers-${_kernver}

	# 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
	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 config crypto drm generated linux math-emu \
		media net pcmcia scsi sound trace uapi video xen; do
		[ -d include/$i ] && cp -a include/$i ${hdrdest}/include
	done

	cd ${wrksrc}
	# Copy files necessary for later builds.
	cp Module.symvers ${hdrdest}
	cp -a scripts ${hdrdest}

	# fix permissions on scripts dir
	chmod og-w -R ${hdrdest}/scripts

	# Add docbook makefile
	install -Dm644 Documentation/DocBook/Makefile \
		${hdrdest}/Documentation/DocBook/Makefile

	# 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

	# add dvb headers for external modules
	mkdir -p ${hdrdest}/include/config/dvb/
	cp include/config/dvb/*.h ${hdrdest}/include/config/dvb/

	# Copy in Kconfig files
	for i in $(find . -name "Kconfig*"); do
		mkdir -p ${hdrdest}/$(echo $i | sed 's|/Kconfig.*||')
		cp $i ${hdrdest}/$i
	done

	# Remove unneeded architectures
	for arch in alpha arm26 avr32 blackfin cris frv h8300 \
		ia64 m* p* s* um v850 x86 xtensa; do
		rm -rf ${hdrdest}/arch/${arch}
	done

	# 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}
}

odroid-c2-kernel-headers_package() {
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	short_desc="The Linux kernel headers for ODROID-C2 (${version%.*} series)"
	pkg_install() {
		vmove usr/src
		vmove usr/lib/modules/${_kernver}/build
	}
}
