# -*- 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           github 1.0

epoch               1
github.setup        facebookresearch faiss 1.7.3 v
revision            1
checksums           rmd160  e2f447edf4bc8bb4add776f96c473074d3d71bff \
                    sha256  dda1b031957789e6a6f3b37d48ad135b9557a33c5013c1c245ef4bced2d787de \
                    size    843640

name                libfaiss
categories          math
license             BSD
maintainers         {@beauby fb.com:hoss} {@mdouze fb.com:matthijs} \
                    openmaintainer

description         Efficient similarity search library from \
                    Facebook AI Research.

long_description    Library for efficient similarity search and \
                    clustering of dense vectors. It contains \
                    algorithms that search in sets of vectors of any \
                    size, up to ones that possibly do not fit in \
                    RAM. It also contains supporting code for \
                    evaluation and parameter tuning. Faiss is written \
                    in C++ with complete wrappers for \
                    Python/numpy. Some of the most useful algorithms \
                    are implemented on the GPU.

github.tarball_from archive

compiler.cxx_standard   2011

if {[string match *clang* ${configure.compiler}]} {
    compiler.openmp_version 2.5

    configure.ldflags-append \
                    -L${prefix}/lib/libomp \
                    -lomp
}

configure.args-append \
                    -DFAISS_ENABLE_GPU:BOOL=OFF \
                    -DFAISS_ENABLE_PYTHON:BOOL=OFF

variant openblas description {Use OpenBLAS instead of Apple's Accelerate framework} {
    depends_lib-append  path:lib/libopenblas.dylib:OpenBLAS
    build.env-delete    "BLASLDFLAGS=-framework Accelerate"
    build.env-append    BLASLDFLAGS=${prefix}/lib/libopenblas.dylib
}

default_variants +openblas

# create Python subports
set python_versions {39 310}
foreach v ${python_versions} {
    subport py${v}-${github.project} {
        PortGroup   python 1.0

        python.default_version  ${v}
    }
}

# Python bindings for supported Python versions
if {[string match "py*" ${subport}]} {
    github.setup        ${github.author} ${github.project} ${github.version} ${github.tag_prefix}

    categories      math python
    description     Python ${python.branch} bindings for ${description}
    long_description \
                    This package provides Python ${python.branch} bindings, \
                    modules, and scripts.

    depends_build-append \
                    port:py${python.version}-setuptools \
                    port:swig \
                    port:swig-python

    depends_lib-append \
                    port:py${python.version}-numpy

    depends_test-append \
                    port:py${python.version}-pytest

    # Build instructions:
    # https://github.com/facebookresearch/faiss/blob/master/INSTALL.md#compile-from-source

    python.pep517   no
    use_configure   yes

    configure.args-replace \
                    -DFAISS_ENABLE_PYTHON:BOOL=OFF \
                    -DFAISS_ENABLE_PYTHON:BOOL=ON
    configure.args-append \
                    -DPython_EXECUTABLE=${python.bin} \
                    -DSWIG_EXECUTABLE=${prefix}/bin/swig
    configure.pre_args-delete \
                    --prefix=${prefix}
    configure.pre_args-replace \
                    "-DCMAKE_MAKE_PROGRAM=${python.bin} setup.py --no-user-cfg" \
                    -DCMAKE_MAKE_PROGRAM=[portbuild::build_getmaketype]

    pre-build {
        system -W ${configure.dir} "[portbuild::build_getmaketype] -w all[portbuild::build_getjobsarg] ${build.post_args}"
    }

    build.post_args-delete \
                    VERBOSE=ON
    build.dir       ${configure.dir}/faiss/python

    destroot.dir    ${build.dir}
    destroot.target install

    post-destroot {
        set docdir ${prefix}/share/doc/${subport}
        set tutorialdir ${prefix}/share/${subport}/tutorial
        xinstall -d \
            ${destroot}${docdir} \
            ${destroot}${tutorialdir}
        xinstall -m 0644 -W ${worksrcpath} README.md LICENSE \
            ${destroot}${docdir}
        copy ${worksrcpath}/tutorial/python \
            ${destroot}${tutorialdir}
    }

    test.run        yes
    test.env-append \
                    "PATH=$env(PATH):${python.prefix}/bin" \
                    "PYTHONPATH=${configure.dir}/faiss/python"

    livecheck.type  none
}
