# Tests by default. No tests: rpm -ba --define 'dotests 0' *.spec
%{!?dotests: %define dotests 0}

%global __tar /opt/freeware/bin/tar
%global patchbin  /usr/opt/rpm/bin/patch
%global name onnxruntime
%global version 1.20.0
%global release 2
%global buildtype Release

#python related configs
%global py_major 3.9
%global py_minor 16

%global python_major python%{py_major}

%global python3_sitearch %(%{python_major} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(0))")
%global python3_sitearch64 %(%{python_major} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")

Summary:    ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator.
Name:       %{name}
Version:    %{version}
Release:    %{release}
License:    MIT
Group:      Development/Libraries
URL:        https://github.com/microsoft/onnxruntime
Source0:    https://github.com/microsoft/onnxruntime/archive/refs/tags/v%{version}.tar.gz

Source100:    onnxruntime-1.20.0-googletest.patch
Source101:    onnxruntime-1.20.0-gsl.patch
#below patch is not upstreamed yet to community
Patch0:   onnxruntime-1.20.0-shared_module.patch
Patch1:   onnxruntime-1.20.0-genai.patch

BuildRequires:  gcc >= 10
BuildRequires:  gcc-c++ >= 10
BuildRequires:  cmake >= 3.30
BuildRequires:  zlib-devel >= 1.2.13

%if %{toolchain} == "gcc"
Requires:       libstdc++ >= 10.3.0
%endif
%if %{toolchain} == "gcc"
Requires:       libgcc >= 10.3.0
%endif
Requires:       zlib >= 1.2.13
Requires:       libiconv >= 1.17

%description
ONNX Runtime is a cross-platform inference and training machine-learning accelerator.

ONNX Runtime inference can enable faster customer experiences and lower costs, supporting models from deep learning 
frameworks such as PyTorch and TensorFlow/Keras as well as classical machine learning libraries 
such as scikit-learn, LightGBM, XGBoost, etc. 
ONNX Runtime is compatible with different hardware, drivers, and operating systems, and provides optimal performance 
by leveraging hardware accelerators where applicable alongside graph optimizations and transforms.

This package internally uses some of the pacakge listed in https://github.com/microsoft/onnxruntime/blob/rel-%{version}/cmake/deps.txt.
Used package list: date,flatbuffers,google_nsync,googletest,gsl,boost,nlohmann_json,onnx,protobuf,pybind11,re2,safeint,eigen

%package devel
Summary:        Header files, libraries and development documentation for %{name}
Group:          Development/Libraries/C and C++

Requires:       libstdc++ >= 10
Requires:       libgcc >= 10
Requires:       zlib >= 1.2.13
Requires:       libiconv >= 1.17
Requires:       %{name} >= %{version}

%description devel
This package contains the Header files, libraries and development documentation for %{name}.

%package python3
Summary:        Python 3 bindings for %{name}
Group:          Development/Libraries/Python

BuildRequires:  %{python_major}-devel >= %{py_major}.%{py_minor}
BuildRequires:  %{python_major}-setuptools >= 65.2.0
BuildRequires:  %{python_major}-packaging >= 19.2

Requires:       %{python_major} >= %{py_major}.%{py_minor}
Requires:       %{python_major}-six >= 1.13.0
Requires:       libstdc++ >= 10
Requires:       libgcc >= 10
Requires:       zlib >= 1.2.13
Requires:       onnx-python3 >= 1.17.0
Requires:       %{python_major}-numpy >= 2.0.2
Requires:       protobuf-python3 >= 4.25.5
Requires:       protobuf-lite >= 4.25.5
Requires:       libiconv >= 1.17
Requires:       %{name} >= %{version}

%description python3
This package contains the Python 3 bindings for %{name}.

%prep
%setup -n onnxruntime-%{version}
%patch0 -p1
%patch1 -p1

%build
#env exports
export OBJECT_MODE=64
export CC="%{__cc__} "
export CXX="%{__cxx__} "
export CPPFLAGS="-I/opt/freeware/include -DFLATBUFFERS_LOCALE_INDEPENDENT=0 -I${RPM_BUILD_DIR}/%{name}-%{version}/build/Linux/%{buildtype}/_deps/pybind11_project-src/include"
export CFLAGS="${CPPFLAGS} -pthread -maix64 -maltivec -mvsx  -Wl,-bbigtoc -Wno-unused-variable -Wno-unused-parameter -Wno-sign-compare -fno-extern-tls-init  -Wl,-berok "
export CXXFLAGS="${CPPFLAGS} -pthread -maix64 -maltivec -mvsx -Wl,-bbigtoc  -Wno-unused-variable -Wno-unused-parameter -Wno-sign-compare -fno-extern-tls-init -Wl,-berok "
export LDFLAGS="-L${RPM_BUILD_DIR}/onnxruntime-%{version}/build/Linux/%{buildtype}/ -L/opt/freeware/lib/pthread -L/opt/freeware/lib64 -L/opt/freeware/lib -lpthread -l%{python_major}"
export LIBPATH="-L${RPM_BUILD_DIR}/onnxruntime-%{version}/build/Linux/%{buildtype}/"

#Build process includes three stages.
#1. First run build.sh which will download the required Open Source Software
./build.sh \
	--config %{buildtype}\
        --build_shared_lib \
        --skip_submodule_sync \
        --enable_pybind \
        --parallel \
        --skip_tests \
        --cmake_extra_defines CMAKE_AIX_SHARED_LIBRARY_ARCHIVE=ON \
        --cmake_extra_defines CMAKE_INSTALL_PREFIX=%{_prefix} || true


#2. Apply the patches applicable to OSS used internally by onnxruntime.
%{patchbin} --no-backup-if-mismatch -p1 --fuzz=0 < %{SOURCE100}
%{patchbin} --no-backup-if-mismatch -p1 --fuzz=0 < %{SOURCE101}

#3. Cleanup and Re-run build.sh
rm -r ${RPM_BUILD_DIR}/%{name}-%{version}/build/Linux/%{buildtype}/CMakeFiles
rm -r ${RPM_BUILD_DIR}/%{name}-%{version}/build/Linux/%{buildtype}/CMakeCache.txt
./build.sh \
	--config %{buildtype}\
        --build_shared_lib \
        --skip_submodule_sync \
        --enable_pybind \
        --parallel \
        --skip_tests \
        --cmake_extra_defines CMAKE_AIX_SHARED_LIBRARY_ARCHIVE=ON \
        --cmake_extra_defines CMAKE_INSTALL_PREFIX=%{_prefix}  

#python package building
cd ${RPM_BUILD_DIR}/%{name}-%{version}/build/Linux/%{buildtype}
%{python_major} ${RPM_BUILD_DIR}/%{name}-%{version}/setup.py build

%install
#env exports
export OBJECT_MODE=64
export CC="%{__cc__} "
export CXX="%{__cxx__} "
export CPPFLAGS="-I/opt/freeware/include -DFLATBUFFERS_LOCALE_INDEPENDENT=0 -I${RPM_BUILD_DIR}/%{name}-%{version}/build/Linux/%{buildtype}/_deps/pybind11_project-src/include"
export CFLAGS="${CPPFLAGS} -pthread -maix64 -maltivec -mvsx  -Wl,-bbigtoc -Wno-unused-variable -Wno-unused-parameter -Wno-sign-compare -fno-extern-tls-init  -Wl,-berok "
export CXXFLAGS="${CPPFLAGS} -pthread -maix64 -maltivec -mvsx -Wl,-bbigtoc  -Wno-unused-variable -Wno-unused-parameter -Wno-sign-compare -fno-extern-tls-init -Wl,-berok "
export LDFLAGS="-L${RPM_BUILD_DIR}/onnxruntime-%{version}/build/Linux/%{buildtype}/ -L/opt/freeware/lib/pthread -L/opt/freeware/lib64 -L/opt/freeware/lib -lpthread -l%{python_major}"
export LIBPATH="-L${RPM_BUILD_DIR}/onnxruntime-%{version}/build/Linux/%{buildtype}/"

cd ${RPM_BUILD_DIR}/%{name}-%{version}/build/Linux/%{buildtype}
make DESTDIR=$RPM_BUILD_ROOT install prefix=%{_prefix}
cp libonnxruntime_providers_shared.so $RPM_BUILD_ROOT/%{_libdir}/libonnxruntime_providers_shared.so

%{python_major} ${RPM_BUILD_DIR}/%{name}-%{version}/setup.py install  \
        --prefix=%{_prefix} \
        --install-data=$RPM_BUILD_ROOT \
        --root  $RPM_BUILD_ROOT \
        --record=INSTALLED_FILES

%files
%defattr(-, root, system,-)
%doc ThirdPartyNotices.txt README.md VERSION_NUMBER LICENSE
%{_bindir}/onnx_test_runner
%{_libdir}/libonnxruntime.a
%{_libdir}/libonnxruntime_providers_shared.so

%files devel
%defattr(-, root, system,-)
%dir %{_includedir}/onnxruntime/
%{_includedir}/onnxruntime/*
%{_libdir}/pkgconfig/libonnxruntime.pc
%doc docs/*

%files python3
%defattr(-, root, system,-)
%doc ThirdPartyNotices.txt README.md VERSION_NUMBER LICENSE 
%{_bindir}/onnxruntime_test
%{python3_sitearch64}/*

%changelog
* Mon Jan 27 2025 Ranjit Ranjan  <ranjit.ranjan@ibm.com> 1.20.0-2
- Rebuild with numpy 2.0.2.

* Tue Nov 05 2024 Ranjit Ranjan  <ranjit.ranjan@ibm.com> 1.20.0-1
- Update for 1.20.0

* Wed Oct 4 2023 Ranjit Ranjan  <ranjit.ranjan@ibm.com> 1.14.1-2
- Removing _beta from rpm name.

* Tue May 9 2023 Ranjit Ranjan  <ranjit.ranjan@ibm.com> 1.14.1-1
- Port on AIX 


