# -*- 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               active_variants 1.1
PortGroup               cmake 1.1

name                    libtapi
conflicts               ${name}-devel
version                 1000.10.8
revision                2
epoch                   1
categories              devel
maintainers             {@barracuda156 gmail.com:vital.had}

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.
platforms               {darwin < 11}

homepage                https://github.com/apple-oss-distributions/tapi

master_sites            ${homepage}/archive/refs/tags/:tapi
distfiles               tapi-${version}.tar.gz:tapi
checksums               tapi-${version}.tar.gz \
                        rmd160  65fc09883a1719ddb83ebc2daa411fd258c5e70a \
                        sha256  e6c7eaf5552a1d1aa367876467f2acce56a7e290f942f7c690ae2ec7b1f90664 \
                        size    204877

set llvm_branch         7.1
set llvm_upstream_v     ${llvm_branch}.0
set llvm_v              ${llvm_branch}.1
set llvm_hash           caa99b536bc6193a22b0527c23d3d49e32b36738
master_sites-append     https://github.com/iains/LLVM-7-branch/archive/${llvm_hash}/:llvm
distfiles-append        LLVM-7-branch-${llvm_hash}.tar.gz:llvm
checksums-append        LLVM-7-branch-${llvm_hash}.tar.gz \
                        rmd160  142b590195f30a24613b752c034b0e7fab894588 \
                        sha256  7090172795738e279c084c03af5e23a837dbda2c1099b1f11f10d2c6ee7f5ccb \
                        size    105414804

configure.cc            ${prefix}/libexec/gcc10-bootstrap/bin/gcc
configure.cxx           ${prefix}/libexec/gcc10-bootstrap/bin/g++
configure.compiler.add_deps no

# Use cmake-bootstrap
depends_build-replace   path:bin/cmake:cmake port:cmake-bootstrap
depends_build-append    port:cctools \
                        port:ld64-97 \
                        port:python27-bootstrap
depends_skip_archcheck-append \
                        cmake-bootstrap python27-bootstrap
configure.cmd           ${prefix}/libexec/cmake-bootstrap/bin/cmake

require_active_variants ld64    ld64_97
require_active_variants ld64-97 llvmppc
require_active_variants cctools llas

depends_lib-append      port:gcc10-bootstrap \
                        port:llvm-powerpc

compiler.cxx_standard   2011

cmake.build_type        Release

compiler.blacklist      *clang* *gcc-4.0 *gcc-5

worksrcdir              tapi_build

post-extract {
    move ${workpath}/LLVM-7-branch-${llvm_hash} ${workpath}/${worksrcdir}
    move ${workpath}/tapi-tapi-${version} ${worksrcpath}/tapi
    delete ${worksrcpath}/clang/tools/tapi
    ln -s ../../tapi ${worksrcpath}/clang/tools/tapi
}

patchfiles-append       0001-Do-not-hardcode-Xcode-ld.patch

# patches from https://github.com/iains/tapi
# and from https://github.com/tpoechtrager/apple-libtapi
# add zippered support for MacOSX10.14.sdk and above
patchfiles-append       0002-tapi-zippered-support-poechtrager.diff

# fix tapi build for llvm version 7 (and newer?)
patchfiles-append       0003-tapi-llvm7-build-fixes.diff

# add support for PPC architectures
patchfiles-append       0004-tapi-add-PPC-support.diff

# fixes to tests
patchfiles-append       0005-tapi-test-fixes.diff

configure.args-append   -DCMAKE_LIBTOOL=${prefix}/bin/libtool \
                        -DLD64_EXECUTABLE=${prefix}/bin/ld \
                        -DLLVM_ENABLE_ASSERTIONS=OFF \
                        -DLLVM_ENABLE_BACKTRACES=OFF \
                        -DLLVM_ENABLE_LTO=ON \
                        -DLLVM_ENABLE_PROJECTS="tapi\;clang" \
                        -DLLVM_INCLUDE_TESTS=OFF \
                        -DLLVM_INCLUDE_EXAMPLES=OFF \
                        -DPYTHON_EXECUTABLE=${prefix}/libexec/python27-bootstrap/bin/python2.7 \
                        -DTAPI_VENDOR="MacOS PowerPC"

# remove need for port:libxml2 dependency
configure.args-append   -DLIBXML2_LIBRARIES=IGNORE \
                        -DLLVM_ENABLE_LIBXML2=OFF

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

# avoid opportunistic linking libedit
configure.args-append   -DLLVM_ENABLE_LIBEDIT=OFF

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

configure.post_args     ${worksrcpath}/llvm

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

build.env               DYLD_LIBRARY_PATH=${prefix}/libexec/llvm-$llvm_v/lib/

build.target            libtapi tapi

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

variant tests description "Enable building of test code" {
    test.run                yes
    configure.args-replace  -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_TESTS=ON

    # tests work on 10.13 - not tested 10.10 - 10.12 as yet

    # there is a conflict between modules and MacPorts' ncurses port: #59992
    # temporarily disabling the ncurses port to run the tests works
    # removing the test that includes modules also works and requires no intervention
    post-patch {
        reinplace "s|add_subdirectory(Mod)||g" ${worksrcpath}/tapi/test/Inputs/CMakeLists.txt
        foreach dir {test unittests} {
            fs-traverse path ${worksrcpath}/tapi/${dir} {
                if {[file isfile ${path}] && [string match {*CMakeLists.txt} ${path}]} {
                    reinplace "s|-arch i386||g" ${path}
                }
            }
        }
    }

    # tests did not work on 10.6.8, first due to missing architecture defines, then
    # due to other errors. Help wanted to fix these tests if possible.
    test.target         check-tapi
}

livecheck.type          regex
livecheck.regex         "tapi-(\[\\d.\]+)"
