# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 66588 2010-04-17 14:08:41Z snc@macports.org $

PortSystem      1.0

name            boost
version         1.42.0
license         Boost-1.0
categories      devel
platforms       darwin
maintainers     nox openmaintainer
description     Collection of portable C++ source libraries

long_description \
    Boost provides free portable peer-reviewed C++ \
    libraries. The emphasis is on portable libraries \
    which work well with the C++ Standard Library.

homepage        http://www.boost.org
master_sites    sourceforge
distname        ${name}_[join [split ${version} .] _]
use_bzip2       yes

checksums       md5     7bf3b4eb841b62ffb0ade2b82218ebe6 \
                sha1    3ce3bb1a1bed504635287017f9e628597b409e7b \
                rmd160  90cea6aaa35f22b97e6eb3f5879ca9fe21098349

patchfiles      patch-libs-random-random_device.cpp.diff

post-extract {
    file mkdir ${worksrcpath}/libs/random/build
    copy ${filespath}/libs-random-build-Jamfile.v2 ${worksrcpath}/libs/random/build/Jamfile.v2
}

depends_build   path:bin/bjam:boost-jam

depends_lib     port:zlib \
                port:expat \
                port:bzip2

configure {
    reinplace -E "s|-install_name \"|&${prefix}/lib/|" \
        ${worksrcpath}/tools/build/v2/tools/darwin.jam
    write_jam "using darwin : : ${configure.cxx} ;"
}

build.cmd       bjam
build.target
build.args      -d2 \
                --layout=tagged \
                --debug-configuration \
                --ignore-site-config \
                --user-config=user-config.jam \
                --without-python \
                --without-mpi \
                -sBZIP2_INCLUDE=${prefix}/include \
                -sBZIP2_LIBPATH=${prefix}/lib \
                -sEXPAT_INCLUDE=${prefix}/include \
                -sEXPAT_LIBPATH=${prefix}/lib \
                -sZLIB_INCLUDE=${prefix}/include \
                -sZLIB_LIBPATH=${prefix}/lib \
                threading=single,multi \
                variant=debug,release

use_parallel_build  no

destroot.cmd        bjam
destroot.destdir    --prefix=${destroot}${prefix}

pre-destroot {
    eval destroot.args  ${build.args}
}

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    set l [expr [string length ${worksrcpath}] + 1]
    fs-traverse f [glob -directory ${worksrcpath} *] {
        set dest ${destroot}${docdir}/[string range ${f} ${l} end]
        if {[file isdirectory ${f}]} {
            if {[file tail ${f}] eq "example"} {
                copy ${f} ${dest}
                continue
            }
            xinstall -d ${dest}
        } elseif {[lsearch -exact {css htm html png svg} [string range [file extension ${f}] 1 end]] != -1} {
            xinstall -m 644 ${f} ${dest}
        }
    }
}

set pythons_suffixes {24 25 26}

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

proc python_dir {} {
    global pythons_suffixes
    foreach s ${pythons_suffixes} {
        if {[variant_isset python${s}]} {
            set p python[string index ${s} 0].[string index ${s} 1]
            return [file normalize [exec ${p} -c "import sys; print sys.prefix"]/lib/${p}/site-packages]
        }
    }
    error "Python support not enabled."
}

proc write_jam s {
    global worksrcpath
    set config [open ${worksrcpath}/user-config.jam a]
    puts ${config} ${s}
    close ${config}
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string index ${s} 1]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    eval [subst {
        variant ${p} description "Build Boost.Python for Python ${v}" conflicts ${c} {
            patchfiles-append   patch-tools-build-v2-tools-python.jam.diff

            post-patch {
                reinplace s|@PREFIX@|${prefix}| ${worksrcpath}/tools/build/v2/tools/python.jam
            }

            depends_lib-append  port:${p}
            build.args-delete   --without-python

            post-configure {
                write_jam "using python : : ${prefix}/bin/python${v} ;"
            }
        }
    }]
}

variant icu description {Enable Unicode support in Boost.Regex through ICU} {
    depends_lib-append  port:icu
    build.args-append   -sICU_PATH=${prefix}
}

variant openmpi description {Build Boost.MPI} {
    depends_lib-append  port:openmpi
    build.args-delete   --without-mpi

    post-configure {
        write_jam "using mpi : ${prefix}/bin/openmpic++ : : ${prefix}/bin/openmpirun ;"
    }

    if {![catch python_dir]} {
        patchfiles-append patch-libs-mpi-build-Jamfile.v2.diff

        post-destroot {
            set site_packages [python_dir]
            xinstall -d ${destroot}${site_packages}/boost
            xinstall -m 644 ${worksrcpath}/libs/mpi/build/__init__.py \
                ${destroot}${site_packages}/boost

            set l ${site_packages}/boost/mpi.so
            move ${destroot}${prefix}/lib/mpi.so ${destroot}${l}
            system "install_name_tool -id ${l} ${destroot}${l}"
        }
    }
}

variant regex_match_extra description \
        "Enable access to extended capture information of submatches in Boost.Regex" {
    notes \
        You enabled +regex_match_extra variant, see the following page for an \
        exhaustive list of the consequences of this feature:\n\t \
        http://www.boost.org/doc/libs/1_41_0/libs/regex/doc/html/boost_regex/ref/sub_match.html

    post-patch {
        reinplace {/#define BOOST_REGEX_MATCH_EXTRA/s:^// ::} \
            ${worksrcpath}/boost/regex/user.hpp
    }
}

variant universal {
    build.args-append   pch=off

    if {[lsearch ${universal_archs} ppc*] != -1} {
        if {[lsearch ${universal_archs} *86*] != -1} {
            build.args-append   architecture=combined
        } else {
            build.args-append   architecture=power
        }

        if {${os.arch} != "powerpc"} {
            build.args-append   --disable-long-double
        }
    } else {
        build.args-append   architecture=x86
    }

    if {[lsearch ${universal_archs} *64] != -1} {
        if {[lsearch ${universal_archs} i386] != -1 || [lsearch ${universal_archs} ppc] != -1} {
            build.args-append   address-model=32_64
        } else {
            build.args-append   address-model=64
        }
    } else {
        build.args-append   address-model=32
    }
}

platform powerpc {
    build.args-append   --disable-long-double
}

platform darwin 8 powerpc {
    if {[variant_isset universal]} {
        build.args-append   macosx-version=10.4
    }
}

livecheck.type  regex
livecheck.url   ${homepage}
livecheck.regex "Version (\\d+\\.\\d+\\.\\d+)"
