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

# FIXME: at least on 32-bit ppc it fails to link, see below.
known_fail              yes

# Peg back to openssl 1.1 release
openssl.branch          1.1
openssl.configure       pkgconfig build_flags

# on macOS nodejs only builds against libc++
# this force is OK as node does not link against any other c++ libs
if {${os.arch} ne "powerpc"} {
    depends_lib-append      port:libcxx
    configure.cxx_stdlib    libc++
    use_xcode               yes
}
compiler.cxx_standard   2011

name                    nodejs12
version                 12.18.4
revision                0
categories              devel net
license                 {MIT BSD}
maintainers             {ciserlohn @ci42} openmaintainer

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 nodejs13 nodejs14 nodejs15 nodejs16 nodejs17 nodejs18 nodejs19 nodejs20 nodejs21 nodejs22

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

checksums               rmd160  88664567205e878158a4850bfa23523acf1578c6 \
                        sha256  25f03cb18e53b6d0959d0c219e701a85eb4693f526bdda7c72bc6199b364f609 \
                        size    23679924

distname                node-v${version}

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

depends_build-append    path:bin/pkg-config:pkgconfig

depends_lib             port:python${py_ver_nodot} \
                        port:zlib

use_xcode               yes

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}

patch.pre_args-replace  -p0 -p1

# Undefined symbols: "__ZN6icu_7610UnicodeSet6addAllERKS0_"
patchfiles-append       patch-configure-icu.diff

# https://github.com/nodejs/node/issues/30323#issuecomment-1209571050
patchfiles-append       0001-check-if-uclibc-has-backtrace-support.patch \
                        0002-support-ppc32-target.patch \
                        0003-Fix-various-DoubleToInt32-conversion.patch \
                        0004-Fix-preprocessor-typo-and-use-ConvertDoubleToInt32No.patch \
                        0005-ppc32-check-float-conversion-exception.patch \
                        0006-ppc32-return-cr-condition-check-as-it-was-before.patch \
                        0007-ppc32-set-result-to-0-in-conversion-failure.patch \
                        0008-ppc-CmpDouble-fix-unordered-comparison-check.patch \
                        0009-ppc32-use-different-register-for-truncation-output.patch \
                        0010-objects-shared-function-info-inl.h-revert-value.patch \
                        0011-ppc-AssembleArchBranch-fix-branching-conditions.patch \
                        0012-ppc-define-kScratchReg-properly.patch \
                        0013-ppc-fix-Float64Sqrt-implementation-in-32-bit.patch

patchfiles-append       0014-Fix-build-system.patch \
                        0015-Fix-some-includes.patch \
                        0016-Unbreak-libuv.patch \
                        0017-inspector-unbreak-build-with-gcc.patch \
                        0018-libsampler-support-powerpc-darwin.patch \
                        0019-platform-posix.cc-fix-powerpc-darwin.patch \
                        0020-hack-isolate-data.h.patch \
                        0021-fix-build_config.patch

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 python|${configure.python}|" ${f}
    }
    foreach gypfile [rec_glob ${worksrcpath} *.gyp*] {
        reinplace -q "s|'python'|'${configure.python}'|" ${gypfile}
    }
    reinplace "s|@OSX_V@|${macosx_deployment_target}|g" ${worksrcpath}/common.gypi
}

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

# FIXME: does not build with ICU 76:
# deps/v8/src/objects/js-list-format.cc:172:69: error:
# 'static icu_76::ListFormatter* icu_76::ListFormatter::createInstance(const icu_76::Locale&, const char*, UErrorCode&)'
# is private within this context
# depends_lib-append      path:lib/pkgconfig/icu-uc.pc:icu
# configure.args-append   --with-intl=system-icu

configure.args-append   --without-npm
configure.args-append   --shared-openssl
configure.args-append   --shared-openssl-includes=[openssl::include_dir]
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

# FIXME: as of now, build links, but fails on snapshot generation with illegal instruction.
if {${configure.build_arch} in [list ppc ppc64]} {
    configure.args-append \
                        --v8-lite-mode \
                        --without-dtrace

    # An attempt to reduce ridiculous size of libs which otherwise fail to link.
    # Perhaps this is way too much, and we only need to cut down on debug info somewhat.
    configure.cflags-append \
                        -g0 --param ggc-min-expand=10
    configure.cxxflags-append \
                        -g0 --param ggc-min-expand=10
    configure.optflags
}

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
    }
    ppc {
        configure.args-append   --dest-cpu=ppc
    }
    ppc64 {
        configure.args-append   --dest-cpu=ppc64
    }
}

compiler.blacklist-append {clang < 800}

build.args-append   CC=${configure.cc} \
                    CXX=${configure.cxx} \
                    CXX.host=${configure.cxx} \
                    CPP=${configure.cpp} \
                    CFLAGS="${configure.cflags}" \
                    CXXFLAGS="${configure.cxxflags}" \
                    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 \
        ${docdir}
}

livecheck.type      none
