# -*- 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           gitlab 1.0
PortGroup           muniversal 1.0
PortGroup           xcodeversion 1.0

gitlab.instance     https://code.videolan.org
# Get latest stable commit id from
# https://code.videolan.org/videolan/x264/-/tree/stable
# Get minor version (X264_BUILD) from
# https://code.videolan.org/videolan/x264/-/blob/stable/x264.h
# Run port livecheck to get patch version
# Change in minor version requires rev-bumping dependents
gitlab.setup        videolan x264 31e19f92f00c7003fa115047ce50978bc98c3a0d
version             0.164.3108
revision            0
epoch               1
categories          multimedia
license             GPL-2+
maintainers         {devans @dbevans} {makr @mohd-akram} openmaintainer
description         x264 - a free h264/avc encoder
long_description    x264 is a free library for encoding H264/AVC video streams. \
                    The code is written by Laurent Aimar, Eric Petit(OS X), Min \
                    Chen (vfw/nasm), Justin Clay(vfw), Måns Rullgård and Loren \
                    Merritt from scratch. It is released under the terms of the \
                    GPL license.
homepage            https://www.videolan.org/x264.html

checksums           rmd160  e006e270e1c87c70ddee225ebaf2f586ad8620f3 \
                    sha256  01a4acb74eea1118c3aa96c3e18ee3384bc1f2bc670f31fb0f63be853e4d9d08 \
                    size    778279

minimum_xcodeversions {9 3.1}

depends_build       port:nasm

patchfiles          altivec-x264.patch

set rev             [lindex [split ${version} .] 2]
set sha             [string range ${gitlab.version} 0 6]

post-patch {
    reinplace "s|ver=\"x\"|ver=\"$rev $sha\"|" \
        ${worksrcpath}/version.sh
    reinplace "s|version=\"\"|version=\" r$rev $sha\"|" \
        ${worksrcpath}/version.sh
}

configure.args      --enable-pic \
                    --enable-shared \
                    --enable-static \
                    --disable-avs \
                    --disable-swscale \
                    --disable-lavf \
                    --disable-ffms \
                    --disable-gpac \
                    --disable-lsmash

platform darwin powerpc {
    configure.args-append   --disable-vsx
}

# as of build 148 asm optimizations are believed to work with x86_64 Snow Leopard and up
# please file a ticket if you have evidence to the contrary
# i386 clang build issues remain which break i386 build when asm optimizations are enabled
# assert configure option --disable-asm for all i386 builds
# see https://mailman.videolan.org/pipermail/x264-devel/2014-February/010371.html, #42672.
if {${os.platform} eq "darwin" && ${configure.build_arch} ne "" && (!${universal_possible} || ![variant_isset universal])} {
    if {${configure.build_arch} eq "i386"} {
        configure.args-append   --host=i686-apple-darwin${os.major} \
                                --disable-asm
    } else {
        configure.args-append   --host=${configure.build_arch}-apple-darwin${os.major}
    }
    configure.ldflags-append {*}${configure.cc_archflags}
}

foreach arch ${configure.universal_archs} {
    if {${arch} eq "i386"} {
        set merger_configure_args(${arch}) "--host=i686-apple-darwin${os.major} --disable-asm"
    } else {
        set merger_configure_args(${arch}) --host=${arch}-apple-darwin${os.major}
    }
}

configure.universal_args-delete --disable-dependency-tracking

platform darwin 8 {
    # https://trac.macports.org/ticket/24893
    if {${configure.build_arch} eq "i386" && (!${universal_possible} || ![variant_isset universal])} {
        configure.cflags-append -msse2
    }
    set merger_configure_cflags(i386) -msse2

    depends_build-append port:gmake
    build.cmd            gmake
}

# sets its own optflags
configure.optflags

destroot.args       DIR_INSTALL=${destroot}${prefix}

build.target        default

# Can't livecheck stable branch but use it to validate version matches commit
livecheck.url       https://artifacts.videolan.org/x264/release-macos-arm64/
livecheck.type      regex
livecheck.version   $rev
livecheck.regex     "r(\[0-9]+)-$sha"
