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

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name                whisper
github.setup        ggerganov whisper.cpp 1.8.2 v
revision            0
categories          audio
license             MIT
maintainers         {ijams.me:nate @exprez135} \
                    openmaintainer
description         Port of OpenAI's Whisper model in C/C++
long_description    High-performance inference of OpenAI's Whisper automatic speech recognition \
                    (ASR) model
github.tarball_from archive
checksums           whisper.cpp-${version}.tar.gz \
                    rmd160  333773b4217eda79da5119e5431e33f569d80a3f \
                    sha256  bcee25589bb8052d9e155369f6759a05729a2022d2a8085c1aa4345108523077 \
                    size    7584994

patchfiles          whisper-add-models.diff \
                    include-missing-headers.diff \
                    ggml-hard-coded.diff \
                    add-metal-path.diff \
                    binary-ops-casting.diff

post-patch {
    reinplace "s|__PREFIX__|${prefix}|" \
        src/whisper.cpp \
        ggml/src/ggml-metal/ggml-metal.metal \
        ggml/src/ggml-metal/ggml-metal-device.m
}

compiler.c_standard     2011
compiler.cxx_standard   2017

depends_build-append    path:bin/git:git

configure.args-append   -DGGML_CCACHE=OFF \
                        -DWHISPER_BUILD_EXAMPLES=ON \
                        -DWHISPER_BUILD_TESTS=OFF \
                        -DWHISPER_BUILD_SERVER=OFF \
                        -DGGML_BLAS=OFF

# Disables building with Metal support on some incompatible OS versions.
if {${os.platform} eq "darwin" && ${os.major} >= 15} {
    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

     patchfiles-delete add-metal-path.diff
}

# TODO: still relevant?
if {[string match *gcc* ${configure.compiler}]} {
    # ggml/src/ggml-cpu/binary-ops.cpp:79:23: error: invalid conversion from 'void (*)(float*, vDSP_Stride, float*, vDSP_Stride, float*, vDSP_Stride, vDSP_Length)'
    # {aka 'void (*)(float*, long int, float*, long int, float*, long int, long unsigned int)'} to 'vDSP_fn_t' {aka 'void (*)(const float*, long int, const float*, long int, float*, long int, long unsigned int)'} [-fpermissive]
    configure.cxxflags-append \
        -fpermissive

    # ___atomic_compare_exchange_8, ___atomic_load_8
    configure.ldflags-append \
        -latomic
}

destroot {
    xinstall -d ${destroot}${prefix}/bin
    xinstall -m 755 ${workpath}/build/bin/whisper-cli ${destroot}${prefix}/bin/whisper
}

post-destroot {
    xinstall -d ${destroot}${prefix}/share/whisper/ggml
    xinstall -d ${destroot}${prefix}/lib
    xinstall ${workpath}/build/src/libwhisper.${version}.dylib ${destroot}${prefix}/lib/libwhisper.1.dylib
    xinstall ${workpath}/build/ggml/src/libggml.dylib ${destroot}${prefix}/lib/libggml.dylib
    xinstall ${workpath}/build/ggml/src/libggml-cpu.dylib ${destroot}${prefix}/lib/libggml-cpu.dylib
    xinstall ${workpath}/build/ggml/src/libggml-base.dylib ${destroot}${prefix}/lib/libggml-base.dylib
    if {${os.platform} eq "darwin" && ${os.major} >= 15} {
        xinstall ${workpath}/build/bin/ggml-metal.metal ${destroot}${prefix}/share/whisper/ggml
        xinstall ${workpath}/build/bin/ggml-common.h ${destroot}${prefix}/share/whisper/ggml
        xinstall ${workpath}/build/ggml/src/ggml-metal/libggml-metal.dylib ${destroot}${prefix}/lib/libggml-metal.dylib
    }
}

# Define model variants

variant tiny description {Install tiny model} {
    distfiles-append        ggml-tiny.bin:tiny
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/:tiny
    checksums-append        ggml-tiny.bin \
                            sha256  be07e048e1e599ad46341c8d2a135645097a538221678b7acdd1b1919c6e1b21 \
                            size    77691713
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-tiny.bin ${destroot}${prefix}/share/whisper/models/tiny.bin
    }
}

variant base description {Install base model} {
    distfiles-append        ggml-base.bin:base
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/:base
    checksums-append        ggml-base.bin \
                            sha256  60ed5bc3dd14eea856493d334349b405782ddcaf0028d4b5df4088345fba2efe \
                            size    147951465
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-base.bin ${destroot}${prefix}/share/whisper/models/base.bin
    }
}

variant small description {Install small model} {
    distfiles-append        ggml-small.bin:small
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/:small
    checksums-append        ggml-small.bin \
                            sha256  1be3a9b2063867b937e64e2ec7483364a79917e157fa98c5d94b5c1fffea987b \
                            size    487601967
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-small.bin ${destroot}${prefix}/share/whisper/models/small.bin
    }
}

variant medium description {Install medium model} {
    distfiles-append        ggml-medium.bin:medium
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/:medium
    checksums-append        ggml-medium.bin \
                            sha256  6c14d5adee5f86394037b4e4e8b59f1673b6cee10e3cf0b11bbdbee79c156208 \
                            size    1533763059
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-medium.bin ${destroot}${prefix}/share/whisper/models/medium.bin
    }
}

variant large description {Install large model} {
    distfiles-append        ggml-large-v3.bin:large
    master_sites-append     https://huggingface.co/ggerganov/whisper.cpp/resolve/main/:large
    checksums-append        ggml-large-v3.bin \
                            sha256  64d182b440b98d5203c4f9bd541544d84c605196c4f7b845dfa11fb23594d1e2 \
                            size    3095033483
    post-destroot {
        xinstall -d ${destroot}${prefix}/share/whisper/models
        xinstall ${distpath}/ggml-large-v3.bin ${destroot}${prefix}/share/whisper/models/large.bin
    }
}

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

        depends_lib-append  path:lib/libopenblas.dylib:OpenBLAS
    }
    post-destroot {
        xinstall ${workpath}/build/ggml/src/ggml-blas/libggml-blas.dylib ${destroot}${prefix}/lib/libggml-blas.dylib
    }
}

default_variants    +blas

notes               "
Install this port's variants to include models, or download
your own and use the built-in `-m` feature.

Remember that whisper.cpp only functions on 16-bit WAV
files. You can use `ffmpeg -i input.mp3 -ar 16000
output.wav` to convert the file first.
"
