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

PortSystem          1.0
PortGroup           github 1.0
PortGroup           muniversal 1.0
PortGroup           active_variants 1.1

github.setup        dlang phobos 2.071.0 v
categories          lang
platforms           darwin
license             Boost-1
maintainers         takeshi openmaintainer
description         Runtime library for the D programming language
long_description \
    Phobos is the standard runtime library that comes with the D language compiler.
homepage            http://dlang.org/

checksums           rmd160  a992eef87c674b6143cda1038a28053ff21dd9f8 \
                    sha256  b645de03dacaa192ccacffb2f8cc18ee419bf93084eb07c609feba8a0014153a

depends_lib         port:druntime

patchfiles          patch-gzlib.c.diff \
                    patch-posix.diff

use_configure       no

post-extract {
    xinstall -m 0644 ${filespath}/makefile_macports_install ${build.dir}
}

post-patch {
    reinplace "s|__MACPORTS_CFLAGS__|${configure.cflags}|g" \
        ${worksrcpath}/posix.mak
}

pre-configure {
    # dmd compiler can not handle universal files, so create thin versions of archive files from libdruntime.a
    if { ![variant_isset universal] } {
        set all_archs ${build_arch}
    } else {
        set all_archs ${universal_archs}
    }
    foreach arch ${all_archs} {
        file mkdir ${workpath}/druntime/${arch}
    }
    if {[active_variants port:druntime universal ""]} {
        # libdruntime.a is universal, so create a thin version
        foreach arch ${all_archs} {
            system \
                -W ${workpath}/druntime/${arch} \
                "/usr/bin/lipo -thin ${arch} ${prefix}/lib/libdruntime.a -output libdruntime.a"
        }
    } else {
        # libdruntime.a is NOT universal, so just copy it
        foreach arch ${all_archs} {
            file copy ${prefix}/lib/libdruntime.a ${workpath}/druntime/${arch}/libdruntime.a
        }
    }
}

pre-build {
    file mkdir ${workpath}/dmd
    system "echo ${version} > ${workpath}/dmd/VERSION"
}
build.args          -f posix.mak \
                    DRUNTIME_PATH=${prefix}/include/druntime \
                    CC=${configure.cc} \
                    DMD=${prefix}/bin/dmd
build.target        ""

if { ![variant_isset universal] } {
    if { ${build_arch} eq "x86_64" || ${build_arch} eq "ppc64" } {
        build.args-append MODEL=64 DRUNTIME=${workpath}/druntime/${build_arch}/libdruntime.a
    } else {
        build.args-append MODEL=32 DRUNTIME=${workpath}/druntime/${build_arch}/libdruntime.a
    }
} else {
    lappend merger_build_args(x86_64) MODEL=64 DRUNTIME=${workpath}/druntime/x86_64/libdruntime.a
    lappend merger_build_args(i386)   MODEL=32 DRUNTIME=${workpath}/druntime/i386/libdruntime.a
    lappend merger_build_args(ppc64)  MODEL=64 DRUNTIME=${workpath}/druntime/ppc64/libdruntime.a
    lappend merger_build_args(ppc)    MODEL=32 DRUNTIME=${workpath}/druntime/ppc/libdruntime.a
}

destroot.args \
    -f makefile_macports_install \
    PREFIX=${prefix}

livecheck.regex     archive/[join ${github.tag_prefix} ""](\[^"-\]+)${extract.suffix}