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

name                mozjs60
version             60.8.0
categories          lang
license             {MPL-2 LGPL-2.1+}
maintainers         {devans @dbevans} openmaintainer
description         JavaScript-C Engine
long_description    SpiderMonkey is Mozilla's JavaScript engine written in C/C++. \
                    It is used in various Mozilla products, including Firefox, \
                    and is available under the MPL2.

homepage            https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey
# build from GNOME releng tarball
master_sites        https://ftp.gnome.org/pub/GNOME/teams/releng/tarballs-needing-help/mozjs/

distname            mozjs-${version}
use_bzip2           yes

checksums           rmd160  babfc85cd14308a1494d719a5c281b3d392a046c \
                    sha256  697331336c3d65b80ded9ca87b4a8ceb804e5342b476eaa133ac638102a9dc5d \
                    size    32802956

depends_build       port:autoconf213 \
                    path:bin/pkg-config:pkgconfig \
                    port:python27 \
                    port:yasm

depends_lib         port:nspr \
                    port:xorg-libX11 \
                    port:xorg-libXt

# requires C++14 compiler to build
compiler.cxx_standard \
                    2014
compiler.blacklist  {clang < 700}

if { ${os.platform} eq "darwin" && ${os.major} < 11 } {
    depends_build-append \
                    port:cctools
    configure.env-append \
                    AR=${prefix}/bin/ar
}

patchfiles          patch-js.pc.in.diff \
                    patch-js-config.in.diff \
                    patch-virtualenv-arm64-codesign.diff

# Use absolute path for install_name
post-patch {
    reinplace "s|@executable_path|${prefix}/lib|g" ${worksrcpath}/config/rules.mk
}

configure.perl      /usr/bin/perl
configure.python    ${prefix}/bin/python2.7

configure.env-append \
                    SHELL=/bin/bash

configure.dir       ${worksrcpath}/js/src/obj
configure.cmd       ../configure

configure.args      --disable-readline \
                    --with-system-nspr

configure.universal_args-delete --disable-dependency-tracking

if { ${os.platform} eq "darwin" && ${os.major} < 13 } {
    configure.args-append       --disable-xcode-checks
}

build.env-append    SHELL=/bin/bash
build.dir           ${worksrcpath}/js/src/obj
destroot.dir        ${worksrcpath}/js/src/obj

post-destroot {
    # make static lib name version specific to avoid conflict with other mozjs versions
    move ${destroot}${prefix}/lib/libjs_static.ajs ${destroot}${prefix}/lib/libjs60_static.ajs
}

if {${universal_possible} && [variant_isset universal]} {
    merger-post-destroot {
        foreach arch ${universal_archs_to_use} {
            set dir ${destroot}-${arch}
            # mozjs60 builds and links with libmozglue.dylib but fails to install it
            xinstall -m 0755 ${worksrcpath}-${arch}/js/src/obj/mozglue/build/libmozglue.dylib ${dir}${prefix}/lib
        }
    }
} else {
    post-destroot {
        # mozjs60 builds and links with libmozglue.dylib but fails to install it
        xinstall -m 0755 ${worksrcpath}/js/src/obj/mozglue/build/libmozglue.dylib ${destroot}${prefix}/lib
    }
}

if {${universal_possible} && [variant_isset universal]} {
    set merger_host(x86_64) x86_64-apple-${os.platform}${os.major}
    set merger_host(i386) i686-apple-${os.platform}${os.major}
    set merger_configure_args(x86_64) "--host=x86_64-apple-${os.platform}${os.major} --target=x86_64-apple-${os.platform}${os.major}"
    set merger_configure_args(i386) "--host=i686-apple-${os.platform}${os.major} --target=i686-apple-${os.platform}${os.major}"
} else {
    if {${build_arch} eq "i386"} {
        configure.args-append \
            --host=i686-apple-${os.platform}${os.major} \
            --target=i686-apple-${os.platform}${os.major}
    } elseif {${build_arch} in "x86_64 ppc ppc64"} {
        configure.args-append \
            --host=${build_arch}-apple-${os.platform}${os.major} \
            --target=${build_arch}-apple-${os.platform}${os.major}
    }
}

if { [string match *gcc* ${configure.compiler}] && ${configure.cxx_stdlib} ne "libc++" } {

    patchfiles-append \
                    patch-libatomic.diff \
                    patch-no-libc++.diff
}

if { ${os.platform} eq "darwin" && ${configure.build_arch} in [list ppc ppc64] } {

    # The C API is littered with bools and references to bools, so we need to
    # make sure clients (including gjs and policykit) also use the magic flag.
    # We do this via pkg-config and js-config; note that with this approach
    # there is some danger that distinct compilation units within the same
    # project may end up with different bool sizes (for example, if a project's
    # Makefile only applies the result of `pkg-config --cflags` to source files
    # that actually consume <jsapi.h>), so some care is needed when compiling
    # against this library. The benefit of this approach is that projects using
    # pkg-config don't need to remember this flag, and well-designed projects
    # can "contain" one-byte bools and not have to expose them downstream.

    patchfiles-append \
                    patch-config-powerpc.diff \
                    patch-powerpc.diff

    # Make structs size-compatible with Intel (fixes several static_asserts)
    configure.cxxflags-append \
                    -mone-byte-bool

    notes-append "
    In order to run on PowerPC, ${name} has been compiled with the special
    compiler flag, -mone-byte-bool. Most users are unaffected; however, if you
    are a software developer compiling against the SpiderMonkey C API (jsapi.h),
    you will need to add this compiler flag to your own projects, or prepare to
    encounter unexpected program behavior. The flag will be added automatically
    to projects that use pkg-config or js-config."
}

# Now for 10.4/10.5 and Rosetta
if { ${os.platform} eq "darwin" && ${os.major} < 10 \
    || (${os.major} == 10 && ${configure.build_arch} eq "ppc") } {

    patchfiles-append \
                    patch-malloc_default_purgeable_zone.diff \
                    patch-js-shell.diff \
                    patch-pthread_setname_np.diff

    post-configure {
        # Some of the patches disrupt the configure timestamp chain
        touch ${worksrcpath}/js/src/configure
        touch ${worksrcpath}/js/src/obj/config.status
    }

    if { ${os.major} == 8 } {
        # Tiger ships with Make 3.80; need 3.81 or later
        depends_build-append \
                            port:gmake
        build.cmd           gmake
        configure.cppflags-append \
                            -D__DARWIN_UNIX03
    }

    # the old gas assembler doesn't like the included ICU files.
    # Also there's a run-time failure in u_init(), so just
    # disable ICU until someone wants to sort it out.
    configure.args-append   --without-intl-api

}

livecheck.type      none
