Receipt for: linux-lts

source stuff/bootloader.sh stuff/check_modules.sh stuff/gztazmod.sh stuff/linux-lts-diff.u stuff/linux-lts-efi.u stuff/linux-lts-freeinitrd.u stuff/linux-lts-romfs.u stuff/linux-lts-slitaz.config stuff/linux-lts-slitaz.config64 stuff/linux-lts-subroot.u stuff/list_modules.sh stuff/modules.list
# SliTaz package receipt.

PACKAGE="linux-lts"
VERSION="6.12.89"
CATEGORY="base-system"
SHORT_DESC="The Linux LTS kernel ($VERSION) and modules."
MAINTAINER="devel@slitaz.org"
LICENSE="GPL2"
TARBALL="linux-$VERSION.tar.xz"
WEB_SITE="https://www.kernel.org/"
WGET_URL="https://cdn.kernel.org/pub/linux/kernel/v${VERSION%%.*}.x/$TARBALL"
HOST_ARCH="i486 x86_64"

DEPENDS="kmod"
# Built natively in the x86_64 chroot with gcc-83 (8.3.0) — same toolchain
# as linux-cloud/linux-phoenix. gcc >= 8 is REQUIRED for 6.x: retpoline and
# return-thunks (RETHUNK) need -mfunction-return=thunk-extern. openssl-dev/
# libcrypto cover scripts/extract-cert when the config keeps a trusted keyring.
# elfutils-dev (gelf.h/libelf.h) is required by tools/objtool, itself pulled
# in by the ORC unwinder + retpoline validation on x86_64. diffutils gives a
# GNU diff (with -I): the kernel tools/ ABI header sync-checks call `diff -I`,
# which busybox diff can't do -> harmless but noisy "Usage: diff" dumps.
BUILD_DEPENDS="gcc83 perl xz patch kmod bc openssl-dev libcrypto elfutils-dev diffutils"
# Kernel = arch-specific by nature (bzImage, not an ELF). Force the x86_64
# tag so autodetection never emits a false "-any" from the *.ko.xz alone.
COOKOPTS="force-arch"

#
# linux-lts = the modern LTS kernel family for SliTaz 5.1. x86_64 first,
# i486 (32-bit config) to follow. The legacy 3.16 `linux`/`linux586` family
# stays frozen as the i486 retro — see ~/.slitaz/KERNEL.md. The live boot
# uses overlayfs (CONFIG_OVERLAY_FS, packaged as linux-lts-fs), no aufs.
#
# KISS split model kept, but coarsened: ~11 module packages grouped by
# kernel source subtree (linux-lts-fs, -net, -wireless, -sound, -gpu,
# -storage, -input, -crypto, -virt, -hw) + technical splits.
#
# Do NOT play with the VERSION string / SUBLEVEL: depmod relies on it.
#

current_version()
{
	wget -O - $WEB_SITE 2> /dev/null | grep -A 1 longterm | \
	sed '/strong/!d;s|.*<strong>||;s|</s.*||;q'
}

install_module_headers()
{
	local path
	path=usr/src/linux-$KVERSION
	mkdir -p $1/lib/modules/$KVERSION
	ln -sf /$path $1/lib/modules/$KVERSION/build
	install -D -m644 $src/Makefile \
		$1/$path/Makefile
	install -D -m644 $src/kernel/Makefile \
		$1/$path/kernel/Makefile
	install -D -m644 $src/.config \
		$1/$path/.config
	mkdir -p $1/$path/include

	cp -a $src/include/* $1/$path/include/
	rm -f $1/$path/include/Kbuild

	# copy arch includes for external modules
	mkdir -p $1/$path/arch/x86
	cp -a $src/arch/x86/include $1/$path/arch/x86/

	# copy files necessary for later builds, like nvidia and vmware
  	cp -a $src/Module.symvers $1/$path/Module.symvers
  	cp -a $src/scripts $1/$path
  	# fix permissions on scripts dir
  	chmod og-w -R $1/$path/scripts
  	mkdir -p $1/$path/.tmp_versions

	mkdir -p $1/$path/arch/x86/kernel
	cp -a $src/arch/x86/Makefile $1/$path/arch/x86/
	cp -a $src/arch/x86/Makefile_32.cpu $1/$path/arch/x86/
	cp -a $src/arch/x86/kernel/asm-offsets.s $1/$path/arch/x86/kernel/

	# add headers for lirc package
	mkdir -p $1/$path/drivers/media
	( cd $src/drivers/media ; find i2c -name '*.h' | cpio -o -H newc ) | \
	( cd $1/$path/drivers/media ; cpio -idmu )

	# add docbook makefile (gone in 5.x: Sphinx replaced DocBook)
	[ -f $src/Documentation/DocBook/Makefile ] && \
	install -D -m644 $src/Documentation/DocBook/Makefile \
    		$1/$path/Documentation/DocBook/Makefile

	# add dm headers
	mkdir -p $1/$path/drivers/md
	cp -a $src/drivers/md/*.h  $1/$path/drivers/md

	# add inotify.h
	mkdir -p $1/$path/include/linux
	cp -a $src/include/linux/inotify.h $1/$path/include/linux/

	# add wireless headers
	mkdir -p $1/$path/net/mac80211/
	cp -a $src/net/mac80211/*.h $1/$path/net/mac80211/

	# add dvb headers for external modules
	# in reference to:
	# http://bugs.archlinux.org/task/9912
	# 5.x: dvb-core public headers moved to include/media/ (already copied
	# above via include/*) -> only copy if any .h still lives here.
	if ls $src/drivers/media/dvb-core/*.h >/dev/null 2>&1; then
		mkdir -p $1/$path/drivers/media/dvb-core
		cp -a $src/drivers/media/dvb-core/*.h $1/$path/drivers/media/dvb-core/
	fi

	# add dvb headers for external modules
	# in reference to:
	# http://bugs.archlinux.org/task/11194
	if [ -d $src/include/config/dvb/ ]; then
		mkdir -p $1/$path/include/config/dvb/
		cp $src/include/config/dvb/*.h $1/$path/include/config/dvb/
	fi

	# add dvb headers for http://mcentral.de/hg/~mrec/em28xx-new
	# in reference to:
	# http://bugs.archlinux.org/task/13146
	mkdir -p $1/$path/drivers/media/dvb-frontends/
	cp -a $src/drivers/media/dvb-frontends/lgdt330x.h $1/$path/drivers/media/dvb-frontends/
	cp -a $src/drivers/media/i2c/msp3400-driver.h $1/$path/drivers/media/dvb-frontends/

	# add dvb headers  
	# in reference to:
	# http://bugs.archlinux.org/task/20402
	mkdir -p $1/$path/drivers/media/usb/dvb-usb
	cp -a $src/drivers/media/usb/dvb-usb/*.h $1/$path/drivers/media/usb/dvb-usb/
	mkdir -p $1/$path/drivers/media/dvb-frontends
	cp -a $src/drivers/media/dvb-frontends/*.h $1/$path/drivers/media/dvb-frontends/
	mkdir -p $1/$path/drivers/media/tuners
	cp -a $src/drivers/media/tuners/*.h $1/$path/drivers/media/tuners/

	# add xfs and shmem for aufs building
	mkdir -p $1/$path/fs/xfs
	mkdir -p $1/$path/mm
	# 5.x: xfs_sb.h moved under fs/xfs/libxfs/
	if [ -f $src/fs/xfs/libxfs/xfs_sb.h ]; then
		cp -a $src/fs/xfs/libxfs/xfs_sb.h $1/$path/fs/xfs/xfs_sb.h
	elif [ -f $src/fs/xfs/xfs_sb.h ]; then
		cp -a $src/fs/xfs/xfs_sb.h $1/$path/fs/xfs/xfs_sb.h
	fi
	cp -a $src/fs/*.h $1/$path/fs/
	
	# copy in Kconfig files
	for i in $(find . -name "Kconfig*"); do 
		mkdir -p $1/$path/$(echo $i | sed 's|/Kconfig.*||')
		cp -a $src/$i $1/$path/$i
	done

	chown -R root:root $1/$path
	find $1/$path -type d -exec chmod 755 {} \;
	# Kconfig and other headers are cp -a'd with source 0664 perms; QA
	# "Checking permissions" rejects group/other-writable files (marks
	# linux broken). Clear g/o write while keeping exec bits.
	find $1/$path -type f -exec chmod go-w {} \;
	# remove unneeded architectures
	rm -rf $1/$path/arch/{alpha,arm,arm26,avr32,blackfin,cris,frv,h8300,ia64,m32r,m68k,m68knommu,mips,microblaze,mn10300,parisc,powerpc,ppc,s390,sh,sh64,sparc,sparc64,um,v850,xtensa}
	# 5.x: scripts/dtc/include-prefixes/* symlink to arch/*/boot/dts;
	# once those arches are removed above they dangle and the postcheck
	# "broken symlinks in install" fails -> purge all broken symlinks.
	# NB: busybox find has no -xtype (GNU only) -> the old -xtype call
	# silently no-op'd in the chroot and shipped dangling symlinks, which
	# made cook's dep_files_ok reject linux-module-headers as a build dep.
	# Portable: list symlinks with -type l, drop those whose target [ -e ]
	# can't resolve (test follows the link; false when the target is gone).
	find $1/$path -type l | while read -r l; do [ -e "$l" ] || rm -f "$l"; done
}	

# Rules to configure and make the package.
compile_rules()
{
	# x86_64 + i486 (32-bit). arm = chantier séparé plus tard.
	case "$ARCH" in
		x86_64)
			echo "Compiling: x86_64 Kernel (native, gcc-83, no aufs)"
			cp -f $stuff/$PACKAGE-slitaz.config64 .config
			# Drop the hardcoded uclibc cross-compiler — we
			# build natively in the x86_64 chroot.
			sed -i 's|^CONFIG_CROSS_COMPILE=.*|CONFIG_CROSS_COMPILE=""|' \
				.config

			# tcp_stealth (out-of-tree, never mainlined) is dropped
			# for good. The SliTaz init patches are applied best-effort
			# on 6.12: written for 3.16, several still need a port —
			# failures are logged, not fatal, so we reach the compile
			# and see exactly what to port next.
			for p in $PACKAGE-diff.u $PACKAGE-freeinitrd.u \
				 $PACKAGE-subroot.u $PACKAGE-romfs.u \
				 $PACKAGE-efi.u; do
				if patch -p1 --dry-run < $stuff/$p >/dev/null 2>&1; then
					echo "Applying $p"; patch -p1 < $stuff/$p
				else
					echo "SKIP $p (does not apply on $VERSION, needs port)"
				fi
			done

			# Migrate the 5.15 config to 6.12 non-interactively.
			# BTF stays off: dwarves/pahole is not packaged yet — a real
			# dep gap, not a crutch. Enable CONFIG_DEBUG_INFO_BTF once
			# dwarves lands. RETPOLINE/RETHUNK build fine with gcc-83
			# (8.3.0), so no crutch here — that is why gcc >= 8 is required.
			echo "# CONFIG_DEBUG_INFO_BTF is not set" >> .config

			# scripts/extract-cert (SYSTEM_TRUSTED_KEYRING) links -lcrypto.
			# The unversioned libcrypto.so link is shipped by *libcrypto*;
			# recreate it if the build chroot only carries libcrypto.so.3,
			# else ld falls back to the static .a and misses dl/pthread.
			[ -e /usr/lib/libcrypto.so ] || \
				ln -sf libcrypto.so.3 /usr/lib/libcrypto.so 2>/dev/null || true

			local mk="make ARCH=x86_64 CC=gcc-83 HOSTCC=gcc-83"
			yes '' | $mk olddefconfig
			$mk bzImage modules &&
			$mk INSTALL_MOD_PATH=$DESTDIR modules_install &&
			$mk INSTALL_HDR_PATH=$DESTDIR/usr headers_install &&
			[ -s arch/x86/boot/bzImage ] || return 1
			mkdir -p $DESTDIR/boot
			cp arch/x86/boot/bzImage \
				$DESTDIR/boot/vmlinuz-$VERSION-slitaz

			KVERSION=$VERSION-slitaz
			if [ -d "$DESTDIR/lib/modules/$KVERSION" ]; then
				$stuff/gztazmod.sh \
					$DESTDIR/lib/modules/$KVERSION
			fi

			# Debug artefacts consumed by linux-debug genpkg_rules
			# (mirror the i386 branch below: it ln/cp these *-modules).
			ln -f System.map System.map-modules
			ln -f vmlinux vmlinux-modules
			ln -f Module.symvers Module.symvers-modules
			cp -f .config config-modules

			# SliTaz db stuff (consumed by linux-source genpkg_rules,
			# which copies $WOK/linux/source/slitaz into the package).
			# The i386 fall-through builds this below; replicate it here
			# for the native x86_64 build.
			[ -d $WOK/$PACKAGE/source/slitaz ] && \
				rm -rf $WOK/$PACKAGE/source/slitaz
			mkdir -p $WOK/$PACKAGE/source/slitaz
			echo "$WGET_URL" > $WOK/$PACKAGE/source/slitaz/url
			cp $stuff/gztazmod.sh $stuff/list_modules.sh \
				$stuff/bootloader.sh $WOK/$PACKAGE/source/slitaz
			cp .config $WOK/$PACKAGE/source/slitaz/config
			for p in $PACKAGE-diff.u $PACKAGE-freeinitrd.u \
				 $PACKAGE-subroot.u $PACKAGE-romfs.u \
				 $PACKAGE-efi.u; do
				echo "$p" >> $WOK/$PACKAGE/source/slitaz/patches
				cp $stuff/$p $WOK/$PACKAGE/source/slitaz
			done

			install_module_headers $DESTDIR
			return 0 ;;
		i486)
			echo "Compiling: i486/i386 Kernel (native, gcc-83)"
			cp -f $stuff/$PACKAGE-slitaz.config .config
			sed -i 's|^CONFIG_CROSS_COMPILE=.*|CONFIG_CROSS_COMPILE=""|' \
				.config

			for p in $PACKAGE-diff.u $PACKAGE-freeinitrd.u \
				 $PACKAGE-subroot.u $PACKAGE-romfs.u \
				 $PACKAGE-efi.u; do
				if patch -p1 --dry-run < $stuff/$p >/dev/null 2>&1; then
					echo "Applying $p"; patch -p1 < $stuff/$p
				else
					echo "SKIP $p (does not apply on $VERSION, needs port)"
				fi
			done

			echo "# CONFIG_DEBUG_INFO_BTF is not set" >> .config
			[ -e /usr/lib/libcrypto.so ] || \
				ln -sf libcrypto.so.3 /usr/lib/libcrypto.so 2>/dev/null || true

			local mk="make ARCH=i386 CC=gcc-83 HOSTCC=gcc-83"
			yes '' | $mk olddefconfig
			$mk bzImage modules &&
			$mk INSTALL_MOD_PATH=$DESTDIR modules_install &&
			$mk INSTALL_HDR_PATH=$DESTDIR/usr headers_install &&
			[ -s arch/x86/boot/bzImage ] || return 1
			mkdir -p $DESTDIR/boot
			cp arch/x86/boot/bzImage \
				$DESTDIR/boot/vmlinuz-$VERSION-slitaz

			KVERSION=$VERSION-slitaz
			if [ -d "$DESTDIR/lib/modules/$KVERSION" ]; then
				$stuff/gztazmod.sh \
					$DESTDIR/lib/modules/$KVERSION
			fi

			ln -f System.map System.map-modules
			ln -f vmlinux vmlinux-modules
			ln -f Module.symvers Module.symvers-modules
			cp -f .config config-modules

			[ -d $WOK/$PACKAGE/source/slitaz ] && \
				rm -rf $WOK/$PACKAGE/source/slitaz
			mkdir -p $WOK/$PACKAGE/source/slitaz
			echo "$WGET_URL" > $WOK/$PACKAGE/source/slitaz/url
			cp $stuff/gztazmod.sh $stuff/list_modules.sh \
				$stuff/bootloader.sh $WOK/$PACKAGE/source/slitaz
			cp .config $WOK/$PACKAGE/source/slitaz/config
			for p in $PACKAGE-diff.u $PACKAGE-freeinitrd.u \
				 $PACKAGE-subroot.u $PACKAGE-romfs.u \
				 $PACKAGE-efi.u; do
				echo "$p" >> $WOK/$PACKAGE/source/slitaz/patches
				cp $stuff/$p $WOK/$PACKAGE/source/slitaz
			done

			install_module_headers $DESTDIR
			return 0 ;;
	esac
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	echo "Packing: $ARCH Kernel + base modules"
	export PACKAGE VERSION
	local path
	cp -a $install/boot $fs

	# Base modules: cherry-pick the essentials from stuff/modules.list.
	# Everything else is carved into the coarse linux-lts-* siblings below.
	path=$fs/lib/modules/$VERSION-slitaz/kernel
	mkdir -p $path
	export src install
	mkdir -p $WOK/$PACKAGE/source/tmp
	$stuff/list_modules.sh \
		$(cat $stuff/modules.list) \
		> $WOK/$PACKAGE/source/tmp/modules.list
	while read module; do
		dir=$(dirname $module)
		[ -d $path/$dir ] || mkdir -p $path/$dir
		cp -a $install/lib/modules/$VERSION-slitaz/kernel/$module \
			$path/$dir
	done < $WOK/$PACKAGE/source/tmp/modules.list

	rm -f $fs/lib/modules/$VERSION-slitaz/build
	rm -f $fs/lib/modules/$VERSION-slitaz/source
	cp -a $install/lib/modules/$VERSION-slitaz/modules.order \
		$install/lib/modules/$VERSION-slitaz/modules.builtin \
		$fs/lib/modules/$VERSION-slitaz

	# Cook the coarse module siblings (WANTED="linux-lts"). linux-lts-firmware
	# ships nothing (in-tree firmware left the kernel in 5.x) -> skipped so a
	# would-be-empty pack never marks the master broken.
	sed -i "/^$PACKAGE\$/d" $CACHE/broken 2>/dev/null || true
	for i in $(cd $WOK; grep -l '^WANTED="linux-lts"' */receipt); do
		p=${i%/receipt}
		case "$p" in
			*-firmware) continue ;;
		esac
		[ ! -d "$install/lib/modules/$VERSION-slitaz" ] && return 1
		cook "$p"
	done
}

# Pre and post install commands for Tazpkg.
post_install()
{
	chroot "$root/" depmod -a $VERSION-slitaz

	# Handle multiarch installation
	case "$SLITAZ_ARCH" in
		arm)
			echo "Kernel : /boot/linux-$VERSION-slitaz-$ARCH"
			echo "Modules: /lib/modules/$VERSION-slitaz"
			return 0 ;;
	esac

	# GRUB stuff.
	if [ -f "$1/boot/grub/menu.lst" ]; then
		# Current root device
		root_dev="/dev/$(lsblk | grep '/$'| grep -o '[a-z]d[a-z][0-9]*')"
		grub_part="$(( $(echo $root_dev | grep -o '[0-9]*$')-1 ))"
		# Use device.map to find grub device number
		grub_dev="$(grep $(echo $root_dev | grep -o  '^/dev/.d.' ) \
			"$1/boot/grub/device.map" | cut -f1 | sed "s/)$/.$grub_part)/g")" 

		# Add and clean kernel entries in case of upgrade for installed system.
		if ! grep -q vmlinuz-$VERSION-slitaz "$1/boot/grub/menu.lst"; then

			# Clean the old entry
			# TODO: old vmlinuz file is removed but the entry is still there.
			# So grub error:15 when selected: http://bugs.slitaz.org/?id=74

			# Add the new entry
			cat >> "$1/boot/grub/menu.lst" << EOT

title SliTaz GNU/Linux (Kernel $VERSION-slitaz)
$grub_dev 
kernel /boot/vmlinuz-$VERSION-slitaz root=$root_dev quiet
EOT
			# Make it the default entry
			last_entry=$(( $(grep -c '^title' "$1/boot/grub/menu.lst")-1 ))
			sed -e "s/^default.*/default $last_entry/g" -i "$1/boot/grub/menu.lst"
		fi

		# Display information message.
		cat <<EOT

----
GRUB is installed, these tree lines must be in your /boot/grub/menu.lst:

title  SliTaz GNU/Linux (Kernel $VERSION-slitaz)
$grub_dev
kernel /boot/vmlinuz-$VERSION-slitaz root=$root_dev
----
EOT
	fi
	true
}