# -*- 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-3+
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/

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

configure.args      --without-mpicc
compilers.choose    cc cxx

if {$subport eq $name && ${os.platform} eq "darwin" && ${os.major} < 11} {
    # on Snow Leopard and earlier we are frozen at the last version that
    # supported that platform; that version works with its contemporary
    # Xcode compilers (except early clang) but not many others, and doesn't
    # support the current version of MPI

    # clang: error: unknown argument: '-mno-dynamic-no-pic'
    compiler.blacklist-append   macports-*clang* macports-*default

    # checking for a supported version of gcc... no
    compiler.blacklist-append   {*gcc-[0-35-9]*} {*gcc-4[0-9]*} *gcc-devel* \
                                macports-gcc {clang < 211}

    # cc1: error: unrecognized command line option "-arch"
    compiler.blacklist-append   {macports-gcc-4.[0-6]}

    # error: unsupported option '--gdwarf2'
    compiler.blacklist-append   apple-gcc*

    # successful
    compiler.whitelist          gcc-4.2 llvm-gcc-4.2 gcc clang macports-gcc-4.9
} else {
    # current valgrind requires C11
    compiler.c_standard     2011
    compiler.cxx_standard   2011
    use_autoreconf          yes

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

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"
        }
    }
}

if {${os.platform} eq "darwin" && ${os.major} >= 11 && ${os.major} < 15} {
    # use MacPorts ld64 to avoid /usr/bin/ld errors with newer compilers
    depends_build-append    port:ld64
    post-patch {
        reinplace "s|\"/usr/bin/ld\"|\"${prefix}/bin/ld\"|" \
            ${worksrcpath}/coregrind/link_tool_exe_darwin.in
    }
}

pre-patch {
    # fix clang-isms if building with gcc
    if {${os.platform} eq "darwin" && [string match *gcc* ${configure.cc}]} {
        if {$subport eq $name && ${os.major} < 12} {
            # old versions only need to handle __private_extern__
            configure.cflags-append \
                {-D__private_extern__='__attribute__((visibility("hidden")))'}
        } else {
            # apply patch with -l because valgrind-macos-devel has different
            # trailing whitespace
            patchfiles-append   patch-gcc.diff
            patch.args          -l
        }
    }
}

post-patch {
    # if compiling with MacPorts Clang 11 or later, use the MacPorts dsymutil
    # from the same LLVM release to avoid /usr/bin/dsymutil abort
    # (mpicc-*-clang90 uses clang-mp-9.0, so match 11 to 89 and devel)
    if {[regexp {^(.*/)?(?:clang-mp-|mpicc-\w+-clang)(1[1-9]|[2-8]\d|devel)$} \
            ${configure.cc} -> llvm_path llvm_vers]} {
        reinplace "s|dsymutil |${llvm_path}dsymutil-mp-${llvm_vers} |" \
            ${worksrcpath}/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} {
    platforms       {darwin >= 9 < 23}
    supported_archs i386 x86_64

    if {${os.platform} eq "darwin" && ${os.major} < 10} {
        # last version that supported Mac OS X 10.5 (Intel only)
        # (PowerPC and earlier Mac OS X releases were never supported)
        version     3.6.1
        license     GPL-2+
        checksums   rmd160  6f78ae50e2fb354547d90bf289abb1f56252043a \
                    sha256  49bdcc4fbcf060049b5f0dcfd8a187a6e90e0b0e57309f633b64e44430726a0e \
                    size    5974836

    } elseif {${os.platform} eq "darwin" && ${os.major} < 11} {
        # last version that supported Mac OS X 10.6
        version     3.8.1
        license     GPL-2+
        checksums   rmd160  7b1e87ffa9baa614006ecd7722f848c10cadddda \
                    sha256  473be00576bed311a662b277a2bfbe97d9cca4058e68619a0e420c9fc19958db \
                    size    7962963

    } elseif {${os.platform} eq "darwin" && ${os.major} < 12} {
        # last version that supported Mac OS X 10.7
        version     3.21.0
        license     GPL-2+
        checksums   rmd160  ed9a6ed812d303de6efa9466b04fc5d5e66486c3 \
                    sha256  10ce1618bb3e33fad16eb79552b0a3e1211762448a0d7fce11c8a6243b9ac971 \
                    size    17449484

    } else {
        # current version
        version     3.28.0
        checksums   rmd160  1b47e0f11248f2cfb43a8d9169987c65c25c154c \
                    sha256  9f7a18d128f05d3a80fea53e6953f34aad7200a425ceaee46099a8a798a7dd9c \
                    size    17531409

        # patch for OS X 10.8, cherry-picked from valgrind-devel fd2587eecf99
        if {${os.platform} eq "darwin" && ${os.major} < 13} {
            patchfiles-append   valgrind-osx108.patch
        }
    }

    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

    long_description-append \n\nThis port reflects the official release, \
        which is compatible with Intel Macs running macOS 13 or earlier. \
        Consider installing valgrind-macos-devel on newer macOS systems.

    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-11-17
    version         3.28.0-r${date}
    git.branch      dc9a669332617902591da89ae887db9a567f2841

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

    long_description-append \n\nThis port reflects the official upstream repository, \
        which is compatible with Intel Macs running macOS 13 or earlier. \
        Consider installing valgrind-macos-devel on newer macOS systems.

    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 {
    # broken for macOS 15 on arm64 and macOS 27
    # https://github.com/LouisBrunner/valgrind-macos/issues/123
    # https://github.com/LouisBrunner/valgrind-macos/issues/205
    if {${os.arch} eq "arm"} {
        platforms   {darwin >= 12 != 24.* < 27}
    } else {
        platforms   {darwin >= 12 < 27}
    }
    supported_archs i386 x86_64 arm64

    set date        2026-11-17
    version         3.28.0-r${date}
    git.branch      3a119d86cda9bc6c06a9c5aa14e3fbd9eead5e5a

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

    # patch to build with Xcode 27 (available on macOS 26.6 and later)
    # https://github.com/LouisBrunner/valgrind-macos/pull/204
    if {${os.platform} eq "darwin" && ${os.major} >= 25} {
        patchfiles-append   valgrind-xcode27.patch
    }

    # patch for OS X 10.8, cherry-picked from valgrind-devel fd2587eecf99
    if {${os.platform} eq "darwin" && ${os.major} < 13} {
        patchfiles-append   valgrind-osx108.patch
    }

    # avoid certificate error with old git
    if {${os.platform} eq "darwin" && ${os.major} < 16} {
        depends_fetch-append    path:bin/git:git
        git.cmd                 ${prefix}/bin/git
    }

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

    long_description-append \n\nThis port reflects an experimental fork that \
        adds support for Apple Silicon Macs and some newer macOS releases.
}

# 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
