# -*- 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           bitbucket 1.0
PortGroup           cmake 1.1
PortGroup           muniversal 1.0
PortGroup           xcode_workaround 1.0

# https://trac.macports.org/ticket/59875
PortGroup           legacysupport 1.1

legacysupport.newest_darwin_requires_legacy 9

name                x265
bitbucket.setup     multicoreware x265_git 4.1
revision            0
checksums           rmd160  15133609f4b6226ef2679fcf40d9ea6297963cc9 \
                    sha256  a31699c6a89806b74b0151e5e6a7df65de4b49050482fe5ebf8a4379d7af8f29 \
                    size    1725279

categories          multimedia
license             GPL-2+
maintainers         nomaintainer

description         H.265 encoder
long_description    x265 is a free software library and application for \
                    encoding video streams into the H.265/MPEG-H HEVC \
                    compression format.

homepage            https://www.videolan.org/developers/x265.html
bitbucket.tarball_from \
                    downloads

distname            ${name}_${version}
worksrcdir          ${worksrcdir}/source

# allow overriding system processor detection
patchfiles          patch-cmakelists-override-processor.diff

# Altivec uses instructions unsupported on Darwin: https://trac.macports.org/ticket/64781
patchfiles-append   patch-ppc.diff

depends_build-append \
                    port:gmake

# x265.pc and the shared library are not installed if git is not found.
# https://bitbucket.org/multicoreware/x265_git/issues/960
depends_build-append \
                    bin:git:git

compiler.blacklist-append *llvm-gcc-4.2

# https://trac.macports.org/ticket/59246
xcode_workaround.fixed_xcode_version 11.2

post-destroot {
    # Sanity check.
    # https://bitbucket.org/multicoreware/x265_git/issues/960
    if {![file exists ${destroot}${prefix}/lib/pkgconfig/${name}.pc]} {
        return -code error "${name}.pc did not get installed"
    }
}

platform darwin 10 {
    # Rosetta build has to override CMAKE_SYSTEM_PROCESSOR: https://trac.macports.org/ticket/64528
    if {${build_arch} eq "ppc"} {
        configure.args-append -DOVERRIDE_SYSTEM_PROCESSOR="ppc"
    }
}

if {${universal_possible} && [variant_isset universal] && ![variant_isset highdepth]} {
    if {"x86_64" in ${configure.universal_archs} || "i386" in ${configure.universal_archs}} {
        depends_build-append port:nasm
        set merger_configure_env(i386)      NASM_EXECUTABLE=${prefix}/bin/nasm
        set merger_configure_env(x86_64)    NASM_EXECUTABLE=${prefix}/bin/nasm
    }

    # error: illegal text-relocation to '_x265_pw_1'
    lappend merger_configure_ldflags(i386) -Wl,-read_only_relocs,suppress

    foreach uarch {arm64 i386 ppc ppc64 x86_64} {
        set merger_configure_args(${uarch}) -DOVERRIDE_SYSTEM_PROCESSOR=${uarch}
    }
} else {
    if {${configure.build_arch} in {i386 x86_64}} {
        depends_build-append port:nasm
        configure.env       NASM_EXECUTABLE=${prefix}/bin/nasm
    }

    if {${configure.build_arch} eq "i386"} {
        # error: illegal text-relocation to '_x265_pw_1'
        configure.ldflags-append -Wl,-read_only_relocs,suppress
    }
}

variant highdepth conflicts universal description {Enable 10-bit and 12-bit encoding} {
    # this builds the recommended multi-library interface
    # https://x265.readthedocs.io/en/master/api.html#multi-library-interface
    # with the default ABI being the standard 8bit ABI as always

    # we can perhaps explore how to use the method below with the muniversal PG

    pre-configure {
        file mkdir ${workpath}/10bit ${workpath}/12bit
    }

    configure {
        set configure.dir ${workpath}/10bit
        configure.args -DHIGH_BIT_DEPTH=ON -DEXPORT_C_API=OFF -DENABLE_SHARED=OFF -DENABLE_CLI=OFF
        portconfigure::configure_main

        set configure.dir ${workpath}/12bit
        configure.args -DHIGH_BIT_DEPTH=ON -DEXPORT_C_API=OFF -DENABLE_SHARED=OFF -DENABLE_CLI=OFF -DMAIN12=ON
        portconfigure::configure_main

        set configure.dir ${workpath}/build
        configure.args -DEXTRA_LIB="x265_main10.a\;x265_main12.a" -DEXTRA_LINK_FLAGS=-L. -DLINKED_10BIT=ON -DLINKED_12BIT=ON
        portconfigure::configure_main
    }

    build {
        set build.dir ${workpath}/10bit
        portbuild::build_main

        set build.dir ${workpath}/12bit
        portbuild::build_main

        ln ${workpath}/10bit/libx265.a ${workpath}/build/libx265_main10.a
        ln ${workpath}/12bit/libx265.a ${workpath}/build/libx265_main12.a

        set build.dir ${workpath}/build
        portbuild::build_main

        move ${workpath}/build/libx265.a ${workpath}/build/libx265_main.a
        system -W ${workpath}/build "libtool -static -o libx265.a libx265_main.a libx265_main10.a libx265_main12.a"
    }
}
