# -*- 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           github 1.0
PortGroup           legacysupport 1.1
PortGroup           makefile 1.0

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name                bwbench
github.setup        RRZE-HPC TheBandwidthBenchmark 3.0 v
revision            0
categories          benchmarks
license             MIT
maintainers         {i0ntempest @i0ntempest} openmaintainer

description         The ultimate memory bandwidth benchmark
long_description    ${description} – heavily inspired by John McCalpin's Stream benchmark \
                    and contains a collection of simple streaming kernels.

checksums           rmd160  975867b608d040d97f1c13323175ee68f02fc183 \
                    sha256  cc6e073788c3b90c4796ee8604eb587d7bfb7986a816397a295c77fb894b086a \
                    size    20902
github.tarball_from archive

patchfiles          patch-fix-mk.diff

post-patch {
    if {[string match *clang* ${configure.compiler}]} {
        reinplace "s|TOOLCHAIN ?= .*|TOOLCHAIN = CLANG|" config.mk
    } elseif {[string match *gcc* ${configure.compiler}]} {
        reinplace "s|TOOLCHAIN ?= .*|TOOLCHAIN = GCC|" config.mk
    }
    reinplace "s|ENABLE_OPENMP ?= true|ENABLE_OPENMP = false|" config.mk
    # Remove compiler suffix in binary filename
    reinplace "s|bwbench-\$(TOOLCHAIN)|bwbench|" Makefile
}

# Build system uses its own
configure.optflags

if {${os.platform} eq "darwin" && ${os.major} < 16} {
    build.cmd-prepend \
                    LIBS=-lMacportsLegacySupport
}

# Makefile does not accept `all`
build.target

destroot {
    xinstall -m 0755 ${worksrcpath}/${name} ${destroot}${prefix}/bin/
}

variant openmp description "Enable parallelism support using OpenMP" {
    compiler.openmp_version 4.5
    compiler.blacklist-append   {macports-clang-[0-9].*}
    post-patch {
        reinplace "s|ENABLE_OPENMP = false|ENABLE_OPENMP = true|" config.mk
    }
}

default_variants-append \
                    +openmp

# Notice that default setting may not work:
# Total allocated datasize:  4000.00 MB
# OpenMP enabled, running with 2 threads
# bwbench(69328) malloc: *** mmap(size=1000001536) failed (error code=12)
# *** error: can't allocate region
# *** set a breakpoint in malloc_error_break to debug
# Error: Insufficient memory to fulfill the request
# If it fails, tweak the setting this way:
# bwbench -s ${value_in_gigabytes}
