PortSystem              1.0
PortGroup               compiler_blacklist_versions 1.0

name                    libunwind
version                 5.0.1
subport ${name}-headers {}
epoch                   1
categories              devel
platforms               darwin
license                 MIT NCSA
maintainers             {jeremyhu @jeremyhu} openmaintainer
description             A version of Apple's libunwind library that is included in libSystem
long_description        Apple's libunwind library (part of libSystem) was released as OSS and is \
                        now stewarded by the LLVM Project.

homepage                http://blog.llvm.org/2013/10/new-libunwind-implementation-in-libcabi.html

master_sites            http://www.llvm.org/releases/${version}/
dist_subdir             llvm
 
use_xz                  yes
distname                ${name}-${version}.src

checksums               rmd160  26d3dac149e2fd355e1b8b367a9bf61e01c38fbf \
                        sha256  6bbfbf6679435b858bd74bdf080386d084a76dfbf233fb6e47b2c28e0872d0fe

use_configure           no

patch.pre_args -p1
patchfiles \
    0001-libunwind-Add-missing-include-of-libunwind_ext.h-to-.patch \
    0002-libunwind-Use-__builtin_trap-rather-than-__builtin_u.patch \
    0003-libunwind-MacPorts-availability.patch \
    0004-MacPorts-Makefile.patch \
    clang-3.4-workaround.patch \
    c89.patch

post-patch {
    if {${os.major} < 9} {
        foreach reg {cr ctr eax ebp ebx ecx edi edx eip esi esp lr mq r0 r1 r10 r11 r12 r13 r14 r15 r16 r17 r18 r19 r2 r20 r21 r22 r23 r24 r25 r26 r27 r28 r29 r3 r30 r31 r4 r5 r6 r7 r8 r9 rax rbp rbx rcx rdi rdx rip rsi rsp srr0 srr1 vrsave xer} {
            reinplace "s|__${reg}|${reg}|g" ${build.dir}/Registers.hpp
        }
    }
}

build.dir       ${worksrcpath}/src
destroot.dir    ${build.dir}

if {${subport} == "${name}-headers"} {
    revision 0

    supported_archs noarch

    build {}

    destroot.target installhdrs
    destroot.args \
        PREFIX="${prefix}"
} else {
    revision 0

    # Technically not needed, but subports will expect depending on libunwind to pull in the headers
    depends_lib-append port:libunwind-headers

    # clang 3.5 and newer are blacklisted to prevent dependency cycles
    foreach ver {3.5 3.6 3.7 3.8 3.9 4.0 5.0 devel} {
        if {![file exists ${prefix}/bin/clang-mp-${ver}]} {
            compiler.blacklist-append macports-clang-${ver}
        }
    }

    # https://trac.macports.org/ticket/49779#comment:3
    compiler.blacklist-append {clang < 100} *gcc-4.\[0123456\]

    variant universal   {}
    default_variants +universal

    set cxx_stdlibflags {}
    if {[string match *clang* ${configure.cxx}]} {
        set cxx_stdlibflags -stdlib=${configure.cxx_stdlib}
    }

    set asflags {}
    if {[variant_isset universal] && [lsearch ${universal_archs} ppc] != -1} {
        if {[string match *clang* ${configure.cc}]} {
            set asflags "-fno-integrated-as"
        }
    }

    if {${os.major} < 11} {
        # Snow Leopard's libSystem's libmacho does not contain getsectiondata()

        depends_build-append port:libmacho
        configure.cppflags-append -DUSE_MACPORTS_LIBUNWIND
        configure.ldflags-append -lmacho
    }

    configure.cflags-append -std=gnu99

    build.args \
        PREFIX="${prefix}" \
        CC="${configure.cc}" \
        CXX="${configure.cxx}" \
        OTHER_CPPFLAGS="${configure.cppflags} -DNDEBUG" \
        OTHER_CFLAGS="${configure.cflags}" \
        OTHER_CXXFLAGS="${configure.cxxflags} ${cxx_stdlibflags}" \
        OTHER_ASFLAGS="${asflags}" \
        OTHER_LDFLAGS="${configure.ldflags} ${cxx_stdlibflags}" \
        RC_ARCHS="[get_canonical_archs]" \
        LIBUNWIND_CURRENT_VERSION=${version}

    if {[vercmp $xcodeversion 3.2] < 0 && [string match "*macports*" ${configure.compiler}]} {
        # Xcode 3.1.4 fails with load commands in the newer toolchain
        depends_build-append port:cctools

        build.args-append \
            AR=${prefix}/bin/ar \
            RANLIB=${prefix}/bin/ranlib
    }

    destroot.target installlibs
    destroot.args \
        PREFIX="${prefix}" \
        STRIP_ON_INSTALL="NO"
}

livecheck.type          none
