%global pkgvers 1
%global scdate0 20250108
%global sctags0 4.11.0
%global source0 https://github.com/opencv/opencv.git

%global scdate1 20250108
%global sctags1 4.11.0
%global source1 https://github.com/opencv/opencv_contrib.git

%global scdate2 20250108
%global sctags2 4.11.0
%global source2 https://github.com/opencv/opencv_extra.git

%define run_tests 0
%define with_docs 0
%define have_cuda 1
%define have_xine 0
%define have_halide 1
%define have_ffmpeg 1
%define have_openvino 0
%define have_cuda_gcc 1

%global vcu_maj 12
%global vcu_min 8

Name:           opencv
Version:        %(curl -s https://raw.githubusercontent.com/opencv/opencv/%{sctags1}/modules/core/include/opencv2/core/version.hpp | grep -e "MAJOR " -e "MINOR " -e "REVISION " | sed 's/[^[:digit:].-]//g' | sed ':a;N;$!ba;s/\n/./g')
Release:        %{scdate0}.%{pkgvers}.cu%{vcu_maj}_%{vcu_min}%{?dist}
Summary:        Collection of algorithms for computer vision
License:        BSD

URL:            http://opencv.org

Patch0:         21386.patch
Patch1:         opencv-glew.patch
Patch2:         opencv-f16.patch
Patch7:         lsc-fix2.patch
Patch8:         opencv-rgbd.patch
Patch9:         https://github.com/opencv/opencv/pull/25658.patch
Patch10:        https://github.com/opencv/opencv_contrib/pull/3744.patch
Patch11:        https://github.com/opencv/opencv_contrib/commit/8c16a489a1fa9e2ccf21ce8eeb7fbb1affe548b1.patch

Patch100:       b32ba55150c5d678836a80700a126dae8c5b67f8.patch

BuildRequires:  git cmake swig libtool chrpath doxygen python3
BuildRequires:  python3-setuptools python3-devel python3-numpy
BuildRequires:  python3-sphinx pkgconfig zlib-devel eigen3-devel
BuildRequires:  gstreamer1-devel gstreamer1-plugins-base-devel libwebp-devel
BuildRequires:  protobuf-devel libpng-devel ocl-icd-devel hdf5-devel suitesparse-devel
BuildRequires:  eigen3-devel gtk3-devel libtheora-devel libvorbis-devel libgphoto2-devel
BuildRequires:  jasper-devel libpng-devel libjpeg-devel libtiff-devel libavif-devel glog-devel
BuildRequires:  qt5-qtbase-devel gflags-devel libv4l-devel libGL-devel openjpeg2-devel metis-devel
%if 0%{?fedora}
BuildRequires:  coin-or-Clp-devel coin-or-CoinUtils-devel ogre-devel openexr-devel
%endif
%if ! (0%{?rhel} == 9)
BuildRequires:  pylint ceres-solver-devel vtk-devel
%endif
%if %{with_docs}
BuildRequires:  python3-flake8
%endif
%if ! ((0%{?rhel} == 9) && ("%{_arch}" != "x86_64"))
BuildRequires:  libva-devel
%endif
%if 0%{?rhel} == 8
%define have_ffmpeg 0
%endif
Requires: qt5-qtbase qt5-qtbase-gui

%global _lto_cflags %{nil}
%undefine _hardened_build
%undefine _annotated_build
%global __cmake_in_source_build 1

%define gpu_target_bin "5.2;6.1;7.5;8.6;9.0"
%define gpu_target_ptx "5.2"

%bcond_without cuda
%bcond_without xine
%bcond_without ffmpeg
%bcond_without openvino

%if %{without cuda}
%define have_cuda 0
%endif

%if %{without halide}
  %define have_halide 0
%endif

%if %{without xine}
  %define have_xine 0
%endif

%if %{without ffmpeg}
  %define have_ffmpeg 0
%endif

%if %{without openvino}
  %define have_openvino 0
%endif

%if %{have_cuda}
%if %{have_cuda_gcc}
%if (0%{?fedora} > 34) || (0%{?rhel} > 8)
BuildRequires:  cuda-gcc-c++
%endif
%endif
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:  libcublas-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcufft-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libnpp-devel-%{vcu_maj}-%{vcu_min}
BuildRequires:  libcudnn9-devel-cuda-%{vcu_maj}
BuildRequires:  libnccl-devel libnccl
%endif

%if %{have_openvino}
BuildRequires:  openvino-devel
%endif

%if %{have_halide}
BuildRequires:  halide-devel halide-python3
%endif

%if 0%{?fedora} >= 1 || (0%{?rhel} >= 9)
%ifnarch s390 s390x
BuildRequires:  libraw1394-devel
BuildRequires:  libdc1394-devel
%endif
%endif

%if 0%{?fedora}
%ifarch %{ix86} x86_64
BuildRequires:  openni-devel
BuildRequires:  openni-primesense
%endif
%endif

%ifarch %{ix86} x86_64 ia64 ppc ppc64 ppc64le armv7hl aarch64
BuildRequires:  tbb-devel
%endif

%if %{have_ffmpeg}
BuildRequires:  ffmpeg-free-devel
%endif

%if %{have_xine}
BuildRequires:  xine-lib-devel
%endif

Requires:       opencv-core%{_isa} = %{version}-%{release}

%description
OpenCV means Intel® Open Source Computer Vision Library. It is a collection of
C functions and a few C++ classes that implement some popular Image Processing
and Computer Vision algorithms.

%package        core
Summary:        OpenCV core libraries
Requires:       %{name} = %{version}-%{release}

%description    core
This package contains the OpenCV C/C++ core libraries.

%package        contrib
Summary:        OpenCV contrib libraries
Requires:       %{name} = %{version}-%{release}

%description    contrib
This package contains the OpenCV C/C++ contrib libraries.

%if %{have_cuda}
%package        cuda
Summary:        OpenCV cuda libraries
Requires:       %{name} = %{version}-%{release}
Requires:       cuda-cudart-%{vcu_maj}-%{vcu_min}
Requires:       cuda-nvrtc-%{vcu_maj}-%{vcu_min}
Requires:       libcublas-%{vcu_maj}-%{vcu_min}
Requires:       libcufft-%{vcu_maj}-%{vcu_min}
Requires:       libnpp-%{vcu_maj}-%{vcu_min}

%description    cuda
This package contains the OpenCV C/C++ cuda libraries.
%endif

%package        devel
Summary:        Development files for using the OpenCV library
Requires:       %{name} = %{version}-%{release}
%if %{have_cuda}
Requires:       %{name}-cuda = %{version}-%{release}
%endif
Requires:       %{name}-static = %{version}-%{release}
Requires:       %{name}-contrib = %{version}-%{release}

%description    devel
This package contains the OpenCV C/C++ library and header files, as well as
documentation. It should be installed if you want to develop programs that
will use the OpenCV library. You should consider installing opencv-devel-docs
package.

%package        devel-docs
Summary:        Development files for using the OpenCV library
Requires:       opencv-devel = %{version}-%{release}
BuildArch:      noarch

%description    devel-docs
This package contains the OpenCV documentation and examples programs.

%package        python3
Summary:        Python3 bindings for apps which use OpenCV
Requires:       python3-numpy
Requires:       %{name} = %{version}-%{release}
Requires:       %{name}-contrib = %{version}-%{release}
Provides:       python%{python3_version}dist(opencv-python)

%description    python3
This package contains Python3 bindings for the OpenCV library.

%package        static
Summary:        OpenCV static addon libraries
Requires:       %{name}-devel = %{version}-%{release}

%description    static
This package contains the OpenCV C/C++ static addon libraries.

%prep
%setup -T -c -n %{name}
git clone --depth 1 -n -b %{sctags0} %{source0} .
git reset --hard %{sctags0}
git log --format=fuller
rm -rf 3dparty

git clone --depth 1 -n -b %{sctags1} %{source1} opencv_contrib
git -C opencv_contrib reset --hard %{sctags1}
git -C opencv_contrib log --format=fuller

%if %{run_tests}
git clone --depth 1 -n -b %{sctags2} %{source2} opencv_extra
git -C opencv_extra reset --hard %{sctags2}
git -C opencv_extra log --format=fuller
%endif

#patch -P 0 -p1
#patch -P 2 -p1
#patch -P 9 -p1
%patch -P 100 -p1

pushd opencv_contrib
#patch -P 1 -p1
%patch -P 7 -p1
%patch -P 8 -p1
#patch -P 10 -p1
#patch -P 11 -p1
popd

# externals
rm -rf 3rdparty/libjasper
rm -rf 3rdparty/libjpeg-turbo
rm -rf 3rdparty/libjpeg
rm -rf 3rdparty/openjpeg
rm -rf 3rdparty/libpng
rm -rf 3rdparty/libtiff
rm -rf 3rdparty/libwebp
rm -rf 3rdparty/tbb
rm -rf 3rdparty/protobuf
rm -rf 3rdparty/zlib
rm -rf 3rdparty/include/opencl
rm -rf 3rdparty/include/vulkan

# gcc13
sed -i '1i #include <thread>' modules/dnn/src/op_halide.cpp
# fixes
sed -i cmake/OpenCVDetectCUDA.cmake -e 's|c++11|c++14|g'
sed -i cmake/OpenCVDetectCUDA.cmake -e 's|6.0;6.1|6.1|g'
sed -i cmake/OpenCVFindLibsGUI.cmake -e 's|if(WITH_GTK AND NOT HAVE_QT)|if(WITH_GTK)|g'
sed -i cmake/OpenCVDetectVTK.cmake -e 's|VTK_VERSION VERSION_EQUAL |VTK_VERSION VERSION_GREATER_EQUAL |g'
sed -i CMakeLists.txt -e 's|${CMAKE_INSTALL_LIBDIR}${LIB_SUFFIX}|${CMAKE_INSTALL_LIBDIR}|g'
sed -i cmake/OpenCVFindLAPACK.cmake -e 's|_find_file_in_dirs(LAPACKE_H_PATH "${LAPACK_LAPACKE_H}" "${LAPACK_INCLUDE_DIR}")|set(LAPACKE_H_PATH "${LAPACK_INCLUDE_DIR}/openblas/${LAPACK_LAPACKE_H}")|g'
sed -i cmake/OpenCVDetectOpenCL.cmake -e '/ocv_install_3rdparty_licenses(opencl-headers/d'
sed -i opencv_contrib/modules/sfm/CMakeLists.txt -e 's|${the_module} ${LIBMV_LIGHT_LIBS}|${the_module} ${LIBMV_LIGHT_LIBS} gflags glog|g'
# numpy
%if ! (0%{?rhel} == 8)
sed -i 's|"import os.*get_numpy_include_dirs()))"|"print(\\\"%{python3_sitearch}/numpy/core/include/numpy\\\")"|' cmake/OpenCVDetectPython.cmake
%endif


%build
mkdir -p build
pushd build
export LD_LIBRARY_PATH="/usr/local/cuda-%{vcu_maj}.%{vcu_min}/%{_lib}/"
%global optflags %(echo "%{optflags} -w -fpermissive")
%cmake -Wno-dev \
 -DCMAKE_SKIP_RPATH=ON \
 -DCMAKE_BUILD_TYPE=RelWithDebInfo \
 -DCMAKE_VERBOSE_MAKEFILE=OFF \
 -DENABLE_PRECOMPILED_HEADERS=0 \
 -DCMAKE_CXX_STANDARD=17 \
 -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
 -DPYTHON_PACKAGES_PATH=%{python3_sitearch} \
 -DPYTHON_EXECUTABLE="%{_bindir}/python3" \
 -DPYTHON3_EXECUTABLE=%{_bindir}/python3 \
 -DBUILD_DOCS=ON \
 -DOPENCV_ENABLE_NONFREE=ON \
%ifarch i686 x86_64
 -DENABLE_FAST_MATH=ON \
%endif
 -DENABLE_OMIT_FRAME_POINTER=ON \
 -DWITH_TBB=ON -DTBB_LIB_DIR=%{_libdir} \
 -DWITH_GSTREAMER=ON \
 -DWITH_EIGEN=ON \
 -DWITH_LAPACK=OFF \
 -DWITH_GTK=ON \
 -DWITH_QT=ON \
 -DHAVE_VTK=ON \
 -DVTK_DIR=%{_libdir}/cmake/vtk \
 -DWITH_OPENCL=ON \
%if 0%{?rhel}
%ifarch ppc64le
 -DWITH_OPENCL=OFF \
%endif
%endif
 -DWITH_OPENCLAMDFFT=OFF \
 -DWITH_OPENCLAMDBLAS=OFF \
 -DWITH_CLP=ON \
%if %{have_halide}
%if (0%{?fedora} > 37) || 0%{?rhel}
 -DWITH_HALIDE=ON \
%else
 -DWITH_HALIDE=OFF \
%endif
%endif
 -DHAVE_OPENMP=YES \
 -DHALIDE_ROOT_DIR=/usr \
 -DBUILD_opencv_cudacodec=0 \
 -DBUILD_PROTOBUF=OFF \
 -DPROTOBUF_UPDATE_FILES=ON \
 -DCVV_DEBUG_MODE=OFF \
%if 0%{?fedora} || (0%{?rhel} > 9)
 -DWITH_OPENNI=ON \
%else
 -DWITH_OPENNI=OFF \
%endif
 -DWITH_OPENGL=ON \
 -DWITH_ONNX=OFF \
%if %{have_openvino}
 -DWITH_NGRAPH=ON \
 -DWITH_INF_ENGINE=ON \
 -DInferenceEngine_DIR=%{_libdir}/cmake/ \
 -Dngraph_DIR=%{_libdir}/cmake/ \
 -DINF_ENGINE_INCLUDE_DIRS=%{_includedir}/inference-engine \
 -DINF_ENGINE_LIB_DIRS=%{_libdir} \
%else
 -DWITH_NGRAPH=OFF \
 -DWITH_INF_ENGINE=OFF \
%endif
 -DOpenGL_GL_PREFERENCE=GLVND \
 -DINSTALL_C_EXAMPLES=ON \
 -DINSTALL_PYTHON_EXAMPLES=ON \
%if %{have_ffmpeg}
 -DWITH_FFMPEG=ON \
%endif
%if %{have_xine}
 -DWITH_XINE=ON \
%endif
%if %{have_cuda}
 -DWITH_CUDA=ON \
 -DWITH_CUFFT=ON \
 -DWITH_CUBLAS=ON \
 -DHAVE_NVCUVID=ON \
 -DCUDA_FAST_MATH=ON \
 -DCUDA_GENERATION="" \
 -DCUDA_ARCH_BIN=%{gpu_target_bin} \
 -DCUDA_ARCH_PTX=%{gpu_target_ptx} \
 -DCUDA_PROPAGATE_HOST_FLAGS=OFF \
%if %{have_cuda_gcc}
%if (0%{?fedora} > 34) || (0%{?rhel} >= 9)
 -DCUDA_HOST_COMPILER="%{_bindir}/cuda-g++" \
%endif
%endif
 -DCUDA_ENABLE_DEPRECATED_GENERATION=ON \
 -DCUDA_NVCC_FLAGS="-Wno-deprecated-gpu-targets -allow-unsupported-compiler" \
%endif
 -DPLANTUML_JAR=/usr/share/java/plantuml.jar \
%if %{run_tests}
 -DWITH_IPP=ON \
 -DBUILD_TESTS=ON \
 -DBUILD_PERF_TESTS=ON \
 -DBUILD_opencv_ts=1 \
%else
 -DWITH_IPP=OFF \
 -DBUILD_TESTS=OFF \
 -DBUILD_PERF_TESTS=OFF \
 -DBUILD_opencv_ts=1 \
 -DBUILD_opencv_java=0 \
 -DBUILD_opencv_matlab=0 \
 -DBUILD_opencv_python2=0 \
%endif
 ..
make %{?_smp_mflags}
popd


%install
rm -rf __devel-doc
pushd build
export LD_LIBRARY_PATH="/usr/local/cuda-%{vcu_maj}.%{vcu_min}/%{_lib}/"
make install DESTDIR=%{buildroot} INSTALL="install -p" CPPROG="cp -p"
find %{buildroot} -name '*.la' -delete

rm -f %{buildroot}%{_datadir}/OpenCV/samples/c/build_all.sh \
      %{buildroot}%{_datadir}/OpenCV/samples/c/cvsample.dsp \
      %{buildroot}%{_datadir}/OpenCV/samples/c/cvsample.vcproj \
      %{buildroot}%{_datadir}/OpenCV/samples/c/facedetect.cmd

# remove unnecessary documentation
rm -rf %{buildroot}%{_datadir}/*/doc
rm -rf %{buildroot}%{_datadir}/*/valgrind.supp
rm -rf %{buildroot}%{_datadir}/*/valgrind_3rdparty.supp

popd

# fix python path
mv -f %{buildroot}/usr/lib/python3.* %{buildroot}/%{_libdir}/ || true

# egg info
pushd build/python_loader/
sed -i "s|package_name = '.*|package_name = 'opencv_python'|" setup.py
%py3_install -- --install-lib %{python3_sitearch}
sed -i "s|package_name = '.*|package_name = 'opencv'|" setup.py
%py3_install -- --install-lib %{python3_sitearch}
popd

# remove junk
rm -rf %{buildroot}/usr/python
rm -rf %{buildroot}/usr/bin/setup_vars_opencv4.sh


%check
%if %{run_tests}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%{_builddir}/%{name}/build/lib/
export OPENCV_TEST_DATA_PATH=%{_builddir}/%{name}/opencv_extra/testdata/

pushd build/bin/

  for task in $(ls opencv_test*); do
    echo "Running [$task]"
    ./$task --gtest_output=xml:results_$task.xml
  done

  for task in $(ls opencv_perf*); do
    echo "Running [$task]"
    ./$task --gtest_output=xml:results_$task.xml
  done

popd
%endif


%ldconfig_scriptlets


%files
%doc LICENSE
%{_datadir}/licenses
%{_bindir}/opencv_*
%{_libdir}/libopencv*_calib3d.so.*
%{_libdir}/libopencv*_ccalib.so.*
%{_libdir}/libopencv*_features2d.so.*
%{_libdir}/libopencv*_highgui.so.*
%{_libdir}/libopencv*_objdetect.so.*
%{_libdir}/libopencv*_stitching.so.*
%{_libdir}/libopencv*_superres.so.*
%{_libdir}/libopencv*_stereo.so.*
%{_libdir}/libopencv*_videostab.so.*
%{_libdir}/libopencv*_imgcodecs.so.*
%{_libdir}/libopencv*_img_hash.so.*
%{_libdir}/libopencv*_shape.so.*
%{_libdir}/libopencv*_videoio.so.*
%{_libdir}/libopencv*_signal.so.*

%dir %{_datadir}/opencv4
%{_datadir}/*/haarcascades
%{_datadir}/*/lbpcascades

%files core
%{_libdir}/libopencv*_core.so.*
%{_libdir}/libopencv*_flann.so.*
%{_libdir}/libopencv*_imgproc.so.*
%{_libdir}/libopencv*_ml.so.*
%{_libdir}/libopencv*_photo.so.*
%{_libdir}/libopencv*_video.so.*
%{_libdir}/libopencv*_cvv.so.*
%{_libdir}/libopencv*_text.so.*
%{_libdir}/libopencv*_dnn.so.*
%{_libdir}/libopencv*_dnn_superres.so.*
%{_libdir}/libopencv*_gapi.so.*
%if ! (0%{?rhel} == 9)
%{_libdir}/libopencv*_viz.so.*
%endif
%{_libdir}/libopencv*_alphamat.so.*

%files contrib
%{_libdir}/libopencv*_bgsegm.so.*
%{_libdir}/libopencv*_bioinspired.so.*
%{_libdir}/libopencv*_ccalib.so.*
%{_libdir}/libopencv*_datasets.so.*
%{_libdir}/libopencv*_face.so.*
%{_libdir}/libopencv*_line_descriptor.so.*
%{_libdir}/libopencv*_optflow.so.*
%{_libdir}/libopencv*_reg.so.*
%{_libdir}/libopencv*_rgbd.so.*
%{_libdir}/libopencv*_saliency.so.*
%{_libdir}/libopencv*_surface_matching.so.*
%{_libdir}/libopencv*_tracking.so.*
%{_libdir}/libopencv*_xfeatures2d.so.*
%{_libdir}/libopencv*_ximgproc.so.*
%{_libdir}/libopencv*_xobjdetect.so.*
%{_libdir}/libopencv*_xphoto.so.*
%{_libdir}/libopencv*_aruco.so.*
%{_libdir}/libopencv*_dpm.so.*
%{_libdir}/libopencv*_sfm.so.*
%{_libdir}/libopencv*_hdf.so.*
%{_libdir}/libopencv*_fuzzy.so.*
%{_libdir}/libopencv*_phase_unwrapping.so.*
%{_libdir}/libopencv*_plot.so.*
%{_libdir}/libopencv*_freetype.so.*
%{_libdir}/libopencv*_structured_light.so.*
%{_libdir}/libopencv*_hfs.so.*
%{_libdir}/libopencv*_dnn_objdetect.so.*
%{_libdir}/libopencv*_quality.so.*
%{_libdir}/libopencv*_rapid.so.*
%{_libdir}/libopencv*_intensity_transform.so.*
%{_libdir}/libopencv*_mcc.so.*
%{_libdir}/libopencv*_wechat_qrcode.so.*

%{_datadir}/*/quality/brisque_model_live.yml
%{_datadir}/*/quality/brisque_range_live.yml

%if %{have_cuda}
%files cuda
%{_libdir}/libopencv*_cudaobjdetect.so.*
%{_libdir}/libopencv*_cudaarithm.so.*
%{_libdir}/libopencv*_cudabgsegm.so.*
%{_libdir}/libopencv*_cudafeatures2d.so.*
%{_libdir}/libopencv*_cudafilters.so.*
%{_libdir}/libopencv*_cudaimgproc.so.*
%{_libdir}/libopencv*_cudastereo.so.*
%{_libdir}/libopencv*_cudawarping.so.*
%{_libdir}/libopencv*_cudev.so.*
#{_libdir}/libopencv*_cudacodec.so.*
%{_libdir}/libopencv*_cudalegacy.so.*
%{_libdir}/libopencv*_cudaoptflow.so.*
%endif

%files devel
%{_includedir}/opencv4
%{_libdir}/lib*.so
%{_libdir}/cmake/*

%files devel-docs
%doc build/doc/doxygen/html
%doc %{_datadir}/*/samples

%files python3
%{python3_sitearch}/*

%files static
%{_libdir}/opencv4

%changelog
* Tue Oct 21 2014 Balint Cristian <cristian.balint@gmail.com>
- github update releases