# -*- 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           meson 1.0
PortGroup           gobject_introspection 1.0
PortGroup           gitlab 1.0

gitlab.instance     https://gitlab.gnome.org
gitlab.setup        GNOME gjs 1.82.1
revision            0

checksums           rmd160  0d227c436a1e1c4033add73966035de9616396f5 \
                    sha256  c276c6b5a46bf6bebc3a2e9a84db10a1796899efd756a2656fd265051b507a40 \
                    size    705725

name                gjs-devel
conflicts           gjs
set my_name         gjs

categories          gnome
license             LGPL-2.1+
maintainers         {devans @dbevans} {mascguy @mascguy} openmaintainer
description         GNOME JavaScript/Spidermonkey bindings
long_description    ${description}
homepage            https://gjs-docs.gnome.org/gjs

# Disable unexpected download of subprojects
meson.wrap_mode     nodownload

set py_ver          3.12
set py_ver_nodot    [string map {. {}} ${py_ver}]

depends_build-append \
                    path:bin/pkg-config:pkgconfig \
                    port:python${py_ver_nodot}

depends_lib-append \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:dbus \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                    port:libffi \
                    port:readline

configure.python    ${prefix}/bin/python${py_ver}

# profiler currently only supported on Linux
configure.args-append \
                    -Dinstalled_tests=false \
                    -Dprofiler=disabled \
                    -Dskip_dbus_tests=true \
                    -Dskip_gtk_tests=true \
                    -Dbsymbolic_functions=false

gobject_introspection yes

platform darwin {
    if {${os.major} < 16} {
        # Fallback version for older systems.
        gitlab.setup    GNOME gjs 1.63.3
        revision        0
        checksums       rmd160  60cbadb35bad345917b153dc115f2c84fc8e55d3 \
                        sha256  63e97e3b76062c931fab1e0afa25f2db701e91e92653452df50e3ab3b51dfbbc \
                        size    435576

        # Use rust-free version of mozjs:
        depends_lib-append      port:mozjs60

        # LD_LIBRARY_PATH => DYLD_LIBRARY_PATH
        patchfiles-append       patch-gjs-test-legacy.diff
        # The gobject_introspection PG needs an 'introspection' option
        patchfiles-append       patch-gjs-meson_options-legacy.diff
        # Fix gsize/size_t mismatch on 32-bit systems
        patchfiles-append       patch-gjs-gsize.diff
        # Fix gint64/int64_t mismatch on x86_64
        patchfiles-append       patch-gjs-gint64.diff
        # gatomic.h: error: argument 2 of '__atomic_load' must not be a pointer to a 'volatile' type
        patchfiles-append       patch-atomic_load.diff

        post-patch {
            reinplace "s|^#!/usr/bin/env python3|#!${configure.python}|" \
                build/compile-gschemas.py \
                build/symlink-gjs.py
        }

        compiler.cxx_standard   2014
    } else {
        depends_lib-append      port:mozjs128

        # The gobject_introspection PG needs an 'introspection' option
        patchfiles-append       patch-gjs-meson_options.diff
        # Completely disable tests, as present meson.build doesn't
        patchfiles-append       patch-gjs-disable-gi-tests.diff

        post-patch {
            reinplace "s|^#!/usr/bin/env python3|#!${configure.python}|" \
                build/symlink-gjs.py \
                tools/heapdot.py \
                tools/heapgraph.py
        }

        compiler.cxx_standard   2017

        if { [string match *clang* ${configure.compiler}] } {
            configure.cxxflags-append \
                        -Wno-invalid-constexpr \
                        -Wno-error=unknown-warning-option \
                        -Wno-unknown-warning-option
        }
    }
}

# NOTE: Tests need to be fixed as of 1.82.x
variant tests description {Enable tests} {
    depends_build-append \
                    port:git

    configure.args-replace \
                    -Dinstalled_tests=false \
                    -Dinstalled_tests=true

    # Note that a few tests in the "Scripts / CommandLine" category may fail as the
    # typelib (gobject-introspection) expects libgjs.0.dylib to be installed at the
    # system level during testing
    test.run            yes
    test.target         test
}
