%global pkg dinghai
%define pkg_version 2.24.30.01
%define release 2

%global debug_package %{nil}

Name:        kmod-%{pkg} 
Version:     %{pkg_version}
Release:     %{release}%{?dist}
Summary:     ZTE SmartNIC and DPU driver
License:     GPLv2
URL:         https://gitee.com/src-openeuler/kmod-dinghai
Source0:     kmod-%{pkg}.tar.gz
Epoch:       1

# 安装依赖, 和内核版本对应
Requires(posttrans): %{_sbindir}/depmod
Requires(postun):    %{_sbindir}/depmod
Requires(posttrans): %{_sbindir}/weak-modules
Requires(postun):    %{_sbindir}/weak-modules
Requires(posttrans): %{_bindir}/sort
Requires(postun):    %{_bindir}/sort

# 构建依赖, 和内核版本对应
BuildRequires:    elfutils-libelf-devel
BuildRequires:    gcc
BuildRequires:    kmod
BuildRequires:    make
BuildRequires:    system-rpm-config
BuildRequires:    perl-Data-Dumper
BuildRequires:    kernel
BuildRequires:    kernel-headers
BuildRequires:    kernel-devel

%define kernel_version %(rpm -q kernel-devel | sed 's/kernel-devel-//')
%define kernel_source /usr/src/kernels/%{kernel_version}

%description
RPM Package for ZXDH Driver

%prep
%setup -q -n kmod-%{pkg}

%build
cat /usr/include/linux/version.h
echo "kernel_version:%{kernel_version}"
pushd crypto/zsda
%{__make} %{?_smp_mflags} -C %{kernel_source} M=$PWD modules CONFIG_ACC_ZSDA_COMMON=m CONFIG_ACC_ZSDA_PF=m CONFIG_ACC_ZSDA_VF=m
popd
pushd net/build
    ./builder.sh -t clean --ksrc %{kernel_source}
    ./builder.sh -t kernel -m CONFIG_DINGHAI_ETH  -m CONFIG_ZXDH_AUXILIARY -m CONFIG_DINGHAI_PF -m CONFIG_ZXDH_SF -m CONFIG_DINGHAI_EN_AUX -m HAVE_DEV_PM_DOMAIN_ATTACH -m HAVE_BUS_FIND_DEVICE_GET_CONST -m CONFIG_DINGHAI_DH_CMD -m CONFIG_DINGHAI_NP -m CONFIG_ZXDH_MSGQ -m CONFIG_ZXDH_HARDWARE_BOND -m CONFIG_DINGHAI_PTP -m CONFIG_DINGHAI_TSN -m HAVE_DEVLINK_ALLOC_GET_1_PARAMS --ksrc %{kernel_source} -j 7
popd
pushd rdma
    ./build.sh --ksrc %{kernel_source} --use_rel_net_path
popd

%install
mkdir -p %{buildroot}/lib/modules/%{kernel_version}/extra/dinghai/crypto/
mkdir -p %{buildroot}/lib/modules/%{kernel_version}/extra/dinghai/net/
mkdir -p %{buildroot}/lib/modules/%{kernel_version}/extra/dinghai/rdma
for ko in $(find crypto/zsda/accdevice/ net/drivers/ rdma/ -name '*.ko'); do
    if [[ $ko == crypto/zsda/accdevice/* ]]; then
        target_dir=%{buildroot}/lib/modules/%{kernel_version}/extra/dinghai/crypto/
    elif [[ $ko == net/drivers/* ]]; then
        target_dir=%{buildroot}/lib/modules/%{kernel_version}/extra/dinghai/net/
    elif [[ $ko == rdma/* ]]; then
        target_dir=%{buildroot}/lib/modules/%{kernel_version}/extra/dinghai/rdma/
    fi
    %{__install} -D -t $target_dir $ko
done

ls -l /lib/modules/%{kernel_version}


# Make .ko objects temporarily executable for automatic stripping
find %{buildroot}/lib/modules -type f -name \*.ko -exec chmod u+x \{\} \+

# Generate depmod.conf
%{__install} -d %{buildroot}/%{_sysconfdir}/depmod.d/
for kmod in $(find %{buildroot}/lib/modules/%{kernel_version}/extra -type f -name \*.ko -printf "%%P\n" | sort)
do
    echo "override $(basename $kmod .ko) * weak-updates/$(dirname $kmod)" >> %{buildroot}/%{_sysconfdir}/depmod.d/%{pkg}.conf
    echo "override $(basename $kmod .ko) * extra/$(dirname $kmod)" >> %{buildroot}/%{_sysconfdir}/depmod.d/%{pkg}.conf
done

# Add zxdh_en_aux udev conf

%{__install} -d %{buildroot}%{_sysconfdir}/udev/rules.d/
echo 'ACTION!="add", GOTO="drivers_end"' > %{buildroot}%{_sysconfdir}/udev/rules.d/80-%{name}-zxdh_en_aux-drivers.rules
echo 'ENV{MODALIAS}=="zxdh_auxiliary:zxdh_pf.en_aux", RUN{builtin}+="kmod load zxdh_en_aux"' >> %{buildroot}%{_sysconfdir}/udev/rules.d/80-%{name}-zxdh_en_aux-drivers.rules
echo 'LABEL="drivers_end"' >> %{buildroot}%{_sysconfdir}/udev/rules.d/80-%{name}-zxdh_en_aux-drivers.rules

%clean
%{__rm} -rf %{buildroot}

%post
depmod -a > /dev/null 2>&1

%preun
list_file="/var/run/rpm-%{pkg}-modules.list"
mkdir -p /var/run
for ko in $(find /lib/modules/%{kernel_version}/extra/dinghai/ -name '*.ko'); do
    echo "${ko}" >> $list_file
done

%postun
depmod -a > /dev/null 2>&1

if [ -x "/usr/sbin/weak-modules" ]; then
    modules=( $(cat /var/run/rpm-%{pkg}-modules.list) )
    printf '%s\n' "${modules[@]}" | /usr/sbin/weak-modules --no-initramfs --remove-modules
fi
rm /var/run/rpm-%{pkg}-modules.list

%files
%defattr(644,root,root,755)
/lib/modules/%{kernel_version}
%config(noreplace) %{_sysconfdir}/depmod.d/%{pkg}.conf
%{_sysconfdir}/udev/rules.d/80-%{name}-zxdh_en_aux-drivers.rules

%changelog
* Fri Nov 29 2024 hcf <han.chengfei@zte.com.cn> - 1:2.24.30.01-2
- rename compile shell

* Fri Nov 29 2024 hcf <han.chengfei@zte.com.cn> - 1:2.24.30.01-1
- update pkg_version

* Mon Oct 28 2024 hcf <han.chengfei@zte.com.cn> - 1:2.24.20.03-1
- Add net and rdma ko

* Wed Aug 28 2024 hcf <han.chengfei@zte.com.cn> - 1:2.24.20.02-1
- Dinghai driver init
