# -*- 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
PortGroup           legacysupport 1.1

# strnlen
legacysupport.newest_darwin_requires_legacy 10

set rev             72b8f72a7587ec776d7d2a57d275a6e9b1781b1d

github.setup        google angle $rev

# Major and minor version from src/common/angle_version.h
# Patch version using `git rev-list HEAD --count`
version             2.1.28727
revision            1
categories          graphics devel
platforms           {darwin >= 10}
license             BSD
maintainers         {makr @mohd-akram} {@barracuda156 macos-powerpc.org:barracuda} openmaintainer

description         OpenGL ES implementation
long_description    Cross-platform, conformant OpenGL ES implementation.

homepage            https://angleproject.org

github.tarball_from archive

# grep -E '/src/(build\.|testing|third_party/zlib)|jsoncpp_revision.:|SPIRV-(Headers|Tools)|Vulkan-Headers|astc-encoder@' DEPS
set astc_encoder    2319d9c4d4af53a7fc7c52985e264ce6e8a02a9b
set cr_build        18940f0d92f236dd7b8672516700afa2b1f3d123
set cr_jsoncpp      f62d44704b4da6014aa231cfc116e7fd29617d2a
set cr_testing      491a1efb39359ee1e82d063ab3c32016f2fee111
set cr_zlib         5eb4d7ed380f214e7a0a23c18f629048d3ba9e00
set jsoncpp         42e892d96e47b1f6e29844cc705e148ec4856448
set spirv_headers   496543121ce6419f23d6fa5d7194ba66c36212d2
set spirv_tools     b40380bfa431d028fb7ca8eb375e4d21ea98a70e
set vk_headers      31386378257ac8653ce5b32c93baec385259ebbe

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-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  8a19e5f64a9bab710c51eb906eae0bedb0265d4f \
                    sha256  658f054a48f0a45d0817307448a006450e36b472f77cd44c92fa484432c2b45c \
                    size    16691013 \
                    astc-encoder-${astc_encoder}${extract.suffix} \
                    rmd160  1c792cca8415346924640772699e6ffc2d3871f4 \
                    sha256  8b5068ef28a8db1cb354d89d9cefd19d43eddfc72c3468fce7ebb92b2431d4c4 \
                    size    36161899 \
                    chromium-src-build-${cr_build}${extract.suffix} \
                    rmd160  d1f5bc310034ae86ddb9acb593b3ecd36877f1d6 \
                    sha256  e481a6d424cb9ca66b8fe7592924346d31658360998d6d246bcb7369b9df09a1 \
                    size    1898346 \
                    chromium-src-testing-${cr_testing}${extract.suffix} \
                    rmd160  aeba6f5600ffe857fcced54ee327aea5b23d6be7 \
                    sha256  5a1957de8ad7ffdc7925ae2d6dc0bcea36779ca2b0839b31b0be8c0a6b26b0ad \
                    size    2055443 \
                    chromium-src-third_party-jsoncpp-${cr_jsoncpp}${extract.suffix} \
                    rmd160  8611455ceb50dd821a942c3b10f2110c418fe182 \
                    sha256  7360eff9ce58208c68da260db23bdc29bbc00c769905af10aa45af0dd308aba4 \
                    size    4411 \
                    chromium-src-third_party-zlib-${cr_zlib}${extract.suffix} \
                    rmd160  eeb47ed54e746a921ff764bfbfed56d46d617662 \
                    sha256  1bd98d6024de834deb46caf5087b5d5408a94cd2d6cdf8d508ce1e7f9ddefead \
                    size    1385558 \
                    jsoncpp-${jsoncpp}${extract.suffix} \
                    rmd160  2938aba554af493df2cc854497fa3a00d55521ee \
                    sha256  0b40e4598d68d3dbd8cab90b249e18f1363ecc694c38f727851f4db34b6887ec \
                    size    216350 \
                    SPIRV-Headers-${spirv_headers}${extract.suffix} \
                    rmd160  6c1ff552bcc8c25e57b64d18a1d34c3d774b789d \
                    sha256  a9bb9c48713245eacf97cc539b6f1d45405a92d8813f8b82e635f0a085ee9898 \
                    size    594379 \
                    SPIRV-Tools-${spirv_tools}${extract.suffix} \
                    rmd160  c6044a37b64135614996ede61b309c3afc3506bd \
                    sha256  ebc06345ece01d6c7ec0b3d0c9f6a27eb2e59ac4d97376f5e4d5ca38abf096c7 \
                    size    3554544 \
                    Vulkan-Headers-${vk_headers}${extract.suffix} \
                    rmd160  510af90c2b3d088e0e8cdaca7a80a07fefd27698 \
                    sha256  efe79256adb6f2a5112bdcd03712d3aeb59a2e1a54a9e97650223dfe57b12067 \
                    size    3405761

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

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

depends_build       port:gn \
                    port:ninja \
                    port:python${py_ver_nodot} \
                    path:lib/pkgconfig/RapidJSON.pc:rapidjson

# Python only needed for scripts
depends_skip_archcheck-append \
                    python${py_ver_nodot}
configure.python    ${prefix}/bin/python${py_ver}

use_xcode           yes

# Upstream MacPorts patches:
patchfiles          patch-commit-id.diff \
                    patch-apple-toolchain.diff \
                    patch-src-common-platform.diff

# Our patches. These are needed / safe to use unconditionally,
# and sufficient to fix build on Catalina.
patchfiles-append   patch-debug.cpp.diff \
                    patch-apple_platform_utils.diff \
                    patch-SystemInfo_macos.mm.diff \
                    patch-build_config.h.diff \
                    patch-driver_utils_mac.mm.diff \
                    patch-no-stupid-error-in-host_byteorder.diff

# May need to be broken down into general vs legacy part:
patchfiles-append   fix-broken-build-system.diff

# Misc fixes, exact threshold is unknown.
# Fixes bugs in build system, throws away rust includes (rust is not used for any archs,
# but gn includes its build spec files, which breaks powerpc build), recognizes powerpc,
# fixes OpenGL includes, fix wrong paths to /Developer contents.
if {${os.platform} eq "darwin" && ${os.major} < 19} {
    # Reverts regression from https://github.com/chromium/chromium/commit/365b67f3ec098a894a369945baa382824e1e7efc
    patchfiles-append \
                    fix-sdk-path.diff \
                    patch-FunctionsCGL.diff \
                    patch-fix-libtool.diff \
                    patch-IOSurfaceSurfaceCGL.cpp.diff \
                    patch-WindowSurfaceCGL.mm.diff \
                    patch-WindowSurfaceCGL-no-blit.diff \
                    patch-DisplayCGL.mm.diff \
                    patch-no-dsyms-and-strip.diff
}

if {${os.platform} eq "darwin" && ${os.major} < 15} {
    patchfiles-append \
                    no-metal-for-goodness-sake.patch
}

# No direct linking to CoreGraphics before 10.8:
if {${os.platform} eq "darwin" && ${os.major} < 12} {
    patchfiles-append \
                    patch-fix-frameworks.diff
}

# GL
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    patchfiles-append \
                    patch-restore-legacy-desktop-gl.diff \
                    patch-legacy-desktop-gl-fixes.diff
}

# legacy-support, adjust if needed.
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    patchfiles-append \
                    patch-legacysupport.diff
}

# Fix wrong struct size on ppc32:
if {${os.platform} eq "darwin" && ${configure.build_arch} eq "ppc"} {
    patchfiles-append \
                    patch-Uniform.h-fix-struct-size.diff
}

# Drop clangisms, fix 32-bit builds with gcc:
if {[string match *gcc* ${configure.compiler}]} {
    patchfiles-append \
                    fix-wrong-flags-for-gcc.diff \
                    patch-fix-threadId.diff \
                    patch-SoftLinking.h.diff \
                    patch-Context.cpp.diff \
                    patch-Display.cpp.diff \
                    patch-global_state.cpp.diff \
                    patch-fix-id-bug.diff

    # https://trac.macports.org/ticket/74110
    patchfiles-append \
                    patch-IntermNode-gcc-array-new.diff

    if {${configure.build_arch} in [list arm i386 ppc]} {
        patchfiles-append \
                    patch-libatomic.diff
    }
}

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

    fs-traverse f ${worksrcpath} {
        if {[string match *.py ${f}]} {
            reinplace -q "s|/usr/bin/python3|${configure.python}|" ${f}
            reinplace -q "s|/usr/bin/env python3|${configure.python}|" ${f}
        }
    }
    reinplace "s|@PYTHON@|${configure.python}|" ${worksrcpath}/scripts/roll_aosp.sh
    reinplace "s|exec python3|exec ${configure.python}|" ${worksrcpath}/build/gn_editor
    reinplace "s|/usr/bin/env python3|${configure.python}|" ${worksrcpath}/build/util/ide_query

    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/build/config/compiler/BUILD.gn
    }

    set fd [open ${worksrcpath}/build_overrides/angle.gni a]
    puts ${fd} {angle_libs_suffix = "_angle"}
    close ${fd}
}

compiler.cxx_standard   2020

# avoid conflict with installed zlib
compiler.limit_flags    yes

if {${macosx_deployment_target} eq ""} {
    # Uses IOSurface
    set macosx_deployment_target 10.6
}

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

# Needed, otherwise the build picks system zlib and everything breaks:
configure.cppflags

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

build.cmd           ninja
build.pre_args      -C out
build.post_args     -v
build.env           ANGLE_UPSTREAM_HASH=[string range $rev 0 11]

destroot.cmd        ninja
destroot.pre_args   -C out
destroot.post_args  -v
destroot.destdir
destroot.target     install_angle

post-destroot {
    copy ${worksrcpath}/out/angle_shader_translator ${destroot}${prefix}/bin/

    delete \
        {*}[glob ${destroot}${prefix}/lib/pkgconfig/*.pc] \
        ${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]

    set headers [glob -tails -directory ${destroot}${prefix}/include *]
    xinstall -d ${destroot}${prefix}/include/angle
    foreach h ${headers} {
        move ${destroot}${prefix}/include/${h} ${destroot}${prefix}/include/angle/${h}
    }

    set pc [open ${destroot}${prefix}/lib/pkgconfig/angle.pc w]
    puts ${pc} "prefix=${prefix}"
    puts ${pc} "libdir=\${prefix}/lib"
    puts ${pc} "includedir=\${prefix}/include/angle"
    puts ${pc} ""
    puts ${pc} "Name: angle"
    puts ${pc} "Description: ${description}"
    puts ${pc} "Version: ${version}"
    puts ${pc} "Libs: -L\${libdir} -lEGL_angle -lGLESv2_angle"
    puts ${pc} "Cflags: -I\${includedir}"
    close ${pc}

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

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