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

# EOL 2019-12-31
# https://github.com/nodejs/Release
deprecated.eol_version  yes

name                    nodejs8
version                 8.17.0
revision                1

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               nodejs10 nodejs12 nodejs14 nodejs16 nodejs18 nodejs20 nodejs22 nodejs24 nodejs26

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

checksums               rmd160  25d6bbd750cec1267fb8501999e81f9b5f9daf2e \
                        sha256  5b0d96db482b273f0324c299ead86ecfbc5d033516e5fc37c92cfccb933ef6ff \
                        size    19013844

distname                node-v${version}

depends_build-append    port:pkgconfig

# Require OpenSSL 1.1 and not generic OpenSSL
# https://trac.macports.org/ticket/64095
depends_lib-append      port:python27 \
                        port:openssl11

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/python2.7

patchfiles              patch-common.gypi.diff \
                        patch-tools-gyp-pylib-gyp-generator-make.py.diff

post-patch {
    foreach f [concat ${worksrcpath}/configure \
                   ${worksrcpath}/tools/gyp/gyp \
                   ${worksrcpath}/deps/cares/gyp_cares \
                   ${worksrcpath}/deps/v8/gypfiles/gyp_v8 \
                   ${worksrcpath}/deps/v8/tools/objdump-v8 \
                   ${worksrcpath}/deps/v8/tools/mingw-generate-makefiles.sh \
                   [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|/usr/bin/env node|${prefix}/bin/node|" ${worksrcpath}/tools/doc/node_modules/marked/bin/marked
    reinplace "s|python|${configure.python}|" ${worksrcpath}/deps/v8/gypfiles/toolchain.gypi
}

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

configure.args-append   --without-npm
configure.args-append   --with-intl=small-icu
configure.args-append   --shared-openssl
configure.args-append   --shared-openssl-includes=${prefix}/include/openssl-1.1
configure.args-append   --shared-openssl-libname=crypto.1.1,ssl.1.1
configure.args-append   --shared-openssl-libpath=${prefix}/lib/openssl-1.1

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

universal_variant       no

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

test.run                yes

use_parallel_build      no

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

# on macOS nodejs only builds against libc++
# this force is OK as node does not link against any other c++ libs
depends_lib-append      port:libcxx
configure.cxx_stdlib    libc++
compiler.cxx_standard   2011
# error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr
compiler.blacklist-append *gcc* {clang < 600}

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 \
        COLLABORATOR_GUIDE.md \
        CONTRIBUTING.md \
        GOVERNANCE.md \
        LICENSE \
        README.md \
        ${docdir}
}

notes "
${name} does not contain npm but it can be installed as a separate port.\
Pick from the choices listed by running:

port search --name --glob 'npm*'
"

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