#!/bin/sh
#
#	Output a simple RPM spec file.
#	This version assumes a minimum of RPM 4.0.3.
#
#	The only gothic bit here is redefining install_post to avoid
#	stripping the symbols from files in the kernel which we want
#
#	Patched for non-x86 by Opencon (L) 2002 <opencon@rio.skydome.net>
#

# how we were called determines which rpms we build and how we build them
if [ "$1" = prebuilt ]; then
	S=DEL
	MAKE="$MAKE -f $srctree/Makefile"
else
	S=
fi

if grep -q CONFIG_MODULES=y .config; then
	M=
else
	M=DEL
fi

if grep -q CONFIG_DRM=y .config; then
	PROVIDES=kernel-drm
fi

# set CTF when configured
if grep -q CONFIG_CTF=y .config; then
	C=
else
	C=DEL
fi

PROVIDES="$PROVIDES kernel-$KERNELRELEASE"
__KERNELRELEASE=$(echo $KERNELRELEASE | sed -e "s/-/_/g")
__LOCALVERSION=-$(echo $KERNELRELEASE | cut -d '-' -f 2-)
EXCLUDES="$RCS_TAR_IGNORE --exclude=.tmp_versions --exclude=*vmlinux* --exclude=*.mod \
--exclude=*.o --exclude=*.ko --exclude=*.cmd --exclude=Documentation \
--exclude=.config.old --exclude=.missing-syscalls.d --exclude=*.s"

# We can label the here-doc lines for conditional output to the spec file
#
# Labels:
#  $S: this line is enabled only when building source package
#  $M: this line is enabled only when CONFIG_MODULES is enabled
#  $C: this line is enabled only when CONFIG_CTF is enabled
sed -e '/^DEL/d' -e 's/^\t*//' <<EOF
	# We have to override the new %%install behavior because, well... the kernel is special.
	%global __spec_install_pre %{___build_pre}

    %{!?with_debuginfo: %global with_debuginfo 1}
	%define debuginfodir /usr/lib/debug
	# Needed because we override almost everything involving build-ids
	# and debuginfo generation. Currently we rely on the old alldebug setting.
	%global _build_id_links alldebug
	%define KVERREL %{version}

	%{!?__brp_compress: %global __brp_compress /usr/lib/rpm/redhat/brp-compress}
	%{!?__brp_strip: %global __brp_strip /usr/lib/rpm/redhat/brp-strip %{__strip}}
	%{!?__brp_strip_comment_note: %global __brp_strip /usr/lib/rpm/redhat/brp-strip-comment-note %{__strip} %{__objdump}}
	%{!?__brp_strip_static_archive: %global __brp_strip_static_archive /usr/lib/rpm/redhat/brp-strip-static-archive %{__strip}}

	Name: kernel
	Summary: The Linux Kernel
	Version: $__KERNELRELEASE
	Release: $(cat .version 2>/dev/null || echo 1)
	License: GPL
	Group: System Environment/Kernel
	Vendor: The Linux Community
	URL: https://www.kernel.org
$S	Source: kernel-$__KERNELRELEASE.tar.gz
	Provides: $PROVIDES

	%if %{with_debuginfo}
	# Fancy new debuginfo generation introduced in Fedora 8/RHEL 6.
	# The -r flag to find-debuginfo.sh invokes eu-strip --reloc-debug-sections
	# which reduces the number of relocations in kernel module .ko.debug files and
	# was introduced with rpm 4.9 and elfutils 0.153.
	BuildRequires: rpm-build >= 4.9.0-1, elfutils >= 0.153-1
	# Most of these should be enabled after more investigation
	%undefine _include_minidebuginfo
	%undefine _find_debuginfo_dwz_opts
	%undefine _unique_build_ids
	%undefine _unique_debug_names
	%undefine _unique_debug_srcs
	%undefine _debugsource_packages
	%undefine _debuginfo_subpackages
	%global _missing_build_ids_terminate_build 1
	%global _no_recompute_build_ids 1

	%define debuginfo_args --strict-build-id -r
	%global _find_debuginfo_opts -r
	%endif

	%description
	The Linux Kernel, the operating system core itself

	%package headers
	Summary: Header files for the Linux kernel for use by glibc
	Group: Development/System
	Obsoletes: kernel-headers
	Provides: kernel-headers = %{version}
	%description headers
	Kernel-headers includes the C header files that specify the interface
	between the Linux kernel and userspace libraries and programs.  The
	header files define structures and constants that are needed for
	building most standard programs and are also needed for rebuilding the
	glibc package.

	%package debuginfo
	Summary: Debug information for package %{name}-debuginfo
	Group: Development/Debug
	Provides: %{name}-debuginfo = %{version}-%{release}
	%description debuginfo
	This package provides debug information for package %{name}-debuginfo
	AutoReqProv: no

	%if %{with_debuginfo}
	%{expand:%%global debuginfo_args %{?debuginfo_args} -p '/.*/%%{KVERREL}%{?1:[+]%{1}}/.*|/.*%%{KVERREL}%{?1:\+%{1}}(\.debug)?' -o debuginfo%{?1}.list}
	%endif

$S$M	%package devel
$S$M	Summary: Development package for building kernel modules to match the $__KERNELRELEASE kernel
$S$M	Group: System Environment/Kernel
$S$M	AutoReqProv: no
$S$M	%description -n kernel-devel
$S$M	This package provides kernel headers and makefiles sufficient to build modules
$S$M	against the $__KERNELRELEASE kernel package.
$S$M
$S	%prep
$S	%setup -q
$S
$S	%build
	%if %{with_debuginfo}
	# This override tweaks the kernel makefiles so that we run debugedit on an
	# object before embedding it.  When we later run find-debuginfo.sh, it will
	# run debugedit again.  The edits it does change the build ID bits embedded
	# in the stripped object, but repeating debugedit is a no-op.  We do it
	# beforehand to get the proper final build ID bits into the embedded image.
	# This affects the vDSO images in vmlinux, and the vmlinux image in bzImage.
	export AFTER_LINK='sh -xc "/usr/lib/rpm/debugedit -b \$\$RPM_BUILD_DIR -d /usr/src/debug -i $@ > $@.id"'
	%endif

$S	$MAKE %{?_smp_mflags} KBUILD_BUILD_VERSION=%{release} LOCALVERSION=$__LOCALVERSION
$S$C	$MAKE %{?_smp_mflags} ctf
$S
	# This macro is used by %%install, so we must redefine it before that.
	%define debug_package %{nil}

	%if %{with_debuginfo}

	%define __debug_install_post \
	  /usr/lib/rpm/find-debuginfo.sh %{debuginfo_args} %{_find_debuginfo_opts} %{_builddir}/%{?buildsubdir} \
	%{nil}

	%global __debug_package 1
	%if "%{rhel}" == "8"
	%files debuginfo -f debugfiles.list
	%defattr(-,root,root)
	/usr/src/debug
	%else
	%files debuginfo -f debuginfo.list
	%defattr(-,root,root)
	/usr/lib/debug/*
	%endif

	%endif


	%define __os_install_post    \
    %{?__brp_compress} \
    %{!?__debug_package:\
    %{!?__brp_strip} \
    %{!?__brp_strip_comment_note} \
    } \
    %{!?__brp_strip_static_archive} \
	%{nil}

	#
	# Disgusting hack alert! We need to ensure we sign modules *after* all
	# invocations of strip occur, which is in __debug_install_post if
	# find-debuginfo.sh runs, and __os_install_post if not.
	#
	%define __spec_install_post \
	  %{?__debug_package:%{__debug_install_post}}\
	  %{?__arch_install_post}\
	  %{?__os_install_post}\
	  %{?__modsign_install_post}

	%install
	mkdir -p %{buildroot}/boot
	%ifarch ia64
	mkdir -p %{buildroot}/boot/efi
	cp \$($MAKE -s image_name) %{buildroot}/boot/efi/vmlinuz-$KERNELRELEASE
	ln -s efi/vmlinuz-$KERNELRELEASE %{buildroot}/boot/
	%else
	cp \$($MAKE -s image_name) %{buildroot}/boot/vmlinuz-$KERNELRELEASE
	%endif
$M	$MAKE %{?_smp_mflags} INSTALL_MOD_PATH=%{buildroot} modules_install LOCALVERSION=$__LOCALVERSION
	$MAKE %{?_smp_mflags} INSTALL_HDR_PATH=%{buildroot}/usr headers_install LOCALVERSION=$__LOCALVERSION

	%if %{with_debuginfo}
	    mkdir -p %{buildroot}/usr/src/kernels/$KERNELRELEASE
	    eu-readelf -n vmlinux | grep "Build ID" | awk '{print \$NF}' > %{buildroot}/usr/src/kernels/$KERNELRELEASE/vmlinux.id

	    #
	    # save the vmlinux file for kernel debugging into the kernel-debuginfo rpm
	    #
	    mkdir -p %{buildroot}%{debuginfodir}/lib/modules/$KERNELRELEASE
	    rm -f %{buildroot}%{debuginfodir}/lib/modules/$KERNELRELEASE/vmlinux
	    cp vmlinux %{buildroot}%{debuginfodir}/lib/modules/$KERNELRELEASE

		find %{buildroot}/lib/modules/$KERNELRELEASE -name "*.ko" -type f >modnames

		# mark modules executable so that strip-to-file can strip them
		xargs --no-run-if-empty chmod u+x < modnames
	%endif

	cp System.map %{buildroot}/boot/System.map-$KERNELRELEASE
	cp .config %{buildroot}/boot/config-$KERNELRELEASE
	# bzip2 -9 --keep vmlinux
	# mv vmlinux.bz2 %{buildroot}/boot/vmlinux-$KERNELRELEASE.bz2
$S$M	rm -f %{buildroot}/lib/modules/$KERNELRELEASE/build
$S$M	rm -f %{buildroot}/lib/modules/$KERNELRELEASE/source
$S$M	mkdir -p %{buildroot}/usr/src/kernels/$KERNELRELEASE
$S$M	tar cf - $EXCLUDES . | tar xf - -C %{buildroot}/usr/src/kernels/$KERNELRELEASE
$S$M	cd %{buildroot}/lib/modules/$KERNELRELEASE
$S$M	ln -sf /usr/src/kernels/$KERNELRELEASE build
$S$M	ln -sf /usr/src/kernels/$KERNELRELEASE source
	/bin/rm -f %{buildroot}/boot/vmlinux-*.bz2

	%clean
	rm -rf %{buildroot}

	%post
	if [ -x /sbin/installkernel -a -r /boot/vmlinuz-$KERNELRELEASE -a -r /boot/System.map-$KERNELRELEASE ]; then
	cp /boot/vmlinuz-$KERNELRELEASE /boot/.vmlinuz-$KERNELRELEASE-rpm
	cp /boot/System.map-$KERNELRELEASE /boot/.System.map-$KERNELRELEASE-rpm
	rm -f /boot/vmlinuz-$KERNELRELEASE /boot/System.map-$KERNELRELEASE
	/sbin/installkernel $KERNELRELEASE /boot/.vmlinuz-$KERNELRELEASE-rpm /boot/.System.map-$KERNELRELEASE-rpm
	rm -f /boot/.vmlinuz-$KERNELRELEASE-rpm /boot/.System.map-$KERNELRELEASE-rpm
	fi

	%preun
	if [ -x /sbin/new-kernel-pkg ]; then
	new-kernel-pkg --remove $KERNELRELEASE --rminitrd --initrdfile=/boot/initramfs-$KERNELRELEASE.img
	elif [ -x /usr/bin/kernel-install ]; then
	kernel-install remove $KERNELRELEASE
	fi

	%postun
	if [ -x /sbin/update-bootloader ]; then
	/sbin/update-bootloader --remove $KERNELRELEASE
	fi

	%files
	%defattr (-, root, root)
$M	/lib/modules/$KERNELRELEASE
$M	%exclude /lib/modules/$KERNELRELEASE/build
$M	%exclude /lib/modules/$KERNELRELEASE/source
	/boot/*

	%files headers
	%defattr (-, root, root)
	/usr/include
$S$M
$S$M	%files devel
$S$M	%defattr (-, root, root)
$S$M	/usr/src/kernels/$KERNELRELEASE
$S$M	/lib/modules/$KERNELRELEASE/build
$S$M	/lib/modules/$KERNELRELEASE/source
EOF
