# -*- 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               active_variants 1.1

name                    creduce
version                 2.10.0
revision                2
categories              devel
license                 NCSA
maintainers             {larryv @larryv}

description             Test-case reduction for C compiler bugs
long_description        C-Reduce is a tool that takes a large C, C++, \
                        or OpenCL file that has a property of interest \
                        (such as triggering a compiler bug) and \
                        automatically produces a much smaller C/C++ \
                        file that has the same property. It is \
                        intended for use by people who discover and \
                        report bugs in compilers and other tools that \
                        process source code.
homepage                http://embed.cs.utah.edu/creduce

patch.pre_args-replace  -p0 -p1

# INSTALL.md notes the specific version of LLVM that is required.
if { !([variant_isset llvm90] || [variant_isset llvm11]) } {
    # LLVM-11 needed for Big Sur and later
    if {${os.major} >= 20} {
        default_variants    +llvm11
    } else {
        default_variants    +llvm90
    }
}

# Note that LLVM 11 and later, each require different upstream patches
if {[variant_isset llvm90]} {
    set llvm_version    9.0
} elseif {[variant_isset llvm11]} {
    set llvm_version    11
    patchfiles-append   patch-llvm-11.diff
} else {
    error "One variant must be selected"
}

# Clang must be installed with `+analyzer`, otherwise we'll be missing some required libs
require_active_variants clang-${llvm_version} analyzer

set perl5.major         5.34

# Build with Clang matching selected LLVM version
configure.compiler      macports-clang-${llvm_version}

# INSTALL.md mentions flex, but the tarball ships with pregenerated parsers.
depends_build-append    port:llvm-${llvm_version}

# Required by the LLVM static libraries.
depends_lib-append      port:ncurses \
                        port:zlib

# Not required at compile time, but the configure script checks for them.
depends_lib-append      port:clang-${llvm_version} \
                        port:perl${perl5.major} \
                        port:p${perl5.major}-exporter-lite \
                        port:p${perl5.major}-file-which \
                        port:p${perl5.major}-getopt-tabular \
                        port:p${perl5.major}-regexp-common \
                        port:p${perl5.major}-term-readkey

depends_skip_archcheck  clang-${llvm_version} \
                        perl${perl5.major} \
                        p${perl5.major}-term-readkey

master_sites            ${homepage}/
checksums               rmd160  183be95aec282812724ec66204952ad33dbc923b \
                        sha256  db1c0f123967f24d620b040cebd53001bf3dcf03e400f78556a2ff2e11fea063 \
                        size    779318

compiler.cxx_standard   2011

platform darwin {
    if {${os.major} <= 9} {
        known_fail          yes
        pre-fetch {
            ui_error "${name} is only supported on Mac OS X 10.6 or newer."
            return -code error "unsupported Mac OS X version"
        }
    } elseif {${os.major} <= 12} {
        # forcing libc++ works on these platforms
        depends_lib-append      port:libcxx
        configure.cxx_stdlib    libc++
    }
}

configure.perl          ${prefix}/bin/perl${perl5.major}
configure.args          --libexecdir=${prefix}/libexec/${name} \
                        --with-llvm=${prefix}/libexec/llvm-${llvm_version}

build.type              gnu

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -m 755 -d ${destroot}${docdir}
    xinstall -m 644 -W ${worksrcpath} \
        AUTHORS COPYING NEWS README.md ${destroot}${docdir}
}

variant llvm90 conflicts llvm11 description {Use LLVM 9.0} {}
variant llvm11 conflicts llvm90 description {Use LLVM 11} {}

test.run                yes
test.dir                ${worksrcpath}/tests
test.cmd                ./run_tests
test.target

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