File displaylink.spec of Package displaylink

#
# spec file for package displaylink
#
# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany.
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via http://bugs.opensuse.org/
#

%bcond_without check

%global debug_package %{nil}
%if 0%{?rhel}
%global kernel_pkg_name kernel-mt
%else
%global kernel_pkg_name kernel
%endif

%if 0%{?is_opensuse} || 0%{?rhel}
%global kernel_headers_name %{kernel_pkg_name}-default
%else
%global kernel_headers_name %{kernel_pkg_name}-headers
%endif

%if 0%{?is_opensuse} && 0%{?sle_version} == 0
%global source_date_epoch_from_changelog 0
%endif

%define _dllibdirorig /usr/libexec/displaylink
%define _dllibdir %{_libdir}/dl

%define _daemon_version 6.1.1-17
%define _daemon_major_version 6.1.1
%define _daemon_file_version 6.1.1-EXE
%define _evdi_version 1.14.10
%define _evdi_dir evdi-%{_evdi_version}

#%define _unpackaged_files_terminate_build 0

Name:           displaylink
Version:        6.1.1
Release:        0
Summary:        DisplayLink VGA/HDMI driver for DL-6xxx, DL-5xxx, DL-41xx and DL-3xxx adapters
License:        GPL-2.0 and LGPL-2.1 and Proprietary
Group:          Hardware/Other
Url:            https://github.com/displaylink-rpm/displaylink-rpm
Source0:        https://github.com/DisplayLink/evdi/archive/v%{_evdi_version}.tar.gz#/evdi-%{_evdi_version}.tar.gz
Source1:        %{name}.service
Source2:        99-%{name}.rules
Source3:        displaylink-sleep-extractor.sh
# From http://www.displaylink.com/downloads/ubuntu.php
Source4:        DisplayLink_USB_Graphics_Software_for_Ubuntu%{_daemon_file_version}.zip
Source5:        20-%{name}.conf
Patch0:         global.patch
Patch1:         opensuse15.5.patch
Patch2:         Tumbleweed.patch
ExclusiveArch:  x86_64
BuildRequires:  chrpath
BuildRequires:  patchelf
BuildRequires:  dkms
BuildRequires:  libdrm-devel
BuildRequires:  systemd-rpm-macros
%{?systemd_requires}
BuildRequires:  udev
BuildRequires:  unzip
BuildRequires:  -post-build-checks
%if 0%{?is_opensuse} && 0%{?sle_version} == 0
BuildRequires:  -brp-check-suse
%endif

BuildRequires:  %{kernel_headers_name} > 4.7
BuildRequires:  %{kernel_pkg_name}-devel > 4.7
Requires:       %{kernel_pkg_name} > 4.7
Requires:       %{kernel_pkg_name}-devel > 4.7
Requires:       %{kernel_headers_name} > 4.7
Requires:       dkms
# %if 0%{?fedora} > 39
# BuildRequires:       libzstd-devel
# %endif


%description
This adds support for HDMI/VGA adapters built upon the DisplayLink
DL-6xxx, DL-5xxx, DL-41xx and DL-3xxx series of chipsets. This 
includes numerous docking stations, USB monitors, and USB adapters.

%prep
%setup -q -c %{_evdi_dir}
cd %{_evdi_dir}

unzip "%{SOURCE4}"
chmod +x displaylink-driver-%{_daemon_version}.run
./displaylink-driver-%{_daemon_version}.run --noexec --keep
# This creates a displaylink-driver-$version subdirectory



%if 0%{?sle_version} == 150500 && 0%{?is_opensuse}
%patch -P1 -p1
%endif


%build
cd %{_evdi_dir}/library/
make %{?_smp_mflags}

%install
mkdir -p %{buildroot}%{_dllibdir}                   \
         %{buildroot}/usr/src/evdi-%{_evdi_version}/      \
         %{buildroot}/usr/lib/systemd/system/       \
         %{buildroot}/usr/lib/systemd/system-sleep  \
         %{buildroot}/usr/lib/udev/rules.d/             \
         %{buildroot}/etc/X11/xorg.conf.d/          \
         %{buildroot}/var/log/displaylink/          \
         %{buildroot}%{_sbindir}

ln -s service %{buildroot}%{_sbindir}/rc%{name}

# Kernel driver sources
cp -a %{_evdi_dir}/module/* %{buildroot}/usr/src/evdi-%{_evdi_version}

# Library
cp %{_evdi_dir}/library/libevdi.so.%{_evdi_version} %{buildroot}%{_dllibdir}/
ln -sf libevdi.so.%{_evdi_version} %{buildroot}%{_dllibdir}/libevdi.so.0
ln -sf libevdi.so.0 %{buildroot}%{_dllibdir}/libevdi.so

# Binaries
# Don't copy libusb-1.0.so.0.1.0 it's already shipped by libusbx
# Don't copy libevdi.so, we compiled it from source
#tree .
#cd {_evdi_dir}/displaylink-driver-{_daemon_version}
cd %{_evdi_dir}/displaylink-driver-%{_daemon_major_version}

sed -i 's/\r//' LICENSE
chmod -x LICENSE
cp LICENSE ../..

%ifarch x86_64
cp -a x64-ubuntu-1604/DisplayLinkManager %{buildroot}%{_dllibdir}
%endif

%ifarch %ix86
cp -a x86-ubuntu-1604/DisplayLinkManager %{buildroot}%{_dllibdir}
%endif

#chrpath -r %{_dllibdir} %{buildroot}%{_dllibdir}/DisplayLinkManager
patchelf --force-rpath --set-rpath %{_dllibdir} %{buildroot}%{_dllibdir}/DisplayLinkManager
# Firmwares
cp -a ella-dock-release.spkg firefly-monitor-release.spkg %{buildroot}%{_dllibdir}

# systemd/udev
sed -i "s_%{_dllibdirorig}_%{_dllibdir}_" %{SOURCE1}
cp -a %{SOURCE1} %{buildroot}/usr/lib/systemd/system/
cp -a %{SOURCE2} %{buildroot}/usr/lib/udev/rules.d/
cp -a %{SOURCE5} %{buildroot}/etc/X11/xorg.conf.d/

# pm-util
bash %{SOURCE3} displaylink-installer.sh > %{buildroot}/usr/lib/systemd/system-sleep/%{name}.sh

chmod +x %{buildroot}/usr/lib/systemd/system-sleep/%{name}.sh

%check
%if 0%{?is_opensuse} && 0%{?sle_version} != 0
CHECKDIR=%{_tmppath}/evdi-dkms-check
mkdir -p $CHECKDIR
DKMS_ARGS="--dkmstree $CHECKDIR \
           --sourcetree %{buildroot}/usr/src
           --installtree $CHECKDIR \
           -k `rpm -q -v %{kernel_headers_name} |sed 's/.*[a-z]-\([0-9.lp-]\+\)\.[0-9].*/\1-default/'`"

%if %{with check}
/usr/sbin/dkms $DKMS_ARGS build -m evdi -v %{_evdi_version} || (cat $CHECKDIR/evdi/%{_evdi_version}/build/make.log && exit 10)
%else
/usr/sbin/dkms $DKMS_ARGS build -m evdi -v %{_evdi_version} || cat $CHECKDIR/evdi/%{_evdi_version}/build/make.log
%endif

%endif

%pre
%if 0%{?is_opensuse}
%service_add_pre %{name}.service
%endif

%post
%if 0%{?is_opensuse}
%service_add_post %{name}.service
%else
%systemd_post %{name}.service
%endif

/usr/bin/systemctl -q is-enabled dkms.service || echo "warning: dkms service not enabled."


%preun

%if 0%{?is_opensuse}
%service_del_preun %{name}.service
%else
%systemd_preun %{name}.service
%endif

/usr/sbin/dkms remove -m evdi -v %{_evdi_version} -q --all || :
rm -Rf /var/lib/dkms/evdi/%{_evdi_version}

%postun
# restart later in posttrans
%if 0%{?is_opensuse}
%service_del_postun_without_restart %{name}.service
%else
%systemd_postun %{name}.service
%endif


# as suggested by https://github.com/dell/dkms/issues/25#issuecomment-360275619
%posttrans
/usr/sbin/dkms add -m evdi -v %{_evdi_version} || :
/usr/sbin/dkms install -m evdi -v %{_evdi_version} -q --force || :
/usr/bin/systemctl restart %{name}.service

%files
%doc LICENSE
/usr/lib/systemd/system/%{name}.service
/usr/lib/systemd/system-sleep/%{name}.sh
/usr/lib/udev/rules.d/99-%{name}.rules
%config /etc/X11/xorg.conf.d/20-%{name}.conf
%dir /usr/src/evdi-%{_evdi_version}
/usr/src/evdi-%{_evdi_version}/*
%dir %{_dllibdir}
%{_dllibdir}/*
%dir /var/log/displaylink/
%{_sbindir}/rc%{name}

%changelog
openSUSE Build Service is sponsored by