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

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

set my_name         libvpx
name                ${my_name}
conflicts           ${my_name}-devel
version             1.16.0
revision            3

categories          multimedia
license             Permissive
maintainers         {mascguy @mascguy} openmaintainer

description         WebM VP8/VP9 Codec SDK
long_description    The WebM VP8/VP9 codecs are being developed \
                    specifically to meet the demand for the consumption \
                    of video over the Internet, including professionally \
                    and amateur produced video-on-demand and conversational \
                    video content. VP9 is an evolution of the VP8 video \
                    codec and includes a number of enhancements and new \
                    coding tools that have been added to improve coding efficiency. \
                    The VP9 encoder in this release is not, as yet, feature-complete.
homepage            http://webmproject.org/tools

distname            ${my_name}-${version}
dist_subdir         ${my_name}

# https://github.com/macos-powerpc/powerpc-ports/issues/76
conflicts_build     gtest

# use git fetch rather than downloading a snapshot tarball
# which delivers a different checksum and size on each download
# this is not a stealth upgrade but a "feature" of this repo

fetch.type          git
git.url             https://chromium.googlesource.com/webm/${my_name}
git.branch          v${version}

post-extract {
    xinstall -d ${worksrcpath}/vp8/common/ppc
    foreach asm_src {filter_altivec.S filter_bilinear_altivec.S loopfilter_filters_altivec.S recon_altivec.S} {
        copy ${filespath}/${asm_src} ${worksrcpath}/vp8/common/ppc/
    }
}

patch.pre_args-replace  -p0 -p1

patchfiles-append   patch-vpx_thread.diff

# Fix upstream bug:
# https://github.com/macos-powerpc/powerpc-ports/issues/180
patchfiles-append   vp9-fix-big-endian-partial-edge-block-entropy-contexts.patch

if {![variant_isset altivec]} {
    patchfiles-append \
                    patch-configure.sh.diff \
                    patch-Makefile.diff \
                    patch-vpx_ports.diff
}

# requires c++11 https://trac.macports.org/ticket/67038
compiler.cxx_standard 2011
# Requires MacPorts -stdlib flag added to ldflags.
# https://trac.macports.org/ticket/67038
configure.ldflags-append \
                    {*}${configure.cxxflags}

# Avoid linking with already-installed libvpx or gtest.
# https://trac.macports.org/ticket/66259
# https://trac.macports.org/ticket/70989
configure.cppflags-delete \
                    -I${prefix}/include
configure.ldflags-delete \
                    -L${prefix}/lib

# Uses newer assembly features on Intel.
# Also blacklist clang 8, due to issues like:
#   error: use of undeclared identifier 'abs'
compiler.blacklist-append \
                    {clang < 900} {macports-clang-3.*}

# As of 1.7.0: builds both static and shared libraries
# doesn't install docs or examples correctly, so disable them.
configure.args-append \
                    --disable-debug-libs \
                    --disable-install-docs \
                    --disable-unit-tests \
                    --enable-examples \
                    --enable-experimental \
                    --enable-multithread \
                    --enable-pic \
                    --enable-postproc \
                    --enable-vp8 \
                    --enable-vp9 \
                    --enable-vp9-highbitdepth

platform darwin {
    configure.args-append \
                    --enable-shared
}

platform darwin 8 {
    depends_build-append    port:gmake
    build.cmd               ${prefix}/bin/gmake

    # Tiger Intel has some unique restrictions https://trac.macports.org/ticket/67041
    if {${configure.build_arch} in [list i386 x86_64]} {
        compiler.blacklist-append macports-gcc-7
        configure.args-append --disable-libyuv
    }
}

build.target
build.args          verbose=1
destroot.args       verbose=1

if {"${configure.sdkroot}" eq ""} {
    configure.sdkroot "/"
}

if {${universal_possible} && [variant_isset universal]} {
    # universal build using muniversal portgroup
    foreach my_arch {i386 x86_64 arm64} {
        # strip the automatic setting of host
        set merger_host(${my_arch}) ""
        # force the target
        set merger_configure_args(${my_arch}) --force-target=${my_arch}-${os.platform}${os.major}-gcc
        # set LD
        set merger_configure_env(${my_arch})  LD=${configure.cc}
    }

    if {[variant_isset altivec]} {
        foreach my_arch {ppc ppc64} {
            set merger_host(${my_arch}) ""
            set merger_configure_args(${my_arch}) --force-target=${my_arch}-${os.platform}${os.major}-gcc
            set merger_configure_env(${my_arch})  LD=${configure.cc}
        }
    } else {
        foreach my_arch {ppc ppc64} {
            set merger_host(${my_arch}) ""
            set merger_configure_args(${my_arch}) --force-target=generic-${os.platform}${os.major}-gcc
            set merger_configure_env(${my_arch})  LD=${configure.cc}
        }
    }

    if {"x86_64" in ${configure.universal_archs} || "i386" in ${configure.universal_archs}} {
        depends_build-append  port:yasm
        depends_skip_archcheck-append yasm

        # for Intel archs, allow runtime cpu detection
        lappend merger_configure_args(i386)   --enable-runtime-cpu-detect
        lappend merger_configure_args(x86_64) --enable-runtime-cpu-detect

        # parses the assembly output by default even when emitting assembly
        if {[string match {*clang*} ${configure.compiler}]} {
            lappend merger_build_args(i386)   CFLAGS_S=-fno-integrated-as
            lappend merger_build_args(x86_64) CFLAGS_S=-fno-integrated-as
        }
    }
} else {
    # single-arch build
    if {${configure.build_arch} in [list ppc ppc64] && ![variant_isset altivec]} {
           configure.args-append --force-target=generic-${os.platform}${os.major}-gcc
    } else {
           configure.args-append --force-target=${configure.build_arch}-${os.platform}${os.major}-gcc
    }
    # set LD
    configure.env-append  LD=${configure.cc}

    if {${configure.build_arch} in [list i386 x86_64]} {
        depends_build-append     port:yasm
        depends_skip_archcheck-append yasm

        configure.args-append    --enable-runtime-cpu-detect

        # parses the assembly output by default even when emitting assembly
        if {[string match {*clang*} ${configure.compiler}]} {
            build.args-append CFLAGS_S=-fno-integrated-as
        }
    }
}

# Shared library uses relative path in install name
# Override with absolute path
post-destroot {
    foreach f [glob ${destroot}${prefix}/lib/${my_name}.*.dylib] {
        system "install_name_tool -id [string map [list ${destroot} ""] ${f}] ${f}"
    }
    foreach f [glob ${destroot}${prefix}/bin/*] {
        system "install_name_tool -change \
        [lindex [split [glob ${destroot}${prefix}/lib/${my_name}.*.dylib] "/"] end] \
        [string map [list ${destroot} ""] [glob ${destroot}${prefix}/lib/${my_name}.*.dylib]] ${f}"
    }
}

if {${configure.build_arch} in [list ppc ppc64]} {
    variant altivec description "Build with Altivec support" {
        # Initial restore of altivec
        patchfiles-append \
                    0001-build-make-update.patch \
                    0002-vpx_dsp-update.patch \
                    0003-vp9-update.patch \
                    0004-vpx_ports-update.patch \
                    0005-vp8-update.patch
        # Actually fixes building
        patchfiles-append \
                    0006-ppc-fix-AltiVec-strict-vector-typing-errors-in-vpx_i.patch \
                    0007-ppc-rename-AltiVec-assembly-sources-from-.s-to-.S-so.patch \
                    0008-ppc-disable-vsx-RTCD-generation-for-the-darwin-PPC-A.patch \
                    0009-ppc-pass-arch-force_cpusubtype_ALL-to-as-1-for-the-d.patch \
                    0010-ppc-make-AltiVec-asm-PIC-safe-fixing-illegal-text-re.patch
        # Make it work
        patchfiles-append \
                    0011-ppc-fix-broken-unaligned-32-64-bit-load-and-store-in.patch \
                    0012-ppc-use-wraparound-not-saturating-add-sub-pack-in-ID.patch \
                    0013-ppc-rewrite-iadst4_vmx-as-a-direct-scalar-C-port-fix.patch \
                    0014-ppc-extend-saturating-wraparound-add-sub-pack-fix-to.patch \
                    0015-ppc-rewrite-iadst8_vmx-as-a-direct-scalar-C-port-fix.patch \
                    0016-ppc-rewrite-iadst16_8col-as-a-direct-scalar-C-port-f.patch \
                    0017-ppc-load-VP9-coefficients-at-correct-width-under-hig.patch
        # Implement missing stuff
        patchfiles-append \
                    0018-ppc-add-AltiVec-VP9-intra-predictors-for-G4-G5.patch \
                    0019-ppc-add-AltiVec-VP9-convolve8-subpel-MC-for-G4-G5.patch \
                    0020-ppc-wire-up-the-AltiVec-VP8-sixtap-bilinear-predicto.patch

        platform darwin {
            configure.cflags-append \
                    -force_cpusubtype_ALL
        }
    }

    default_variants-append +altivec
}

variant tests description "Enable testing" {
    configure.args-replace \
                    --disable-unit-tests \
                    --enable-unit-tests
    test.run    yes
    test.target test
}

livecheck.type  regex
livecheck.url   https://chromium.googlesource.com/webm/${my_name}
livecheck.regex ">v(\\d+(?:\\.\\d+)*)<"
