# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0

PortGroup           github 1.0
PortGroup           legacysupport 1.1

# strnlen, arc4random_buf, clock_gettime, utimensat
legacysupport.newest_darwin_requires_legacy 16

github.setup        MoarVM MoarVM 2026.06
github.tarball_from releases
revision            0
categories          lang devel
license             Artistic-2 BSD ISC MIT public-domain
maintainers         nomaintainer
description         A virtual machine for NQP and Rakudo
long_description    MoarVM is a modern virtual machine built for the Rakudo \
                    compiler implementing the Raku programming language, \
                    and the NQP compiler toolchain.

homepage            https://moarvm.org/

checksums           rmd160  a7f3df7ff16ed6063563a335d4558edbdb0b3a26 \
                    sha256  4d0735e1ca1f4a34869599370eec6fc12dae8bdf2d719a9cbbc940c9fc3b75d9 \
                    size    15611292

depends_build-append \
                    path:bin/perl:perl5 \
                    path:bin/pkg-config:pkgconfig

# libatomic_ops:    https://trac.macports.org/ticket/53171
depends_lib-append  port:dyncall \
                    port:libatomic_ops \
                    port:libtommath

patchfiles          patch-Configure.diff \
                    patch-dyncall.diff \
                    patch-setup.pm.diff

# TODO: update patches once this is settled and merged:
# https://github.com/macports/macports-ports/pull/27671
if ![variant_isset macports_libuv] {
patchfiles-append   patch-libuv-includes-path.diff \
                    patch-libuv-legacy.diff \
                    patch-moar.pc.diff
}

if {[string match *gcc* ${configure.compiler}]} {
    patchfiles-append patch-gcc.diff
}

post-patch {
    reinplace "s,/usr/local,${prefix},g" ${worksrcpath}/Configure.pl
    # The following are not built, but better have them fixed nevertheless:
    reinplace "s,/usr/local,${prefix},g" ${worksrcpath}/3rdparty/dyncall/configure
    reinplace "s,/usr/local,${prefix},g" ${worksrcpath}/3rdparty/dyncall/configure.rc
    reinplace "s,/usr/local,${prefix},g" ${worksrcpath}/3rdparty/libatomicops/configure
    reinplace "s,/usr/local,${prefix},g" ${worksrcpath}/3rdparty/libtommath/makefile.unix
    reinplace "s,/usr/local,${prefix},g" ${worksrcpath}/3rdparty/libtommath/makefile_include.mk
}

# Building nqp on PPC results in malloc errors (though build still succeeds):
# malloc: *** error for object: incorrect checksum for freed object - object was probably modified after being freed.
legacysupport.redirect_bins moar

# https://trac.macports.org/ticket/53950
# uv-common.h:41:11: fatal error: 'stdatomic.h' file not found
compiler.c_standard 2011
compiler.blacklist-append \
                    {clang < 700}

configure.cmd       ${prefix}/bin/perl Configure.pl
configure.args      --cc=${configure.cc} \
                    --has-dyncall \
                    --has-libatomic_ops \
                    --has-libtommath \
                    --no-mimalloc

configure.cflags-append \
                    -I${prefix}/include/libtommath

# libuv/src/unix/fs.c:478:34: error: passing argument 3 of 'scandir' from incompatible pointer type
configure.cflags-append \
                    -Wno-error=incompatible-pointer-types

# The build system puts CPPFLAGS and LDFLAGS before its own flags which leads to
# build failures if MacPorts libuv is installed. The right -I and -L flags are
# inserted later by the build system so the ones MacPorts adds are not needed.
# https://github.com/MoarVM/MoarVM/issues/1818
configure.cppflags-delete   -I${prefix}/include
configure.ldflags-delete    -L${prefix}/lib

# Disable silent rules
build.args-append   NOISY=1

# https://github.com/MoarVM/MoarVM/issues/414
universal_variant   no
# Unsupported by Configure.pl
# configure.universal_args-delete --disable-dependency-tracking

variant macports_libuv description "Use MacPorts' libuv" {
    # Once libuv is updated, this should be changed to path-style dependency:
    depends_lib-append  port:libuv-devel

    configure.args-append \
                    --has-libuv
}

variant mimalloc description "Build with mimalloc support" {
    configure.args-delete \
                    --no-mimalloc
}

# stats.c:569:31: error: variable has incomplete type 'struct mach_task_basic_info'
# mimalloc code uses the API available on macOS 10.8+
if {${os.platform} eq "darwin" && ${os.major} > 11} {
    default_variants    +mimalloc
}

# This can be enabled once libuv port is updated.
# See: https://trac.macports.org/ticket/68464
# default_variants-append macports_libuv
