# -*- 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       cmake 1.1
PortGroup       github 1.0
PortGroup       legacysupport 1.1

# _clock_gettime
legacysupport.newest_darwin_requires_legacy 15

set real_name   quickjs
name            ${real_name}-ng
github.setup    ${real_name}-ng ${real_name} 0.16.1 v
revision        0
conflicts       ${real_name} ${real_name}-devel
categories      devel
license         MIT
maintainers     {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description     small and embeddable JavaScript engine
long_description \
                ${name} is a {*}${description}. \
                It supports the ES2020 specification including modules, \
                asynchronous generators, proxies and BigInt.
checksums       rmd160  394d1d7876946e88a9d3def0576af5a41d0e00fe \
                sha256  4b3c11f37dab2c58bdeccbaeb23b923fa4a9798a45e50be6af55f3e75b616ea0 \
                size    894607
github.tarball_from archive

compiler.c_standard 2011
compiler.blacklist-append \
                    {clang < 700}

# See: https://github.com/quickjs-ng/quickjs/issues/1122
# QuickJS ships pre-generated bytecode (the builtin-*.h headers compiled into
# the library and the gen/*.c blobs used by the CLI/examples) serialized in
# little-endian byte order. It is validated by an embedded version/checksum
# at load time, so on big-endian hosts (e.g. 32-bit PowerPC) it fails
# with a "checksum error". This patch teaches the CMake build to regenerate
# the bytecode from source with a freshly built qjsc on native big-endian builds.
# On little-endian hosts it is a no-op (the committed blobs are used as-is).
patchfiles-append \
                patch-be-bytecode-regen.diff

# https://github.com/quickjs-ng/quickjs/issues/1023
configure.args-append \
                -DQJS_BUILD_LIBC=ON

if {[string match *gcc* ${configure.cc}]} {
    configure.ldflags-append \
                -latomic
}

# configure.pre_args-replace \
#                 -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
#                 -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF

# Test executable tries to find non-existing file:
# run-test262: sta.js: No such file or directory
# test.run        yes
# test.cmd        ${cmake.build_dir}/run-test262
# test.target

post-destroot {
    xinstall -d ${destroot}${prefix}/lib/pkgconfig
    xinstall -m 0644 ${filespath}/${github.project}.pc ${destroot}${prefix}/lib/pkgconfig
    reinplace "s|@@PREFIX@@|${prefix}|" ${destroot}${prefix}/lib/pkgconfig/${github.project}.pc
    reinplace "s|@@VERSION@@|${version}|" ${destroot}${prefix}/lib/pkgconfig/${github.project}.pc
}
