%define dotests  1
%define _libdir64 %{_prefix}/lib64

Name:		json-c
Version:	0.15
Release:	2
Summary:	A JSON implementation in C
Group:		Development/Libraries
License:	MIT
URL:		https://github.com/json-c/json-c/wiki
Source0:	https://s3.amazonaws.com/json-c_releases/releases/%{name}-%{version}.tar.gz
Source1:	libjson-c.so.4-aix32
Source2:	libjson-c.so.4-aix64

Patch0: %{name}-0.13.1-aix.patch
Patch1: json-c-getopt.patch

BuildRequires:	patch make
Requires: libgcc >= 8.3.0


%description
JSON-C implements a reference counting object model that allows you to easily
construct JSON objects in C, output them as JSON formatted strings and parse
JSON formatted strings back into the C representation of JSON objects.


%package devel
Summary:	Development headers and library for json-c
Group:		Development/Libraries
Requires:	%{name} = %{version}-%{release}
Requires:	pkg-config

%description devel
This package contains the development headers and library for json-c.


%package doc
Summary:	Reference manual for json-c
Group:		Documentation

%description doc
This package contains the reference manual for json-c.


%prep
%setup -q
%patch0
%patch1

mkdir ../32bit
mv * ../32bit
mv ../32bit .
mkdir 64bit
cd 32bit
tar cf - . | (cd ../64bit ; tar xpf -)


%build
export CC="gcc -maix64 -pthread"
export CFLAGS="-fPIC"

cd 64bit
# first build the 64-bit version
export LDFLAGS="-lm -L/opt/freeware/lib64 -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib/pthread/ppc64:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib:/lib"
export OBJECT_MODE=64
mkdir json-c-build
cd json-c-build

cmake ../ -DCMAKE_INSTALL_PREFIX=%{_prefix} -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=OFF -DCMAKE_INSTALL_LIBDIR=/opt/freeware/lib64

gmake %{?_smp_mflags}

if [ "%{dotests}" == 1 ]
     then
         ( make -k test || true )
         /usr/sbin/slibclean
     fi
cd ../

cd ../32bit
# now build the 32-bit version
export CC="gcc -maix32 -pthread"
export CFLAGS="-fPIC"
export LDFLAGS="-lm -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib/pthread:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000"
export OBJECT_MODE=32
mkdir json-c-build
cd json-c-build

cmake ../ -DCMAKE_INSTALL_PREFIX=%{_prefix} -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=OFF -DCMAKE_INSTALL_LIBDIR=/opt/freeware/lib

gmake %{?_smp_mflags}

if [ "%{dotests}" == 1 ]
     then
  	( gmake -k test || true )
       	/usr/sbin/slibclean
 fi

cd ../


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

cd 64bit
export OBJECT_MODE=64
cd json-c-build
make install DESTDIR=${RPM_BUILD_ROOT} 
cd ../

cd ../32bit
export OBJECT_MODE=32
cd json-c-build
make install DESTDIR=${RPM_BUILD_ROOT}
cd ../

(
  cd ${RPM_BUILD_ROOT}%{_libdir64}
  for f in *.a ; do
    /usr/bin/ar -X64 -x ${f}
  done

  cd ${RPM_BUILD_ROOT}%{_libdir}
  for f in *.a ; do
    /usr/bin/ar -X32 -x ${f}
  done
)

# add the 64-bit shared objects to the shared libraries containing already the
# 32-bit shared objects
/usr/bin/ar -X64 -q ${RPM_BUILD_ROOT}%{_libdir}/lib%{name}.a ${RPM_BUILD_ROOT}%{_libdir64}/lib%{name}.so*

# Add the older version  shared members for compatibility with older apps
# (make sure they're set for LOADONLY with 'strip -e')
# 32bit
cp %{SOURCE1} libjson-c.so.4
/usr/bin/strip -X32 -e libjson-c.so.4
/usr/bin/ar -X32 -q ${RPM_BUILD_ROOT}%{_libdir}/libjson-c.a libjson-c.so.4
# 64bit
cp %{SOURCE2} libjson-c.so.4
/usr/bin/strip -X64 -e libjson-c.so.4
/usr/bin/ar -X64 -q ${RPM_BUILD_ROOT}%{_libdir64}/libjson-c.a libjson-c.so.4
/usr/bin/ar -X64 -q ${RPM_BUILD_ROOT}%{_libdir}/libjson-c.a libjson-c.so.4


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


%files
%defattr(-,root,system,-)
%doc 32bit/AUTHORS 32bit/ChangeLog 32bit/COPYING 32bit/NEWS
%doc 32bit/README 32bit/README.html
%{_libdir64}/*.a
%{_libdir}/*.a
%{_libdir}/*.so*
%{_libdir64}/*.so*

%files devel
%defattr(-,root,system,-)
%{_includedir}/*
%{_libdir}/pkgconfig/*.pc
%{_libdir64}/pkgconfig/*.pc

%files doc
%defattr(-,root,system,-)
%doc 32bit/doc/html/*


%changelog
* Mon Dec 07 2020 Reshma V Kumar <reskumar@in.ibm.com> - 0.15-2
- Rebuild after adding Requires:libgcc

* Wed Aug 19 2020 Baanu Tumma <btumma15@in.ibm.com> - 0.15-1
- Update to version 0.15-1 to fix CVE-2020-12762	 
- no longer shipping files from /usr directory

* Mon Apr 30 2018 Ravi Hirekurabar <rhirekur@in.ibm.com>
- Initial port to AIX
