# $Id$

PortSystem              1.0
PortGroup               compiler_blacklist_versions 1.0

name                    libcxx
version                 3.7.1
epoch                   1
categories              lang
platforms               darwin
license                 MIT NCSA
maintainers             jeremyhu
description             libc++ is a new implementation of the C++ standard library with support for C++11 and portions of C++14.
long_description        ${description} \
                        Because objects cannot be passed between different versions of the C++ runtime, this port must \
                        replace the host version in order to be used.  On Snow Leopard and earlier, this is done \
                        automatically because there is no existing host version of this library.  On Lion and later, \
                        users must build the port with +replacemnt_libcxx and install the root manually if they wish \
                        to replace the existing host implementation.

homepage                http://libcxx.llvm.org/

master_sites            http://www.llvm.org/releases/${version}/
dist_subdir             llvm

use_xz                  yes
distname                ${name}-${version}.src

checksums               rmd160  b63d75f0547eb17e3f3f4df0875dfe1d0c32525d \
                        sha256  357fbd4288ce99733ba06ae2bec6f503413d258aeebaab8b6a791201e6f7f144

variant universal {}
default_variants +universal
use_configure no

set roots_path ${prefix}/var/system_roots/
set root_name  ${name}-${version}-${revision}

platform darwin {
    if {${os.major} < 9} {
        pre-fetch {
            ui_error "${name} is not supported on Tiger or earlier."
            error "unsupported platform"
        }
    }

    if {${os.major} < 11} {
        post-activate {
            system "cd / && tar xzf ${roots_path}/${root_name}.tgz"
        }
    }
}

variant replacemnt_libcxx description {EXPERTS ONLY: Build a replacement libcxxabi and libcxx even if it is already part of the base OS.} {}

if {${os.major} < 11 || [variant_isset replacemnt_libcxx]} {
    compiler.blacklist *gcc* {clang < 300}

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

    depends_lib port:libcxxabi

    supported_archs i386 x86_64 ppc

    if {[variant_isset universal] && [lsearch ${universal_archs} ppc] != -1} {
        # http://trac.macports.org/wiki/LibcxxOnOlderSystems#Leopardppc
        compiler.whitelist macports-clang-3.6
    }

    patch.pre_args -p1
    patchfiles-append \
        0001-buildit-build-fix-for-Leopard.patch \
        0002-buildit-Set-compatibility-version-to-RC_ProjectSourc.patch \
        0003-Fix-local-and-iterator-when-building-with-Lion-and-n.patch \
        0004-implement-atomic-using-mutex-lock_guard-for-64b-ops-.patch \
        0005-string-Fix-exception-declaration.patch

    build.dir ${worksrcpath}/lib
    build.cmd ./buildit
    build.env-append \
        CC="${configure.cc}" \
        CXX="${configure.cxx}" \
        RC_XBS=1 \
        RC_ProjectSourceVersion="${version}" \
        RC_CFLAGS="[get_canonical_archflags]" \
        TRIPLE="-apple-darwin${os.major}"

    # We want to use the host's libc++ everywhere because we want to ensure
    # that only *ONE* copy of this library is in any process's address space.
    # On Snow Leopard and earlier, this port provides this library in /usr for
    # binary compatibility with Lion and later.
    #
    # On newer OS version, this port does not install the content to /usr and
    # just leaves it to expert users who may want to replace their system
    # runtime.

    destroot {
        xinstall -m 755 -d ${destroot}${roots_path}/${root_name}/usr/lib
        xinstall -m 755 ${worksrcpath}/lib/libc++.1.dylib ${destroot}${roots_path}/${root_name}/usr/lib
        ln -s libc++.1.dylib ${destroot}${roots_path}/${root_name}/usr/lib/libc++.dylib

        system "cd ${destroot}${roots_path}/${root_name} && tar czf ../${root_name}.tgz ."
        file delete -force ${destroot}${roots_path}/${root_name}
    }
} else {
    distfiles
    fetch {}
    build {}
    destroot {
        xinstall -d ${destroot}${prefix}/share/doc/${name}
        system "echo ${name} is an empty port on this OS version because the functionality is already provided by the OS. > ${destroot}${prefix}/share/doc/${name}/README.txt"
    }
}

livecheck.type none