# This recipe creates 3 packages
# libopencv, py-opencv, opencv
#
# opencv actually contains all the files, including the site-packages
# file needed to run opencv with python.
#
# libopencv and py-opencv are aliases
#
# opencv-python-headless is the headless variant of opencv (i.e. no dep on qt)
#
# By putting all the generated files in 1 package, this makes the build process
# much easier, at the expense of a few MBs in the 'lib' package.
{% set version = "4.10.0" %}
{% set build_num = "0" %}
{% set major_version = version.split('.')[0] %}
{% set PY_VER_MAJOR = PY_VER.split('.')[0] %}
{% set PY_VER_MINOR = PY_VER.split('.')[1] %}

{% if build_variant == "headless" %}
{%   set package_name = "opencv-python-headless" %}
{% else %}
{%   set package_name = "opencv" %}
{% endif %}

package:
  name: opencv-suite
  version: {{ version }}

source:
  - url: https://github.com/opencv/opencv/archive/{{ version }}.tar.gz
    fn: opencv-{{ version }}.tar.gz
    sha256: b2171af5be6b26f7a06b1229948bbb2bdaa74fcf5cd097e0af6378fce50a6eb9
    patches:
      # backport https://github.com/opencv/opencv/pull/21611 (unmerged as of 06/2023)
      - patches_opencv/0001-Add-installation-of-pip-metadata-from-cmake.patch
      - patches_opencv/0001-Also-install-metadata-for-opencv-python-headless.patch
      - patches_opencv/0002-delete-lines-that-download-opencv.patch
      - patches_opencv/0003-find-pkgconfig-on-windows.patch
      - patches_opencv/0004-fix-detection-for-protobuf-23.x.patch
  - url: https://github.com/opencv/opencv_contrib/archive/{{ version }}.tar.gz
    fn: opencv_contrib-{{ version }}.tar.gz
    sha256: 65597f8fb8dc2b876c1b45b928bbcc5f772ddbaf97539bf1b737623d0604cba1
    folder: opencv_contrib
    patches:
      # Allow attempt to find HDF5 on cross-compile
      - patches_opencv_contrib/cmake_hdf5_xpile.patch
  - fn: test.avi
    url: https://github.com/opencv/opencv_extra/raw/master/testdata/highgui/video/VID00003-20100701-2204.avi
    sha256: 78884f64b564a3b06dc6ee731ed33b60c6d8cd864cea07f21d94ba0f90c7b310

build:
  number: {{ build_num }}
  # Package not supported on s390x
  skip: true  # [s390x]

requirements:
  build:
    - m2-patch  # [win]
    - patch     # [unix]

outputs:
  - name: {{ package_name }}
    script: build_opencv.sh   # [not win]
    script: build_opencv.bat  # [win]
    version: {{ version }}
    build:
      number: {{ build_num }}
    run_exports:
      # We are cautiously optimistic here that patch-level updates won't break
      # ABI. However, it sounds like upstream won't guarantee *any* sort of
      # binary compatibility any longer. See also
      #
      #     https://github.com/opencv/opencv/wiki/OE-4.-OpenCV-4
      #
      - {{ pin_subpackage('opencv', max_pin='x.x') }} # [build_variant == 'normal']
      - {{ pin_subpackage('opencv-python-headless', max_pin='x.x') }} # [build_variant == 'headless']
    requirements:
      build:
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}    
        - pkg-config
        - cmake
        - ninja-base
        # pkg-config on win pulls in python
        # adding here explicitely to pull in the same version as in host
        - python                         # [win]
        - {{ cdt('libselinux') }}        # [linux]
        - {{ cdt('libxau-devel') }}      # [linux]
        - {{ cdt('libxcb') }}            # [linux]
        - {{ cdt('libxdamage') }}        # [linux]
        - {{ cdt('libxdamage-devel') }}  # [linux]
        - {{ cdt('libxext-devel') }}     # [linux]
        - {{ cdt('libxfixes') }}         # [linux]
        - {{ cdt('libxfixes-devel') }}   # [linux]
        - {{ cdt('libxi-devel') }}       # [linux]
        - {{ cdt('libxrender-devel') }}  # [linux]
        - {{ cdt('libxxf86vm') }}        # [linux]
        - {{ cdt('mesa-libgl-devel') }}  # [linux]
        - {{ cdt('mesa-libegl-devel') }} # [linux]
        - {{ cdt('mesa-dri-drivers') }}  # [linux]
      host:
        - python
        - numpy {{ numpy }}
        - eigen 3.4.0
        - ffmpeg 4                  # [not win]
        - freetype {{ freetype }}
        # harfbuzz, glib, gettext are both needed for freetype support
        - harfbuzz {{ harfbuzz }}
        - libglib {{ glib }}
        - gst-plugins-base {{ gst_plugins_base }} # [unix]
        - gstreamer {{ gstreamer }} # [unix]
        - gst-plugins-base 1.22.3 # [win]
        - gstreamer 1.22.3 # [win]
        - hdf5 {{ hdf5 }}
        - jpeg {{ jpeg }}           # [not win]
        - openjpeg {{ openjpeg }}
        - libiconv 1.16             # [unix]
        - libpng  {{ libpng }}
        - libprotobuf 4.25.3
        - libabseil 20240116.2
        - libtiff 4.5
        - libwebp-base {{ libwebp }}
        - qt-main 5                 # [build_variant == "normal"]
        - zlib {{ zlib }}
      run:
        - _openmp_mutex         # [linux]
        - python
        - {{ pin_compatible('numpy') }}
        # bounds set through run exports
        - eigen
        - ffmpeg                # [not win]
        - freetype
        - harfbuzz
        - libglib
        - gst-plugins-base
        - gstreamer
        - hdf5
        - jpeg            # [not win]
        - openjpeg
        - libiconv        # [unix]
        - libpng
        - libprotobuf
        - libabseil
        - libtiff
        - libwebp-base
        - qt-main         # [build_variant == "normal"]
        - zlib

    test:
        imports:
          - cv2
          - cv2.xfeatures2d
          - cv2.freetype
        source_files:
          - test.avi
        requires:
          - {{ compiler('c') }}
          - {{ compiler('cxx') }}
          - pkg-config                    # [not win]
          - cmake
          - ninja
          - pip
        files:
          - CMakeLists.txt
          - test.cpp
          - run_py_test.py
          - color_palette_alpha.png
          - test_1_c1.jpg
        commands:
            # Verify dynamic libraries on all systems
            {% set win_ver_lib = version|replace(".", "") %}
            # The bot doesn't support multiline jinja, so use
            # single line jinja.
            {% set opencv_libs = [] %}
            {{ opencv_libs.append("alphamat") or "" }}
            {{ opencv_libs.append("aruco") or "" }}
            {{ opencv_libs.append("bgsegm") or "" }}
            {{ opencv_libs.append("calib3d") or "" }}
            {{ opencv_libs.append("ccalib") or "" }}
            {{ opencv_libs.append("core") or "" }}
            {{ opencv_libs.append("datasets") or "" }}
            {{ opencv_libs.append("dnn_objdetect") or "" }}
            {{ opencv_libs.append("dnn_superres") or "" }}
            {{ opencv_libs.append("dnn") or "" }}
            {{ opencv_libs.append("dpm") or "" }}
            {{ opencv_libs.append("face") or "" }}
            {{ opencv_libs.append("features2d") or "" }}
            {{ opencv_libs.append("flann") or "" }}
            {{ opencv_libs.append("fuzzy") or "" }}
            {{ opencv_libs.append("gapi") or "" }}
            {{ opencv_libs.append("hfs") or "" }}
            {{ opencv_libs.append("highgui") or "" }}
            {{ opencv_libs.append("img_hash") or "" }}
            {{ opencv_libs.append("imgcodecs") or "" }}
            {{ opencv_libs.append("imgproc") or "" }}
            {{ opencv_libs.append("intensity_transform") or "" }}
            {{ opencv_libs.append("line_descriptor") or "" }}
            {{ opencv_libs.append("mcc") or "" }}
            {{ opencv_libs.append("ml") or "" }}
            {{ opencv_libs.append("objdetect") or "" }}
            {{ opencv_libs.append("optflow") or "" }}
            {{ opencv_libs.append("phase_unwrapping") or "" }}
            {{ opencv_libs.append("photo") or "" }}
            {{ opencv_libs.append("plot") or "" }}
            {{ opencv_libs.append("quality") or "" }}
            {{ opencv_libs.append("rapid") or "" }}
            {{ opencv_libs.append("reg") or "" }}
            {{ opencv_libs.append("rgbd") or "" }}
            {{ opencv_libs.append("saliency") or "" }}
            {{ opencv_libs.append("shape") or "" }}
            {{ opencv_libs.append("stereo") or "" }}
            {{ opencv_libs.append("stitching") or "" }}
            {{ opencv_libs.append("structured_light") or "" }}
            {{ opencv_libs.append("superres") or "" }}
            {{ opencv_libs.append("surface_matching") or "" }}
            {{ opencv_libs.append("text") or "" }}
            {{ opencv_libs.append("tracking") or "" }}
            {{ opencv_libs.append("video") or "" }}
            {{ opencv_libs.append("videoio") or "" }}
            {{ opencv_libs.append("videostab") or "" }}
            {{ opencv_libs.append("wechat_qrcode") or "" }}
            {{ opencv_libs.append("xfeatures2d") or "" }}
            {{ opencv_libs.append("ximgproc") or "" }}
            {{ opencv_libs.append("xobjdetect") or "" }}
            {{ opencv_libs.append("xphoto") or "" }}
            {{ opencv_libs.append("freetype") or "" }}
            - export MACOSX_DEPLOYMENT_TARGET={{ MACOSX_DEPLOYMENT_TARGET }}      # [osx]
            - export CONDA_BUILD_SYSROOT={{ CONDA_BUILD_SYSROOT }}                # [osx]
            - OPENCV_FLAGS=`pkg-config --cflags opencv4`                          # [unix]
            - $CXX -std=c++11 test.cpp ${OPENCV_FLAGS} -o test        # [unix]
            - if [[ $(./test) != $PKG_VERSION ]]; then exit 1 ; fi                # [unix]
            {% for each_opencv_lib in opencv_libs %}
            - echo Testing for presence of {{ each_opencv_lib }}
            - test -f $PREFIX/lib/libopencv_{{ each_opencv_lib }}${SHLIB_EXT}     # [unix]
            - if not exist %PREFIX%\\Library\\bin\\opencv_{{ each_opencv_lib }}{{ win_ver_lib }}.dll exit 1  # [win]
            - echo Found                   {{ each_opencv_lib }}
            {% endfor %}
            - test -f $PREFIX/lib/libopencv_bioinspired${SHLIB_EXT}  # [unix]
            - test -f $PREFIX/lib/libopencv_hdf${SHLIB_EXT}          # [unix]
            - mkdir -p cmake_build_test && pushd cmake_build_test
            - cmake -G "Ninja" ..
            - cmake --build . --config Release
            - popd
            - python run_py_test.py
            - if [[ $(python -c 'import cv2; print(cv2.__version__)') != $PKG_VERSION ]]; then exit 1; fi  # [unix]
            - python -c "import cv2; assert 'Unknown' not in cv2.videoio_registry.getBackendName(cv2.CAP_V4L)"  # [linux]
            - pip check
            - pip list
            - test $(pip list | grep opencv-python | wc -l) -eq 2  # [unix]
            - test $(pip list | grep opencv-python-headless | wc -l) -eq 1  # [unix]

  {% if build_variant == "normal" %}
  # This recipe has merged the outputs for the compiled libraries with the
  # python bindings that used to be vendored as libopencv and py-opencv. This
  # was done to try to improve maintainibility. The following two output
  # sections provide no files other than those already in the opencv package.
  # These "virtual" packages provide backwards compatibility with recipes
  # depending on either of the two old packages.
  - name: libopencv
    script: install-nothing.sh                  # [not win]
    script: install-nothing.bat                 # [win]
    version: {{ version }}
    build:
      number: {{ build_num }}
    run_exports:
      # We are cautiously optimistic here that patch-level updates won't break
      # ABI. However, it sounds like upstream won't guarantee *any* sort of
      # binary compatibility any longer. See also
      #
      #     https://github.com/opencv/opencv/wiki/OE-4.-OpenCV-4
      #
      - {{ pin_subpackage('libopencv', max_pin='x.x') }}
    requirements:
      host:
        - {{ package_name }} =={{ version }}
      run:
        - {{ package_name }} =={{ version }}
    test:
      commands:
        - echo "tested in other outputs"

  - name: py-opencv
    script: install-nothing.sh                  # [not win]
    script: install-nothing.bat                 # [win]
    version: {{ version }}
    build:
      number: {{ build_num }}
    requirements:
      host:
        - {{ package_name }} =={{ version }}
      run:
        - {{ package_name }} =={{ version }}
    test:
      commands:
        - echo "tested in other outputs"
  {% endif %}

about:
  home: https://opencv.org/
  license: Apache-2.0
  license_family: Apache
  license_file: LICENSE
  summary: Computer vision and machine learning software library.
  description: Computer vision and machine learning software library.
  dev_url: https://github.com/opencv/opencv
  doc_url: https://docs.opencv.org/{{ major_version }}.x/

extra:
  recipe-maintainers:
    - h-vetinari
    - xhochy
    - jakirkham
    - msarahan
    - patricksnape
    - zym1010
    - hajapy
    - ocefpaf
    - hmaarrfk
    - ilya-lavrenov
