# -*- 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
version             11.1.0
revision            1

categories          lang
license             NCSA
maintainers         {mascguy @mascguy} openmaintainer
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++.
homepage            https://libcxx.llvm.org

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

master_sites
distfiles
use_configure no
build {}
variant universal {}

variant experimental description {Install LLVM 18-based libc++ (experimental)} {}

# Support for C++23 became stable only starting with Clang/LLVM version 18
# however, Snow Leopard is not compatible
# In that case, you can try the latest versions of GCC and libstdc++
# using this repository: https://github.com/macos-powerpc/powerpc-ports
if {${os.platform} eq "darwin" && ${os.major} > 10} {
    default_variants +experimental
}

# For the default case, where the user doesn't explicitly request +experimental,
# we do that for them. However, it's critical that the following block - which
# keys on whether that variant is enabled - not be run until after that.
set clangversion ""
set llvm_include_path ""
set llvm_libcxx_path ""
if {[variant_isset experimental]} {
    set clangversion 18
    set llvm_include_path include/c++/v1
    set llvm_libcxx_path lib/libc++
} else {
    # the clang-11 version in use when this port is updated will be used
    set clangversion 11
    set llvm_include_path lib/c++/v1
    set llvm_libcxx_path lib
}

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
}

set port_include      ${prefix}/include/libcxx
set port_lib          ${prefix}/lib/libcxx
set port_unwind       ${port_lib}/libunwind
set destroot_include  ${destroot}${port_include}
set destroot_lib      ${destroot}${port_lib}
set destroot_unwind   ${destroot}${port_unwind}
set llvm_base         ${prefix}/libexec/llvm-${clangversion}
set llvm_unwind       ${llvm_base}/lib/libunwind

destroot {
    xinstall -d ${destroot_include}
    copy ${llvm_base}/${llvm_include_path} \
         ${destroot_include}/

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

    xinstall -d ${destroot_lib}
    copy ${llvm_base}/${llvm_libcxx_path}/libc++.1.0.dylib \
         ${destroot_lib}/libc++.1.0.dylib
    system -W ${destroot_lib} \
         "install_name_tool -id ${port_lib}/libc++.1.0.dylib libc++.1.0.dylib"
    system -W ${destroot_lib} \
         "install_name_tool -change @rpath/libc++abi.1.dylib ${port_lib}/libc++abi.1.dylib libc++.1.0.dylib"
    system -W ${destroot_lib} \
         "ln -s libc++.1.0.dylib libc++.1.dylib"
    system -W ${destroot_lib} \
         "ln -s libc++.1.dylib libc++.dylib"

    copy ${llvm_base}/${llvm_libcxx_path}/libc++abi.1.0.dylib \
         ${destroot_lib}/libc++abi.1.0.dylib
    system -W ${destroot_lib} \
         "install_name_tool -id ${port_lib}/libc++abi.1.0.dylib libc++abi.1.0.dylib"
    system -W ${destroot_lib} \
         "ln -s libc++abi.1.0.dylib libc++abi.1.dylib"
    system -W ${destroot_lib} \
         "ln -s libc++abi.1.dylib libc++abi.dylib"

    copy ${llvm_base}/${llvm_libcxx_path}/libc++.a \
         ${destroot_lib}/libc++.a
    copy ${llvm_base}/${llvm_libcxx_path}/libc++abi.a \
         ${destroot_lib}/libc++abi.a
    copy ${llvm_base}/${llvm_libcxx_path}/libc++experimental.a \
         ${destroot_lib}/libc++experimental.a

}

# LLVM 18's libc++ may not have rpaths to delete
if {![variant_isset experimental]} {
    post-destroot {
        system -W ${destroot_lib} \
            "install_name_tool -delete_rpath @loader_path/../lib libc++.1.0.dylib"
        system -W ${destroot_lib} \
            "install_name_tool -delete_rpath @loader_path/../lib libc++abi.1.0.dylib"
    }
}

# libc++ from LLVM 18+ uses its own libunwind library
if {[variant_isset experimental]} {
    post-destroot {
        xinstall -d ${destroot_unwind}
        copy ${llvm_unwind}/libunwind.1.0.dylib \
             ${destroot_unwind}/libunwind.1.0.dylib
        system -W ${destroot_unwind} \
             "install_name_tool -id ${port_unwind}/libunwind.1.0.dylib libunwind.1.0.dylib"
        system -W ${destroot_lib} \
             "install_name_tool -change @rpath/libunwind.1.dylib ${port_unwind}/libunwind.1.dylib libc++.1.0.dylib"
        system -W ${destroot_lib} \
             "install_name_tool -change @rpath/libunwind.1.dylib ${port_unwind}/libunwind.1.dylib libc++abi.1.0.dylib"
        system -W ${destroot_unwind} \
             "install_name_tool -change @rpath/libunwind.1.dylib ${port_unwind}/libunwind.1.dylib libunwind.1.0.dylib"
        system -W ${destroot_unwind} \
             "ln -s libunwind.1.0.dylib libunwind.1.dylib"
        system -W ${destroot_unwind} \
             "ln -s libunwind.1.dylib libunwind.dylib"

        copy ${llvm_unwind}/libunwind.a \
             ${destroot_unwind}/libunwind.a
    }
}

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

legacysupport.use_mp_libcxx yes

"
