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

PortSystem          1.0
PortGroup           github 1.0
PortGroup           boost 1.0

github.setup        BVLC caffe 1de4cebfb81d50267d0d8c2595372b14e1408248
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
version             20170817
revision            27

categories          math science
maintainers         nomaintainer

description         a fast framework for deep learning
long_description    {*}${description}

homepage            http://caffe.berkeleyvision.org/
license             BSD

checksums           rmd160  fbf514385ccfb2e7c0ef3ccd85c15ffbbd52ec88 \
                    sha256  3fae039f041bc024c966c3f6e99395602cbcd5a29a540f6f4336b6007f8d79b6

compiler.cxx_standard \
                    2011

boost.version       1.71

depends_lib-append  port:google-glog \
                    port:gflags \
                    port:protobuf3-cpp \
                    port:leveldb \
                    port:snappy \
                    port:lmdb \
                    port:hdf5 \
                    path:lib/opencv3/libopencv_core.dylib:opencv3 \
                    port:szip

depends_run-append  port:wget

patchfiles          patch-Makefile.diff \
                    patch-Makefile.config.diff

post-extract {
    copy ${worksrcpath}/Makefile.config.example ${worksrcpath}/Makefile.config
}

use_configure       no

variant universal {}

set defs "-DGTEST_HAS_TR1_TUPLE=0 -Wno-deprecated -Wunused-local-typedef"
build.args          CXX="${configure.cxx}" \
                    _CXXFLAGS="${configure.cxxflags} [get_canonical_archflags cxx] ${defs}" \
                    _PREFIX=${prefix}
build.target        tools/

test.run            yes
test.args           ${build.args}
test.target         test runtest

set caffe_root ${prefix}/libexec/${name}
destroot.args       ${build.args} \
                    DISTRIBUTE_DIR=${destroot}${prefix}
destroot.target     dist

post-destroot {
    # fix libraries
    system "install_name_tool -id ${prefix}/lib/libcaffe.so \
            ${destroot}${prefix}/lib/libcaffe.so"

    # copy files to caffe_root
    xinstall -m 755 -d ${destroot}${caffe_root}
    foreach dir {data examples models scripts} {
        copy ${worksrcpath}/${dir} ${destroot}${caffe_root}
    }

    # copy binary data of examples
    file mkdir ${destroot}${caffe_root}/build
    copy ${worksrcpath}/.build_release/examples ${destroot}${caffe_root}/build/

    # install additional documents
    set docdir ${prefix}/share/doc/${name}
    xinstall -m 755 -d ${destroot}${docdir}
    xinstall -m 644 -W ${worksrcpath} \
        CONTRIBUTORS.md LICENSE README.md \
        ${destroot}${docdir}
}

notes "
To try examples, copy ${caffe_root} and run commands.
"

if {[variant_isset cudnn]} { default_variants +gpu }

if {![variant_isset gpu]} { default_variants +cpu }

default_variants    +openblas

variant cpu conflicts gpu description {Use CPU only} {
    patchfiles-append  patch-cpu-only.diff
    notes-append "
This port is for CPU-only. Do not forget to train on CPU, not on GPU.
"
}

variant gpu conflicts cpu description {Use GPU and CUDA in /usr/local/cuda} {}

variant cudnn description {Use cuDNN} {
    patchfiles-append  patch-cudnn.diff
}

variant openblas description {Use OpenBLAS} {
    depends_lib-append path:lib/libopenblas.dylib:OpenBLAS
    patchfiles-append  patch-openblas.diff
}

variant python27 description {Install Python 2.7 interface} {
    depends_lib-append \
                    port:python27 \
                    port:py27-cython \
                    port:py27-numpy \
                    port:py27-scipy \
                    port:py27-scikit-image \
                    port:py27-scikit-learn \
                    port:py27-matplotlib \
                    port:py27-ipython \
                    port:py27-h5py \
                    port:py27-networkx \
                    port:py27-nose \
                    port:py27-pandas \
                    port:py27-protobuf3 \
                    port:py27-gflags \
                    port:py27-leveldb \
                    port:py27-dateutil \
                    port:py27-pydot

    build.target-append  pycaffe
    test.target-append   pytest

    post-destroot {
        set packages_dir \
            ${frameworks_dir}/Python.framework/Versions/2.7/lib/python2.7/site-packages
        xinstall -m 755 -d ${destroot}${packages_dir}
        system "install_name_tool -change @rpath/libcaffe.so.1.0.0 ${prefix}/lib/libcaffe.so \
                ${worksrcpath}/python/caffe/_caffe.so"
        copy ${worksrcpath}/python/caffe ${destroot}${packages_dir}
    }
}
