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

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.27281
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        97e2bd1131c90d68561f34776219613b9dfc13ba
set cr_testing      867a91fb26b8e90951e564dead1ff59793148c55
set astc_encoder    2319d9c4d4af53a7fc7c52985e264ce6e8a02a9b
set cr_rust         ba6ceef355cf48cc50493296d4fc862d9745b742
set spirv_headers   f88a2d766840fc825af1fc065977953ba1fa4a91
set spirv_tools     7d8d9e58c384949f1615c069d4c9346bf51b9738
set vk_headers      74d8a6cb930c68ef617b202c3ff3c59d919e086b
set cr_zlib         b80f1d1e5256ac25f6aea3f31f13d458981cb1f9

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  5cc6b0760d5d2502d80c72f43e6388f9971327db \
                    sha256  045dcc97e99747da0c098528237d6b7f022837229b3b463c30857cd254a37c64 \
                    size    16627229 \
                    chromium-src-build-${cr_build}${extract.suffix} \
                    rmd160  81865c175e5c270d003837d3bf118e86631e8195 \
                    sha256  4a9c593a774ca5d46f3c07837771d810b70c8f10f00e8a1b14eb501afec6295a \
                    size    1756470 \
                    chromium-src-testing-${cr_testing}${extract.suffix} \
                    rmd160  de73a0cdbafe3ad793fbb08135dc1911aa72e30e \
                    sha256  284dd52bfc735ec340135e5fdf404afa3a216c1a7fa0568313d70a7bba972d38 \
                    size    2042993 \
                    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  1edde5efdfa44e9cb25423f6340147c9b50a4076 \
                    sha256  7600745577ee1831ad952bf72a9416158eb055c7a956538f8d6bbeaa2b98dac3 \
                    size    25991280 \
                    chromium-src-third_party-zlib-${cr_zlib}${extract.suffix} \
                    rmd160  1781e5a13bb84b6f3be67933e6cf04d9971b0c88 \
                    sha256  7ac2fa3b153b89cb68a06d455761982865f6d4c3fa2aac302b53838afc63db66 \
                    size    1379964 \
                    SPIRV-Headers-${spirv_headers}${extract.suffix} \
                    rmd160  414e47fcaefcc5b645396cec5179d7b4d6c459e3 \
                    sha256  028424e4934f2ed8567319939e79d0a87e529ac7c504501b8b406f98598bdadd \
                    size    562966 \
                    SPIRV-Tools-${spirv_tools}${extract.suffix} \
                    rmd160  099c12ac21dede1ce336723c6f691560fe4ec1f7 \
                    sha256  e48b296026314d766c796ebe0ac5c59a3996c85c05159510fac39667f267890c \
                    size    3442549 \
                    Vulkan-Headers-${vk_headers}${extract.suffix} \
                    rmd160  4a43e8eed1deeb23905f916011ea553078ca6da0 \
                    sha256  75967a8288ff16044a86d1af462b96d2e75c435937efb75f2bb4102ab0382f29 \
                    size    3029927 \
                    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
