%bcond_without dotests

# Current .so version is 5
# See SET_TARGET_PROPERTIES(... SOVERSION 5) in lib/CMakeLists.txt
%define soversion 5

%define compiler_msg This version has been compiled with GCC.

%define _libdir64     %{_prefix}/lib64


Name:    libzip
Version:	1.11.3
Release:	1
Summary: C library for reading, creating, and modifying zip archives

License: BSD
Group:   Applications/File
URL:     https://libzip.org/
Source0:	https://libzip.org/download/libzip-%{version}.tar.gz

Patch0:  libzip-1.8.0-getopt.patch

BuildRequires:	gcc >= 10.3.0
BuildRequires:	bzip2-devel >= 1.0.8
BuildRequires:	xz-devel >= 5.4.3
BuildRequires:	zlib-devel >= 1.2.13

BuildRequires:	cmake >= 3.30.3
BuildRequires:	sed

# Needed to run the test suite
# find regress/ -type f | /usr/lib/rpm/perl.req
# find regress/ -type f | /usr/lib/rpm/perl.prov
BuildRequires:	perl(perl)
BuildRequires:	perl(Cwd)
BuildRequires:	perl(File::Copy)
BuildRequires:	perl(File::Path)
BuildRequires:	perl(Getopt::Long)
BuildRequires:	perl(IPC::Open3)
BuildRequires:	perl(Storable)
BuildRequires:	perl(Symbol)
BuildRequires:	perl(UNIVERSAL)
BuildRequires:	perl(strict)
BuildRequires:	perl(warnings)

%if %{toolchain} == "gcc"
Requires:	libgcc >= 10.3.0
%endif
Requires:	bzip2 >= 1.0.8
Requires:	xz-libs >= 5.4.3
Requires:	zlib >= 1.2.13
Requires:	libzstd >= 1.5.2


%description
libzip is a C library for reading, creating, and modifying zip archives. Files
can be added from data buffers, files, or compressed data copied directly from 
other zip archives. Changes made without closing the archive can be reverted. 
The API is documented by man pages.

%{compiler_msg}


%package devel
Summary:   Development files for %{name}
Requires: %{name} = %{version}-%{release}
Group:    Applications/File
Requires:	bzip2-devel >= 1.0.8
Requires:	xz-devel >= 5.4.3
Requires:	zlib-devel >= 1.2.13

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

%package tools
Summary:  Command line tools from %{name}
Requires: %{name} = %{version}-%{release}
Group:    Applications/File

%description tools
The %{name}-tools package provides command line tools split off %{name}:
- zipcmp
- zipmerge
- ziptool


%prep
export PATH=/opt/freeware/bin:$PATH

%setup -q
%patch0 -p0

# unwanted in package documentation
rm INSTALL.md

# drop skipped test which make test suite fails (cmake issue ?)
sed -e '/clone-fs-/d' \
    -i regress/CMakeLists.txt

# Duplicate source for 32 & 64 bits
rm -rf   /tmp/%{name}-%{version}-32bit
cp -pr . /tmp/%{name}-%{version}-32bit
rm -rf ..?* .[!.]* *
mv       /tmp/%{name}-%{version}-32bit 32bit
cp -pr 32bit 64bit


%build
export PATH=/opt/freeware/bin:$PATH
export AR="/usr/bin/ar"

export CC="%{__cc__}"
export CXX="%{__cxx__}"
###use full path for gcc except for ... which requires a string beginning with gcc:  export CC="gcc"

#Build 32bit mode
cd 32bit
export OBJECT_MODE=32
export CFLAGS="-O2 -maix32 -fPIC -D_LARGE_FILES"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-L/opt/freeware/lib -Wl,-bmaxdata:0x80000000 -Wl,-blibpath:/opt/freeware/lib:/usr/lib:/lib"

rm -f CMakeCache.txt

cmake -L . \
    -G"Unix Makefiles" \
    -DCMAKE_SYSTEM_NAME="AIX"  \
    -DCMAKE_C_COMPILER="${CC}"  \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DCMAKE_INSTALL_PREFIX=%{_prefix} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DCMAKE_INSTALL_MANDIR="%{_mandir}" \
    -DBUILD_SHARED_LIBS=ON  \
    -DBUILD_STATIC_LIBS=OFF \
    -DCMAKE_AR="${AR}" \
    -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=FALSE

gmake VERBOSE=1 --print-directory %{?_smp_mflags} -j16

/usr/sbin/slibclean


#Build 64bit mode
cd ../64bit
export OBJECT_MODE=64
export CFLAGS="-O2 -maix64 -fPIC"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-L/opt/freeware/lib64 -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib:/lib"

rm -f CMakeCache.txt

cmake -L . \
    -G"Unix Makefiles" \
    -DCMAKE_SYSTEM_NAME="AIX"  \
    -DCMAKE_C_COMPILER="${CC}"  \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DCMAKE_INSTALL_PREFIX=%{_prefix} \
    -DCMAKE_INSTALL_LIBDIR=lib64 \
    -DCMAKE_INSTALL_MANDIR="%{_mandir}" \
    -DBUILD_SHARED_LIBS=ON  \
    -DBUILD_STATIC_LIBS=OFF \
    -DCMAKE_AR="${AR}" \
    -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=FALSE

gmake VERBOSE=1 --print-directory %{?_smp_mflags} -j16


%install
export PATH=/opt/freeware/bin:$PATH
export AR="/usr/bin/ar -X32_64"

export INSTALL="/opt/freeware/bin/install "

export LN="/usr/bin/ln -s"
export RM="/usr/bin/rm -f"

[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}

# ==========================
# Install the 64 bit version
# ==========================
cd 64bit

export OBJECT_MODE=64

gmake VERBOSE=1 DESTDIR=${RPM_BUILD_ROOT} install 

# Move the 64 bit utilities
(
  cd  ${RPM_BUILD_ROOT}%{_bindir}
  for f in $(ls -1| grep -v -e _32 -e _64)
    do
    mv ${f} ${f}_64
    /usr/bin/strip -X32_64 ${f}_64 || :
  done
)


# ==========================
# Install the 32 bit version
# ==========================
cd ../32bit

export OBJECT_MODE=32

gmake VERBOSE=1 DESTDIR=${RPM_BUILD_ROOT} install 

(
    # Extract .so from 64bit .a libraries
    cd ${RPM_BUILD_ROOT}%{_libdir64}
    ${AR} -x %{name}.a

    # Create 32 bits libraries with 32/64bit members
    cd ${RPM_BUILD_ROOT}%{_libdir}
    ${AR} -q %{name}.a ${RPM_BUILD_ROOT}%{_libdir64}/%{name}.so.%{soversion}
    rm ${RPM_BUILD_ROOT}%{_libdir64}/%{name}.so.%{soversion}
)

(
  cd  ${RPM_BUILD_ROOT}%{_bindir}
  for f in $(ls -1| grep -v -e _32 -e _64)
    do
    mv ${f} ${f}_32
    /usr/bin/strip -X32_64 ${f}_64 || :
    $LN ${f}_64 ${f}
  done
)


%check
%if %{with dotests}
cd 64bit
( gmake --trace --print-directory -k check || true )
/usr/sbin/slibclean
cd ../32bit
( gmake --trace --print-directory -k check || true )
/usr/sbin/slibclean
%endif


%clean
[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}


%files
%defattr(-,root,system)
%doc 32bit/LICENSE
%{_libdir}/libzip.a
%{_libdir64}/libzip.a

%files tools
%defattr(-,root,system)
%{_bindir}/zipcmp*
%{_bindir}/zipmerge*
%{_bindir}/ziptool*
%{_mandir}/man1/zip*

%files devel
%defattr(-,root,system)
%doc 32bit/AUTHORS 32bit/THANKS* 32bit/*.md
%{_includedir}/zip.h
%{_includedir}/zipconf*.h
%{_libdir}/pkgconfig/libzip.pc
%{_mandir}/man3/libzip*
%{_mandir}/man3/zip*
%{_mandir}/man3/ZIP*


%changelog
* Wed Feb 12 2025 Parth Patel <parth.patel7@ibm.com> - 1.11.3-1 
- Update to version 1.11.3
- Build libzip-1.11.3 with OpenSSL3

* Tue Mar 19 2024 Raghu N Arava <raghuara@in.ibm.com> - 1.10.1-1 
- Update to version 1.10.1

* Thu Jun 30 2022 Reshma V Kumar <reskumar@in.ibm.com> - 1.8.0-1
- Initial port to AIX toolbox
