# -*- 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           mpi 1.0

name                valgrind
categories          devel
license             GPL-2
maintainers         nomaintainer

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            https://valgrind.org
master_sites        https://sourceware.org/pub/valgrind/

# older clangs identify as applellvm-4.2 and are rejected
# https://trac.macports.org/ticket/53525
compiler.blacklist-append \
                    { clang < 426 }

# priv/guest_s390_toIR.c:2056:39: error: invalid suffix "b00010000" on integer constant
compiler.blacklist-append \
                    llvm-gcc-4.2

compilers.choose    cc cxx

if {${os.platform} eq "darwin" && ${os.arch} eq "arm"} {
    # error: this compiler does not support 'arm64e'
    compiler.blacklist-append *gcc*
    mpi.setup       -gcc
} else {
    mpi.setup       -gcc44 -gcc45 -gcc46
}

depends_build       bin:perl:perl5
# Ignore trace reports about boost, Qt and OpenMP
# as they are only used for tests

configure.args      --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 "${subport} does not build +universal on a 32-bit only CPU!"
            return -code error "unsupported CPU"
        }
    }
}

pre-patch {
    if {[gcc_variant_isset]} {
        # if compiling with gcc, fix clang-isms (applying patch with -l
        # because valgrind-macos-devel has different trailing whitespace), and
        # ensure that configure is regenerated after patching configure.ac
        patchfiles-append   patch-gcc.diff
        patch.args          -l
        use_autoreconf      yes
    }
}

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

if {$subport eq $name} {
    platforms       {darwin >= 9 < 23}
    supported_archs i386 x86_64

    if {${os.platform} eq "darwin" && ${os.major} < 12} {
        version     3.16.1
        checksums   rmd160  f06068b1f2aa9f6c2377816a0af809459da2c5a8 \
                    sha256  c91f3a2f7b02db0f3bc99479861656154d241d2fdb265614ba918cc6720a33ca \
                    size    16262776
    } else {
        version     3.27.1
        checksums   rmd160  6ebfaca6297ce2b46854c32379db2a21d46c7e2d \
                    sha256  5d589152eb8071c02feab8ce6ab719e431a1fbc3e2b1700f5432632a8b9264dc \
                    size    17372682

        # patches for older macOS versions
        if {${os.platform} eq "darwin" && ${os.major} < 19} {
            patchfiles-append   legacymacos.diff readbootstrap.diff
        }
    }

    conflicts       valgrind-devel valgrind-macos-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} >= 23 || ${os.arch} eq "arm")} {
            ui_error "${subport} @${version} is only compatible with Intel Macs running macOS 13 or earlier."
            ui_error "Consider installing valgrind-macos-devel on newer macOS systems."
            return -code error "incompatible macOS version"
        }
    }

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

subport valgrind-devel {
    platforms       {darwin >= 12 < 23}
    supported_archs i386 x86_64

    epoch           1
    set date        2026-05-20
    version         3.28.0-r${date}
    git.branch      c651c83f24cc8be21386f019dfd75e9bb04351bc

    # patch for older macOS versions
    if {${os.platform} eq "darwin" && ${os.major} < 13} {
        patchfiles-append   readbootstrap.diff
    }

    conflicts       valgrind valgrind-macos-devel
    fetch.type      git
    git.url         https://sourceware.org/git/valgrind.git
    use_autoreconf  yes
    livecheck.type  none

    pre-fetch {
        if {${os.platform} eq "darwin" && (${os.major} >= 23 || ${os.arch} eq "arm")} {
            ui_error "${subport} @${version} is only compatible with Intel Macs running macOS 13 or earlier."
            ui_error "Consider installing valgrind-macos-devel on newer macOS systems."
            return -code error "incompatible macOS version"
        }
    }
}

subport valgrind-macos-devel {
    # support for macOS 15 on arm64 is broken
    # https://github.com/LouisBrunner/valgrind-macos/issues/123
    if {${os.arch} eq "arm"} {
        platforms   {darwin >= 9 != 24.*}
    } else {
        platforms   {darwin >= 9}
    }
    supported_archs i386 x86_64 arm64

    set date        2026-05-20
    version         3.28.0-r${date}
    git.branch      472bff07019119605b1f85deaf3601a59f08d22e

    description     A powerful memory debugger with latest macOS support (experimental)

    # patches for older macOS versions
    if {${os.platform} eq "darwin" && ${os.major} < 19} {
        patchfiles-append   legacymacos.diff readbootstrap.diff
    }

    conflicts       valgrind valgrind-devel
    fetch.type      git
    git.url         https://github.com/LouisBrunner/valgrind-macos.git
    use_autoreconf  yes
    livecheck.type  none
}

# detection of optional C23 support; intentional error if not C23
configure.checks.implicit_function_declaration.whitelist-append alignof
configure.checks.implicit_int no
# detection of optional nested function support
configure.checks.implicit_function_declaration.whitelist-append foo
# detection of optional function aio_readv in aio.h; not available on macOS
configure.checks.implicit_function_declaration.whitelist-append aio_readv
