#needsrootforbuild
%global __cargo_skip_build 0
%global __cargo_common_opts %{?__cargo_common_opts} --all
Name:           sysboost
Version:        1.0.0
Release:        5
Summary:        Numa Aware Scheduling
License:        Mulan PSL v2
URL:            https://gitee.com/openeuler/native-turbo

Source0:        %{name}.tar.xz

BuildRequires: meson >= 0.49.2, clang >= 12.0.1
BuildRequires: make >= 4.0, bison >= 2.7, binutils >= 2.30-17, gcc >= 10.3.1
BuildRequires: xz
BuildRequires: ncurses-devel
BuildRequires: rust rust-packaging cargo
BuildRequires: kernel-devel

%define kern_devel_ver %(ver=`rpm -qa|grep kernel-devel`;echo ${ver#*kernel-devel-})

%description
sysboost can merge ELF files to improve performance.

# Add devel subpackage
%package devel
Summary: Development files for %{name}
Requires: %{name} = %{version}-%{release}
Requires: xz
BuildRequires: binutils

%description devel
The %{name}-devel package contains some tools of sysboost.

%prep
export RPM_BUILD_DIR=%_topdir/BUILD
export RPM_BUILD_SOURCE=%_topdir/SOURCES

tar -Jxf $RPM_BUILD_SOURCE/%{name}.tar.xz

cp $RPM_BUILD_SOURCE/objreloc.sh $RPM_BUILD_DIR/objreloc
cp $RPM_BUILD_DIR/native-turbo/sysboost/Cargo.toml $RPM_BUILD_DIR/

%cargo_prep

%cargo_generate_buildrequires

%build
namer=%{kern_devel_ver}
cd $RPM_BUILD_DIR/native-turbo/sysboost/src/binfmt_rto
%ifarch aarch64
make %{?_smp_mflags} ARCH=arm64 KDIR=/lib/modules/${namer}/build
%else
make %{?_smp_mflags} ARCH=x86_64 KDIR=/lib/modules/${namer}/build
%endif

cd $RPM_BUILD_DIR/native-turbo/sysboost
%cargo_build -a

cd $RPM_BUILD_DIR/native-turbo
make release
make

%install
mkdir -p $RPM_BUILD_ROOT/%{_bindir}
mkdir -p $RPM_BUILD_ROOT/usr/lib/systemd/system
mkdir -p $RPM_BUILD_ROOT/lib/modules/sysboost
mkdir -p $RPM_BUILD_ROOT/usr/lib/relocation

install -m 440 $RPM_BUILD_DIR/native-turbo/sysboost/src/binfmt_rto/binfmt_rto.ko $RPM_BUILD_ROOT/lib/modules/sysboost/

pushd native-turbo/build
install -D -p ./sysboost/sysboost $RPM_BUILD_ROOT/%{_bindir}/sysboost
xz ./sysboost/src/static_template/sysboost_static_template
install -D -p -m 400 ./sysboost/src/static_template/sysboost_static_template.xz $RPM_BUILD_ROOT/usr/lib/relocation/sysboost_static_template.relocation
xz ./sysboost/src/libhook/libhook.so
install -D -p -m 400 ./sysboost/src/libhook/libhook.so.xz $RPM_BUILD_ROOT/usr/lib/relocation/libhook.so.relocation
popd

pushd native-turbo/sysboost
install -D -p -m 440 %{name}.service $RPM_BUILD_ROOT/usr/lib/systemd/system/%{name}.service
install -D -p ./target/release/sysboostd $RPM_BUILD_ROOT%{_bindir}/
popd

install -m 755 $RPM_BUILD_DIR/objreloc $RPM_BUILD_ROOT%{_bindir}/

mkdir -p %{buildroot}/etc/sysboost.d/
# for DB
mkdir -p %{buildroot}/var/lib/sysboost/

%check

%files
%defattr(-,root,root,-)
%dir %attr(0700,root,root) /etc/sysboost.d/
%dir %attr(0700,root,root) /var/lib/sysboost/
%attr(0500,root,root) %{_bindir}/sysboost
%attr(0500,root,root) %{_bindir}/sysboostd
%attr(0400,root,root) /usr/lib/relocation/sysboost_static_template.relocation
%attr(0400,root,root) /usr/lib/relocation/libhook.so.relocation
%attr(0440,root,root) /usr/lib/systemd/system/%{name}.service
%attr(0400,root,root) /lib/modules/sysboost/*

%files devel
%defattr(0755,root,root)
%{_bindir}/objreloc

%doc

%post
%systemd_post %{name}.service

%preun
%systemd_preun %{name}.service

%postun
%systemd_postun_with_restart %{name}.service

%changelog
* Wed Jun 7 2023 Zhou Kang <gameoverboss@163.com> - 1.0.0-5
- Type:bugfix
- ID:NA
- SUG:NA
- DESC: fix path of relocation

* Fri May 19 2023 liutie <liutie4@huawei.com> - 1.0.0-4
- Type:enhancement
- ID:NA
- SUG:NA
- DESC: Add binfmt_rto

* Tue May 16 2023 liutie <liutie4@huawei.com> - 1.0.0-3
- Type:enhancement
- ID:NA
- SUG:NA
- DESC: Add sysboostd service for sysboost

* Mon May 8 2023 liutie <liutie4@huawei.com> - 1.0.0-2
- Type:enhancement
- ID:NA
- SUG:NA
- DESC: Independently generate the objreloc tool

* Mon May 8 2023 liutie <liutie4@huawei.com> - 1.0.0-1
- Type:enhancement
- ID:NA
- SUG:NA
- DESC: Independently generate the objreloc tool

* Thu Apr 6 2023 liutie <liutie4@huawei.com> - 1.0.0-0
