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

github.setup        antirez ds4 2964a93257e0dde636774071ca9c81b7a5df2b23
github.tarball_from archive
name                dwarfstar4
version             20260514
revision            0

categories          llm
license             MIT
maintainers         nomaintainer

description         DeepSeek V4 Flash-specific native inference engine

long_description    DwarfStar 4 is a small native inference engine specific \
                    for DeepSeek V4 Flash, with a CLI, HTTP server, Metal \
                    backend, KV state handling, and model download helper.

checksums           rmd160  7ff81c60c55b5182034f771bd0e4e5801a1b9353 \
                    sha256  70debf39728b2f076d62e16fd7f3989daead6515ba2b366c8cb4d20d1c57b6ba \
                    size    4371191

patchfiles          patch-metal-residency-sdk.diff

depends_run         path:bin/curl:curl

compiler.c_standard 1999

configure.cflags-append \
                    -std=c99 \
                    -ffast-math
configure.objcflags-append \
                    -ffast-math \
                    -fobjc-arc

build.args-append   NATIVE_CPU_FLAG=

post-patch {
    reinplace "s|\\\[paths addObject:spec\\\[1\\\]\\\];|\\\[paths addObject:\\\[@\"${prefix}/share/${name}/\" stringByAppendingString:spec\\\[1\\\]\\\]\\\]; \\\[paths addObject:spec\\\[1\\\]\\\];|" \
                    ${worksrcpath}/ds4_metal.m
    reinplace {s|ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)|ROOT=${DS4_ROOT:-"$HOME/.local/share/ds4"}|} \
                    ${worksrcpath}/download_model.sh
    reinplace "s|curl -fL|${prefix}/bin/curl -fL|g" \
                    ${worksrcpath}/download_model.sh
}

test.run            yes
test.cmd            ./ds4
test.target
test.args           --help

destroot {
    xinstall -m 0755 ${worksrcpath}/ds4 \
                    ${worksrcpath}/ds4-server \
                    ${worksrcpath}/ds4-bench \
                    ${destroot}${prefix}/bin/
    xinstall -m 0755 ${worksrcpath}/download_model.sh \
                    ${destroot}${prefix}/bin/ds4-download-model

    xinstall -d     ${destroot}${prefix}/share/${name}/metal
    xinstall -m 0644 {*}[glob ${worksrcpath}/metal/*.metal] \
                    ${destroot}${prefix}/share/${name}/metal/

    xinstall -d     ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 ${worksrcpath}/LICENSE \
                    ${worksrcpath}/README.md \
                    ${destroot}${prefix}/share/doc/${name}/
}

notes "
The upstream model download script has been installed as:

    ${prefix}/bin/ds4-download-model

Run it before using ds4 to run a supported GGUF model. By default, the
script stores models under ~/.local/share/ds4/gguf and updates the symlink:

    ~/.local/share/ds4/ds4flash.gguf

The ds4 tools still default to looking for ds4flash.gguf in the current working
directory. Use -m to point at the downloaded model, for example:

    ds4-download-model q2-imatrix
    ds4 -m ~/.local/share/ds4/ds4flash.gguf
"

livecheck.type      none
