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

set rev             94bad2affa7ee4b2bea8075fdde921879f389cbd

github.setup        google angle $rev
github.tarball_from archive

# Major and minor version from src/common/angle_version.h
# Patch version using `git rev-list HEAD --count`
version             2.1.28849
revision            0
categories          graphics devel
license             BSD
maintainers         {makr @mohd-akram} openmaintainer

description         OpenGL ES implementation

long_description    A cross-platform, conformant OpenGL ES implementation.

homepage            https://angleproject.org

# grep -E 'jsoncpp_revision.:|(build.git|testing|astc-encoder|rust|SPIRV-(Headers|Tools)|Vulkan-Headers|zlib)@' DEPS
set jsoncpp         42e892d96e47b1f6e29844cc705e148ec4856448
set cr_jsoncpp      f62d44704b4da6014aa231cfc116e7fd29617d2a
set cr_build        e6aa79b579ec7536ac9cbb92b47c1d3c2ecec984
set cr_testing      617d94f985f3b2e535b62d5e87c1a0d649394265
set astc_encoder    2319d9c4d4af53a7fc7c52985e264ce6e8a02a9b
set cr_rust         5d1eef11ee101253a9ba316fff0517f758ec12fd
set spirv_headers   04fd3caa1e8267e4d95c806cad901181728e1006
set spirv_tools     d6f63db7031c181c146963206da772445704d5a0
set vk_headers      ee2ec5fd83dafce291024683b50dc89219333076
set cr_zlib         285e94b8fa95ad3b7d16b80798ec8dce6febb8c8

master_sites-append https://github.com/gsource-mirror/chromium-src-build/archive/${cr_build}:chromium-build
distfiles-append    chromium-src-build-${cr_build}${extract.suffix}:chromium-build

master_sites-append https://github.com/gsource-mirror/chromium-src-testing/archive/${cr_testing}:chromium-testing
distfiles-append    chromium-src-testing-${cr_testing}${extract.suffix}:chromium-testing

master_sites-append https://github.com/gsource-mirror/chromium-src-third_party-jsoncpp/archive/${cr_jsoncpp}:chromium-jsoncpp
distfiles-append    chromium-src-third_party-jsoncpp-${cr_jsoncpp}${extract.suffix}:chromium-jsoncpp

master_sites-append https://github.com/gsource-mirror/chromium-src-third_party-rust/archive/${cr_rust}:chromium-rust
distfiles-append    chromium-src-third_party-rust-${cr_rust}${extract.suffix}:chromium-rust

master_sites-append https://github.com/gsource-mirror/chromium-src-third_party-zlib/archive/${cr_zlib}:chromium-zlib
distfiles-append    chromium-src-third_party-zlib-${cr_zlib}${extract.suffix}:chromium-zlib

master_sites-append https://github.com/KhronosGroup/SPIRV-Headers/archive/${spirv_headers}:spirv-headers
distfiles-append    SPIRV-Headers-${spirv_headers}${extract.suffix}:spirv-headers

master_sites-append https://github.com/KhronosGroup/SPIRV-Tools/archive/${spirv_tools}:spirv-tools
distfiles-append    SPIRV-Tools-${spirv_tools}${extract.suffix}:spirv-tools

master_sites-append https://github.com/KhronosGroup/Vulkan-Headers/archive/${vk_headers}:vulkan-headers
distfiles-append    Vulkan-Headers-${vk_headers}${extract.suffix}:vulkan-headers

master_sites-append https://github.com/ARM-software/astc-encoder/archive/${astc_encoder}:astc-encoder
distfiles-append    astc-encoder-${astc_encoder}${extract.suffix}:astc-encoder

master_sites-append https://github.com/open-source-parsers/jsoncpp/archive/${jsoncpp}:jsoncpp
distfiles-append    jsoncpp-${jsoncpp}${extract.suffix}:jsoncpp

checksums           ${distname}${extract.suffix} \
                    rmd160  48e6cf1971264da8532a69fe131aa668004624d2 \
                    sha256  5b296c9cbb7fc12df0fd5bd7df9555b6b3947a6d11d8af836e414ef35750dc97 \
                    size    16721061 \
                    chromium-src-build-${cr_build}${extract.suffix} \
                    rmd160  d4f0407e6efdec77747d3034a0d7cebdcefda7e1 \
                    sha256  b2b7ce21cc65df7a1141c044dec1a2890b58d45f3beb671378f69b9410b8b4d8 \
                    size    1902176 \
                    chromium-src-testing-${cr_testing}${extract.suffix} \
                    rmd160  c5e9fe93b5c6b25c764335b9f44784929bf69816 \
                    sha256  fdd31b5ea5a5dabe4f231b40f6348d8fffa4c05f5a8de6b8c23bef0025f2b88c \
                    size    2057644 \
                    chromium-src-third_party-jsoncpp-${cr_jsoncpp}${extract.suffix} \
                    rmd160  8611455ceb50dd821a942c3b10f2110c418fe182 \
                    sha256  7360eff9ce58208c68da260db23bdc29bbc00c769905af10aa45af0dd308aba4 \
                    size    4411 \
                    chromium-src-third_party-rust-${cr_rust}${extract.suffix} \
                    rmd160  07b122fd1630442143eba649f19b7a8431d839eb \
                    sha256  765a526ed5fc396d9bc394620ab491401363235bda21c039c1cc84f08fbe2bfe \
                    size    35154082 \
                    chromium-src-third_party-zlib-${cr_zlib}${extract.suffix} \
                    rmd160  3b966f46c5f6014353a3e68447bb094e43883025 \
                    sha256  ff1b55551aad16c58cfca896a4d87d616cf70125829a8048278fd31c9278993d \
                    size    1385580 \
                    SPIRV-Headers-${spirv_headers}${extract.suffix} \
                    rmd160  d4c6862b4c31ff0bc43ea16f0382b542183fa28a \
                    sha256  392f4801409aad9c4f1b77745f179952fd6264e4c8bd0fc1bc45dfa6807bf6d0 \
                    size    594644 \
                    SPIRV-Tools-${spirv_tools}${extract.suffix} \
                    rmd160  694fff8969ca2698e80d647092f9075d82008215 \
                    sha256  2f036f62a160b9697474e908e01a65dcf4b788fac29df29dae1b84460a4324e6 \
                    size    3564064 \
                    Vulkan-Headers-${vk_headers}${extract.suffix} \
                    rmd160  c6666a1ffd903fba22a9f17a9075cd5b246f7f59 \
                    sha256  df7a152381a7fdaf98e36de7716b8d6d6ead776764bc76d1a1aef9049d1bca89 \
                    size    3416190 \
                    astc-encoder-${astc_encoder}${extract.suffix} \
                    rmd160  1c792cca8415346924640772699e6ffc2d3871f4 \
                    sha256  8b5068ef28a8db1cb354d89d9cefd19d43eddfc72c3468fce7ebb92b2431d4c4 \
                    size    36161899 \
                    jsoncpp-${jsoncpp}${extract.suffix} \
                    rmd160  2938aba554af493df2cc854497fa3a00d55521ee \
                    sha256  0b40e4598d68d3dbd8cab90b249e18f1363ecc694c38f727851f4db34b6887ec \
                    size    216350

set py_ver          3.14
set py_ver_nodot    [string map {. {}} ${py_ver}]

configure.python    ${prefix}/bin/python${py_ver}

depends_build       port:gn \
                    port:python${py_ver_nodot} \
                    port:ninja \
                    port:rapidjson

use_xcode           yes

patchfiles          patch-commit-id.diff \
                    patch-apple-toolchain.diff \
                    patch-src-common-platform.diff

post-extract {
    delete ${worksrcpath}/build
    move ${workpath}/chromium-src-build-${cr_build} ${worksrcpath}/build

    delete ${worksrcpath}/testing
    move ${workpath}/chromium-src-testing-${cr_testing} ${worksrcpath}/testing

    delete ${worksrcpath}/third_party/jsoncpp
    move ${workpath}/chromium-src-third_party-jsoncpp-${cr_jsoncpp} \
        ${worksrcpath}/third_party/jsoncpp

    delete ${worksrcpath}/third_party/rust
    move ${workpath}/chromium-src-third_party-rust-${cr_rust} \
        ${worksrcpath}/third_party/rust

    delete ${worksrcpath}/third_party/zlib
    move ${workpath}/chromium-src-third_party-zlib-${cr_zlib} \
        ${worksrcpath}/third_party/zlib

    delete ${worksrcpath}/third_party/spirv-headers/src
    move ${workpath}/SPIRV-Headers-${spirv_headers} \
        ${worksrcpath}/third_party/spirv-headers/src

    delete ${worksrcpath}/third_party/spirv-tools/src
    move ${workpath}/SPIRV-Tools-${spirv_tools} \
        ${worksrcpath}/third_party/spirv-tools/src

    delete ${worksrcpath}/third_party/vulkan-headers/src
    move ${workpath}/Vulkan-Headers-${vk_headers} \
        ${worksrcpath}/third_party/vulkan-headers/src

    delete ${worksrcpath}/third_party/astc-encoder/src
    move ${workpath}/astc-encoder-${astc_encoder} \
        ${worksrcpath}/third_party/astc-encoder/src

    delete ${worksrcpath}/third_party/jsoncpp/source
    move ${workpath}/jsoncpp-${jsoncpp} \
        ${worksrcpath}/third_party/jsoncpp/source

    copy ${filespath}/gclient_args.gni ${worksrcpath}/build/config/
}

post-patch {
    reinplace "s|@COMMIT_POSITION@|[lindex [split ${version} .] 2]|" \
        ${worksrcpath}/src/commit_id.py
}

compiler.cxx_standard   2020

# avoid conflict with installed zlib
compiler.limit_flags    yes

configure.cmd       gn gen
configure.pre_args  --script-executable=${configure.python}
configure.args      --args='\
                    mac_sdk_min=\"${macosx_deployment_target}\" \
                    mac_deployment_target=\"${macosx_deployment_target}\" \
                    install_prefix=\"${destroot}${prefix}\" \
                    is_official_build=true \
                    is_clang=false \
                    treat_warnings_as_errors=false \
                    fatal_linker_warnings=false \
                    use_custom_libcxx=false \
                    enable_rust=false \
                    angle_build_tests=false \
                    angle_enable_metal=false \
                    angle_enable_vulkan=false'
configure.post_args out

if {${os.platform} eq "darwin" && ${os.major} < 19} {
    configure.cppflags-append   -DTARGET_OS_MACCATALYST=0 \
                                -DMTLGPUFamilyMac2=2002
}
if {${os.platform} eq "darwin" && ${os.major} < 21} {
    configure.cppflags-append   -DkIOMainPortDefault=kIOMasterPortDefault
}
configure.cxx-append    {*}${configure.cppflags}

build.cmd           ninja
build.target        angle
build.args          -C out
build.env           ANGLE_UPSTREAM_HASH=[string range $rev 0 11]

destroot.cmd        ninja
destroot.target     install_angle
destroot.args       -C out
destroot.destdir

post-destroot {
    reinplace "s|^prefix=.*$|prefix=${prefix}|" \
        {*}[glob ${destroot}${prefix}/lib/pkgconfig/*.pc]

    delete \
        ${destroot}${prefix}/include/CL \
        ${destroot}${prefix}/include/GLX \
        ${destroot}${prefix}/include/WGL \
        ${destroot}${prefix}/include/GLSLANG \
        ${destroot}${prefix}/include/vulkan \
        ${destroot}${prefix}/include/platform \
        {*}[glob ${destroot}${prefix}/include/*.h] \
        {*}[glob ${destroot}${prefix}/include/*/README.md] \
        {*}[glob ${destroot}${prefix}/include/*/.clang-format]

    platform darwin {
        foreach f [glob -tails -directory ${destroot} ${prefix}/lib/*.dylib] {
            system "install_name_tool -id /$f ${destroot}/$f"
        }

        system "install_name_tool -change ./libGLESv2.dylib \
            ${prefix}/lib/libGLESv2.dylib \
            ${destroot}${prefix}/lib/libGLESv1_CM.dylib"
    }
}

livecheck.url       https://chromiumdash.appspot.com/fetch_releases?channel=Stable&platform=Mac
livecheck.type      regex
livecheck.regex     {"angle":"([0-9a-f]+)"}
livecheck.version   $rev
