# -*- 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
PortGroup           xcode 1.0

name                libdispatch-legacy
version             84.5.5
revision            2
categories          devel
platforms           {darwin == 10}
# TODO: ppc64 is not yet supported in 10.6 SDK.
supported_archs     i386 ppc x86_64
license             Apache-2
maintainers         nomaintainer
description         Grand Central Dispatch for PowerPC systems and pre-Snow Leopard x86
long_description    Grand Central Dispatch (GCD or libdispatch) provides comprehensive support \
                    for concurrent code execution on multicore hardware. This port in meant \
                    for systems without a native libdispatch. It corresponds to macOS 10.6.8 release.
homepage            https://opensource.apple.com/releases
set apple_sources   https://github.com/apple-oss-distributions
master_sites-append ${apple_sources}/Libc/archive/refs/tags/:Libc \
                    ${apple_sources}/libclosure/archive/refs/tags/:libclosure \
                    ${apple_sources}/libdispatch/archive/refs/tags/:libdispatch \
                    ${apple_sources}/xnu/archive/refs/tags/:xnu

set LIBC            Libc-594.9.5
set LIBCLOSURE      libclosure-38
set LIBDISPATCH     libdispatch-${version}
set XNU             xnu-1504.15.3

distfiles           ${LIBDISPATCH}.tar.gz:libdispatch \
                    ${LIBC}.tar.gz:Libc \
                    ${LIBCLOSURE}.tar.gz:libclosure \
                    ${XNU}.tar.gz:xnu

checksums           ${LIBDISPATCH}.tar.gz \
                    rmd160  cd291e5bc6e65bc75995017f763fa2f69c5ed76c \
                    sha256  26e87bb08ccfc71b8d867d2732f154a56b8500d33311108ca5fff81fc9eaea3f \
                    size    171043 \
                    ${LIBC}.tar.gz \
                    rmd160  309ac37e61607088a9bab0f2f889de95c02e0342 \
                    sha256  71a60d57ff02b1125be6c689d9cfc66f4f76de85293068504ed63f881b8dbcf3 \
                    size    2128046 \
                    ${LIBCLOSURE}.tar.gz \
                    rmd160  37a0c0c1748d8f6e090b950ca8ed88fbe4da5d4a \
                    sha256  5739bdb251444acb7d2c7f7032763db6601de36a20c2ed55af63b56f093a257c \
                    size    139164 \
                    ${XNU}.tar.gz \
                    rmd160  438bef8a0c85b45e7b0e87fe455906bd64446285 \
                    sha256  05cc3e9617581f8f9bfa434901fb902761c89bbcf770d822e84a31dd625c2b58 \
                    size    8872451

extract {
    ui_msg "---> Extracting sources"
    foreach libsource [list ${LIBC} ${LIBCLOSURE} ${LIBDISPATCH} ${XNU}] {
        xinstall -d ${workpath}/${libsource}
        system -W ${workpath} "tar -xf ${distpath}/$libsource.tar.gz -C $libsource --strip-components 1"
    }
}

post-extract {
    ln -s ${workpath}/${XNU}/osfmk ${workpath}/${XNU}/libkern/System
}

patch.dir           ${workpath}
patchfiles-append   patch-project.pbxproj.diff
patchfiles-append   pthread_threadid_np-expose-on-PowerPC.diff

# This is not clear why __crashreporter_info__ does not work, it allegedly exists in 10.5+,
# but other libs may fail to link with libdispatch if this symbol is used:
# Undefined symbols: "___crashreporter_info__", referenced from:
# ___crashreporter_info__$non_lazy_ptr in libdispatch.a(semaphore.o)
# However, it is only used in a single instance for debugging info.
# We can verily live without that.
patchfiles-append   patch-no-crashreporter_info.diff
# The following symbol is not supported in libpthreads for powerpc.
# So we may get something like this:
# Undefined symbols: "_pthread_workqueue_attr_setovercommit_np", referenced from:
# __dispatch_root_queues_init in libdispatch.a(queue.o)
# FIXME: perhaps it can be borrowed or reimplemented.
# See: https://people.freebsd.org/~sson/thrworkq/pthread_workqueue_2009_12_14.diff
patchfiles-append   patch-pthread-overcommit.diff

worksrcdir          ${workpath}/${LIBDISPATCH}

compiler.blacklist  apple* macports*

# sterilize MacPorts build environment; we want nothing picked from MP prefix
compiler.cpath
compiler.library_path

configure.cxx_stdlib

configure.cflags
configure.cxxflags
configure.cppflags
configure.optflags
configure.ldflags

post-patch {
    reinplace "s|@LIBC@|${LIBC}|g" ${worksrcpath}/libdispatch.xcodeproj/project.pbxproj
    reinplace "s|@LIBCLOSURE@|${LIBCLOSURE}|g" ${worksrcpath}/libdispatch.xcodeproj/project.pbxproj
    reinplace "s|@XNU@|${XNU}|g" ${worksrcpath}/libdispatch.xcodeproj/project.pbxproj
}

xcode.target        libdispatch

use_parallel_build  no

# Preserve original directory structure, just prefix everything with ${prefix}/libexec/dispatch:
xcode.destroot.type
destroot.destdir    DSTROOT=${destroot}${prefix}/libexec/dispatch

# However, create a symlink, so that adding ${prefix}/libexec/dispatch/usr to PATH makes everything available:
post-destroot {
    ln -s ${prefix}/libexec/dispatch/usr/local/lib/system ${destroot}${prefix}/libexec/dispatch/usr/lib
}

default_variants    +universal
