# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           cmake 1.1
PortGroup           compiler_blacklist_versions 1.0

name                opencv
version             3.3.1
revision            1
categories          graphics science
license             BSD

maintainers         stromnov openmaintainer

description         Intel(R) Open Source Computer Vision Library

long_description    OpenCV is a library that is mainly aimed at real time \
                    computer vision. Some example areas would be \
                    Human-Computer Interaction (HCI), Object Identification, \
                    Segmentation and Recognition, Face Recognition, Gesture \
                    Recognition, Motion Tracking, Ego Motion, Motion \
                    Understanding, Structure From Motion (SFM), and Mobile \
                    Robotics.

homepage            http://opencv.org
master_sites        sourceforge:project/opencvlibrary/opencv-unix/${version}:opencv \
                    https://github.com/Itseez/opencv_contrib/archive:opencv_contrib

distfiles           ${distname}.zip:opencv

use_zip             yes

checksums           ${distname}${extract.suffix} \
                        rmd160  2ec36bda0facee5546eba7edf98c17949373d9b1 \
                        sha256  e59412c7e0d398113b1e454d600fcdff54cd77100a84483ac0d82bbf52496932 \
                    ${version}.zip \
                        rmd160  79a231b9a428732e8405292fe1099675c89af912 \
                        sha256  4140441557a2d5ab0c4bb8cd891e00c4dd27618f8d310d344ee470dc7790fc48

depends_build-append \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:zlib \
                    port:bzip2 \
                    port:libpng \
                    port:jpeg \
                    port:jasper \
                    port:tiff \
                    port:webp \
                    port:ilmbase \
                    port:openexr \
                    path:lib/libavcodec.dylib:ffmpeg

# only search for openexr in ${prefix}
patchfiles-append   patch-find-openexr.diff

# recognize dylib as a valid library suffix
patchfiles-append   patch-dylib_suffix.diff

# recognize AppleClang as well as Clang as a Clang compiler
patchfiles-append   patch-appleclang.diff

patchfiles-append   patch-compat.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/cmake/OpenCVFindOpenEXR.cmake
}

# TODO: check and adapt those patches
#                    candidates/patch-modules_imgcodecs_src_grfmt_tiff.cpp.diff
#                    candidates/patch-modules_core_src_parallel.cpp.diff
#                    candidates/patch-modules-highgui-src-window_cocoa.mm.diff

# # not currently used in the cmake build system:
#                     -DWITH_GTK_2_X=OFF
#                     -DWITH_NVCUVID=OFF
#                     -DCMAKE_VERBOSE=ON
configure.args-append \
                    -DWITH_1394=OFF \
                    -DWITH_CARBON=OFF \
                    -DWITH_VTK=OFF \
                    -DWITH_CUDA=OFF \
                    -DWITH_CUFFT=OFF \
                    -DWITH_CUBLAS=OFF \
                    -DWITH_EIGEN=OFF \
                    -DWITH_FFMPEG=ON \
                    -DWITH_GSTREAMER=OFF \
                    -DWITH_GTK=OFF \
                    -DWITH_IPP=OFF \
                    -DWITH_JASPER=ON \
                    -DWITH_JPEG=ON \
                    -DWITH_WEBP=ON \
                    -DWITH_OPENEXR=ON \
                    -DWITH_OPENGL=ON \
                    -DWITH_OPENNI=OFF \
                    -DWITH_OPENNI2=OFF \
                    -DWITH_PNG=ON \
                    -DWITH_PVAPI=OFF \
                    -DWITH_GIGEAPI=OFF \
                    -DWITH_QT=OFF \
                    -DWITH_QUICKTIME=OFF \
                    -DWITH_TBB=OFF \
                    -DWITH_OPENMP=OFF \
                    -DWITH_CSTRIPES=OFF \
                    -DWITH_PTHREADS_PF=OFF \
                    -DWITH_TIFF=ON \
                    -DWITH_UNICAP=OFF \
                    -DWITH_V4L=OFF \
                    -DWITH_LIBV4L=OFF \
                    -DWITH_XIMEA=OFF \
                    -DWITH_XINE=OFF \
                    -DWITH_CLP=OFF \
                    -DWITH_OPENCL=OFF \
                    -DWITH_OPENCL_SVM=OFF \
                    -DWITH_OPENCLAMDFFT=OFF \
                    -DWITH_OPENCLAMDBLAS=OFF \
                    -DWITH_INTELPERC=OFF \
                    -DWITH_IPP_A=OFF \
                    -DWITH_VA=OFF \
                    -DWITH_VA_INTEL=OFF \
                    -DWITH_GDAL=OFF \
                    -DWITH_GPHOTO2=OFF \
                    \
                    -DBUILD_SHARED_LIBS=ON \
                    -DBUILD_opencv_apps=ON \
                    -DBUILD_DOCS=OFF \
                    -DBUILD_EXAMPLES=OFF \
                    -DBUILD_PACKAGE=OFF \
                    -DBUILD_PERF_TESTS=OFF \
                    -DBUILD_TESTS=OFF \
                    -DBUILD_WITH_DYNAMIC_IPP=OFF \
                    \
                    -DBUILD_ZLIB=OFF \
                    -DBUILD_TIFF=OFF \
                    -DBUILD_JASPER=OFF \
                    -DBUILD_JPEG=OFF \
                    -DBUILD_PNG=OFF \
                    -DBUILD_OPENEXR=OFF \
                    -DBUILD_TBB=OFF \
                    \
                    -DBUILD_opencv_java=OFF \
                    \
                    -DPYTHON_EXECUTABLE=OFF \
                    -DBUILD_opencv_python2=OFF \
                    -DBUILD_opencv_python3=OFF \
                    -DINSTALL_C_EXAMPLES=OFF \
                    -DINSTALL_PYTHON_EXAMPLES=OFF \
                    -DENABLE_PRECOMPILED_HEADERS=ON \
                    -DENABLE_SSSE3=OFF

platform darwin {
    configure.args-append \
                    -DZLIB_LIBRARY=${prefix}/lib/libz.dylib \
                    -DBZIP2_LIBRARIES=${prefix}/lib/libbz2.dylib

    # do not opportunistically find other LAPACK implementations
    # see cmake/OpenCVFindLAPACK.cmake
    configure.args-append \
                    -DLAPACK_IMPL=Apple \
                    -DLAPACK_LIBRARIES="-framework Accelerate" \
                    -DLAPACK_CBLAS_H=Accelerate/Accelerate.h \
                    -DLAPACK_LAPACKE_H=Accelerate/Accelerate.h

    if { ${build_arch} eq "i386" || ${build_arch} eq "ppc" || [variant_isset universal] } {
        #The Accelerate ABI requires 32-bit signed integers.
        #In clapack.h, __CLPK_integer is a typedef for either
        #   int (64-bit) or long int (32-bit).
        #opencv only uses ints.
        #Casting between pointers is never recommended, but
        #   int and long int should have the same representations
        #   on 32-bit systems.
        patchfiles-append patch-32bit_accelerate.diff
    }
}

if { !${configure.ccache} } {
    configure.args-append \
        -DENABLE_CCACHE=NO
}

set opencv_linker_libs ""

# gcc-4.0 exits with a bus error
compiler.blacklist-append *gcc-4.0

# Xcode 3.1.4's and Xcode 3.2.6's gcc-4.2 fails to build opencv for i386 from v2.4.10 onwards.
compiler.blacklist-append *gcc-4.2*

# llvm-gcc-4.2 appears to be broken on old XCode (#35368)
compiler.blacklist-append {llvm-gcc-4.2 < 2336.11}

# Older clang also don't work (#36956)
compiler.blacklist-append {clang < 318}

compiler.cxx_standard \
                    2011
configure.cxxflags-append \
                    -std=gnu++11

# TODO: Actually fix this
if {[string match "*clang*" ${configure.compiler}]} {
    post-patch {
        reinplace "/-Werror=sequence-point/d" ${worksrcpath}/cmake/OpenCVCompilerOptions.cmake
    }
}

platform darwin powerpc {
    # persistence.cpp:7627:31: error: comparing the result of pointer addition '(src + ((sizetype)off))' and NULL [-Werror=address]
    patchfiles-append       patch-gnucxx-opts.diff

    post-patch {
        reinplace "s|-mcpu=G3 -mtune=G5|-mtune=native|" ${worksrcpath}/cmake/OpenCVCompilerOptimizations.cmake
    }

    # Switch to GTK3 instead.
    configure.args-append   -DCV_DISABLE_OPTIMIZATION=ON \
                            -DENABLE_POWERPC=ON \
                            -DENABLE_VSX=OFF \
                            -DWITH_AVFOUNDATION=OFF
#                            -DWITH_CARBON=ON

    configure.args-replace  -DENABLE_PRECOMPILED_HEADERS=ON \
                            -DENABLE_PRECOMPILED_HEADERS=OFF \
                            -DWITH_OPENMP=OFF \
                            -DWITH_OPENMP=ON
}

# PCH broken when universal
if {[variant_isset universal]} {
    configure.args-replace  -DENABLE_PRECOMPILED_HEADERS=ON \
                            -DENABLE_PRECOMPILED_HEADERS=OFF
}

variant avx conflicts avx2 description {Enable AVX (and SSE4*) support} {
    configure.args-append   -DENABLE_AVX=ON \
                            -DENABLE_SSE41=ON -DENABLE_SSE42=ON
}

variant avx2 conflicts avx description {Enable AVX, AVX2 (and SSE4*) support} {
    configure.args-append   -DENABLE_AVX=ON -DENABLE_AVX2=ON \
                            -DENABLE_SSE41=ON -DENABLE_SSE42=ON
}

variant eigen description {Enable eigen support.} {
    depends_lib-append      path:share/pkgconfig/eigen3.pc:eigen3
    configure.args-replace  -DWITH_EIGEN=OFF \
                            -DWITH_EIGEN=ON
    configure.args-append   -DEIGEN_INCLUDE_PATH=${prefix}/include/eigen3
}

variant opencl description {Enable OpenCL support.} {
    pre-configure {
        if {${os.platform} eq "darwin" && ${os.major} < 11} {
            ui_error "The OpenCL variant only works with OS X 10.7 Lion or later."
            return -code error "incompatible OS X version"
        }
    }
    configure.args-replace  -DWITH_OPENCL=OFF \
                            -DWITH_OPENCL=ON
}

variant openni description {Enable OpenNI support.} {
    depends_lib-append      port:openni
    patchfiles-append       patch-cmake_OpenCVFindOpenNI.cmake.diff
    configure.args-replace  -DWITH_OPENNI=OFF \
                            -DWITH_OPENNI=ON

    post-patch {
         reinplace "s,@@PREFIX@@,${prefix},g" ${worksrcpath}/cmake/OpenCVFindOpenNI.cmake
    }
}

variant dc1394 description {Use libdc1394 for FireWire camera. Breaks compatibility with Apple iSight FireWire camera.} {
    depends_lib-append      port:libdc1394
    configure.args-replace  -DWITH_1394=OFF \
                            -DWITH_1394=ON
    configure.args-append   -DHAVE_DC1394_2=ON
}

variant qt4 conflicts qt5 description {Build with Qt4 Backend support.} {
    PortGroup               qt4 1.0
    configure.args-replace  -DWITH_QT=OFF \
                            -DWITH_QT=4
}

variant qt5 conflicts qt4 description {Build with Qt5 Backend support.} {
    PortGroup               qt5 1.0
    configure.args-replace  -DWITH_QT=OFF \
                            -DWITH_QT=5
}

variant java description {Add Java bindings.} {
    depends_build-append    port:apache-ant
    configure.args-replace  -DBUILD_opencv_java=OFF \
                            -DBUILD_opencv_java=ON
}

if {[variant_isset java] && ![variant_isset python27] && ![variant_isset python34]} {
    configure.args-replace  -DPYTHON_EXECUTABLE=OFF \
                            -DPYTHON_EXECUTABLE=/usr/bin/python
    configure.args-delete   -DBUILD_opencv_python2=OFF
}

variant python27 description {Add Python 2.7 bindings.} {
    depends_lib-append      port:python27 \
                            port:py27-numpy
    patchfiles-delete       patch-modules_python_CMakeLists.txt.diff
    configure.args-delete   -DINSTALL_PYTHON_EXAMPLES=OFF \
                            -DBUILD_opencv_python2=OFF
    configure.args-replace  -DPYTHON_EXECUTABLE=OFF \
                            -DPYTHON_EXECUTABLE=${prefix}/bin/python2.7
    configure.args-append   -DINSTALL_PYTHON_EXAMPLES=ON \
                            -DPYTHON2_EXECUTABLE=${prefix}/bin/python2.7 \
                            -DPYTHON_LIBRARIES=${prefix}/lib/libpython2.7.dylib \
                            -DPYTHON2_LIBRARIES=${prefix}/lib/libpython2.7.dylib \
                            -DPYTHON2_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/2.7/Headers \
                            -DPYTHON2_PACKAGES_PATH=${frameworks_dir}/Python.framework/Versions/2.7/lib/python2.7/site-packages
}


set pythonversions {3.9 3.10 3.11}
foreach pdv ${pythonversions} {
    set pv [join [lrange [split ${pdv} .] 0 1] ""]
    set conflist ""
    foreach v ${pythonversions} {
        if {${v} ne ${pdv}} {
            set vv [join [lrange [split ${v} .] 0 1] ""]
            set conflist "${conflist} python${vv}"
        }
    }
    variant python${pv} conflicts {*}${conflist} description "Add bindings for Python ${pdv}" {
        patchfiles-delete   patch-modules_python_CMakeLists.txt.diff
        configure.args-delete \
                            -DINSTALL_PYTHON_EXAMPLES=OFF \
                            -DBUILD_opencv_python3=OFF
    }
    # settings that depend on loop variables must be set in an appropriate if, not in the
    # variant declaration scope.
    if {[variant_isset python${pv}]} {
        depends_lib-append  port:python${pv} \
                            port:py${pv}-numpy
        if {![variant_isset python27]} {
            configure.args-replace \
                            -DPYTHON_EXECUTABLE=OFF \
                            -DPYTHON_EXECUTABLE=${prefix}/bin/python${pdv}
        }
        configure.args-append \
                            -DINSTALL_PYTHON_EXAMPLES=ON \
                            -DPYTHON3_EXECUTABLE=${prefix}/bin/python${pdv} \
                            -DPYTHON3_LIBRARY=${frameworks_dir}/Python.framework/Versions/${pdv}/lib/libpython${pdv}.dylib \
                            -DPYTHON3_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/${pdv}/Headers \
                            -DPYTHON3_PACKAGES_PATH=${frameworks_dir}/Python.framework/Versions/${pdv}/lib/python${pdv}/site-packages
    }
}

variant tbb description {Use Intel TBB.} {
    depends_lib-append      port:tbb
    configure.args-replace  -DWITH_TBB=OFF \
                            -DWITH_TBB=ON
    configure.args-append   -DHAVE_TBB=ON \
                            -DTBB_INCLUDE_DIRS=${prefix}/include
}

variant vtk description {Include VTK support.} {
    depends_lib-append      port:vtk
    configure.args-replace  -DWITH_VTK=OFF \
                            -DWITH_VTK=ON
}

variant gdal description {Include GDAL support.} {
    depends_lib-append      port:gdal
    configure.args-replace  -DWITH_GDAL=OFF \
                            -DWITH_GDAL=ON
}

variant contrib description {Build OpenCV with extra modules.} {
    distfiles-append        ${version}.zip:opencv_contrib
    configure.args-append   -DOPENCV_EXTRA_MODULES_PATH=${workpath}/opencv_contrib-${version}/modules
    depends_lib-append      port:protobuf-cpp
    post-extract {
        # less than ideal way for patchfiles to be applied to contrib directory
        ln -s ${workpath}/opencv_contrib-${version} ${worksrcpath}/opencv_contrib
    }
}

platform darwin {
    post-patch {
        if {${os.major} < 10} {
            reinplace "s:HAVE_GCD 1:HAVE_GCD 0:" ${worksrcpath}/cmake/OpenCVFindLibsPerf.cmake
        }
    }
}

variant legacy_dispatch description "Enable dispatch via libdispatch-legacy port" {
    depends_lib-append \
        port:libdispatch-legacy
    configure.cppflags-prepend \
        -I${prefix}/libexec/dispatch/usr/include
    configure.ldflags-append \
        ${prefix}/libexec/dispatch/usr/lib/libdispatch.a
}

pre-configure {
    configure.args-append   -DOPENCV_LINKER_LIBS=\"[join ${opencv_linker_libs} " "]\"
}

post-destroot {
    # http://trac.macports.org/ticket/42702
    if {[variant_isset qt4] || [variant_isset qt5]} {
        reinplace "s|-L//System/Library/Frameworks ||g" ${destroot}${prefix}/lib/pkgconfig/opencv.pc
        reinplace "s|-lAGL.framework|-framework AGL|g" ${destroot}${prefix}/lib/pkgconfig/opencv.pc
        reinplace "s|-lOpenGL.framework|-framework OpenGL|g" ${destroot}${prefix}/lib/pkgconfig/opencv.pc
    }
}

livecheck.type      sourceforge
livecheck.name      opencvlibrary
livecheck.regex     {/[Oo]pen[Cc][Vv]-([0-9.]+)[a-z]?\.[tz]}
