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

# This pre-configure block needs to run before those defined in the cmake-1.1
# PortGroup, or that will have passed an argument based on a wrong value of
# macosx_sdk_version.
pre-configure {
    ui_warn "If the build fails, make sure you have a metal toolchain installed; run `sudo xcodebuild -downloadComponent MetalToolchain` to do so."

    # The CMakeLists.txt will use `xcrun --show-sdk-version` to determine what
    # to enable, if we set a different sysroot, the build may fail because of
    # missing headers.
    set macosx_sdk_version [exec xcrun --show-sdk-version]
}

PortGroup           cmake 1.1

github.setup        ml-explore mlx 0.31.2 v
revision            0
checksums           ${name}-${version}.tar.gz \
                    rmd160  6373be38bf1fad84b6de3e28a18b3c7c2c97a9fb \
                    sha256  bdb9b619f80962dd00c0bffb65e59c53f565c2b550f189a1467f8bc6089401ab \
                    size    4251596 \
                    gguf-tools-8fa6eb65236618e28fd7710a0fba565f7faa1848.tar.gz \
                    rmd160  065bc0c2ae0c4e5baa01f39a8d15ec68c748474f \
                    sha256  9e30bc1eb82cc2231150d39ce37dcdd6f844d6994fba18da83fc537a487ba86f \
                    size    34394 \
                    metal-cpp_26.zip \
                    rmd160  0034942bd6ea351e599727f53e36af24990c1f57 \
                    sha256  4df3c078b9aadcb516212e9cb03004cbc5ce9a3e9c068fa3144d021db585a3a4 \
                    size    260405
github.tarball_from archive

# Additional dependencies that would otherwise be downloaded at runtime by CMake
master_sites-append https://github.com/antirez/gguf-tools/archive/8fa6eb65236618e28fd7710a0fba565f7faa1848:gguf
distfiles-append    gguf-tools-8fa6eb65236618e28fd7710a0fba565f7faa1848.tar.gz:gguf

master_sites-append https://developer.apple.com/metal/cpp/files:metal
distfiles-append    metal-cpp_26.zip:metal

categories          llm
maintainers         {@neverpanic cal} openmaintainer
supported_archs     arm64

platforms           {darwin >= 23}

license             MIT
description         \
    MLX is an array framework for machine learning on Apple silicon, brought to \
    you by Apple machine learning research.
long_description    ${description}

use_xcode           yes

extract.only        ${name}-${version}.tar.gz \
                    gguf-tools-8fa6eb65236618e28fd7710a0fba565f7faa1848.tar.gz

patchfiles          diff-CMakeLists.txt-use-system-nlohmann_json.patch \
                    diff-mlx-CMakeLists.txt-prefer-local-includes.patch

set port_libfmt     libfmt12
cmake.module_path-append \
                    ${prefix}/lib/${port_libfmt}/cmake

depends_extract-append \
                    bin:unzip:unzip

depends_lib-append  port:nlohmann-json \
                    port:${port_libfmt}

configure.args-append   \
                    -DMLX_BUILD_TESTS=Off \
                    -DMLX_BUILD_EXAMPLES=Off \
                    -DBUILD_SHARED_LIBS=On \
                    -DUSE_SYSTEM_FMT=On \
                    -DFETCHCONTENT_FULLY_DISCONNECTED=On \
                    -DFETCHCONTENT_SOURCE_DIR_GGUFLIB=${workpath}/gguf-tools-8fa6eb65236618e28fd7710a0fba565f7faa1848 \
                    -DFETCHCONTENT_SOURCE_DIR_METAL_CPP=${workpath}/metal-cpp
# This breaks linking the tests
configure.pre_args-delete \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON

post-extract {
    # This is a bit of a hack to extract the metal-cpp_26.zip file, because it
    # would be ignored by the normal extract step.
    use_zip         yes
    extract.only    metal-cpp_26.zip
    portextract::extract_main
}


variant tests description "Build with tests" {
    master_sites-append     https://github.com/onqtam/doctest/archive/v2.4.12:doctest
    distfiles-append        doctest-2.4.12.tar.gz:doctest
    checksums-append        doctest-2.4.12.tar.gz \
                            rmd160  abde774b8c61a7e928a116de60f84562d5d671b4 \
                            sha256  73381c7aa4dee704bd935609668cf41880ea7f19fa0504a200e13b74999c2d70 \
                            size    2188849
    extract.only-append     doctest-2.4.12.tar.gz
    configure.args-replace \
        -DMLX_BUILD_TESTS=Off \
        -DMLX_BUILD_TESTS=On
    configure.args-append \
        -DFETCHCONTENT_SOURCE_DIR_DOCTEST=${workpath}/doctest-2.4.12
    test.run                yes

    post-destroot {
        file delete -force \
            ${destroot}${prefix}/include/doctest \
            ${destroot}${prefix}/lib/cmake/doctest
    }
}
