# -*- 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: Portfile 118774 2014-04-11 00:13:27Z sean@macports.org $

PortSystem          1.0
PortGroup           mpi 1.0

compilers.choose    cc cxx
mpi.choose
mpi.setup           -gcc44 -gcc45 -gcc46

name                valgrind
version             3.9.0
revision            1
categories          devel
platforms           darwin
supported_archs     i386 x86_64
license             GPL-2
maintainers         raimue \
                    openmaintainer

description         A powerful open-source memory debugger
long_description \
    Valgrind is a powerful open-source memory debugger. When valgrind is \
    attached to a program, it intercepts calls to malloc/new/free/delete and \
    all memory operations are checked for invalid read or write.

homepage            http://valgrind.org
master_sites        http://valgrind.org/downloads/

checksums           md5     0947de8112f946b9ce64764af7be6df2 \
                    sha1    9415e28933de9d6687f993c4bb797e6bd49583f1 \
                    sha256  e6af71a06bc2534541b07743e1d58dc3caf744f38205ca3e5b5a0bdf372ed6f0

# due to -mno-dynamic-no-pic in Makefile.all.am we need to regenerate the
# configure scripts
use_autoreconf      yes
depends_build       bin:perl:perl5
# Ignore trace reports about boost, Qt and OpenMP
# as they are only used for tests

configure.args      --mandir=${prefix}/share/man \
                    --without-mpicc

if {${build_arch} eq "i386"} {
    configure.args-append --enable-only32bit
} elseif {${build_arch} eq "x86_64"} {
    configure.args-append --enable-only64bit
}

# valgrind sets up -arch by itself, adding our own interferes with the build
configure.cc_archflags {}
configure.ld_archflags {}

variant universal {
    configure.args-delete --enable-only32bit --enable-only64bit
    configure.universal_cflags {}
    configure.universal_cxxflags {}
    configure.universal_ldflags {}

    pre-configure {
        if {${build_arch} eq "i386"} {
            # See #25684 and https://bugs.kde.org/show_bug.cgi?id=243362
            ui_error "valgrind does not build +universal on a 32-bit only CPU!"
            return -code error "unsupported CPU"
        }
    }
}

pre-patch {
    if {[gcc_variant_isset]} {
        patchfiles-append patch-gcc.diff
    }

    reinplace -W $worksrcpath "s,-mno-dynamic-no-pic,,g" Makefile.all.am
}

pre-configure {
    if {[mpi_variant_isset]} {
        configure.args-delete --without-mpicc
        configure.args-append --with-mpicc=${mpi.cc}
    }
}

if {$subport eq $name} {
    conflicts           valgrind-devel
    use_bzip2           yes

    # Avoid conflict of faq.html and FAQ.html, #30541
    extract.post_args-append --exclude=${distname}/docs/html/FAQ.html

    pre-fetch {
        if {${os.platform} eq "darwin" && (${os.major} < 9 || ${os.major} > 12)} {
            ui_error "${name} ${version} is only compatible with Mac OS X 10.5, 10.6, 10.7 and 10.8"
            return -code error "incompatible Mac OS X version"
        }
    }

    patchfiles-append   link_tool_exe_darwin-ccache.diff \
                        patch-compat-snowleo.diff

    livecheck.type      regex
    livecheck.url       ${homepage}
    livecheck.regex     ${name}-(\\d(?:\\.\\d)+)
}

subport valgrind-devel {
    conflicts           valgrind

    svn.revision        13710
    set vex_revision    2797
    version             3.9.0-r${svn.revision}
    revision            1

    fetch.type      svn
    svn.url         svn://svn.valgrind.org/valgrind/trunk
    svn.args        --ignore-externals
    worksrcdir      trunk

    post-fetch {
        system -W ${worksrcpath} "${svn.cmd} ${svn.pre_args} ${svn.method} svn://svn.valgrind.org/vex/trunk@${vex_revision} VEX"
    }

    livecheck.type none
}
