# -*- 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        compiler_blacklist_versions 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} 19abf1888db5884a5758036ff6e7fa2b340acedc
    github.tarball_from archive
    version         20250405
    revision        1
    checksums       rmd160  f7e78f2f04101f01a34fe2e8dc0d58e3d6d06064 \
                    sha256  20e931a015376de4c38609b889016e926ddbf17a741ced24ef741c1c721f9e53 \
                    size    570178
} else {
    # quickjs-devel
    conflicts       ${name}
    github.setup    bellard ${name} f10ef299a6ab4c36c4162cc5840f128f74ec197c
    github.tarball_from archive
    version         20250520
    revision        1
    checksums       rmd160 b1ac071d6601277d89c1900f8cb7a63e76bd302d \
                    sha256 6e1db4fc459f1eb4fc9f5c2716d02e5d6f6494937f7e24d7c3dd7a1777a62408 \
                    size   590714
    post-extract {
        # `VERSION` is only updated for "real" releases.
        reinplace "s|2025-04-26|${version}|" ${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
}
