# $Id$

PortSystem              1.0
PortGroup               compiler_blacklist_versions 1.0

name                    libunwind
version                 3.9.0
subport ${name}-headers {}
epoch                   1
categories              devel
platforms               darwin
license                 MIT NCSA
maintainers             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.  This version installs headers outside \
                        of the normal filesystem hierarchy in order to not accidentally conflict \
                        with the host implementation that other ports may expect.

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  0181a9c886032a9a24a35e4286d99bf64917eff6 \
                        sha256  66675ddec5ba0d36689757da6008cb2596ee1a9067f4f598d89ce5a3b43f4c2b

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-ppc-build-fix.patch \
    0004-libunwind-Only-include-Availability.h-on-Leopard-and.patch \
    0005-libunwind-MacPorts-availability.patch \
    0006-MacPorts-Makefile.patch \
    0007-libunwind-Fix-fallback-implementation-of-_dyld_find_.patch \
    0008-Define-__STDC_FORMAT_MACROS-to-bring-in-format-macro.patch \
    0009-config.h-Add-missing-include-of-stdint.h-for-uint64_.patch \
    clang-3.4-workaround.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"} {
    supported_archs noarch

    build {}

    destroot.target installhdrs
    destroot.args \
        PREFIX="${prefix}"
} else {
    # 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} {
        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}" \
        CPPFLAGS="${configure.cppflags} -DNDEBUG" \
        CFLAGS="${configure.cflags}" \
        CXXFLAGS="${configure.cxxflags} ${cxx_stdlibflags}" \
        ASFLAGS="${asflags}" \
        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