%undefine _annotated_build
%global _lto_cflags %{nil}
%global __cmake_in_source_build 1
# We don't have cuda on openEuler
%bcond_without cuda
%global have_cuda 0

%if %{without cuda}
%ifarch x86_64 aarch64
%global have_cuda 1
%endif
%endif

%global vcu_maj 12
%global vcu_min 3

Name:           ncnn
Version:        20240820
Release:        1
Summary:        Neural network inference computing framework optimized for mobile
License:        BSD
Source0:        https://github.com/Tencent/ncnn/archive/refs/tags/%{version}.tar.gz
URL:            https://github.com/Tencent/ncnn

BuildRequires:  git doxygen cmake ocl-icd-devel
BuildRequires:  python3-setuptools python3-devel
BuildRequires:  opencv protobuf-devel pybind11-devel
BuildRequires:  glslang-devel gcc-c++
BuildRequires:  protobuf-lite-devel
%if %{have_cuda}
BuildRequires:  cuda-nvcc-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvtx-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-cudart-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvml-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-nvrtc-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-driver-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  cuda-profiler-api-%{vcu_maj}-%{vcu_min}
BuildRequires:  libnccl-devel
%endif

%description
High-performance neural network inference computing
framework optimized for mobile platforms.

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

%description    devel
This package contains development files for %{name}.

%package        python3
Summary:        Python files for %{name}
Requires:       %{name} = %{version}-%{release}
Requires:       python3-numpy python3-tqdm python3-requests python3-portalocker opencv

%description    python3
This package contains python files for %{name}.

%prep
%autosetup -p1

%build
rm -rf glslang
sed -i 's|ncnn STATIC|ncnn SHARED|' src/CMakeLists.txt
sed -i '/PRIVATE -fvisibility=hidden/d' src/CMakeLists.txt
sed -i 's|DESTINATION lib|DESTINATION %{_lib}|g' src/CMakeLists.txt
sed -i 's|ncnn PROPERTIES|ncnn PROPERTIES SOVERSION 1 VERSION 1.0|' src/CMakeLists.txt
sed -i 's|add_subdirectory(pybind11)|find_package(pybind11 REQUIRED)|' python/CMakeLists.txt
sed -i '1i #include <cstdint>' tools/darknet/darknet2ncnn.cpp
mkdir build
pushd build

%global optflags %(echo %{optflags} | sed 's|-fno-exceptions||')
%cmake .. -Wno-dev \
       -DCMAKE_SKIP_RPATH=ON \
       -DCMAKE_VERBOSE_MAKEFILE=OFF \
       -DCMAKE_BUILD_TYPE=RelWithDebInfo \
       -DCMAKE_INSTALL_LIBDIR=%{_lib} \
       -DCMAKE_CXX_FLAGS="%{optflags} -fpermissive" \
       -DProtobuf_FOUND=OFF \
%if %{have_cuda}
       -DCUDA_TOOLKIT_ROOT_DIR="/usr/local/cuda-%{vcu_maj}.%{vcu_min}/" \
%endif
       -DNCNN_SYSTEM_GLSLANG=ON \
%ifnarch x86_64 aarch64
       -DNCNN_AVXVNNI=OFF \
       -DNCNN_AVX512FP16=OFF \
       -DNCNN_AVX512BF16=OFF \
%endif
       -DNCNN_SHARED_LIB=ON \
%if "%{_lto_cflags}" == "%{nil}"
       -DNCNN_ENABLE_LTO=OFF \
%else
       -DNCNN_ENABLE_LTO=ON \
%endif
       -DNCNN_DISABLE_EXCEPTION=OFF \
       -DNCNN_PYTHON=ON

make %{?_smp_mflags}
popd


%install
rm -rf %{buildroot}

pushd build
make install DESTDIR=%{buildroot}
popd

pushd python
%{__python3} setup.py install --root %{buildroot}
popd
rm -rf %{buildroot}/%{python3_sitelib}/*.egg-info


%files
%doc README.md CONTRIBUTING.md
%{_bindir}/*
%license LICENSE.txt
%{_libdir}/lib*.so*

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

%files python3
%{python3_sitelib}/*


%changelog
* Thu Nov 07 2024 jinzhiguang <jinzhiguang@kylinos.cn> - 20240820-1
- update to 20240820

* Wed Jan 24 2024 misaka00251 <liuxin@iscas.ac.cn> - 20240102-1
- Init package, import from Balint Cristian