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

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.26625
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        1bd2ee1a985e60cd0ea2d44502d3b3d8e1d7456d
set cr_testing      e1aee0f6d933794e3789053f74bb2578e7f13cdf
set astc_encoder    2319d9c4d4af53a7fc7c52985e264ce6e8a02a9b
set cr_rust         e6ee35e1c94c62365c41db142d17bb96210b6dad
set spirv_headers   b824a462d4256d720bebb40e78b9eb8f78bbb305
set spirv_tools     f410b3c178740f9f5bd28d5b22a71d4bc10acd49
set vk_headers      2fa203425eb4af9dfc6b03f97ef72b0b5bcb8350
set cr_zlib         63d7e16739d83e3a16c673692a348e52db1a3a11

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  ef4facb8883b01300134c4a34fd516ae61ec6ddd \
                    sha256  84e163a9df03fb2c99e16963b75d4eae73aa2d2fa96e59ffe6b4020415c8e10e \
                    size    16456480 \
                    chromium-src-build-${cr_build}${extract.suffix} \
                    rmd160  4378d91540358729d4d7ad35f5e13b6e2a1cc784 \
                    sha256  be96c74a7c50af8e27b038ae673ea16f20e3b044e205bf16b0072e2493316a64 \
                    size    1727881 \
                    chromium-src-testing-${cr_testing}${extract.suffix} \
                    rmd160  782f640d62827df49ab41caee903deb473e33ed2 \
                    sha256  73596060a0e832df32b818dc69e3e5082086546fca72cc518512e1bfc35ee30e \
                    size    2043361 \
                    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  1c053db351cc4e46d691b4901caaf05a70d9eb6e \
                    sha256  46a3d2288217863cde53592344a80734b0b5bdc35bd880aa2105bfe79d40c1b3 \
                    size    22623916 \
                    chromium-src-third_party-zlib-${cr_zlib}${extract.suffix} \
                    rmd160  2a83d23c3fa0bcc11e1e430418533be7e2403c53 \
                    sha256  942197648a0f34abf149163684b3224bacdbfc9fd3ce9aaa65e76f5a4a811bd5 \
                    size    616474 \
                    SPIRV-Headers-${spirv_headers}${extract.suffix} \
                    rmd160  3a4ae286af7a471cffa157b99289304b7f2a583e \
                    sha256  c693867f10a7760ef1bcf85419d51783586768cc2c601d03841bc6a8b2554b9c \
                    size    558965 \
                    SPIRV-Tools-${spirv_tools}${extract.suffix} \
                    rmd160  f170d2731ac92ddd04e6245cdb7c357651c23141 \
                    sha256  16e6f389775791522e1d7d3f2c0e210a3f0139aa486235f6a1496779a6e6ab9f \
                    size    3430066 \
                    Vulkan-Headers-${vk_headers}${extract.suffix} \
                    rmd160  4a63f3eb3968913d0c67d7d1bcc0ff8a0ad67f78 \
                    sha256  66e0dc7932203def85d94bf65a4fa3a3b29e61366713404f0465cdeb02e6ffd3 \
                    size    2871954 \
                    astc-encoder-${astc_encoder}${extract.suffix} \
                    rmd160  1c792cca8415346924640772699e6ffc2d3871f4 \
                    sha256  8b5068ef28a8db1cb354d89d9cefd19d43eddfc72c3468fce7ebb92b2431d4c4 \
                    size    36161899 \
                    jsoncpp-${jsoncpp}${extract.suffix} \
                    rmd160  2938aba554af493df2cc854497fa3a00d55521ee \
                    sha256  0b40e4598d68d3dbd8cab90b249e18f1363ecc694c38f727851f4db34b6887ec \
                    size    216350

depends_build       port:gn \
                    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

configure.cmd       gn
configure.pre_args  gen out
configure.args      --args='\
                    mac_sdk_min=\"0\" \
                    install_prefix=\"${destroot}${prefix}\" \
                    is_official_build=true \
                    is_clang=false \
                    treat_warnings_as_errors=false \
                    fatal_linker_warnings=false \
                    use_custom_libcxx=false \
                    rust_sysroot_absolute=\"${prefix}\" \
                    angle_build_tests=false \
                    angle_enable_metal=false \
                    angle_enable_vulkan=false'

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

platform darwin {
    post-destroot {
        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"

        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]
    }
}

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