# -*- 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                quickjs

categories          devel www
license             MIT
maintainers         nomaintainer
description         A small and embeddable Javascript engine
long_description    ${name} is a small and embeddable Javascript engine. \
                    It supports the ES2023 specification including modules, asynchronous \
                    generators, proxies and BigInt.
homepage            https://bellard.org/quickjs

subport             ${name}-devel {}

github.tarball_from archive

# FIXME: on next update ensure that reported by the executable version reflects reality.
if {${subport} eq ${name}} {
    conflicts       ${name}-devel ${name}-ng
    github.setup    bellard ${name} 3d5e064e9dd67c70f7962836505a7fa067bf0a4e
    version         20260604
    revision        0
    checksums       rmd160  219bddcd2835fbcb7de6e01b18f4a6582a5f8fd2 \
                    sha256  afccd11533e21a4e49af43e6909fc7236ed0b92f25e01e1061ceb0b47e15e05d \
                    size    635373

    # Use the changelog link to check stable releases
    livecheck.type  regex
    livecheck.version \
                    [regsub {^(\d{4})(\d{2})(\d{2})$} ${version} {\1-\2-\3}]
    livecheck.url   https://bellard.org/quickjs/Changelog
    livecheck.regex (\\d{4}-\\d{2}-\\d{2})
} else {
    # quickjs-devel
    conflicts       ${name} ${name}-ng
    github.setup    bellard ${name} 04be246001599f5995fa2f2d8c91a0f198d3f34c
    version         20260616
    revision        0
    checksums       rmd160  076b7eb2d019627aa810bb1cb35e77c9f11b0713 \
                    sha256  2a87ffcca6c870f764ce70a7736351bd7cff3dc1fb95a8fb059c260979f1e01a \
                    size    636419

    # This is the -devel port, there is no point to print a stable version
    set fixed-version [regsub {^(\d{4})(\d{2})(\d{2})$} $version {\1-\2-\3}]
    post-patch {
        reinplace "s|2026-06-04|${fixed-version}|g" ${worksrcpath}/VERSION
    }
}

compiler.c_standard 2011

if {${os.platform} eq "darwin" && ${os.major} < 16} {
    configure.cflags-append \
                    -D_DARWIN_C_SOURCE -isystem${prefix}/include/LegacySupport

    # See https://github.com/bsekisser/quickjs/commit/c35e6bf
    if {[string match *gcc* ${configure.compiler}]} {
            patchfiles-append \
                    patch-darwin-gcc.diff
            configure.ldflags-append \
                    -latomic
    } elseif {[string match *clang* ${configure.compiler}]} {
            patchfiles-append \
                    patch-darwin-clang.diff
    }
}

platform darwin 8 {
    depends_build-append \
                    port:gmake
    build.cmd       ${prefix}/bin/gmake
    configure.cflags-append -Wno-error=incompatible-pointer-types
}

post-destroot {
    file mkdir ${destroot}${prefix}/lib/pkgconfig
    copy ${filespath}/${name}.pc ${destroot}${prefix}/lib/pkgconfig
    reinplace "s|@@PREFIX@@|${prefix}|" ${destroot}${prefix}/lib/pkgconfig/${name}.pc
    reinplace "s|@@VERSION@@|${version}|" ${destroot}${prefix}/lib/pkgconfig/${name}.pc
}
