# -*- 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 b8c5ac40267aa5f6004dd38cc2b2cd84f2d9d555
github.tarball_from     archive
name                    libtapi
set libtapi_version     1300.6.5
version                 ${libtapi_version}-20231128
revision                0
categories              devel
maintainers             nomaintainer

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                http://opensource.apple.com/source/tapi/

checksums               rmd160  3d23582b49139c042574f3e46a81ae927eff9fe9 \
                        sha256  3b75098ee01571102e605c76b219075c607df0dd459e65fc04cb282fbe7c65cb \
                        size    38637843

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

depends_build-append    port:python${python_version}
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 zlib
configure.args-append   -DLLVM_ENABLE_ZLIB=OFF

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

compiler.cxx_standard   2017
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-3.* *gcc-4.* *gcc-5.* *gcc-6.* *gcc-7.*

if {[string match macports-clang-* ${configure.compiler}]} {
    depends_build-append port:cctools
    depends_skip_archcheck-append cctools
    configure.args-append   -DCMAKE_LIBTOOL=${prefix}/bin/libtool
}

if {(${os.platform} eq "darwin" && ${os.major} < 11) || [string match macports-gcc-* ${configure.compiler}]} {
    # the default ld64-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 cutoff is
    configure.args-append -DLLVM_ENABLE_LTO=OFF
} else {
    # 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/
}

# 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.
if {[string match macports-clang-* ${configure.compiler}]} {
    configure.compiler.add_deps no

    depends_build-append   port:clang-11-bootstrap
    depends_skip_archcheck-append \
                           clang-11-bootstrap

    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 \
                        -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_INCLUDE_TESTS=OFF \
                        -DTAPI_INCLUDE_DOCS=ON \
                        -DLLVM_ENABLE_PROJECTS="tapi\;clang" \
                        -DTAPI_REPOSITORY_STRING=${libtapi_version} \
                        -DTAPI_FULL_VERSION=${libtapi_version}

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

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

github.livecheck.branch ${version}
