%global pkgvers 0
%global scdate0 20250308
%global schash0 bc58ac508ab25ea214e0cdd7bb8483911af604b9
%global branch0 main
%global source0 https://github.com/onnx/onnx.git

%global sshort0 %{expand:%%{lua:print(('%{schash0}'):sub(1,8))}}

Name:           onnx
Version:        %(curl -s https://raw.githubusercontent.com/onnx/onnx/main/VERSION_NUMBER | sed 's|[^[:digit:].]||g')
Release:        %{scdate0}.%{pkgvers}.git%{sshort0}%{?dist}
Summary:        Open Neural Network Exchange
License:        Apache

URL:            https://onnx.ai

Patch0:         onnx-el9.patch
Patch1:         onnx-mypi.patch

BuildRequires:  doxygen cmake gcc-c++ git
BuildRequires:  python3-devel python3-setuptools
BuildRequires:  pybind11-devel python3-pytest-runner
%if 0%{?fedora}
BuildRequires:  protobuf-compat-devel >= 3.21
BuildRequires:  python3-protobuf-compat >= 3.21
%else
BuildRequires:  protobuf-devel
BuildRequires:  python3-protobuf
%endif

Requires:       wget
Requires:       %{name}-libs = %{version}-%{release}
Requires:       %{name}-python3 = %{version}-%{release}

%description
Open Neural Network Exchange

%package        libs
Summary:        Library files for onnx

%description    libs
This package contains library files for onnx.

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

%description    devel
This package contains development files for onnx.


%package        python3
Summary:        Python files for onnx
Requires:       %{name} = %{version}-%{release}
Requires:       %{name}-libs = %{version}-%{release}
Requires:       python3-numpy python3-typing-extensions
%if 0%{?fedora}
Requires:       python3-protobuf-compat >= 3.21
%else
Requires:       python3-protobuf
%endif

%description    python3
This package contains python files for onnx.


%prep
%setup -T -c -n %{name}
git clone --depth 1 -n -b %{branch0} %{source0} .
git fetch --depth 1 origin %{schash0}
git reset --hard %{schash0}
git --no-pager log --format=fuller
%if 0%{?rhel}
%patch0 -p1
%patch1 -p1
%endif
# external libraries
rm -rf third_party/benchmark
rm -rf third_party/pybind11
# python3
for f in `find . -name '*.py'`;
do
  sed -i -e 's|#!/usr/bin/env python|#!/usr/bin/python3|' $f
done
# requirements
sed -i '/wget/d' requirements.txt
sed -i 's|protobuf.*|protobuf|' requirements.txt
# libonnx_proto
sed -i '/VISIBILITY_/d' CMakeLists.txt
# fix protobuf > 3.11
sed -i 's|dllexport_decl=ONNX_API:||g' CMakeLists.txt
sed -i '/gen_proto,/d' onnx/__init__.py
# python36
sed -i 's|setuptools.find_namespace_packages|setuptools.find_packages|' setup.py
# python 3.9
sed -i 's|self.editable_mode|False|' setup.py
# pybind 11
sed -i 's|pybind11 .* CONFIG|pybind11 CONFIG|g' CMakeLists.txt


%build
# flags
export ONNX_ML=0
export CMAKE_ARGS="-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_INSTALL_PREFIX:PATH=/usr -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_SKIP_RPATH=ON -DCMAKE_VERBOSE_MAKEFILE=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo -DONNX_USE_PROTOBUF_SHARED_LIBS=ON"
# build
%{__python3} setup.py build %{?_smp_mflags}


%install
pushd .setuptools-cmake-build
make install DESTDIR=%{buildroot}
popd

# flags
export ONNX_ML=0
export CMAKE_ARGS="-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_INSTALL_PREFIX:PATH=/usr -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_SKIP_RPATH=ON -DCMAKE_VERBOSE_MAKEFILE=OFF -DCMAKE_BUILD_TYPE=RelWithDebInfo -DONNX_USE_PROTOBUF_SHARED_LIBS=ON"
# install
%{__python3} setup.py install --root %{buildroot}

# fix binaries
mkdir -p %{buildroot}/%{_bindir}/
sed -i '1 i #!/usr/bin/python3' %{buildroot}/%{python3_sitearch}/onnx/bin/checker.py
mv -f %{buildroot}/%{python3_sitearch}/onnx/bin/checker.py %{buildroot}/%{_bindir}/
sed -i '1 i #!/usr/bin/python3' %{buildroot}/%{python3_sitearch}/onnx/tools/net_drawer.py
mv -f %{buildroot}/%{python3_sitearch}/onnx/tools/net_drawer.py  %{buildroot}/%{_bindir}/
rm -rf %{buildroot}/%{python3_sitearch}/onnx/bin %{buildroot}/%{python3_sitearch}/onnx/tools
chmod +x %{buildroot}/%{_bindir}/*

# remove bogus
rm -rf %{buildroot}/%{python3_sitearch}/onnx/backend/test
rm -rf %{buildroot}/%{python3_sitearch}/onnx/gen_proto.py
rm -rf %{buildroot}/%{python3_sitearch}/onnx/__pycache__/gen_proto*.pyc
rm -rf %{buildroot}/%{python3_sitearch}/onnx/defs/gen_doc.py
rm -rf %{buildroot}/%{python3_sitearch}/onnx/defs/__pycache__/gen_doc*.pyc
%if !0%{?rhel}
sed -i '/^[^[]/ s|[!~<=>].*||' %{buildroot}/%{python3_sitearch}/*egg-info/requires.txt
%endif

# remove C & headers from python
find %{buildroot}/%{python3_sitearch} -type f -regex '.*\.\(h\|c\|cc\)' -exec rm -rf {} \;
for d in `find %{buildroot}/%{python3_sitearch} -type d -empty`
do
  rm -rf $d | true
done

# fix cmake path
sed -i -e 's|/lib/|/%{_lib}/|g' %{buildroot}/%{_libdir}/cmake/ONNX/ONNXTargets-*.cmake


%files
%doc README.md
%license LICENSE
%{_bindir}/*

%files libs
%{_libdir}/lib*.so*

%files devel
%{_includedir}/*
%{_libdir}/cmake/*

%files python3
%{python3_sitearch}/*


%changelog
* Tue Mar 26 2019 Balint Cristian <cristian.balint@gmail.com>
- github update releases