# mckernel spec file usage:
# cmake /path/to/mckernel && make dist
# cp -i ./mckernel-@MCKERNEL_VERSION@.tar.gz <rpmbuild>/SOURCES
# rpmbuild -ba scripts/mckernel.spec
#
# for cross compilation also pass at least the following to rpmbuild:
# --target aarch64 -D 'kernel_version <target kernel>'
# you will need to setup your your environment as per
# cmake/README.cross_compilation

# Prevent rpmbuild from including build-id directories into package
%define _build_id_links none

%{!?kernel_version: %global kernel_version @UNAME_R@}
%{!?kernel_dir: %global kernel_dir /usr/src/kernels/%{kernel_version}}
%define krequires %(echo %{kernel_version} | sed "s/.%{_target_cpu}$//")
%define ktag %(echo %{krequires} | tr '-' '_' | sed -e 's/\.el[0-9]*$//' | sed -e 's/\.\([a-zA-Z]\)/_\1/')

Name: mckernel
Version: @MCKERNEL_VERSION@
Release: @MCKERNEL_RELEASE@_%{ktag}%{?dist}
Summary: IHK/McKernel
License: GPLv2
Source0: mckernel-%{version}.tar.gz

# kernel_module_package macro does not handle cross build...
%if "%{?_host_cpu}" == "x86_64" && "%{?_target_cpu}" == "aarch64"
%define cross_compile 1
%if 0%{?rhel} >= 8
Requires: kernel >= %{krequires}
%else
Requires: kernel = %{krequires}
%endif
Requires(post):   /usr/sbin/depmod
Requires(postun): /usr/sbin/depmod
%else
BuildRequires: systemd-devel numactl-devel binutils-devel kernel-devel
# Friendly reminder of the fact that kernel-rpm-macros is no longer included in kernel-devel
%if 0%{?rhel} >= 8
BuildRequires: redhat-rpm-config kernel-rpm-macros elfutils-libelf-devel kmod
%endif
%if %{defined kernel_module_package_buildreqs}
BuildRequires: %kernel_module_package_buildreqs
%kernel_module_package %{?kmod_flavors}
%endif
%endif

%description
Interface for Heterogeneous Kernels and McKernel.

%package	devel
Summary:	Headers and libraries required for build apps using IHK/McKernel
Requires:	%{name} = %{version}-%{release}

%description	devel
This package contains headers and libraries required for build apps using IHK/McKernel.

%prep
%setup -q

%build
%if 0%{?cross_compile}
%{!?toolchain_file: %global toolchain_file cmake/cross-aarch64.cmake}
%{!?build_target: %global build_target smp-arm64}
%{!?cmake_libdir: %global cmake_libdir /usr/lib64}
%endif

# We need to remove ld flags like relro for the final mckernel.img link, as well as remove cflags for mckernel
# ideally mckernel should use different environment variables for the user tools and the kernel tools altogether...
%define __global_ldflags ""
%define optflags -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4 -grecord-gcc-switches -mtune=generic

%cmake -DCMAKE_BUILD_TYPE=Release \
	-DUNAME_R=%{kernel_version} \
	-DKERNEL_DIR=%{kernel_dir} \
	%{?cmake_libdir:-DCMAKE_INSTALL_LIBDIR=%{cmake_libdir}} \
	%{?build_target:-DBUILD_TARGET=%{build_target}} \
	%{?toolchain_file:-DCMAKE_TOOLCHAIN_FILE=%{toolchain_file}} \
	.
%make_build

%install
%make_install

%files
%ifarch x86_64
%{_datadir}/mckernel/smp-x86/mckernel.img
%endif
%ifarch aarch64
%{_datadir}/mckernel/smp-arm64/mckernel.img
%endif
%{_sbindir}/mcreboot.sh
%{_sbindir}/mcstop+release.sh
%{_sbindir}/ihkconfig
%{_sbindir}/ihkosctl
%{_sbindir}/ihkmond
%{_bindir}/mcexec
%{_bindir}/eclair
%{_bindir}/vmcore2mckdump
%{_bindir}/mcstat
%{_libdir}/libihk.so
%{_libdir}/libsched_yield.so.1.0.0
%{_libdir}/libsched_yield.so
%{_libdir}/libldump2mcdump.so
%{_sysconfdir}/irqbalance_mck.in
%{_mandir}/man1/mcreboot.1.gz
%{_mandir}/man1/ihkconfig.1.gz
%{_mandir}/man1/ihkosctl.1.gz
%{_mandir}/man1/mcexec.1.gz

%if 0%{?cross_compile}
/lib/modules/%{kernel_version}/extra/mckernel/ihk.ko
/lib/modules/%{kernel_version}/extra/mckernel/mcctrl.ko
%ifarch x86_64
/lib/modules/%{kernel_version}/extra/mckernel/ihk-smp-x86_64.ko
%endif
%ifarch aarch64
/lib/modules/%{kernel_version}/extra/mckernel/ihk-smp-arm64.ko
%endif
%endif

%files	devel
%{_includedir}/ihklib.h
%{_includedir}/ihk/affinity.h
%{_includedir}/ihk/ihk_rusage.h
%{_includedir}/ihk/archdefs.h
%{_includedir}/ihk/status.h
%{_includedir}/ihk/ihk_monitor.h
%{_includedir}/ihk/ihk_debug.h
%{_includedir}/ihk/ihk_host_driver.h
/lib/modules/%{kernel_version}/extra/mckernel/ihk/linux/core/Module.symvers

%if 0%{?cross_compile}
# scripts from /usr/lib/rpm/redhat/kmodtool (kernel_module_package) as well
%post
if [ -e "/boot/System.map-%{kernel_version}" ]; then
    /usr/sbin/depmod -aeF "/boot/System.map-%{kernel_version}" "%{kernel_version}" > /dev/null || :
fi

modules=( $(find /lib/modules/%{kernel_version}/extra/%{name} | grep '\.ko$') )
if [ -x "/sbin/weak-modules" ]; then
    printf '%s\n' "${modules[@]}" \
    | /sbin/weak-modules --add-modules
fi

%preun
rpm -ql %{name}-%{version} | grep '\.ko$' > /var/run/rpm-kmod-%{name}-modules

%postun
if [ -e "/boot/System.map-%{kernel_version}" ]; then
    /usr/sbin/depmod -aeF "/boot/System.map-%{kernel_version}" "%{kernel_version}" > /dev/null || :
fi

modules=( $(cat /var/run/rpm-kmod-%{name}-modules) )
rm /var/run/rpm-kmod-%{name}-modules
if [ -x "/sbin/weak-modules" ]; then
    printf '%s\n' "${modules[@]}" \
    | /sbin/weak-modules --remove-modules
fi
%endif

%changelog
* Tue Feb 12 2019 Dominique Martinet <dominique.martinet@cea.fr> - 1.6.0-0
- Initial package
