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

name                MoltenVK

# This version number should stay synchronized with
# whatever corresponds to 'MoltenVK-latest'.
version             1.2.5

categories          graphics
license             Apache-2
maintainers         {@gcenx gmail.com:gcenx83} \
                    @jasonliu-- openmaintainer

description         an implementation of Vulkan for Metal
long_description    ${name} is an implementation of the \
                    high-performance, industry-standard Vulkan \
                    graphics and compute API, that runs on Apple's \
                    Metal graphics framework.

# MoltenVK would build for i386, but it uses Metal which only works on
# x86_64 and arm64 (also, Metal was introduced in OS X 10.11)
supported_archs     arm64 x86_64
variant universal   {}

# This github.setup is technically for MoltenVK-latest, but by putting
# it here, the GitHub website will get saved as the value for
# ${homepage} for all of the subports' 'port info' entries.
github.setup        KhronosGroup ${name} ${version} v

if {${subport} eq ${name}} {
    PortGroup       stub 1.0

    epoch           2
    platforms       {darwin any} {macosx any >= 15}

    if {${os.major} == 15} {
        depends_run port:${name}-1.0.41
    } elseif {${os.major} == 16} {
        depends_run port:${name}-1.1.9
    } else {
        depends_run port:${name}-latest
    }
}

use_configure       no
build {}

# GitHub artifacts deployment target is 10.13 (Xcode14+)
subport ${name}-latest {
    revision        1
    platforms       {darwin any} {macosx >= 17}
    conflicts       ${name}-1.1.9 ${name}-1.0.41

    dist_subdir     ${name}/${version}
    distname        ${name}-macos
    use_tar         yes

    checksums       sha256  bd9fde5a0a10e4b8f85cc2a77a958c560fcf57fa5da39dab85748d31f78ecce2 \
                    rmd160  42cba8034d4138f90ca562da95c01048b8d775c9 \
                    size    48031744
}

set lunarG_dlsite   https://sdk.lunarg.com/sdk/download

subport ${name}-1.1.9 {
    github.setup    KhronosGroup ${name} 1.1.9 v
    revision        1
    platforms       {darwin any} {macosx >= 16}
    conflicts       ${name}-latest ${name}-1.0.41

    set sdkversion  1.3.211.0
    distname        vulkansdk-macos-${sdkversion}
    use_dmg         yes
    use_tar         no

    # URL only works for the latest available SDK,
    # older versions will 404
    master_sites    $lunarG_dlsite/${sdkversion}/mac/

    checksums       sha256  bfe654af00030b6e65521f834f0830f15e18c828594226865f15c92a9ea68363 \
                    rmd160  3674f67b37f7bcc1746d55c3baa975d0cca9dbaa \
                    size    275553243

    depends_build   port:p7zip
    depends_skip_archcheck p7zip

    build {
        # bypass the installer that requires macOS 10.13
        system [join [list \
            "${prefix}/bin/7z x -aoa" \
            "${worksrcpath}/InstallVulkan.app/Contents/Resources/installer.dat" \
            "-o${workpath}/VulkanSDK" \
        ]]
    }
}

if {${subport} eq "${name}-latest" || ${subport} eq "${name}-1.1.9"} {
    github.tarball_from releases
    destroot {
        if {${subport} eq "${name}-latest"} {
            set output_dir ${workpath}/MoltenVK
        } elseif {${subport} eq "${name}-1.1.9"} {
            set output_dir ${workpath}/VulkanSDK
        }

        # Static library is located inside XCFramework
        copy ${output_dir}/MoltenVK/MoltenVK.xcframework \
             ${destroot}${frameworks_dir}
        ln -s ${frameworks_dir}/MoltenVK.xcframework/macos-arm64_x86_64/libMoltenVK.a \
            ${destroot}${prefix}/lib

        if {${subport} eq "${name}-latest"} {
            set dylib_path ${output_dir}/MoltenVK/dylib/macOS
        } elseif {${subport} eq "${name}-1.1.9"} {
            set dylib_path ${output_dir}/macOS/lib
        }
        copy $dylib_path/libMoltenVK.dylib ${destroot}${prefix}/lib

        if {![variant_isset universal] || ![variant_exists universal]} {
            system -W ${destroot}${prefix}/lib [join [list \
                "lipo -thin ${configure.build_arch}" \
                "libMoltenVK.dylib -o libMoltenVK.dylib 2> /dev/null" \
            ]]
        }

        if {${subport} eq "${name}-1.1.9"} {
            copy ${output_dir}/macOS/bin/MoltenVKShaderConverter \
                 ${destroot}${prefix}/bin
            file attributes \
                 ${destroot}${prefix}/bin/MoltenVKShaderConverter \
                 -permissions +x
        }

        # vulkan and vk_video are provided via vulkan-headers
        copy ${output_dir}/MoltenVK/include/MoltenVK \
             ${destroot}${prefix}/include
    }
}

# The last version before moving to APFS format dmg
subport ${name}-1.0.41 {
    github.setup    KhronosGroup ${name} 1.0.41 v
    revision        1
    conflicts       ${name}-latest ${name}-1.1.9

    set sdkversion  1.2.135.0
    distname        vulkansdk-macos-${sdkversion}
    extract.suffix  .tar.gz

    master_sites    [join [list \
                        "https://web.archive.org/web" \
                        "20200413122419if_" \
                        "$lunarG_dlsite/${sdkversion}/mac/" \
                    ] "/"]

    checksums       sha256  81da27908836f6f5f41ed7962ff1b4be56ded3b447d4802a98b253d492f985cf \
                    rmd160  a3300b70e81d2968161f194eff50c4992835a0bc \
                    size    183100867

    destroot {
        copy ${worksrcpath}/MoltenVK/macOS/framework/MoltenVK.framework \
             ${destroot}${frameworks_dir}
        copy ${worksrcpath}/MoltenVK/macOS/dynamic/libMoltenVK.dylib \
             ${worksrcpath}/MoltenVK/macOS/static/libMoltenVK.a \
             ${destroot}${prefix}/lib

        # vulkan and vk_video are provided via vulkan-headers
        copy ${worksrcpath}/MoltenVK/include/MoltenVK \
             ${destroot}${prefix}/include
    }
}

if {${subport} ne ${name} && ${subport} ne "${name}-latest"} {
    livecheck.type  none
}
