# -*- 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

name                macports-libcxx
categories          lang
platforms           darwin
maintainers         nomaintainer
license             NCSA
homepage            https://libcxx.llvm.org

description         provides a newer libc++ from llvm for older systems
long_description    This port installs a recent libc++ from llvm \
                    to use on older systems instead of the system libc++.

# for now, we will leverage the already-built libc++ in the appropriate clang port
# later, we can build this independently if we choose to do so, much like libtapi

# the clang-11 version in use when this port is updated will be used
version             11.1.0
set clangversion    11
revision            0

depends_build       port:clang-${clangversion}

# these cannot match or base will add a depends.skip_archcheck on the needed clang
# version and universal builds won't work
# this port builds by copying files from an installed clang compiler
# if it so happens that the default configure.compiler is the same as the
# clang compiler to be used during the installation, macports-base will have
# added a depends.skip_archcheck on the designated compiler, and so a universal
# variant will be ignored.
# see: https://github.com/macports/macports-ports/pull/23230
# see: https://trac.macports.org/ticket/69189

if {[string match ${configure.compiler} "macports-clang-${clangversion}"]} {
    configure.compiler  cc
}

master_sites
distfiles
fetch {}
checksum {}
use_configure no
build {}
variant universal {}

destroot {

    xinstall -d ${destroot}${prefix}/include/libcxx
    copy  ${prefix}/libexec/llvm-${clangversion}/lib/c++/v1 ${destroot}${prefix}/include/libcxx/

    # disable Apple libc++ availability tests, as we're using a new libc++ with these headers
    system -W ${destroot}${prefix}/include/libcxx/v1 "patch -p0 < ${filespath}/patch-disable-availabilty.diff"

    xinstall -d ${destroot}${prefix}/lib/libcxx
    copy  ${prefix}/libexec/llvm-${clangversion}/lib/libc++.1.0.dylib ${destroot}${prefix}/lib/libcxx/libc++.1.0.dylib
    system -W  ${destroot}${prefix}/lib/libcxx/  "install_name_tool -id ${prefix}/lib/libcxx/libc++.1.0.dylib libc++.1.0.dylib"
    system -W  ${destroot}${prefix}/lib/libcxx/  "install_name_tool -delete_rpath @loader_path/../lib libc++.1.0.dylib"
    system -W  ${destroot}${prefix}/lib/libcxx/  "install_name_tool -change @rpath/libc++abi.1.dylib ${prefix}/lib/libcxx/libc++abi.1.dylib libc++.1.0.dylib"
    system -W  ${destroot}${prefix}/lib/libcxx/  "ln -s libc++.1.0.dylib libc++.1.dylib"
    system -W  ${destroot}${prefix}/lib/libcxx/  "ln -s libc++.1.dylib libc++.dylib"

    copy  ${prefix}/libexec/llvm-${clangversion}/lib/libc++abi.1.0.dylib ${destroot}${prefix}/lib/libcxx/libc++abi.1.0.dylib
    system -W  ${destroot}${prefix}/lib/libcxx/  "install_name_tool -id ${prefix}/lib/libcxx/libc++abi.1.0.dylib libc++abi.1.0.dylib"
    system -W  ${destroot}${prefix}/lib/libcxx/  "install_name_tool -delete_rpath @loader_path/../lib libc++abi.1.0.dylib"
    system -W  ${destroot}${prefix}/lib/libcxx/  "ln -s libc++abi.1.0.dylib libc++abi.1.dylib"
    system -W  ${destroot}${prefix}/lib/libcxx/  "ln -s libc++abi.1.dylib libc++abi.dylib"

    copy  ${prefix}/libexec/llvm-${clangversion}/lib/libc++.a ${destroot}${prefix}/lib/libcxx/libc++.a
    copy  ${prefix}/libexec/llvm-${clangversion}/lib/libc++abi.a ${destroot}${prefix}/lib/libcxx/libc++abi.a
    copy  ${prefix}/libexec/llvm-${clangversion}/lib/libc++experimental.a ${destroot}${prefix}/lib/libcxx/libc++experimental.a

}

notes "
To enable a newer libc++, use a recent compiler and the legacysupport 1.1 PortGroup:

legacysupport.use_mp_libcxx yes

"