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

PortSystem          1.0
PortGroup           cmake 1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1
PortGroup           LTO 1.0

github.setup        DeadSix27 waifu2x-converter-cpp 57520b25864e648c133ea0b2f4e7e07410ddc1f4
version             5.3.4
revision            1
epoch               1
categories          graphics manga
# TODO: once +cl variant works on Leopard, allow it here.
platforms           {darwin >= 10}
license             MIT
maintainers         nomaintainer

description         Improved fork of Waifu2X C++
long_description    {*}${description} using OpenCV.

checksums           rmd160  ceb9077591a9fbcdf0507af25d8a145230bd983c \
                    sha256  f8f0236e4b13974ce22e079c1fc3a897897cb23bbcaed54f0db046592a652ea3 \
                    size    12532435
github.tarball_from archive

# waifu2x-converter-cpp(18007) malloc: *** error for object 0xa04dc754: pointer being freed was not allocated
legacysupport.newest_darwin_requires_legacy 0
legacysupport.redirect_bins ${name}

# modelHandler_OpenCL.cpp:55:21: error: 'std::__fs' has not been declared
patchfiles          fix-wrong-filesystem-namespace.patch

patchfiles-append   patch-CMakeLists.txt

post-patch {
    reinplace "s|@BRANCH@|master|" ${worksrcpath}/CMakeLists.txt
    reinplace "s|@HASH@|${github.version}|" ${worksrcpath}/CMakeLists.txt
    reinplace "s|@VER@|${version}|" ${worksrcpath}/CMakeLists.txt
}

LTO.gcc_lto_jobs    auto

set cvv             4
cmake.prefix_path-append \
                    ${prefix}/libexec/opencv${cvv}/cmake

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

# Prioritize a newer version:
depends_lib-append  path:lib/opencv${cvv}/pkgconfig/opencv${cvv}.pc:opencv${cvv}-devel

# Note that upstream lies about OpenCL: the option is supposed to exist, but in fact it does nothing.
# However, the build does not link to OpenCL library, so w/e.
configure.args-append \
                    -DENABLE_CUDA=OFF \
                    -DENABLE_OPENCV=ON \
                    -DENABLE_OPENCL=OFF \
                    -DENABLE_TESTS=OFF \
                    -DENABLE_UNICODE=ON \
                    -DINSTALL_MODELS=ON

# filesystem
compiler.cxx_standard   2017

if {![variant_isset cl]} {
    # Avoid this happenning:
    # /opt/local/include/CL/cl_version.h:22:104: note: '#pragma message: cl_version.h:
    # CL_TARGET_OPENCL_VERSION is not defined. Defaulting to 300 (OpenCL 3.0)'
    PortGroup       conflicts_build 1.0

    conflicts_build opencl-headers
}

# TODO: not clear if it is being used at the moment.
# The app does not complain about missing CL with this set-up,
# but outputs the following, before proceeding with conversion:
# second level name page_free_count in vm.page_free_count is invalid
# machdep: class is not implemented

variant cl description "Build with OpenCL, experimental" {
    depends_build-append \
                    port:opencl-headers
    # Likely rather a runtime dependency:
    depends_lib-append \
                    port:freeocl

    # Prefer OpenCL from ports:
    patchfiles-append \
                    patch-find-opencl.diff

    configure.args-replace \
                    -DENABLE_OPENCL=OFF -DENABLE_OPENCL=ON
    configure.args-append \
                    -DOpenCL_INCLUDE_DIRS=${prefix}/include
    configure.cppflags-prepend \
                    -DCL_TARGET_OPENCL_VERSION=120
}
