%bcond_without compat
%bcond_without sanitizers
%bcond_with abitest

%define build_compat32 %{?_with_compat32:1}%{!?_with_compat32:0}

# Compatible with the following zlib version.
# see ZLIB_VERSION from https://github.com/zlib-ng/zlib-ng/blob/develop/zlib.h.in
%global zlib_ver 1.3.1
# Obsoletes zlib versions less than.
%global zlib_obsoletes 1.3

# ABI files for ix86 and s390x are not available upstream.
%global supported_abi_test aarch64 ppc64le x86_64

Name:		zlib-ng
Summary:	Zlib replacement with optimizations
Summary(ja):	最適化されたZlib代替パッケージ
Version:	2.1.7
Release:	1%{?_dist_release}
Group:		system
Vendor:		Project Vine
Distribution:	Vine Linux

License:	Zlib
Url:		https://github.com/zlib-ng/zlib-ng
Source0:	https://github.com/zlib-ng/zlib-ng/archive/%{version}/%{name}-%{version}.tar.gz
Source99:	Changelog.fedora

Patch:		far.diff

BuildRequires:	cmake >= 3.1
BuildRequires:	gcc-c++
BuildRequires:	cmake(GTest) gmock
%if %{with abitest}
BuildRequires:	libabigail
%endif
%global __provides_exclude_from ^%{_libdir}/pkgconfig/zlib\.pc$

%description
zlib-ng is a zlib replacement that provides optimizations for "next generation"
systems.


%package	devel
Summary:	Development files for %{name}
Group:		programming
Requires:	%{name}%{?_isa} = %{version}-%{release}

%description	devel
The %{name}-devel package contains libraries and header files for developing
application that use %{name}.


%if %{with compat}

%package	compat
Summary:	Zlib implementation provided by %{name}
Group:		system
Provides:	zlib = %{zlib_ver}
Provides:	zlib%{?_isa} = %{zlib_ver}
Conflicts:	zlib%{?_isa}
Obsoletes:	zlib < %{zlib_obsoletes}

%description	compat
zlib-ng is a zlib replacement that provides optimizations for "next generation"
systems.
The %{name}-compat package contains the library that is API and binary
compatible with zlib.


%package	compat-devel
Summary:	Development files for %{name}-compat
Group:		programming
Requires:	%{name}-compat%{?_isa} = %{version}-%{release}
Provides:	zlib-devel = %{zlib_ver}
Provides:	zlib-devel%{?_isa} = %{zlib_ver}
Conflicts:	zlib-devel%{?_isa}
Obsoletes:	zlib-devel < %{zlib_obsoletes}
Provides:	pkgconfig(zlib) = %{zlib_ver}.zlib-ng

%description	compat-devel
The %{name}-compat-devel package contains libraries and header files for
developing application that use zlib.


%package	compat-static
Summary:	Static libraries for %{name}-compat
Group:		programming
Requires:	%{name}-compat-devel%{?_isa} = %{version}-%{release}
Provides:	zlib-static = %{zlib_ver}
Provides:	zlib-static%{?_isa} = %{zlib_ver}
Conflicts:	zlib-static%{?_isa}
Obsoletes:	zlib-static < %{zlib_obsoletes}

%description	compat-static
The %{name}-compat-static package contains static libraries needed for
developing applications that use zlib.


%package -n	compat32-zlib-ng-compat
Summary:	Zlib replacement with optimizations (32bit)
Summary(ja):	最適化されたZlib代替パッケージ (32bit)
Group:		system,legacy
Requires: zlib-ng-compat = %{version}-%{release}
Provides:	compat32-zlib = %{zlib_ver}

%description -n	compat32-zlib-ng-compat
This package contains 32bit version of zlib-ng-compat.


%package -n	compat32-zlib-ng-compat-devel
Summary:	Development files for %{name}-compat (32bit)
Summary(ja):	zlib-ng-compatを使ったプログラム開発に必要なヘッダファイル/ライブラリ (32bit)
Group:		programming,legacy
Requires:	zlib-ng-compat = %{version}-%{release}
Requires:	zlib-ng-compat-devel = %{version}-%{release}
Provides:	compat32-zlib-devel = %{zlib_ver}

%description -n compat32-zlib-ng-compat-devel
This package contains 32bit version of zlib-ng-compat-devel.

%endif


%debug_package


%prep
%autosetup -p1 -n %{name}-%{version}
cp -f %{SOURCE99} ./


%build
cat <<_EOF_
###########################################################################
#
# Build the default zlib-ng library
#
###########################################################################
_EOF_

# zlib-ng uses a different macro for library directory.
%global cmake_param %{?with_sanitizers:-DWITH_SANITIZER=ON}

# Setting __cmake_builddir is not necessary in this step, but do it anyway for symmetry.
%global __cmake_builddir %{_vpath_builddir}
%cmake %{cmake_param}
%cmake_build

%if %{with compat}
cat <<_EOF_
###########################################################################
#
# Build the compat mode library
#
###########################################################################
_EOF_

%global __cmake_builddir %{_vpath_builddir}-compat
# defining BUILD_SHARED_LIBS disables the static library
%undefine _cmake_shared_libs
# Disable new strategies in order to keep compatibility with zlib.
%cmake %{cmake_param} -DZLIB_COMPAT=ON -DWITH_NEW_STRATEGIES=OFF
%cmake_build
%endif


%check
cat <<_EOF_
###########################################################################
#
# Run the zlib-ng tests
#
###########################################################################
_EOF_

%global __cmake_builddir %{_vpath_builddir}
%ctest

%ifarch ppc64le
# Workaround Copr, that sets _target_cpu to ppc64le.
%global target_cpu powerpc64le
%else
%global target_cpu %{_target_cpu}
%endif

%ifarch x86_64
%global vendor pc
%else
%global vendor unknown
%endif

%if %{with abitest}
%ifarch %{supported_abi_test}
CHOST=%{target_cpu}-%{vendor}-linux-gnu sh test/abicheck.sh
%endif
%endif

%if %{with compat}
cat <<_EOF_
###########################################################################
#
# Run the compat mode tests
#
###########################################################################
_EOF_

%global __cmake_builddir %{_vpath_builddir}-compat
%ctest
%if %{with abitest}
%ifarch %{supported_abi_test}
CHOST=%{target_cpu}-%{vendor}-linux-gnu sh test/abicheck.sh --zlib-compat
%endif
%endif
%endif


%install
%global __cmake_builddir %{_vpath_builddir}
%cmake_install

%if %{with compat}
%global __cmake_builddir %{_vpath_builddir}-compat
%cmake_install
%endif

%files
%license LICENSE.md
%doc README.md Changelog.fedora
%{_libdir}/libz-ng.so.*

%files devel
%{_includedir}/zconf-ng.h
%{_includedir}/zlib-ng.h
%{_includedir}/zlib_name_mangling-ng.h
%{_libdir}/libz-ng.so
%{_libdir}/pkgconfig/%{name}.pc
%{_libdir}/cmake/zlib-ng/*

%if %{with compat}
%files compat
%{_libdir}/libz.so.*

%files compat-devel
%{_includedir}/zconf.h
%{_includedir}/zlib.h
%{_includedir}/zlib_name_mangling.h
%{_libdir}/libz.so
%{_libdir}/pkgconfig/zlib.pc
%{_libdir}/cmake/ZLIB/*

%files compat-static
%{_libdir}/libz.a

%if %{build_compat32}
%files -n compat32-%{name}-compat
%defattr(-,root,root)
%{_libdir}/libz.so.*

%files -n compat32-%{name}-compat-devel
%defattr(-,root,root)
%{_libdir}/libz.so
%{_libdir}/pkgconfig/zlib.pc
%{_libdir}/cmake/ZLIB/*
%endif
%endif


%changelog
* Wed Jun 26 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 2.1.7-1
- new upstream release.

* Sun Jun 09 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 2.1.6-4
- added compat32-* packages.

* Tue Apr 09 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 2.1.6-3
- initial build for Vine Linux.
