File ethercat.spec of Package ethercat

#-----------------------------------------------------------------------------
# SPEC file for EtherCAT master
#-----------------------------------------------------------------------------

%define kmod_rt 1

%define kernelrevstrip %(rpm -q --qf '%%{r}' kernel-default | sed -r 's@^(.+)\.[0-9]+$@\\1@')
%define ksourcedir %(rpm -q --qf '/usr/src/linux-%%{v}-@MODREV@-obj/%%{arch}/default' kernel-default | sed -e 's/@MODREV@/%{kernelrevstrip}/')
%define kernelversion %(rpm -q --qf '%%{v}' kernel-default)
%define kernelrelease %(cat %{ksourcedir}/include/config/kernel.release)

%define rtkernelversion %(rpm -q --qf '%%{v}' kernel-rt)
# since openSUSE Leap 15.2 SUSE prvides an own RT kernel again
%if 0%{?sle_version} < 150200
%define rtksourcedir /usr/src/linux-%{rtkernelversion}
%else
%define rtkernelrevstrip %(rpm -q --qf '%%{r}' kernel-rt | sed -r 's@^(.+)\.[0-9]+$@\\1@')
%define rtksourcedir %(rpm -q --qf '/usr/src/linux-%%{v}-@MODREV@-rt-obj/%%{arch}/rt' kernel-rt | sed -e 's/@MODREV@/%{rtkernelrevstrip}/')
%endif
%define rtkernelrelease %(cat %{rtksourcedir}/include/config/kernel.release)

# default kernel version
%define defaultkmajor %(rpm -q --qf '%%{v}' kernel-default | cut -d . -f 1)
%if %{defaultkmajor} == 2
%define defaultkminor %(rpm -q --qf '%%{v}' kernel-default | cut -b 3-)
%else
%define defaultkminor %(rpm -q --qf '%%{v}' kernel-default | cut -d . -f 2)
%endif

%if %kmod_rt

# RT kernel version
%define rtkmajor %(rpm -q --qf '%%{v}' kernel-rt | cut -d . -f 1)
%if %{rtkmajor} == 2
%define rtkminor %(rpm -q --qf '%%{v}' kernel-rt | cut -b 3-)
%else
%define rtkminor %(rpm -q --qf '%%{v}' kernel-rt | cut -d . -f 2)
%endif

%endif

# set by set_version service
%define version_unconverted 1.5
%define master_version %{version_unconverted}

#-----------------------------------------------------------------------------

Name:    ethercat
Version: 1.5
Release: 1
Summary: Master Tools and Utilities
Group:   System/Daemons
License: GPL-2.0
Url:     http://www.etherlab.org/en/ethercat
Source0: http://www.etherlab.org/download/ethercat/ethercat-%{version}.tar.bz2
Patch0: openSUSE-Leap-42.3-ndo.patch
Prereq: /sbin/ldconfig

%{?systemd_requires}

BuildRequires: kernel-syms modutils
BuildRequires: gcc gcc-c++ coreutils module-init-tools doxygen
BuildRequires: autoconf automake libtool
BuildRequires: mercurial

%description
Master implementation of the EtherCAT protocol;
part of the Open-Source Automation Toolkit "EtherLab".

#-----------------------------------------------------------------------------

%package -n ethercat-doc
Summary:    Master Documentation
Group:      System/Libraries

%description -n ethercat-doc
Doxygen-generated API documentation

#-----------------------------------------------------------------------------

%package -n libethercat1
Summary:    Master Userspace Library
Group:      System/Libraries

%description -n libethercat1
Userspace Library providing the EtherCAT Master API

#-----------------------------------------------------------------------------

%package -n libethercat-devel
Summary:    Development files for Master Userspace Library
Group:      Development/Libraries/C and C++
Requires:   libethercat1 = %{version}

%description -n libethercat-devel
Development and header files for the
EtherCAT Master and its user-space library

#-----------------------------------------------------------------------------

%package    kmp-default
Summary:    Master Kernel Modules
Version:    %{nil}_%{kernelversion}
Group:      System/Kernel
Requires:   kernel-default
Enhances:   kernel-default
BuildRequires: kernel-default-devel
BuildRequires: kernel-default
BuildRequires: module-init-tools
BuildRequires: kernel-syms modutils

%description kmp-default
Kernel modules for EtherCAT master (default kernel)

#-----------------------------------------------------------------------------

%if %kmod_rt

%package       kmp-rt
Summary:       Master Kernel Modules
Version:       %{nil}_%{rtkernelversion}
Group:         System/Kernel
Requires:      kernel-rt
Enhances:      kernel-rt

# since openSUSE Leap 15.2 SUSE prvides an own RT kernel again, but no header
# package
%if 0%{?sle_version} < 150200
BuildRequires: kernel-rt-headers
%else
BuildRequires: kernel-rt-devel
%endif

BuildRequires: kernel-rt
BuildRequires: module-init-tools
BuildRequires: kernel-syms modutils

%description kmp-rt
Kernel modules for EtherCAT master (PREEMPT_RT kernel)

%endif

#-----------------------------------------------------------------------------

%package        bash-completion
Summary:        Bash Completion for %{name}
Group:          System/Shells
Requires:       bash-completion

%if 0%{?sle_version} >= 120100 && 0%{?sle_version} <= 120300 && 0%{?is_opensuse}
# Binary operators not supported in Leap 42.1-3
%else
Supplements:    (%{name} and bash)
%endif

BuildArch:      noarch

%description bash-completion
Bash completions for the EtherCAT tool.

#-----------------------------------------------------------------------------

%prep

%if %kmod_rt

%setup -c -n ethercat-rt
echo "Using preempt_rt sources in %{rtksourcedir}"
echo "Version %{rtkernelversion}, release %{rtkernelrelease}"
echo "RT master version %{version}"

%endif

%setup -c -n ethercat-default
echo "Using default sources in %{ksourcedir}"
echo "Version %{kernelversion}, release %{kernelrelease}"

# now in last one, default
pushd %{name}-%{master_version}

# Check for openSUSE Leap 42.3 - it uses a patched
# kernel with a more recent networking API.
# Apply patch to default kernel build only!
echo "Checking for openSUSE version:"
echo "sle_version is %{?sle_version}%{!?sle_version:undefined}."
%if 0%{?sle_version} == 120300
%patch0 -p1
%endif

# Determine available Ethernet drivers for default kernel
KMODSDEF=""
for km in 8139too e100 r8169; do
    if [ -f devices/$km-%{defaultkmajor}.%{defaultkminor}-ethercat.c ]; then
        KMODSDEF="$KMODSDEF --enable-$km"
    else
        KMODSDEF="$KMODSDEF --disable-$km"
    fi
done
for km in e1000 e1000e igb; do
    if [ -f devices/$km/$km-%{defaultkmajor}.%{defaultkminor}-ethercat.h ]; then
        KMODSDEF="$KMODSDEF --enable-$km"
    else
        KMODSDEF="$KMODSDEF --disable-$km"
    fi
done

./bootstrap
%configure \
    --with-linux-dir=%{ksourcedir} \
    --enable-sii-assign --disable-rt-syslog \
    --enable-generic --enable-ccat $KMODSDEF
popd

%if %kmod_rt

cd ../ethercat-rt
pushd %{name}-%{master_version}

# Determine available Ethernet drivers for RT kernel
KMODSDEF_RT=""
for km in 8139too e100 r8169; do
    if [ -f devices/$km-%{rtkmajor}.%{rtkminor}-ethercat.c ]; then
        KMODSDEF_RT="$KMODSDEF_RT --enable-$km"
    else
        KMODSDEF_RT="$KMODSDEF_RT --disable-$km"
    fi
done
for km in e1000 e1000e igb; do
    if [ -f devices/$km/$km-%{rtkmajor}.%{rtkminor}-ethercat.h ]; then
        KMODSDEF_RT="$KMODSDEF_RT --enable-$km"
    else
        KMODSDEF_RT="$KMODSDEF_RT --disable-$km"
    fi
done

./bootstrap
%configure \
    --with-linux-dir=%{rtksourcedir} \
    --with-systemdsystemunitdir=%{_unitdir} \
    --enable-sii-assign --disable-rt-syslog \
    --enable-generic --enable-ccat $KMODSDEF_RT
popd

%endif

#-----------------------------------------------------------------------------

%build

pushd %{name}-%{master_version}
make %{?_smp_mflags} all modules doc
popd

%if %kmod_rt

cd ../ethercat-rt
pushd %{name}-%{master_version}
make %{?_smp_mflags} modules
popd

%endif

#-----------------------------------------------------------------------------

%install

export INSTALL_MOD_PATH=%{buildroot}

pushd %{name}-%{master_version}
make modules_install DEPMOD=/bin/true
mv doxygen-output/html ..
mv AUTHORS COPYING COPYING.LESSER FEATURES README TODO ..
make install DESTDIR=%{buildroot}

ln -sv /usr/sbin/service %{buildroot}/%{_sbindir}/rcethercat

rm %{buildroot}%{_sysconfdir}/init.d/ethercat

install -d %{buildroot}/usr/lib/udev/rules.d
echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\" > %{buildroot}/usr/lib/udev/rules.d/99-EtherCAT.rules

rm -rv %{buildroot}%{_sysconfdir}/sysconfig
rm %{buildroot}%{_libdir}/*.la

popd

%if %kmod_rt

cd ../ethercat-rt
pushd %{name}-%{master_version}
make modules_install DEPMOD=/bin/true
popd

%endif

#-----------------------------------------------------------------------------

%pre
%service_add_pre %{name}.service

%post
%service_add_post %{name}.service

%preun
%service_del_preun %{name}.service

%postun
/bin/systemctl --system daemon-reload &>/dev/null || :
%service_del_postun %{name}.service

#-----------------------------------------------------------------------------

%post -n libethercat1
/sbin/ldconfig

%postun -n libethercat1
/sbin/ldconfig

#-----------------------------------------------------------------------------

%post kmp-default
depmod -a -F /boot/System.map-%{kernelrelease} %{kernelrelease}

%postun kmp-default
depmod -a -F /boot/System.map-%{kernelrelease} %{kernelrelease}

#-----------------------------------------------------------------------------

%if %kmod_rt

%post kmp-rt
depmod -a -F /boot/System.map-%{rtkernelrelease} %{rtkernelrelease}

%postun kmp-rt
depmod -a -F /boot/System.map-%{rtkernelrelease} %{rtkernelrelease}

%endif

#-----------------------------------------------------------------------------

%files
%defattr(-,root,root,-)
%doc AUTHORS COPYING COPYING.LESSER FEATURES README TODO
%config(noreplace) %{_sysconfdir}/ethercat.conf
%{_unitdir}/%{name}.service
%dir /usr/lib/udev
%dir /usr/lib/udev/rules.d
/usr/lib/udev/rules.d/99-EtherCAT.rules
%{_bindir}/ethercat
%{_sbindir}/ethercatctl
%{_sbindir}/rcethercat

%files -n ethercat-doc
%defattr(-,root,root,-)
%doc html

%files -n libethercat1
%defattr(-,root,root,-)
%{_libdir}/libethercat.so.*

%files -n libethercat-devel
%defattr(-,root,root,-)
%{_includedir}/ecrt.h
%{_includedir}/ectty.h
%{_libdir}/libethercat.a
%{_libdir}/libethercat.so
%dir %{_libdir}/cmake
%dir %{_libdir}/cmake/ethercat
%{_libdir}/cmake/ethercat/ethercat-config.cmake
%dir %{_libdir}/pkgconfig
%{_libdir}/pkgconfig/libethercat.pc

%files -n ethercat-kmp-default
%defattr(-,root,root,-)
%if 0%{?usrmerged}
/usr/lib/modules/%{kernelrelease}/ethercat
%else
/lib/modules/%{kernelrelease}/ethercat
%endif

%if %kmod_rt

%files kmp-rt
%defattr(-,root,root,-)
/lib/modules/%{rtkernelrelease}/ethercat

%endif

%files bash-completion
%defattr(-,root,root,-)
%dir %{_datadir}/bash-completion/completions
%{_datadir}/bash-completion/completions/ethercat

#-----------------------------------------------------------------------------

%changelog
* Mon May 16 2022 fp@igh.de
- Added bash completion
* Wed Apr 6 2022 fp@igh.de
- Added udev file to grant read permissions to normal users
* Wed Mar 17 2021 fp@igh.de
- Included openSUSE's kernel-rt
* Tue Jul 9 2019 fp@igh.de
- Separate check for RT kernel Ethernet modules
* Wed Feb 28 2018 fp@igh.de
- Added option --enable-sii-assign

#-----------------------------------------------------------------------------
openSUSE Build Service is sponsored by