File ethercat.spec of Package ethercat

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

# needssslcertforbuild

# 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
Source1: preamble
Patch0:  openSUSE-Leap-42.3-ndo.patch

%{?systemd_requires}

BuildRequires: kernel-syms modutils
BuildRequires: %{kernel_module_package_buildreqs}
BuildRequires: gcc gcc-c++ coreutils module-init-tools doxygen
BuildRequires: autoconf-archive
BuildRequires: autoconf automake libtool
BuildRequires: pesign-obs-integration
%ifarch %ix86
BuildRequires: kernel-pae-devel
%else
BuildRequires: kernel-default-devel
%endif
BuildRequires: pkgconfig
BuildRequires: pkgconfig(systemd)

# since openSUSE Leap 15.2 SUSE provides an own RT kernel again, not for aarch64
%if 0%{?sle_version} >= 150200
%ifnarch aarch64 %arm
BuildRequires: kernel-rt-devel
%endif
%endif

%kernel_module_package -p %_sourcedir/preamble -c %_sourcedir/_projectcert.crt

%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 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

%setup -q -c -n source

# 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
cd %{name}-%{version}
%patch0 -p1
cd ..
%endif

# Write revision information (known here though set_version) to file, so that
# revision hooks can find it.
echo %{version} >> %{name}-%{version}/revision 

cd ..

for flavor in %flavors_to_build; do
    kmajorminor=`make -s -C %{kernel_source $flavor} kernelversion | cut -d . --complement -f 3-`
    echo "using $flavor in version $kmajorminor"
    KMODSDEF=""
    for km in 8139too e100; do
        if [ -f source/%{name}-%{version}/devices/$km-$kmajorminor-ethercat.c ]; then
            KMODSDEF="$KMODSDEF --enable-$km"
        else
            KMODSDEF="$KMODSDEF --disable-$km"
        fi
    done
    for km in e1000 igb igbvf ixgbevf igc; do
        if [ -f source/%{name}-%{version}/devices/$km/$km-$kmajorminor-ethercat.h ]; then
            KMODSDEF="$KMODSDEF --enable-$km"
        else
            KMODSDEF="$KMODSDEF --disable-$km"
        fi
    done
    if [ -f source/%{name}-%{version}/devices/e1000e/e1000-$kmajorminor-ethercat.h ]; then
        KMODSDEF="$KMODSDEF --enable-e1000e"
    else
        KMODSDEF="$KMODSDEF --disable-e1000e"
    fi

    if [ -f source/%{name}-%{version}/devices/r8169-$kmajorminor-ethercat.c -o \
         -f source/%{name}-%{version}/devices/r8169/r8169-$kmajorminor-ethercat.h ]; then
        KMODSDEF="$KMODSDEF --enable-r8169"
    else
        KMODSDEF="$KMODSDEF --disable-r8169"
    fi

    if [ -r source/%{name}-%{version}/devices/genet/bcmgenet-$kmajorminor-ethercat.h ]; then
        KMODSDEF="$KMODSDEF --enable-genet"
    else
        KMODSDEF="$KMODSDEF --disable-genet"
    fi

    if [ -r source/%{name}-%{version}/devices/stmmac/dwmac-intel-$kmajorminor-ethercat.c ]; then
        KMODSDEF="$KMODSDEF --enable-dwmac-intel"
    else
        KMODSDEF="$KMODSDEF --disable-dwmac-intel"
    fi

    if [ -r source/%{name}-%{version}/devices/stmmac/stmmac_pci-$kmajorminor-ethercat.c ]; then
        KMODSDEF="$KMODSDEF --enable-stmmac-pci"
    else
        KMODSDEF="$KMODSDEF --disable-stmmac-pci"
    fi

    cp -r source/%{name}-%{version} build-$flavor
    pushd build-$flavor
    
    ./bootstrap
    CFLAGS="%{optflags} -ffat-lto-objects" %configure \
        --with-linux-dir=%{kernel_source $flavor} \
        --with-module-dir=extra \
        --enable-sii-assign \
        --disable-rt-syslog \
        --enable-generic \
        --enable-ccat \
        $KMODSDEF
    popd
done

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

%build

cd ..
for flavor in %flavors_to_build; do
    pushd build-$flavor
    make %{?_smp_mflags} modules V=1
    popd
done

pushd build-default
make %{?_smp_mflags} all doc
popd

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

%install

export INSTALL_MOD_PATH=%{buildroot}

cd ..
for flavor in %flavors_to_build; do
    pushd build-$flavor
    make %{?_smp_mflags} modules_install
    popd
done

pushd build-default
mv doxygen-output/html ../source/
mv AUTHORS COPYING COPYING.LESSER FEATURES.md README.md INSTALL.md CodingStyle.md TODO ../source/
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

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

%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

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

%files
%defattr(-,root,root,-)
%doc AUTHORS COPYING COPYING.LESSER FEATURES.md README.md INSTALL.md CodingStyle.md 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,-)
%dir %{_libdir}/cmake
%dir %{_libdir}/pkgconfig
%{_includedir}/ecrt.h
%{_includedir}/ectty.h
%{_libdir}/libethercat.a
%{_libdir}/libethercat.so
%{_libdir}/cmake/%{name}
%{_libdir}/pkgconfig/libethercat.pc

%files bash-completion
%defattr(-,root,root,-)
%{_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
* Mon Dec 6 2021 vh@igh.de
- Remove non-systemd option
- use kernel_module_package macro
* 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