# -*- 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               github 1.0
PortGroup               cmake 1.1
PortGroup               legacysupport 1.1

github.setup            ggerganov llama.cpp 4508 b
github.tarball_from     archive
set git-commit          a1649cc
# This line is for displaying commit in CLI only
revision                0
categories              llm
maintainers             {i0ntempest @i0ntempest} openmaintainer
license                 MIT

description             LLM inference in C/C++
long_description        The main goal of ${name} is to enable LLM inference with minimal\
                        setup and state-of-the-art performance on a wide variety of hardware\
                         - locally and in the cloud.

checksums               rmd160  b6700c8d3311bfa382ef9db1881846e10329c990 \
                        sha256  3fcc3ae6b2605dfcfaf871cfa4281ac7cf215eb738e876e193890e7837d256f2 \
                        size    20472113

# error: 'filesystem' file not found on 10.14
legacysupport.newest_darwin_requires_legacy \
                        18
legacysupport.use_mp_libcxx \
                        yes

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

depends_lib-append      port:curl

# Upstream patch for errno-related build failure; will be included in future release
# See: https://trac.macports.org/ticket/71880
# See: https://github.com/ggerganov/llama.cpp/issues/11295
patchfiles-append       patch-llama-mmap-errno.diff

# cmake relies on git for version info. We need to set them manually.
post-patch {
    reinplace "s|@BUILD_NUMBER@|${version}|" ${worksrcpath}/common/build-info.cpp.in
    reinplace "s|@BUILD_COMMIT@|${git-commit}|" ${worksrcpath}/common/build-info.cpp.in
}

compiler.cxx_standard   2017

configure.args-append   -DGGML_LTO=ON \
                        -DGGML_CCACHE=OFF \
                        -DGGML_OPENMP=OFF \
                        -DLLAMA_CURL=ON

# error: use of undeclared identifier 'MTLGPUFamilyApple7' on 10.14
if {${os.platform} eq "darwin" && ${os.subplatform} eq "macosx" && \
    (${os.major} >= 20 && ${configure.sdk_version} >= 11)} {
    configure.args-append \
                        -DGGML_METAL=ON \
                        -DGGML_METAL_EMBED_LIBRARY=ON \
                        -DGGML_METAL_MACOSX_VERSION_MIN=${macos_version_major}
} else {
    configure.args-append \
                        -DGGML_METAL=OFF \
                        -DGGML_METAL_EMBED_LIBRARY=OFF
}

variant blas description {Uses BLAS, improves performance} {
    configure.args-append \
                        -DGGML_BLAS=ON
    if {${os.platform} eq "darwin" && ${os.subplatform} eq "macosx"} {
        configure.args-append \
                        -DGGML_ACCELLERATE=ON \
                        -DGGML_BLAS_VENDOR=Apple
    } else {
        configure.args-append \
                        -DGGML_ACCELLERATE=OFF \
                        -DGGML_BLAS_VENDOR=OpenBLAS

        depends_lib-append  path:lib/libopenblas.dylib:OpenBLAS
    }
}

variant native description "Force local build and optimize for CPU" {
    configure.args-append \
                        -DGGML_NATIVE=ON
}

default_variants        +blas
