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

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
    revision        0
    epoch           2
    supported_archs noarch
    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        0
    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  f4feaf6a4988352de8e6d49874ccc1cd6a45021e3cb476e8531bef7ecc73e93a \
                    rmd160  ea681b667f61bca574e3355dcbb2bfb4e7d81ce9 \
                    size    59027456
}

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

subport ${name}-1.1.9 {
    github.setup    KhronosGroup ${name} 1.1.9 v
    revision        3
    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
            set dylib_path ${output_dir}/MoltenVK/dylib/macOS
            set framework_path ${output_dir}/MoltenVK/static
        } elseif {${subport} eq "${name}-1.1.9"} {
            set output_dir ${workpath}/VulkanSDK
            set dylib_path ${output_dir}/macOS/lib
            set framework_path ${output_dir}/MoltenVK
        }

        copy ${dylib_path}/libMoltenVK.dylib ${destroot}${prefix}/lib
        copy ${framework_path}/MoltenVK.xcframework ${destroot}${frameworks_dir}

        if {${subport} eq "${name}-1.1.9"} {
            copy ${output_dir}/macOS/bin/MoltenVKShaderConverter \
                 ${destroot}${prefix}/bin
            file attributes \
                 ${destroot}${prefix}/bin/MoltenVKShaderConverter \
                 -permissions +x
            if {![variant_isset universal] || ![variant_exists universal]} {
                system -W ${destroot}${prefix}/bin [join [list \
                    "lipo -thin ${configure.build_arch}" \
                    "MoltenVKShaderConverter -o MoltenVKShaderConverter 2> /dev/null" \
                ]]
            }
        }

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

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

        # 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        2
    conflicts       ${name}-latest ${name}-1.1.9

    supported_archs x86_64
    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
}
