# -*- 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           compiler_blacklist_versions 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 2025.05
github.tarball_from releases
revision            0
categories          lang devel
license             Artistic-2 BSD ISC MIT public-domain
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
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  365c64cda64583d396e657fb1cac707fdf3fd904 \
                    sha256  61fc9ecc8b479ccc8d389fc73cce927c9f5b1070a9c62c60a0817f89dc832d91 \
                    size    15114902

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
compiler.blacklist  cc gcc-* apple-gcc-* llvm-gcc-*
# uv-common.h:41:11: fatal error: 'stdatomic.h' file not found
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 dylib machinery uses -rpath, not available on Tiger
platform darwin 8 {
    patchfiles-append patch-build-setup-tiger.diff
}

# 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+
# not enabled at will by the maintainer, possibly to be fixed on older systems
#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
