%define  dotests 1
%define _libdir64 %{_prefix}/lib64
%define  realver	3450300
%define  docrealver	3450300
Summary: Library that implements an embeddable SQL database engine
Name: 	 sqlite
Version:	3.45.3
Release:	1
License: Public Domain
Group: 	 Applications/Databases
URL: 	 https://www.sqlite.org/
Source0:	https://www.sqlite.org/2024/sqlite-src-3450300.zip
Source1:	https://www.sqlite.org/2024/sqlite-doc-3450300.zip

# Support a system-wide lemon template
#Patch1:  sqlite-3.35.5-lemon-system-template.patch
# Shut up stupid tests depending on system settings of allowed open fd's
Patch2:  sqlite-3.32.3-new-stupid-openfiles-test.patch
Patch4:  sqlite-3.32.3-aix-new.patch 

Obsoletes: sqlite3 sqlite3-devel

BuildRequires:	make
BuildRequires:	patch
BuildRequires:	unzip
BuildRequires:	readline-devel >= 8.2
BuildRequires:	tcl >= 8.6.13
Requires:	ncurses >= 6.4
Requires:	readline >= 8.2
%if %{toolchain} == "gcc"
Requires:	libgcc >= 10.3.0
%endif
Requires:	zlib >= 1.2.13

%description
SQLite is a C library that implements an embeddable SQL database engine.
Programs that link with the SQLite library can have SQL database access
without running a separate RDBMS process. The distribution comes with a
standalone command-line access program (sqlite) that can be used to
administer an SQLite database and which serves as an example of how to
use the SQLite library.


%package devel
Summary: Header files and libraries for developing apps which will use sqlite.
Group: Development/Libraries
Requires: %{name} = %{version}-%{release}
Requires:	pkg-config

%description devel
The sqlite-devel package contains the header files and libraries needed
to develop programs that use the sqlite database library.

If you are compiling a 32-bit program, no special compiler options are
needed.

If you are compiling a 64-bit program, you have to compile and link your
application with "xlc -q64" or "gcc -maix64".


%package doc
Summary: Documentation for sqlite
Group: Documentation

%description doc
This package contains most of the static HTML files that comprise the
www.sqlite.org website, including all of the SQL Syntax and the
C/C++ interface specs and other miscellaneous documentation.


%package -n lemon
Summary: A parser generator
Group: Development/Tools

%description -n lemon
Lemon is an LALR(1) parser generator for C or C++. It does the same
job as bison and yacc. But lemon is not another bison or yacc
clone. It uses a different grammar syntax which is designed to reduce
the number of coding errors. Lemon also uses a more sophisticated
parsing engine that is faster than yacc and bison and which is both
reentrant and thread-safe. Furthermore, Lemon implements features
that can be used to eliminate resource leaks, making is suitable for
use in long-running programs such as graphical user interfaces or
embedded controllers.


# all this hoopla is required because RPM V3 (which we are stuck with on AIX)
# does not understand ZIP archives as source files :-(
%prep
/usr/bin/rm -rf %{name}-src-%{realver} %{name}-doc-%{docrealver}
unzip %{SOURCE0}
unzip %{SOURCE1}
%setup -T -D -n %{name}-src-%{realver}

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

#%patch1 -p1 -b .lemon-system-template
%patch2 -p1
%patch4 -p1 -b .aix

rm -rf   /tmp/%{name}-%{version}-32bit
cp -pr . /tmp/%{name}-%{version}-32bit
rm -fr *
mv       /tmp/%{name}-%{version}-32bit 32bit
cp -pr 32bit 64bit
mkdir -p  doc
/usr/bin/mv ../%{name}-doc-%{docrealver}/* doc/ 
/usr/bin/rmdir ../%{name}-doc-%{docrealver}  


%build

# setup environment for 32-bit and 64-bit builds
export RM="/usr/bin/rm -f"
export AR="/usr/bin/ar -X32_64"
export NM="/usr/bin/nm -X32_64"


cd 64bit
# first build the 64-bit version
autoconf
export CC="%{__cc__} -maix64 -O2"
export OBJECT_MODE=64
export LDFLAGS="-L/opt/freeware/lib64 -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib:/lib"

./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir64} \
    --enable-shared --enable-static \
    --disable-tcl \
    --enable-threadsafe \
    --enable-cross-thread-connections \
    --enable-load-extension \
    --with-readline-inc=-I/opt/freeware/include

# "correct" Makefile
cat Makefile | sed -e "s|THREADLIB|LIBPTHREAD|" > Makefile.tmp
mv Makefile.tmp Makefile

gmake %{?_smp_mflags}

if [ "%{dotests}" == 1 ]
     then
	export SED=/opt/freeware/bin/sed
	$SED -i "s/^LIBTCL = $/LIBTCL = -ltcl/g" Makefile
	(gmake -k test || true)
        /usr/sbin/slibclean
fi



# now build the 32-bit version
cd ../32bit
autoconf
export CC="%{__cc__} -maix32 -D_LARGE_FILES -O2"
export OBJECT_MODE=32
export LDFLAGS="-L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000"

./configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir} \
    --enable-shared --enable-static \
    --disable-tcl \
    --enable-threadsafe \
    --enable-cross-thread-connections \
    --enable-load-extension \
    --with-readline-inc=-I/opt/freeware/include

# "correct" Makefile
cat Makefile | sed -e "s|THREADLIB|LIBPTHREAD|" > Makefile.tmp
mv Makefile.tmp Makefile

gmake %{?_smp_mflags}

if [ "%{dotests}" == 1 ]
     then
        export SED=/opt/freeware/bin/sed
        $SED -i "s/^LIBTCL = $/LIBTCL = -ltcl/g" Makefile
        (gmake -k test || true)
        /usr/sbin/slibclean
fi

%install

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

# setup environment for 32-bit and 64-bit builds
export RM="/usr/bin/rm -f"
export AR="/usr/bin/ar -X32_64"


cd 64bit
#Install 64bit version of binaries and libraries 
export OBJECT_MODE=64
gmake DESTDIR=${RPM_BUILD_ROOT} install
cp lemon ${RPM_BUILD_ROOT}%{_bindir}/lemon
chmod 0755 ${RPM_BUILD_ROOT}%{_bindir}/lemon

/usr/bin/strip -X64 ${RPM_BUILD_ROOT}%{_bindir}/* || :

(
	cd  ${RPM_BUILD_ROOT}/%{_bindir}
	for fic in $(ls -1| grep -v -e _32 -e _64)
	do
             mv $fic "$fic"_64
        done
)

mkdir -p ${RPM_BUILD_ROOT}%{_datadir}/lemon
cp tool/lempar.c ${RPM_BUILD_ROOT}%{_datadir}/lemon/lempar.c
chmod 0644 ${RPM_BUILD_ROOT}%{_datadir}/lemon/lempar.c

# remove cgi-script erroneously included in sqlite-doc
rm -r  %{_builddir}/sqlite-src-%{docrealver}/doc/search
rm  -r %{_builddir}/sqlite-src-%{docrealver}/doc/search.d

cd ../32bit

#install 32bit version version of binaries and libraries

export OBJECT_MODE=32
gmake DESTDIR=${RPM_BUILD_ROOT} install
cp lemon ${RPM_BUILD_ROOT}%{_bindir}/lemon
chmod 0755 ${RPM_BUILD_ROOT}%{_bindir}/lemon

/usr/bin/strip -X32 ${RPM_BUILD_ROOT}%{_bindir}/* || :

(
    cd  ${RPM_BUILD_ROOT}/%{_bindir}
    for fic in $(ls -1| grep -v -e _32 -e _64)
    do
        mv $fic "$fic"_32
        ln -sf "$fic"_64 $fic
    done
)

# add the 64-bit shared objects to the shared library containing already the
${AR} -q ${RPM_BUILD_ROOT}%{_libdir}/libsqlite3.a  ../64bit/.libs/libsqlite3.so.0
cp ../64bit/.libs/libsqlite3.so.0                  ${RPM_BUILD_ROOT}%{_libdir64}/libsqlite3.so
cp .libs/libsqlite3.so.0              		   ${RPM_BUILD_ROOT}%{_libdir}/libsqlite3.so


mkdir -p ${RPM_BUILD_ROOT}%{_mandir}/man1/
cp sqlite3.1 ${RPM_BUILD_ROOT}%{_mandir}/man1
chmod 644 ${RPM_BUILD_ROOT}%{_mandir}/man1/*


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


%files
%defattr(-,root,system)
#%doc README
%{_bindir}/sqlite3*
%{_libdir}/*.a
%{_libdir}/*.so
%{_libdir}64/*.a
%{_libdir}64/*.so
%{_mandir}/man?/*


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


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


%files -n lemon
%defattr(-,root,system)
%{_bindir}/lemon*
%{_datadir}/lemon


%changelog
* Thu Apr 25 2024 Raghu N Arava <raghuara@in.ibm.com> - 3.45.3-1 
- Update to version 3.45.3 to fix CVE-2023-7104


* Thu Mar 30 2023 Harshith K A <harshith.k.a@ibm.com> - 3.41.2-1
- Update to fix CVE-2022-46908

* Tue Oct 4 2022 Shraddha Patil <shrapat1@in.ibm.com> -3.39.3-1
- Update to latest version

* Fri Feb 25 2022 Sushree Das <sushree.das@ibm.com> - 3.37.2-1
- Update to latest version 

* Fri Apr 23 2021 Reshma V Kumar <reskumar@in.ibm.com> - 3.35.5-1
- Update to fix CVE-2021-20227

* Mon Jun 01 2020 Rishita Saha <risaha16@in.ibm.com> - 3.32.3-1
- Update to fix CVE-2020-13871

* Mon Jun 01 2020 Rishita Saha <risaha16@in.ibm.com> - 3.32.1-1
- Updated to 3.32.1

* Thu May 23 2019 Ravi Hirekurabar <rhirekur@in.ibm.com> - 3.28.0-1
- Updating to 3.28.0 to fix following CVE's
- CVE-2019-5018 CVE-2018-20346 CVE-2019-9937  CVE-2019-9936

* Thu Feb 21 2019 Ravi Hirekurabar <rhirekur@in.ibm.com> - 3.27.1-1
- Updated to 3.27.1
- Built with gcc
- Built both 32 and 64bit binaries making 64bit default
- Created patch for renaming thread_wait function as 
- it is predifined in AIX

* Tue Apr 10 2018 Ravi Hirekurabar <rhirekur@in.ibm.com> - 3.23.0-1
- Updated to 3.23.0-1 For security vulnerability fix. 

* Fri Dec 16 2016 Ravi Hirekurabar <rhirekur@in.ibm.com> - 3.15.2-1
- Build requires tcl >= 8.5
- Updated to 3.15.2 

* Tue Feb 26 2013 Gerard Visiedo <gerard.visiedo@bull.net> - 3.715.2-2
- Add lemon package

* Fri Feb 22 2013 Gerard Visiedo <gerard.visiedo@bull.net> - 3.715.2-1
- Update to version 3.7.15.2-1

* Thu Feb 02 2012 Gerard Visiedo <gerard.visiedo@bull.net> - 3.7.6.3-3
- Initial port on Aix6.1

* Tue Oct 04 2011 Gerard Visiedo <gerard.visiedo@bull.net> - 3.7.6.3-2
- rebuild for compatibility with new libiconv.a 1.13.1-2

* Wed Jun 22 2011 Gerard Visiedo <gerard.visiedo@bull.net> 3.7.6.3-1
- Update to 3.7.6.3 

* Tue Jan 19 2010 Jean Noel Cordenner <jean-noel.cordenner@bull.net> 3.6.22-1
- Initial port for AIX
