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

# needed for strnlen
legacysupport.newest_darwin_requires_legacy 10

github.setup            tpoechtrager apple-libtapi 593d29141bf176d24021208c75af54a2ef23c38b
name                    libtapi-devel
conflicts               libtapi
set libtapi_version     1600.0.11.8
version                 ${libtapi_version}-20260405
revision                0
categories              devel
maintainers             {@barracuda156 macos-powerpc.org:barracuda}

license                 APSL-2
description             ${name} adds ld64 linker support for text-based API libraries
long_description        {*}${description}. Newer MacOS SDKs contain only these TAPI stubs.

homepage                https://opensource.apple.com/source/tapi

checksums               rmd160  08ebd1f21f088edb81b203aa4ec5f57fd20ffaec \
                        sha256  dbad2a41f2351b052367db1fe251bebe43ee9ca5815e46b725c4bc71c339186f \
                        size    45944359
github.tarball_from     archive

patch.pre_args-replace  -p0 -p1
patchfiles-append       0001-tapi-Infer-macOS-platform-for-load-command-less-ppc-.patch \
                        0002-tapi-Don-t-drop-classic-symtab-exports-absent-from-t.patch \
                        0003-tapi-Add-ppc-ppc64-to-KNOWN_ARCHS-so-the-build-confi.patch \
                        0004-llvm-Don-t-require-pthread-qos.h-on-pre-10.10-Darwin.patch \
                        0005-clang-Restore-Darwin-PowerPC-target-info-from-iains-.patch \
                        0006-Do-not-hardcode-Xcode-ld.patch \
                        0007-fix-llvm-HashMappedTrie.cpp.diff \
                        0008-fix-llvm-Host.cpp.diff \
                        0009-tapi-CMakeLists.diff

cmake.generator         Ninja

set python_branch       3.14
set python_version      [string map {. {}} ${python_branch}]

depends_build-append    port:cctools \
                        port:ld64 \
                        port:python${python_version}

depends_skip_archcheck-append cctools

configure.args-append   -DCMAKE_LIBTOOL=${prefix}/bin/libtool

configure.args-append   -DPYTHON_EXECUTABLE=${prefix}/bin/python${python_branch}

# Remove need for port:libxml2 dependency
configure.args-append   -DLIBXML2_LIBRARIES=IGNORE

# Avoid opportunistic linking ncuses
configure.args-append   -DLLVM_ENABLE_TERMINFO=OFF

# Avoid opportunistic linking zlib
configure.args-append   -DLLVM_ENABLE_ZLIB=OFF

compiler.cxx_standard   2014
cmake.build_type        Release

# Needs support for llvm::Expect, which requires c++17 inline variables (clang 3.9+)
compiler.blacklist-append   {clang < 801} {macports-clang-3.[3-8]}

# Needs gcc8 or later to compile
# $SRC_DIR/src/llvm/projects/libtapi/lib/Core/APIJSONSerializer.cpp:1202:10: error: could not convert
# 'result' from 'tapi::internal::API' to 'llvm::Expected<tapi::internal::API>'    return result;
compiler.blacklist-append   *gcc-5.* *gcc-6.* *gcc-7.*

if {[string match macports-clang-* ${configure.compiler}]} {
    # libtapi might be used as part of bootstrap process for clang, avoid dependency cycle
    # when MacPorts decided to use MacPorts clang by replacing compiler to clang-11-bootstrap.
    depends_build-append    port:clang-11-bootstrap
    depends_skip_archcheck-append \
                            clang-11-bootstrap
    configure.compiler.add_deps no

    configure.cc            ${prefix}/libexec/clang-11-bootstrap/bin/clang
    configure.cxx           ${prefix}/libexec/clang-11-bootstrap/bin/clang++

    configure.cxx_stdlib    libc++
    depends_lib-append      port:libcxx
}

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # Proxy for eliminating the dependency on native TLS
    # http://trac.macports.org/ticket/46887
    configure.args-append   -DLLVM_ENABLE_BACKTRACES=OFF

    # https://llvm.org/bugs/show_bug.cgi?id=25680
    configure.cxxflags-append \
                            -U__STRICT_ANSI__
}

if {${configure.build_arch} in [list ppc ppc64]} {
    configure.args-append   -DLLVM_TARGETS_TO_BUILD="PowerPC" \
                            -DTAPI_SUPPORTED_ARCHS="ppc\;ppc64"
}

configure.post_args     ${worksrcpath}/src/llvm

# needed for libtapi to find some required files
configure.cxxflags-append \
                        -I${worksrcpath}/src/llvm/projects/clang/include \
                        -I${cmake.build_dir}/projects/clang/include

configure.args-append   -DLLVM_ENABLE_LTO=OFF \
                        -DLLVM_ENABLE_PROJECTS="tapi\;clang" \
                        -DLLVM_INCLUDE_TESTS=OFF \
                        -DTAPI_INCLUDE_DOCS=ON \
                        -DTAPI_FULL_VERSION=${libtapi_version} \
                        -DTAPI_REPOSITORY_STRING=${libtapi_version}

build.target            libtapi \
                        tapi \
                        tapi-docs \
                        tapi-headers

destroot.target         install-libtapi \
                        install-tapi \
                        install-tapi-docs \
                        install-tapi-headers

variant lto description "Enable LTO" {
    configure.args-replace \
                        -DLLVM_ENABLE_LTO=OFF -DLLVM_ENABLE_LTO=ON
}

# The default ld64-97/127 linker cannot build this with LTO at present in this configuration.
# If we could test the linker version, we might be able to enable it,
# if a newer linker than 127 is installed – not sure where the cut-off is.
if {(${os.platform} ne "darwin" || ${os.major} > 10) && [string match *clang* ${configure.compiler}]} {
    default_variants-append +lto
    # libtool will error on llvm bitcode generated by a clang version
    # newer than the libLTO.dylib cctools is built against.
    # Making sure cctools is built against the newest llvm in use prevents this,
    # but we can't always guarantee that will be the case.
    # We can instead set DYLD_LIBRARY_PATH to the libLTO.dylib that matches the building clang,
    # then libtool will use that libLTO.dylib instead, which matches the clang objects.
    set clangversion        [lindex [split ${configure.compiler} -] 2]
    build.env               DYLD_LIBRARY_PATH=${prefix}/libexec/llvm-$clangversion/lib/
}

github.livecheck.branch ${version}
