%global _binaries_in_noarch_packages_terminate_build 0

%define __spec_install_post /usr/lib/rpm/brp-compress || :
%define debug_package %{nil}
%define extstable	0
%define build_perf		1
%define build_cpupower		1

%define options_preun -R
%define options_post  %nil

%define _modulesdir	/lib/modules
%define _bootdir	/boot
%define target_boot %{buildroot}%{_bootdir}
%define target_modules %{buildroot}%{_modulesdir}
%define kxargs xargs %([ -z "$RPM_BUILD_NCPUS" ] \\\
	&& RPM_BUILD_NCPUS="`/usr/bin/getconf _NPROCESSORS_ONLN`"; \\\
	[ "$RPM_BUILD_NCPUS" -gt 1 ] && echo "-P $RPM_BUILD_NCPUS")


%define kversion 5.16.18
%define rpmrel %mkrel 1

%define _kerneldir /usr/src/kernel-%{kversion}-%{rpmrel}

Name: 		kernel-%kversion
Summary: 	The Linux Kernel
Version: 	1
Release: 	%rpmrel
Distribution:	blackPanther OS.
Packager:  	Karoly Barcza <kbarcza@blackpanther.hu>
License: 	GPL
Group: 		System Environment/Kernel
URL: 		http://www.blackpanther.hu
Source: 	linux-%kversion.tar.xz
Source1:	kernel-x86_64.config  
Source2:	kernel-x86_64-2.config
Source50:	cpupower.service
Source51:	cpupower.config
Patch:		patch-5.16-blackPanther-POWER.patch

Provides: 	kernel-drm 
Provides:	kernel-%kversion-%release
Provides:	kernel-desktop-%kversion-%release = %version-%release

%description
The Vanila Linux Kernel, the operating system core itself

%files
%defattr (-, root, root)
%dir /lib/modules
/lib/modules/*
/boot/*%release

%if %{build_perf}
%package -n perf
Version:	%{kversion}
Release:	%{rpmrel}
Summary:	perf tool and the supporting documentation
Group:		System/Kernel and hardware

%description -n perf
the perf tool and the supporting documentation.
%endif

%if %{build_cpupower}
%package -n cpupower
Version:	%{kversion}
Release:	%{rpmrel}
Summary:	the cpupower tools
Group:		System/Kernel and hardware
Requires(post):  rpm-helper >= 0.24.8-1
Requires(preun): rpm-helper >= 0.24.8-1
Obsoletes:	cpufreq cpufrequtils

%description -n cpupower
the cpupower tools.

%post -n cpupower
%_post_service cpupower

%preun -n cpupower
%_preun_service cpupower

%package -n cpupower-devel
Version:	%{kversion}
Release:	%{rpmrel}
Summary:	devel files for cpupower
Group:		Development/Kernel
Requires:	cpupower = %{kversion}-%{rpmrel}
Conflicts:	%{_lib}cpufreq-devel

%description -n cpupower-devel
This package contains the development files for cpupower.
%endif


%if %{build_perf}
%files -n perf
%{_bindir}/perf
%{_bindir}/trace
%{_datadir}/perf-core/
%{_datadir}/doc/perf-tip/tips.txt
%dir %{_libdir}/traceevent
%dir %{_libdir}/traceevent/plugins
%{_libdir}/traceevent/plugins/plugin_*
%dir %{_prefix}/libexec/perf-core
%{_prefix}/libexec/perf-core/*
%{_mandir}/man[1-8]/perf*
%{_sysconfdir}/bash_completion.d/perf
#exclude %{_prefix}/src/debug
%endif

%if %{build_cpupower}
%files -n cpupower -f ../cpupower.lang
%_datadir/bash-completion/completions/cpupower
%{_bindir}/cpupower
%{_libdir}/libcpupower.so.0
%{_libdir}/libcpupower.so.0.0.1
%{_unitdir}/cpupower.service
%{_mandir}/man[1-8]/cpupower*
%config(noreplace) %{_sysconfdir}/sysconfig/cpupower
#exclude %{_prefix}/src/debug


%files -n cpupower-devel
%{_libdir}/libcpupower.so
%{_includedir}/cpuidle.h
%{_includedir}/cpufreq.h
%endif

%package -n kernel-source-%kversion
Requires: 	glibc-devel, ncurses-devel, make, gcc, perl, diffutils
Summary: 	The blackPanther OS source code for %{name}-%{release}
Group: 		Development/Kernel
Autoreqprov: 	no
Provides: 	kernel-source = %{kversion}
Buildarch:	noarch
Provides:	kernel-desktop-devel
Provides:	kernel-desktop-devel-%kversion-%release = %version-%release
Provides:	kernel-desktop-devel-%kversion-%release(x86-64) = %version-%release
Provides:	kernel-devel = %kversion-%release

%description -n kernel-source-%kversion
The %{kname}-source package contains the source code files for the blackPanther
kernel. Theese source files are only needed if you want to build your
own custom kernel that is better tuned to your particular hardware.

If you only want the files needed to build 3rdparty (nVidia, Ati, dkms-*,...)
drivers against, install the *-devel-* rpm that is matching your kernel.

%files -n kernel-source-%kversion
/usr/src/kernel-%{kversion}-%{rpmrel}
/usr/include/perf
/usr/lib/perf

%prep

%setup -q -n linux-%{version}
%autopatch -p1

install %SOURCE1 .config

#ln -s /usr/src/linux-%version-1bP $RPM_BUILD_DIR/linux-%version
%if %extstable
LC_ALL=C perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = .%{extstable}-%{release}/" Makefile
%else
LC_ALL=C perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -%{release}/" Makefile
%endif


%build
%make_build

%install
mkdir -p $RPM_BUILD_ROOT/boot $RPM_BUILD_ROOT/lib $RPM_BUILD_ROOT/lib/modules
INSTALL_MOD_PATH=%buildroot make modules_install
cp arch/x86/boot/bzImage $RPM_BUILD_ROOT/boot/vmlinuz-%version-%release
cp System.map $RPM_BUILD_ROOT/boot/System.map-%version-%release
cp .config $RPM_BUILD_ROOT/boot/config-%version-%release

find %{target_modules} -name "*.ko" | xargs xz -T0 -9

#for i in %{target_modules}/*; do
#  rm -f $i/build $i/modules.*
#done

for i in %{target_modules}/*; do
	rm -f $i/build $i/source
done

pushd %{target_modules}
for i in *; do
	/sbin/depmod -ae -b %{buildroot} -F %{target_boot}/System.map-$i $i
	echo $?
done

for i in *; do
    
	pushd $i
	echo "Creating modules.description for $i"
	[ -f modules.description ]&&rm -f modules.description
	for modules in `find . -name "*.ko.[g,x]z"`;do
	#modules=`find . -name "*.ko.[g,x]z"`
	echo "Modules: $modules $(echo $modules | xargs -P4 /sbin/modinfo)"
	echo $modules | %kxargs /sbin/modinfo \
	| perl -lne 'print "$name\t$1" if $name && /^description:\s*(.*)/; $name = $1 if m!^filename:\s*(.*)\.k?o!; $name =~ s!.*/!!' >> modules.description
	done
	if [ "x" != "x$(cat modules.description | grep 'ko.xz')" ];then
	    echo "Fixating wrong description for $modules"
	    sed -i 's|/.*.ko.xz||' modules.description
	    sleep 0.1
	fi
    popd

done
popd

%if %extstable
LC_ALL=C perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = .%{extstable}-%{release}/" Makefile
%else
LC_ALL=C perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -%{release}/" Makefile
%endif
%if %{build_perf}

make -s -C tools/perf NO_PERF_READ_VDSO32=1 NO_PERF_READ_VDSOX32=1 WERROR=0 NO_LIBUNWIND=1 \
	HAVE_CPLUS_DEMANGLE=1 NO_GTK2=1 NO_STRLCPY=1 NO_BIONIC=1 NO_JVMTI=1 prefix=%{_prefix} lib=%{_lib} all
make -C tools/perf -s prefix=%{_prefix} man
make -s -C tools/perf V=1 DESTDIR=%{buildroot} NO_PERF_READ_VDSO32=1 NO_PERF_READ_VDSOX32=1 WERROR=0 NO_LIBUNWIND=1 \
	HAVE_CPLUS_DEMANGLE=1 NO_GTK2=1 NO_STRLCPY=1 NO_BIONIC=1 NO_JVMTI=1 prefix=%{_prefix} lib=%{_lib} install

make -C tools/perf V=1 DESTDIR=%{buildroot} prefix=%{_prefix} install-man

%endif

%if %{build_cpupower}
chmod +x tools/power/cpupower/utils/version-gen.sh
%make_build -C tools/power/cpupower CPUFREQ_BENCH=false
make -C tools/power/cpupower DESTDIR=%{buildroot} libdir=%{_libdir} mandir=%{_mandir} CPUFREQ_BENCH=false install
rm -f %{buildroot}%{_libdir}/*.{a,la}
%find_lang cpupower
mv cpupower.lang ../
chmod 0755 %{buildroot}%{_libdir}/libcpupower.so*
mkdir -p %{buildroot}%{_unitdir} %{buildroot}%{_sysconfdir}/sysconfig
install -m644 %{SOURCE50} %{buildroot}%{_unitdir}/cpupower.service
install -m644 %{SOURCE51} %{buildroot}%{_sysconfdir}/sysconfig/cpupower

%endif

#rm -rf %buildroot/usr/lib/perf
#rm -rf %{buildroot}/usr/include/perf
#rm -rf %{buildroot}/usr/lib/perf/examples
#rm -rf %{buildroot}/usr/lib/perf/include/bpf

ln -sf /usr/src/linux-%version-%release $RPM_BUILD_ROOT/lib/modules/%version-%release/build 
ln -sf /usr/src/linux-%version-%release $RPM_BUILD_ROOT/lib/modules/%version-%release/source

mkdir -p %buildroot%_kerneldir
cp -a * %buildroot%_kerneldir
pushd %buildroot%_kerneldir
make clean
cd tools/perf
make clean
cd ../power/cpupower
make clean
popd

%clean
echo -rf $RPM_BUILD_ROOT


%preun
/sbin/installkernel %options_preun %version-%release
exit 0

%posttrans
/sbin/installkernel %options_post %version-%release

%post
[ ! -n "$(grep initrd-%kversion %_bootdir/grub2/grub.cfg 2>/dev/null)" ] && /usr/bin/update-grub2 ||:

%postun
/sbin/kernel_remove_initrd %kversion-%release
[ -n "$(grep initrd-%kversion-%release %_bootdir/grub2/grub.cfg 2>/dev/null)" ] && /usr/bin/update-grub2 ||:



%changelog
* Fri Jan 20 2023 Charles K. Barcza <info@blackpanther.hu> 5.16.18-1bP
- build package for blackPanther OS v22.x 32/64 bit or ARM
------------------------------------------------------------------------

* Sat Dec 24 2022 Charles K. Barcza <info@blackpanther.hu> 5.15.85-1bP
- build package for blackPanther OS v22.x 32/64 bit or ARM
- security update
-------------------------------------------------------------------------

* Fri Oct 16 2020 Charles K. Barcza <info@blackpanther.hu> 5.6.14-2bP
- added CVE-2020-12352 patch for bluetooth fix
-------------------------------------------------------------------------

* Thu Jun 25 2020 Charles K. Barcza <info@blackpanther.hu> 5.6.14-1bP
- build new version
- added patches 5002 ->
- with gcc 10
-------------------------------------------------------------------------

* Tue Jun 25 2019 Charles K. Barcza <info@blackpanther.hu> 5.1.15-1bP
- 5.1.15
- drop merged patches
-------------------------------------------------------------------------

* Tue Jun 04 2019 Charles K. Barcza <info@blackpanther.hu> 5.1.11-1bP
- 5.1.11
-------------------------------------------------------------------------

* Tue Oct 23 2018 Charles K. Barcza <info@blackpanther.hu> 4.18.14-1bP
- build package for blackPanther OS v17-19.x 32/64 bit or ARM
- suspend & hibernation fix
-------------------------------------------------------------------------

* Sun Oct 14 2018 Charles K. Barcza <info@blackpanther.hu> 4.18.14-1bP
- build package for blackPanther OS v17-19.x 32/64 bit or ARM
------------------------------------------------------------------------

* Tue Jan 30 2018 Charles Barcza <info@blackpanther.hu> 4.15.0-1bP
- build for blackPanther OS v16.x
- for v18 development
--------------------------------------------------------------

* Thu Jan 18 2018 Charles Barcza <info@blackpanther.hu> 4.14.14-1bP
- New release 
------------------------------------------------------------------

* Sat Jan 13 2018 Charles Barcza <info@blackpanther.hu> 4.14.13-2bP
- add intel performance patch
- PIT enable for PAE
------------------------------------------------------------------

* Wed Jan 10 2018 Charles Barcza <info@blackpanther.hu> 4.14.13-1bP
- platform/x86: wmi: Call acpi_wmi_init() later
- kvm: vmx: Scrub hardware GPRs at VM-exit
  (partial mitigation for CVE 2017-5715 and CVE 2017-5753)
- update to 4.14.13
------------------------------------------------------------------