# -*- 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               legacysupport 1.1
PortGroup               openssl 1.0
PortGroup               deprecated 1.0

# EOL 2023-09-11
# https://github.com/nodejs/Release
deprecated.eol_version  yes

configure.cxx_stdlib    libc++
# C++17 required for ICU
compiler.cxx_standard   2017
configure.cxxflags-append   -std=c++17

name                    nodejs16
version                 16.20.2
revision                4

categories              lang net
license                 {MIT BSD}
maintainers             {ciserlohn @ci42} openmaintainer

# Attempting to use node on Mac OS X 10.7 causes a kernel panic. libuv could
# be patched to fix this; see https://trac.macports.org/ticket/71475
platforms               {darwin != 11.*}

description             Evented I/O for V8 JavaScript

long_description        Node's goal is to provide an easy way to build scalable network programs in JavaScript. \
                        Node is similar in design to and influenced by systems like Ruby's Event \
                        Machine or Python's Twisted. Node takes the event model a bit further-it \
                        presents the event loop as a language construct instead of as a library.

conflicts               nodejs8 nodejs10 nodejs12 nodejs14 nodejs18 nodejs20 nodejs22 nodejs24

homepage                https://nodejs.org/
master_sites            ${homepage}dist/v${version}
use_xz                  yes

checksums               rmd160  faf20f558857f1dbe126fd5b7ace2215799931c4 \
                        sha256  576f1a03c455e491a8d132b587eb6b3b84651fc8974bb3638433dd44d22c8f49 \
                        size    36834468

distname                node-v${version}

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

depends_build-append    port:pkgconfig \
                        port:python${py_ver_nodot}
depends_lib-append      path:lib/pkgconfig/icu-uc.pc:icu \
                        port:zlib

if {![variant_isset openssl3]} {
    openssl.branch          1.1
}

compiler.blacklist-append {clang < 900}

# use the system libuv instead of the bundled version, as it is fixed for older systems
if { ${os.platform} eq "darwin" && ${os.major} < 15 } {
    depends_lib-append    path:lib/libuv.dylib:libuv
    configure.args-append --shared-libuv

    if {${configure.cxx_stdlib} eq "libc++"} {
        depends_lib-append        port:macports-libcxx
        configure.cxx-append      -Wl,-L${prefix}/lib/libcxx
    }
}

# suppress a warning-as-error that shows up with newer clang compilers
if {[string match *clang* ${configure.compiler}]} {
    configure.cxx-append      -Wno-error=enum-constexpr-conversion

    #the ventura buildbot will fail because it doesn't recognise this flag
    #so we disable unknown warnings as errors for older compilers
    configure.cxx-append      -Wno-error=unknown-warning-option
}

proc rec_glob {basedir pattern} {
    set files [glob -directory $basedir -nocomplain -type f $pattern]
    foreach dir [glob -directory $basedir -nocomplain -type d *] {
        lappend files {*}[rec_glob $dir $pattern]
    }
    return $files
}

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

patchfiles              patch-common.gypi.diff \
                        patch-configure.diff \
                        patch-tools-getsharedopensslhasquic.py.diff

post-patch {
    foreach f [concat ${worksrcpath}/configure \
                   ${worksrcpath}/tools/gyp/gyp \
                   ${worksrcpath}/deps/v8/tools/objdump-v8 \
                   [rec_glob ${worksrcpath} *.py]] {
        reinplace -q "s|/usr/bin/env python3|${configure.python}|" ${f}
        reinplace -q "s|/usr/bin/env python|${configure.python}|" ${f}
    }
    foreach gypfile [rec_glob ${worksrcpath} *.gyp*] {
        reinplace -q "s|'python3'|'${configure.python}'|" ${gypfile}
        reinplace -q "s|'python'|'${configure.python}'|" ${gypfile}
    }
    if { ${os.platform} eq "darwin" && ${os.major} <= 17 } {
        # Officially nodejs requires Xcode 11 to compile, however, the
        # only item that Xcode 11 has that Xcode 10.1 doesn't is
        # os/signpost.h, so disable system instrumentation which uses
        # it. This provides support for macOS 10.13 which can only use
        # Xcode 10.1.
        reinplace -q "s|'v8_enable_system_instrumentation': 1|'v8_enable_system_instrumentation': 0|" ${worksrcpath}/tools/v8_gypfiles/features.gypi
    }
}

pre-configure {
    # Copy zlib headers here because we do not want to prepend the entire
    # ${prefix}/include to the include path (the build will then attempt to use
    # ICU headers)
    file mkdir ${workpath}/zlib-inc
    file copy ${prefix}/include/zconf.h ${prefix}/include/zlib.h \
        ${workpath}/zlib-inc/
}

configure.args-append   --without-npm
configure.args-append   --with-intl=system-icu
configure.args-append   --shared-openssl
configure.args-append   --shared-openssl-includes=[openssl::include_dir]/openssl
configure.args-append   --shared-openssl-libpath=[openssl::lib_dir]
configure.args-append   --shared-zlib
configure.args-append   --shared-zlib-includes=${workpath}/zlib-inc
configure.args-append   --shared-zlib-libpath=${prefix}/lib

# V8 only supports ARM and IA-32 processors
supported_archs         i386 x86_64 arm64

universal_variant       no

# "V8 doesn't like cache."
configure.ccache        no

test.run                yes
test.cmd                ${build.cmd} -j${build.jobs}

switch $build_arch {
    i386 {
        configure.args-append   --dest-cpu=ia32
    }
    x86_64 {
        configure.args-append   --dest-cpu=x64
    }
   arm64 {
        configure.args-append   --dest-cpu=arm64
    }
}

# error: integer value is outside the valid range of values for this enumeration type
configure.cxx-append \
                    -Wno-enum-constexpr-conversion \
                    -Wno-unknown-warning-option

build.args-append   CC=${configure.cc} \
                    CXX=${configure.cxx} \
                    CXX.host=${configure.cxx} \
                    CPP=${configure.cpp} \
                    CPPFLAGS="${configure.cppflags}" \
                    CFLAGS="${configure.cflags} ${configure.cppflags}" \
                    CXXFLAGS="${configure.cxxflags} ${configure.cppflags}" \
                    LDFLAGS="${configure.ldflags} [legacysupport::get_library_link_flags]" \
                    PYTHON=${configure.python} \
                    V=1

destroot {
    set bindir ${destroot}${prefix}/bin
    set libdir ${destroot}${prefix}/lib
    set libndir ${libdir}/node
    set libddir ${libdir}/dtrace
    set incdir ${destroot}${prefix}/include/node
    set docdir ${destroot}${prefix}/share/doc/${name}

    xinstall -d ${bindir}
    xinstall -d ${libdir}
    xinstall -d ${libndir}
    xinstall -d ${libddir}
    xinstall -d ${incdir}
    xinstall -d ${docdir}

    # install binaries
    xinstall -m 755 -W ${worksrcpath} \
        out/Release/node \
        ${bindir}

    # install headers
    xinstall -m 644 {*}[glob ${worksrcpath}/src/*.h]                ${incdir}
    xinstall -m 644 {*}[glob ${worksrcpath}/deps/v8/include/*.h]    ${incdir}
    xinstall -m 644 {*}[glob ${worksrcpath}/deps/uv/include/*.h]    ${incdir}
    xinstall -m 644 {*}[glob ${worksrcpath}/deps/cares/include/*.h] ${incdir}

    # install dtrace script
    xinstall -m 644 -W ${worksrcpath} \
        src/node.d \
        ${libddir}

    # install manpage
    xinstall -m 644 -W ${worksrcpath} \
        doc/node.1 \
        ${destroot}${prefix}/share/man/man1

    # install docs
    xinstall -m 644 -W ${worksrcpath} \
        AUTHORS \
        CHANGELOG.md \
        CONTRIBUTING.md \
        GOVERNANCE.md \
        LICENSE \
        README.md \
        SECURITY.md \
        ${docdir}
}

variant openssl3 description {Link OpenSSL 3 instead of 1.1} {}

livecheck.url       ${homepage}dist/
livecheck.type      regex
livecheck.regex     {v(16\.\d+\.\d+)}
