# -*- 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
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 {}

if {${subport} eq ${name}} {
    conflicts       ${name}-devel
    github.setup    bellard ${name} fa628f8c523ecac8ce560c081411e91fcaba2d20
    github.tarball_from archive
    version         20250913
    revision        0
    checksums       rmd160  39e947aced6544a85fad6ab91594181615710281 \
                    sha256  2623e6336b1b8d82a9e509899f5c934bdfeda0e6f518fe5b7ad4fdaa01cafb7e \
                    size    594843

    # 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}
    github.setup    bellard ${name} f1139494d18a2053630c5ed3384a42bb70db3c53
    github.tarball_from archive
    version         20251222
    revision        0
    checksums       rmd160 25b2853365f4d3c2fdb597a8d12774dac439d18a \
                    sha256 0364fd5136857117c8204c2f0c203b71b25b9f025dc1a0605ea51f0fd7f92511 \
                    size   618177

    # 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|2025-09-13|${fixed-version}|g" ${worksrcpath}/VERSION
    }
}

compiler.c_standard  2011
# fatal error: 'stdatomic.h' file not found
compiler.blacklist-append {clang < 700}

# without this linking to legacysupport is broken
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
    }
}

destroot.destdir PREFIX=${destroot}${prefix}

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
}
