%global git_version a0d77f18516d2da7468a96b0de3b737266f23176

Name:       onnx
Version:    1.14.0
Release:    2%{?dist}
Summary:    Open standard for machine learning interoperability
License:    Apache-2.0

URL:        https://github.com/onnx/onnx
Source0:    https://github.com/onnx/onnx/archive/v%{version}/%{name}-%{version}.tar.gz
# Need to patch the generated headers
Source1:    add-export-protobuf-headers.patch
# Build shared libraries and fix install location 
Patch0:     onnx-install.patch
# Add what is missing to run tox, disable tests that require network
Patch1:     onnx-tox.patch
# Use system protobuf and require parameterized
Patch2:     onnx-requirements.patch 

# https://bugzilla.redhat.com/show_bug.cgi?id=2212096
ExcludeArch:    s390x

BuildRequires:  cmake >= 3.13
BuildRequires:  make
BuildRequires:  findutils
BuildRequires:  gcc
BuildRequires:  gcc-c++
BuildRequires:  zlib-devel
BuildRequires:  python3-devel
BuildRequires:  python3-pip
BuildRequires:  python3-pybind11
BuildRequires:  protobuf-lite-devel

%global _description %{expand:
%{name} provides an open source format for AI models, both deep learning and
traditional ML. It defines an extensible computation graph model, as well as
definitions of built-in operators and standard data types.}

%description %_description

%package libs
Summary:    Libraries for %{name}

%description libs %_description

%package devel
Summary:    Development files for %{name}
Requires:   %{name}-libs = %{version}-%{release} 

%description devel %_description

%package -n python3-onnx
Summary:    %{summary}
Requires:   %{name}-libs = %{version}-%{release}

%description -n python3-onnx %_description

%prep
%autosetup -p1 -n onnx-%{version}

%generate_buildrequires
%pyproject_buildrequires -t

%build
%cmake \
    -DONNX_USE_LITE_PROTO=ON \
    -DONNX_USE_PROTOBUF_SHARED_LIBS=ON \
    -DBUILD_ONNX_PYTHON=ON \
    -DPYTHON_EXECUTABLE=%{python3} \
    -DPY_EXT_SUFFIX=%{python3_ext_suffix} \
    -DPY_SITEARCH=%{python3_sitearch}
# Generate protobuf header and source files
%cmake_build -- gen_onnx_proto
# Add missing ONNX_API
(cd "%__cmake_builddir"; patch -p1 < "%{_sourcedir}/add-export-protobuf-headers.patch")
# Build 
%cmake_build

%install
%cmake_install
# Need to remove empty directories
find "%{buildroot}/%{_includedir}" -type d -empty -delete
find "%{buildroot}/%{python3_sitearch}" -type d -empty -delete
# This file is normally generated by setup.py
cat > "%{buildroot}/%{python3_sitearch}/%{name}/version.py" <<EOF
version = "%{version}"
git_version = "%{git_version}"
EOF
# Executable modules
chmod a+x "%{buildroot}/%{python3_sitearch}/%{name}/gen_proto.py"
chmod a+x "%{buildroot}/%{python3_sitearch}/%{name}/defs/gen_doc.py"
%py3_shebang_fix "%{buildroot}/%{python3_sitearch}/%{name}/gen_proto.py"
%py3_shebang_fix "%{buildroot}/%{python3_sitearch}/%{name}/defs/gen_doc.py"
# Install *.proto files
install -p "./onnx/"*.proto -t "%{buildroot}/%{_includedir}/onnx/"

%check
export LD_LIBRARY_PATH=%{buildroot}/%{_libdir}
%tox

%files libs
%license LICENSE
%doc README.md
%{_libdir}/libonnx.so.%{version}
%{_libdir}/libonnx_proto.so.%{version}

%files devel
%{_libdir}/libonnx.so
%{_libdir}/libonnx_proto.so
%{_libdir}/cmake/ONNX
%{_includedir}/%{name}/

%files -n python3-onnx
%{python3_sitearch}/%{name}/

%changelog
* Wed Jun 07 2023 Alejandro Alvarez Ayllon <a.alvarezayllon@gmail.com> - 1.14.0-2
- Patch protobuf headers with ONNX_API
- Ship .proto files

* Sat Jun 03 2023 Alejandro Alvarez Ayllon <a.alvarezayllon@gmail.com> - 1.14.0-1
- Release 1.14.0

* Thu May 18 2023 Diego Herrera <dherrera@redhat.com> - 1.13.0-3
- Fix License entry to comply with SPDX
- Add onnx-libs as an explicit dependency to the python package

* Sat Dec 17 2022 Alejandro Alvarez Ayllon <aalvarez@fedoraproject.org> - 1.13.0-2
- Release 1.13.0

* Wed Nov 23 2022 Alejandro Alvarez Ayllon <aalvarez@fedoraproject.org> - 1.12.0-1
- Release 1.12.0