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

# arc4random_buf
legacysupport.newest_darwin_requires_legacy 10

# V8 8.3–8.4 corresponds to nodejs-14.
name                    v8-8.3
# No support for ppc32 in master presently; no support for ppc64 on macOS.
# See: https://github.com/nodejs/node/issues/30323
# Too many breaking commits after this version. Start from simple.
github.setup            bnoordhuis v8-cmake 8.3.110.13
revision                1

categories              lang devel
license                 BSD
maintainers             nomaintainer

description             Google’s open-source JavaScript engine
long_description        {*}${description}. CMake build system.
homepage                https://v8.dev

checksums               rmd160  74c70d4b0af5b3cf24f1727fb6c1efd1decabcaa \
                        sha256  084c9b5195387d18ab6b7e6ea0a530bff517f918a6fec7be2c5323f7e044e414 \
                        size    26713356
github.tarball_from     archive

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

depends_build-append    port:gettext \
                        path:bin/pkg-config:pkgconfig \
                        port:python${py_ver_nodot} \
                        port:py${py_ver_nodot}-jinja2
depends_lib-append      port:gettext-runtime \
                        path:lib/pkgconfig/glib-2.0.pc:glib2

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

patch.pre_args-replace  -p0 -p1

patchfiles-append       0001-Restore-ppc32-patches-from-upstream.patch \
                        0002-inspector-unbreak-build-with-gcc.patch \
                        0003-cpu.cc-support-powerpc-darwin.patch \
                        0004-semaphore-use-libdispatch-only-where-it-is-available.patch \
                        0005-runtime-utils.h-fix-static_cast.patch \
                        0006-Register-specs-for-powerpc-darwin.patch \
                        0007-platform-posix.cc-for-powerpc-darwin.patch \
                        0008-isolate-data.h-disable-asserts-for-now.patch \
                        0009-embedded-file-writer-mac-support-powerpc.patch \
                        0010-Add-some-missing-headers-and-use-cinttypes-consisten.patch \
                        0011-libsampler-support-powerpc-and-aarch64.patch \
                        0012-macro-assembler-ppc.h-fix-asserts-for-ppc.patch \
                        0013-CMakeLists-support-ppc-and-aarch64.patch

# The following patch is needed to compile with C++17:
# patchfiles-append       0014-tnode.h-backport-fix-for-is_convertible.patch

post-patch {
    # Upstream mixes variations of Python shebangs, so account for all:
    fs-traverse f ${worksrcpath} {
        if {[string match *.py ${f}]} {
            reinplace -q "s|/usr/bin/env python2|${configure.python}|" ${f}
            reinplace -q "s|/usr/bin/env python3|${configure.python}|" ${f}
            reinplace -q "s|/usr/bin/env python|${configure.python}|" ${f}
        }
    }

    # https://stackoverflow.com/questions/69381312/importerror-cannot-import-name-mapping-from-collections-using-python-3-10
    reinplace "s|from collections import Mapping|from collections.abc import Mapping|" ${worksrcpath}/v8/third_party/jinja2/tests.py
}

# std::is_convertible
compiler.cxx_standard   2017

# type_traits: error: invalid use of incomplete type 'class v8::internal::JSAggregateError' [-fpermissive]
if {[string match *gcc* ${configure.compiler}]} {
    configure.cxxflags-append \
                        -fpermissive
    if {${configure.build_arch} in [list i386 ppc]} {
        configure.ldflags-append \
                        -latomic
    }
}

if {[string match *clang* ${configure.compiler}] && ${os.major} > 23} {
    configure.cppflags-append   -Wno-enum-constexpr-conversion
}

configure.args-append   -DPYTHON_EXECUTABLE=${configure.python}

destroot {
    set v8_execprefix   ${prefix}/libexec/${name}
    set docdir          ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${v8_execprefix}/bin
    xinstall -d ${destroot}${v8_execprefix}/lib
    xinstall -d ${destroot}/${docdir}
    copy ${worksrcpath}/v8/include ${destroot}${v8_execprefix}
    foreach binary [list d8 mksnapshot torque] {
        copy ${cmake.build_dir}/${binary} ${destroot}${v8_execprefix}/bin
    }
    foreach slib [glob ${cmake.build_dir}/*.a] {
        copy ${slib} ${destroot}${v8_execprefix}/lib
    }
    copy ${worksrcpath}/v8/docs ${destroot}${docdir}
}
