File onnxruntime.spec of Package onnxruntime

#
# spec file for package onnxruntime
#
# Copyright (c) 2024 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#

# %%define cpuinfo_ver ca67895
%define onnx_ver 1.16.1
%define safeint_ver 3.0.28
%define eigen_ver e7248b26

%define so_suffix 1_19_2

%if 0%{?gcc_version} < 12
%define with_gcc 12
%endif

%bcond_with     check
%bcond_with     test

%{?sle15_python_module_pythons}
Name:           onnxruntime
Version:        1.19.2
Release:        0
Summary:        Cross-platform, high performance ML inferencing and training accelerator
License:        MIT AND Apache-2.0 AND MPL-2.0 AND BSD-3-Clause
URL:            https://github.com/microsoft/onnxruntime/
Source0:        %{name}-%{version}.tar.xz
Source5:        onnx-%{onnx_ver}.tar.xz
Source7:        safeint-%{safeint_ver}.tar.xz
Source9:        eigen-%{eigen_ver}.tar.xz

Patch0:         onnxruntime-dont-install-tests.patch
Patch1:         onnxruntime-system-flatbuffers.patch
Patch2:         onnxruntime-system-protobuf.patch
Patch3:         onnxruntime-disable-power10.patch
Patch4:         onnxruntime-disable-wil.patch
Patch5:         onnxruntime-system-abseil.patch
Patch6:         onnxruntime-no-nsync.patch
Patch7:         onnxruntime-system-boost-and-date.patch
Patch8:         onnxruntime-exact-python-version.patch
Patch9:         onnxruntime-fetch-remaining-deps.patch
Patch10:        onnxruntime-fix-missing-cstring-include.patch
Patch11:        onnxruntime-fix-cxx20-template-construction.patch
Patch12:        onnxruntime-gcc-ignore-warnings.patch
Patch13:        onnxruntime-fix-std-variant.patch
Patch14:        onnxruntime-fix-string-conversion.patch
Patch15:        onnxruntime-disable-torch-tests.patch
Patch16:        onnxruntime-fix-flatbuffers-test.patch
# Patch16:        onnxruntime-system-cpuinfo.patch
# ensure pybind11 integration doesn't allow code execution from stack
Patch20:        onnxruntime-pybind11_state-noexecstack.patch
# fully version the onnxruntime library's soname
Patch21:        onnxruntime-soname.patch
# add versioned soname support to providers library
Patch22:        versioned-onnxruntime_providers_shared.patch
BuildRequires:  %{python_module devel}
BuildRequires:  %{python_module numpy-devel}
BuildRequires:  %{python_module numpy}
BuildRequires:  %{python_module packaging}
BuildRequires:  %{python_module pip}
BuildRequires:  %{python_module pybind11-devel}
BuildRequires:  %{python_module pytest}
BuildRequires:  %{python_module setuptools}
BuildRequires:  %{python_module sympy}
BuildRequires:  %{python_module wheel}
%if %{with test} || %{with check}
BuildRequires:  %{python_module psutil}
BuildRequires:  googletest-devel
BuildRequires:  googlemock-devel
%endif
BuildRequires:  hhdate-devel
BuildRequires:  cmake
BuildRequires:  ninja
BuildRequires:  fdupes
# at least gcc version 9
BuildRequires:  gcc%{?with_gcc}-c++
BuildRequires:  libtool
%if 0%{?with_gcc}
BuildRequires:  abseil-cpp-devel-gcc12 >= 20240116
%else
BuildRequires:  cmake(absl) >= 20240116
%endif
# BuildRequires:  cmake(cpuinfo)
BuildRequires:  ms-gsl-devel
BuildRequires:  nlohmann_json-devel
BuildRequires:  protobuf-devel = 21.12
BuildRequires:  flatbuffers-devel >= 23.5.9
BuildRequires:  re2-devel
BuildRequires:  (cmake(date) >= 3.0.0 and cmake(date) < 4.0.0)
BuildRequires:  libboost_headers-devel-impl >= 1.79.0
# BuildRequires:  eigen3-devel >= 1.34
BuildRequires:  pkgconfig
BuildRequires:  python-rpm-macros

BuildRequires:  pkgconfig(libpng)
BuildRequires:  pkgconfig(zlib)

Requires(post):   update-alternatives
Requires(postun):  update-alternatives

%python_subpackages

%description
ONNX Runtime is a performance-focused scoring engine for Open Neural Network
Exchange (ONNX) models.  For more information on ONNX Runtime, please see
<https://aka.ms/onnxruntime/> or <https://github.com/microsoft/onnxruntime/>.

%package  -n lib%{name}%{so_suffix}
Summary:       Cross-platform, high performance ML inferencing and training accelerator

%description -n lib%{name}%{so_suffix}
ONNX Runtime is a performance-focused scoring engine for Open Neural Network
Exchange (ONNX) models.  For more information on ONNX Runtime, please see
<https://aka.ms/onnxruntime/> or <https://github.com/microsoft/onnxruntime/>.

%package -n %{name}-devel
Summary:       Development files for %{name}
Requires:      lib%{name}%{so_suffix} = %{version}

%description -n %{name}-devel
Development headers and libraries for ONNX Runtime.  For more information on
ONNX Runtime, please see <https://aka.ms/onnxruntime/> or
<https://github.com/microsoft/onnxruntime/>.

%if %{with test}
%package -n %{name}-test
Summary:       Test tools for %{name}
Requires:      %{name}-devel = %{version}

%description -n %{name}-test
Testing tools for ONNX Runtime  For more information on ONNX Runtime, please
see <https://aka.ms/onnxruntime/> or <https://github.com/microsoft/onnxruntime/>.
%endif

%prep
%autosetup -p1

sed -i -e '/FIND_PACKAGE_ARGS 20240116 NAMES absl/d' cmake/external/abseil-cpp.cmake

mkdir -p cmake/deps

tar xf %{SOURCE5} -C cmake/deps
tar xf %{SOURCE7} -C cmake/deps
tar xf %{SOURCE9} -C cmake/deps

shebang_files=""
shebang_files+="onnxruntime/python/tools/quantization/calibrate.py "
shebang_files+="tools/python/util/convert_onnx_models_to_ort.py "
shebang_files+="tools/python/util/make_dynamic_shape_fixed.py "
shebang_files+="tools/python/util/optimize_onnx_model.py "
shebang_files+="tools/python/util/qdq_helpers/optimize_qdq_model.py "
shebang_files+="tools/python/util/update_onnx_opset.py "

for s in ${shebang_files}
do
    echo "[Removing shebang line for ${s}]"
    sed -i -e '/^#![/]usr[/]bin[/]env python3/d' ${s}
done

%build
%global _lto_cflags %{_lto_cflags} -ffat-lto-objects

%if 0%{?with_gcc}
export CXX=g++-%{with_gcc}
export CC=gcc-%{with_gcc}
%endif

# construct cmake opts list
_cmake_opts=""

# cmake general settings
_cmake_opts+="-DBUILD_SHARED_LIBS:BOOL=OFF "

# FetchContent offline mode settings
_cmake_opts+="-DFETCHCONTENT_FULLY_DISCONNECTED=ON "
_cmake_opts+="-DFETCHCONTENT_QUIET=OFF "
_cmake_opts+="-DFETCHCONTENT_TRY_FIND_PACKAGE_MODE=ALWAYS "

# FetchContent Source Dir overrides
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_ONNX=%{_builddir}/%{name}-%{version}/cmake/deps/onnx-%{onnx_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_SAFEINT=%{_builddir}/%{name}-%{version}/cmake/deps/safeint-%{safeint_ver} "
_cmake_opts+="-DFETCHCONTENT_SOURCE_DIR_EIGEN=%{_builddir}/%{name}-%{version}/cmake/deps/eigen-%{eigen_ver} "

# onnxruntime settings
_cmake_opts+="-Donnxruntime_DISABLE_ABSEIL=ON "
_cmake_opts+="-Donnxruntime_ENABLE_CPUINFO=OFF "
_cmake_opts+="-Donnxruntime_ENABLE_PYTHON=ON "
_cmake_opts+="-Donnxruntime_BUILD_SHARED_LIB=ON "
_cmake_opts+="-Donnxruntime_BUILD_BENCHMARKS=OFF "
_cmake_opts+="-Donnxruntime_USE_FULL_PROTOBUF=ON "
_cmake_opts+="-DONNX_CUSTOM_PROTOC_EXECUTABLE=%{_bindir}/protoc "

# testing related options
%if %{with test} || %{with check}
_cmake_opts+="-Donnxruntime_BUILD_UNIT_TESTS=ON "
%else
_cmake_opts+="-Donnxruntime_BUILD_UNIT_TESTS=OFF "
%endif
%if %{with test}
_cmake_opts+="-Donnxruntime_INSTALL_UNIT_TESTS=ON "
%else
_cmake_opts+="-Donnxruntime_INSTALL_UNIT_TESTS=OFF "
%endif

# use eigen3 preinstalled sources - currently doesn't work 
#_cmake_opts+="-Donnxruntime_USE_PREINSTALLED_EIGEN=ON "
#_cmake_opts+="-Deigen_SOURCE_PATH=/usr/include/eigen3 "

%define __builder ninja
%define __sourcedir cmake
%define py_setup ../../setup.py

%{python_expand #
# Re-generate flatbuffer headers
$python onnxruntime/core/flatbuffers/schema/compile_schema.py --flatc %{_bindir}/flatc
%define __builddir cmake/build-${python_flavor}
%cmake ${_cmake_opts} -DPYTHON_VERSION=%{$python_version}
%cmake_build
%{$python_build}
cd -
}

%install
%{python_expand #
%define __builddir cmake/build-${python_flavor}
%cmake_install
pushd %{__builddir}
%{$python_install}
popd
}

%python_clone -a %{buildroot}%{_bindir}/onnxruntime_test
%python_expand %fdupes %{buildroot}%{$python_sitearch} %{buildroot}%{$python_sitelib}

%check
%if %{with check}
export GTEST_FILTER=-"CApiTensorTest.load_huge_tensor_with_external_data:Random.InvalidDType"
%{python_expand #
%define __builddir cmake/build-${python_flavor}
%ctest
# pushd %%{__builddir}
# %%{$python_pytest} -v
# popd
}
%endif

%post
%python_install_alternative onnxruntime_test
%postun
%python_uninstall_alternative onnxruntime_test

%post -n lib%{name}%{so_suffix} -p /sbin/ldconfig
%postun -n lib%{name}%{so_suffix} -p /sbin/ldconfig

%files -n lib%{name}%{so_suffix}
%license LICENSE ThirdPartyNotices.txt
%doc README.md
%{_libdir}/libonnxruntime.so.%{version}
%{_libdir}/libonnxruntime_providers_shared.so.%{version}

%if %{with test}
%files -n %{name}-test
%{_bindir}/onnx_test_runner
%endif

%files -n %{name}-devel
%dir %{_includedir}/onnxruntime/
%{_includedir}/onnxruntime/*
%{_libdir}/libonnxruntime.so
%{_libdir}/libonnxruntime_providers_shared.so
%dir %{_libdir}/cmake/onnxruntime/
%{_libdir}/cmake/onnxruntime/*.cmake
%{_libdir}/pkgconfig/libonnxruntime.pc

%files %{python_files}
%license LICENSE ThirdPartyNotices.txt
%doc README.md
%{python_sitearch}/onnxruntime*
%python_alternative %{_bindir}/onnxruntime_test

%changelog
openSUSE Build Service is sponsored by