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

github.setup        Constellation iv 64c3a9c7c517063f29d90d449180ea8f6f4d946f
version             2015.03.24
revision            0
categories          devel
license             BSD
maintainers         nomaintainer
description         ${name} is ECMA262 5.1 lexer and parser and engine project
long_description    {*}${description} written in C++/JS.
checksums           rmd160  8a5446eedf849aede8c7610fcdb39922f30805f6 \
                    sha256  f001266dd3a365e7ac3e7eb12fb77629177f58707d7e7e7a94a9de76d9c80b8c \
                    size    9268616
github.tarball_from archive

patchfiles          patch-lv5.diff \
                    patch-do-not-fetch-tools.diff

# FIXME: building on aarch64 is fixed now, but binary still complains on launch:
# https://github.com/Constellation/iv/issues/107
# Also, ideally we would want to build against our boehmgc,
# but as of now it fails with undefined symbols:
# GC_throw_bad_alloc() etc.
# https://github.com/Constellation/iv/pull/109
patchfiles-append   patch-gc-arm64.diff

# https://github.com/Constellation/iv/pull/110
patchfiles-append   patch-static-liblv5.diff

# https://github.com/Constellation/iv/pull/111
patchfiles-append   patch-fpu.h-fix-i386-on-macOS.diff

post-patch {
    reinplace "s|__PREFIX__|${prefix}|" \
                    ${worksrcpath}/iv/lv5/CMakeLists.txt
    reinplace "s|__ARCH__|${configure.build_arch}|" \
                    ${worksrcpath}/iv/lv5/CMakeLists.txt
}

if {${os.platform} eq "darwin" && ${os.major} < 14} {
    depends_build-append \
                    port:git
    git.cmd         ${prefix}/bin/git
}

compiler.cxx_standard 2011
compiler.blacklist-append {clang}

configure.args-append \
                    -DJIT=OFF \
                    -DLTO=ON

if {${os.platform} eq "darwin" && ${os.major} > 21} {
    configure.cppflags-append \
                    -D_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION
}

# https://github.com/Constellation/iv/issues/104
if {${configure.build_arch} in [list arm i386 ppc]} {
    configure.args-append \
                    -DM32=ON
    if {[string match *gcc* ${configure.compiler}]} {
        configure.ldflags-append \
                    -latomic
    }
}

platform powerpc {
    configure.args-replace \
                    -DLTO=ON -DLTO=OFF
}

universal_variant   no

destroot {
    copy ${cmake.build_dir}/iv/lv5/lv5 ${destroot}${prefix}/bin
    foreach f {iv/lv5/liblv5.a \
                iv/lv5/third_party/gc/libgc-lib.a \
                iv/lv5/third_party/gc/libgcmt-dll.dylib
                iv/lv5/third_party/gc/libgcmt-lib.a
                iv/lv5/third_party/netlib_dtoa/libnetlib_dtoa.a
                iv/lv5/third_party/v8_dtoa/libv8_dtoa.a} {
        copy ${cmake.build_dir}/${f} ${destroot}${prefix}/lib
    }
}

# FIXME: there is a failure in the second batch of tests on PowerPC:
# https://github.com/Constellation/iv/issues/106
# Note: use gdb-apple to debug, since native gdb is useless here due to:
# https://gcc-help.gcc.gnu.narkive.com/sI2gcsUK/what-does-dwarf-error-cannot-find-type-of-die-mean
test.run            yes
test.cmd            ${cmake.build_dir}/iv/lv5/third_party/gc/tests/gctest \
                    && ${cmake.build_dir}/iv/test/iv_unit_tests \
                    && ${cmake.build_dir}/iv/lv5/test/lv5_unit_tests
test.target
