File python-onnxruntime.spec of Package python-onnxruntime

#
# spec file for package python-onnxruntime
#
# Copyright (c) 2020 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/
#


%{?!python_module:%define python_module() python-%{**} python3-%{**}}
%define skip_python2 1
%define skip_python36 1
Name:           python-onnxruntime
Version:        1.3.1
Release:        0
Summary:        ONNX Runtime Python bindings
License:        MIT AND Apache-2.0 AND MPL-2.0
URL:            https://github.com/microsoft/onnxruntime/
Source0:        https://github.com/microsoft/onnxruntime/archive/v%{version}.tar.gz#/%{name}-%{version}.tar.gz
# not provided by system cmake
Source1:        FindRe2.cmake
# single hpp include, not packaged
# License10: MIT
Source10:       https://github.com/HowardHinnant/date/archive/v2.4.1.tar.gz#/date-2.4.1.tar.gz
# Is used to compile libonnx and libonnx_proto with the protobuf files from onnxruntime. Consuming
# the system shared library is not supported right now.
# License12: MIT
Source12:       https://github.com/onnx/onnx/archive/v1.7.0.tar.gz#/onnx-1.7.0.tar.gz
# can't use system eigen, see https://github.com/microsoft/onnxruntime/issues/2812
# License13:    MPL-2.0
Source13:       eigen-efd9867ff0e8df23016ac6c9828d0d7bf8bec1b1.tar.gz
# just single hpp include, not packaged
# License13: MIT
Source14:       https://raw.githubusercontent.com/dcleblanc/SafeInt/3.24/SafeInt.hpp
# not packaged
# License15: Apache-2.0
Source15:       https://github.com/google/gemmlowp/archive/42c53187a56c12dc5518cc25e778e5e3e7dbaf72.tar.gz#/gemmlowp-42c53187a56c12dc5518cc25e778e5e3e7dbaf72.tar.gz
Patch0:         probuf-name-fix.patch
Patch1:         use-system-libnsync.patch
Patch3:         gcc10.patch
# compile onnx and onnx_proto shared libraries, but do not install them as installation
# would confuse libprotobuf, when using onnx and onnxruntime in parallel
# see https://github.com/microsoft/onnxruntime/issues/4508
Patch4:         make-onnx_proto-shared-lib.patch
Patch5:         Add-compatibility-with-Protobuf-3.12-4291.patch
BuildRequires:  %{python_module devel}
BuildRequires:  %{python_module numpy-devel >= 1.15.0}
BuildRequires:  %{python_module numpy}
BuildRequires:  %{python_module onnx}
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}
BuildRequires:  cmake
BuildRequires:  eigen3-devel
BuildRequires:  fdupes
BuildRequires:  gcc-c++
BuildRequires:  gmock
BuildRequires:  gsl-devel
BuildRequires:  gtest
BuildRequires:  libnsync-devel
BuildRequires:  libtool
BuildRequires:  nlohmann_json-devel
BuildRequires:  openblas-devel
BuildRequires:  pkgconfig
BuildRequires:  protobuf-devel
BuildRequires:  python-rpm-macros
BuildRequires:  re2-devel
BuildRequires:  pkgconfig(libpng)
BuildRequires:  pkgconfig(zlib)
Requires:       python-numpy >= 1.15.0
Requires:       python-onnx >= 1.2.3
%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/>.

%prep
%setup -q -n onnxruntime-%{version}
%autopatch -p1
# put additional sources in place
%define make_depend_src() test -e $(basename %{1}| sed 's/-.*//') && rmdir %{?2}%{!?2:$(basename %{1}| sed 's/-.*//')}; test -e %{2} && rmdir %{2}; tar xzf %{1}; mv $(basename %{1} | sed 's/\.tar\.gz//' ) %{?2}%{!?2:$(basename %{1}| sed 's/-.*//')}
pushd cmake/external
%make_depend_src %{SOURCE10}
%make_depend_src %{SOURCE12}
%make_depend_src %{SOURCE13}
cp %{S:14} SafeInt/safeint/
%make_depend_src %{SOURCE15}
pushd json
mkdir single_include
ln -s %{_includedir}/nlohmann single_include/
popd

# find re2
cp %{SOURCE1}  Findre2.cmake
popd

%build
# The build is done with cmake, but some of its standard flags make the build
# fail.  The flags which break compilation are explained below.  Also some
# additional flags to onnxruntime so that as much system libraries as possible
# are used.
#
# NOTE: The unit tests have to build in order to enable the python bindings, there
# is no way to avoid the tests during build and getting the python bindings
pushd cmake
%global _lto_cflags %{_lto_cflags} -ffat-lto-objects
%cmake \
    -Donnxruntime_BUILD_UNIT_TESTS=ON \
    -Donnxruntime_BUILD_SHARED_LIB=ON \
    -Donnxruntime_USE_FULL_PROTOBUF=ON \
    -Donnxruntime_ENABLE_PYTHON=ON \
    -Donnxruntime_PREFER_SYSTEM_LIB=ON  \
    -DCMAKE_SKIP_RPATH:BOOL=OFF \
    -DBUILD_SHARED_LIBS:BOOL=OFF \
%{nil}
%cmake_build
popd
# must be called in this subdirectory
pushd cmake/build
%python_exec ../../setup.py build
popd

%install
# must be called in this subdirectory
pushd cmake/build
%python_exec ../../setup.py install -O1 --skip-build --force --root %{buildroot} --prefix %{_prefix}
%if "%{_lib}" == "lib64"
# rename the folder from sitelib to sitearch
%python_expand mkdir -p %{buildroot}%{$python_sitearch}
%python_expand mv %{buildroot}%{$python_sitelib}/* %{buildroot}%{$python_sitearch}
%endif
popd
%python_expand %fdupes %{buildroot}%{$python_sitearch}

%check
pushd cmake
%ctest
popd

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

%changelog
openSUSE Build Service is sponsored by