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

github.setup        google skia 148 chrome/m
revision            0
categories          graphics devel
license             BSD
maintainers         {@barracuda156 macos-powerpc.org:barracuda}
description         2D graphics library for drawing text, geometries and images
long_description    Skia is a complete 2D graphic library for drawing text, \
                    geometries and images. It serves as the graphics engine \
                    for Google Chrome, ChromeOS, Android, Flutter and many \
                    other products.

homepage            https://skia.org

checksums           rmd160  05854795c4d58f552adc772332b5581e65f2dab4 \
                    sha256  cd8cbfcb3d624b56352fe6f8d654e32165bf40f646146c376d0100091fb1c6c7 \
                    size    67172185
github.tarball_from archive

compiler.cxx_standard   2020

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

depends_build-append \
                    port:gn \
                    port:ninja \
                    path:bin/pkg-config:pkgconfig \
                    port:python${py_ver_nodot}

depends_lib-append  port:expat \
                    path:lib/pkgconfig/fontconfig.pc:fontconfig \
                    path:lib/pkgconfig/freetype2.pc:freetype \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    path:lib/pkgconfig/icu-uc.pc:icu \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpng \
                    port:webp \
                    port:zlib

# Build system patches
patchfiles          patch-BUILD.gn.diff \
                    patch-gn-skia-BUILD.gn.diff \
                    patch-gn-toolchain-BUILD.gn.diff

# Generic patches
patchfiles-append   patch-src-utils-SkJSONWriter.cpp.diff

# macOS compatibility patches (10.6+ CoreText support)
patchfiles-append   patch-include-core-SkTypes.h.diff \
                    patch-include-private-base-SkLoadUserConfig.h.diff \
                    patch-src-ports-SkFontMgr_mac_ct.cpp.diff \
                    patch-src-ports-SkScalerContext_mac_ct.cpp.diff \
                    patch-src-ports-SkTypeface_mac_ct.cpp.diff \
                    patch-src-ports-SkTypeface_mac_ct.h.diff \
                    patch-src-utils-mac-SkCTFont.cpp.diff \
                    patch-SkCreateCGImageRef.diff

# Big-endian (PowerPC) support patches
patchfiles-append   patch-fix-struct-GrGLVertexArray.diff \
                    patch-modules-jsonreader-SkJSONReader.cpp.diff \
                    patch-modules-jsonreader-SkJSONReader.h.diff \
                    patch-modules-skcms-skcms.cc.diff \
                    patch-modules-skcms-src-skcms_internals.h.diff \
                    patch-powerpc-macros.diff \
                    patch-src-codec-SkCodecPriv.h.diff \
                    patch-src-core-Sk4px.h.diff \
                    patch-src-core-SkColor.cpp.diff \
                    patch-src-core-SkColorPriv.h.diff \
                    patch-src-ports-SkRasterPipeline_opts.h.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/BUILD.gn
    reinplace "s|/usr/include|${prefix}|g" \
        ${worksrcpath}/third_party/freetype2/BUILD.gn \
        ${worksrcpath}/third_party/harfbuzz/BUILD.gn
}

proc gn_target_cpu {arch} {
    switch ${arch} {
        arm64   { return arm64 }
        i386    { return x86 }
        ppc     { return ppc }
        ppc64   { return ppc64 }
        x86_64  { return x64 }
        default { return ${arch} }
    }
}

if {${configure.sdkroot} eq ""} {
    set sdk_root "/"
} else {
    set sdk_root ${configure.sdkroot}
}

if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
    set extra_cflags_cc {["-D_GLIBCXX_USE_CXX11_ABI=0"]}
} else {
   set extra_cflags_cc {[]}
}

configure.cmd       ${prefix}/bin/gn
configure.pre_args  gen out
configure.post_args -v

# skia_use_system_zlib is broken, as of v144, so link manually instead.
configure.args      --args=' \
                    cc=\"${configure.cc}\" \
                    cxx=\"${configure.cxx}\" \
                    extra_cflags=\[\"-I${prefix}/include\",\"-I${prefix}/include/harfbuzz\",\"-I${prefix}/include/freetype2\"\] \
                    extra_cflags_cc=${extra_cflags_cc} \
                    extra_ldflags=\[\"-L${prefix}/lib\",\"-lz\"\] \
                    is_component_build=true \
                    is_debug=false \
                    is_official_build=true \
                    mac_deployment_target=\"${macosx_deployment_target}\" \
                    skia_build_rust_targets=false \
                    skia_enable_graphite=false \
                    skia_use_dawn=false \
                    skia_use_dng_sdk=false \
                    skia_use_fontations=false \
                    skia_use_gl=true \
                    skia_use_metal=false \
                    skia_use_piex=false \
                    skia_use_system_expat=true \
                    skia_use_system_harfbuzz=true \
                    skia_use_system_icu=true \
                    skia_use_system_libjpeg_turbo=true \
                    skia_use_system_libpng=true \
                    skia_use_system_libwebp=true \
                    skia_use_vulkan=false \
                    skia_use_wuffs=false \
                    target_cpu=\"[gn_target_cpu ${configure.build_arch}]\" \
                    target_os=\"mac\" \
                    xcode_sysroot=\"${sdk_root}\"' \
                    --script-executable=${prefix}/bin/python${py_ver}

build.cmd           ${prefix}/bin/ninja
build.dir           ${worksrcpath}/out
build.target
build.args          -v

destroot {
    foreach lib [glob -nocomplain ${worksrcpath}/out/*.dylib] {
        set libname [file tail ${lib}]
        set basename [file rootname ${libname}]
        set versioned_name ${basename}.${version}.dylib
        xinstall -m 0755 ${lib} ${destroot}${prefix}/lib/${versioned_name}
        ln -s ${versioned_name} ${destroot}${prefix}/lib/${libname}
    }

    foreach lib [glob -nocomplain ${worksrcpath}/out/*.a] {
        xinstall -m 0644 ${lib} ${destroot}${prefix}/lib/
    }

    set incdir ${destroot}${prefix}/include/skia
    xinstall -d ${incdir}

    fs-traverse f ${worksrcpath}/include {
        set dest [string map [list ${worksrcpath}/include ${incdir}/include] ${f}]
        if {[file isdirectory ${f}]} {
            xinstall -d ${dest}
        } elseif {[file isfile ${f}] && [file extension ${f}] eq ".h"} {
            xinstall -m 0644 ${f} ${dest}
        }
    }

    fs-traverse f ${worksrcpath}/modules {
        set dest [string map [list ${worksrcpath}/modules ${incdir}/modules] ${f}]
        if {[file isdirectory ${f}]} {
            xinstall -d ${dest}
        } elseif {[file isfile ${f}] && [file extension ${f}] eq ".h"} {
            xinstall -m 0644 ${f} ${dest}
        }
    }

    xinstall -d ${destroot}${prefix}/lib/pkgconfig
    set pc [open ${destroot}${prefix}/lib/pkgconfig/skia.pc w]
    puts ${pc} "prefix=${prefix}"
    puts ${pc} "exec_prefix=\${prefix}"
    puts ${pc} "libdir=\${prefix}/lib"
    puts ${pc} "includedir=\${prefix}/include/skia"
    puts ${pc} ""
    puts ${pc} "Name: skia"
    puts ${pc} "Description: 2D graphic library for drawing text, geometries and images"
    puts ${pc} "URL: https://skia.org/"
    puts ${pc} "Version: ${version}"
    puts ${pc} "Libs: -L\${libdir} -lskia"
    puts ${pc} "Cflags: -I\${includedir}"
    close ${pc}

    # Only process versioned dylibs (not symlinks)
    foreach lib [glob -nocomplain ${destroot}${prefix}/lib/*.${version}.dylib] {
        set versioned_name [file tail ${lib}]
        set basename [string map [list .${version}.dylib ""] ${versioned_name}]
        set unversioned_name ${basename}.dylib

        system "install_name_tool -id ${prefix}/lib/${unversioned_name} ${lib}"

        foreach dep [glob -nocomplain ${destroot}${prefix}/lib/*.${version}.dylib] {
            set dep_versioned [file tail ${dep}]
            set dep_base [string map [list .${version}.dylib ""] ${dep_versioned}]
            set dep_unversioned ${dep_base}.dylib
            catch {system "install_name_tool -change @rpath/${dep_unversioned} ${prefix}/lib/${dep_unversioned} ${lib}"}
        }
    }
}
