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

PortSystem          1.0

name                pypy
version             5.4.1
categories          lang python devel
license             MIT PSF
maintainers         jmr openmaintainer
description         implementation of Python in Python
long_description \
   PyPy is both a reimplementation of Python in Python, and a framework for \
   implementing interpreters and virtual machines for programming languages, \
   especially dynamic languages.

platforms           darwin

homepage            http://pypy.org/
master_sites        https://bitbucket.org/pypy/pypy/downloads/
use_bzip2           yes
distname            ${name}2-v${version}-src
checksums           md5 d1d197d16331aa23a7fd4b5d4c3c1717 \
                    rmd160 1fa3b9972637aeb597970c7b4666db51a6cffbd6 \
                    sha256 45dbc50c81498f6f1067201b8fc887074b43b84ee32cc47f15e7db17571e9352

depends_build       port:pkgconfig
depends_lib         port:libffi \
                    port:sqlite3 \
                    port:bzip2 \
                    path:lib/libssl.dylib:openssl \
                    port:gettext \
                    port:ncurses

patchfiles          darwin.py.diff \
                    make_output.diff \
                    configure.py.diff \
                    ffiplatform.py.diff \
                    cbuild.py.diff

post-patch {
    reinplace "s|__PREFIX__|${prefix}|" ${worksrcpath}/lib_pypy/cffi/ffiplatform.py \
                                        ${worksrcpath}/ctypes_configure/cbuild.py
}

use_configure       no

# use pypy to build if it's already installed
#if {[file executable ${prefix}/lib/pypy/pypy]} {
#    build.cmd       ${prefix}/lib/pypy/pypy
#} else {
    if {${os.platform} eq "darwin" && ${os.arch} eq "i386" && ![catch {sysctl hw.cpu64bit_capable} is64bit] && $is64bit == 1} {
        depends_build-append port:pypy-bootstrap
        build.cmd       ${prefix}/lib/pypy-bootstrap/bin/pypy
    } else {
        depends_build-append port:python27
        build.cmd       ${prefix}/bin/python2.7
    }
#}

# a lot of memory is used before the C compiler even runs, so limit build.jobs
# according to available memory more tightly than the default
if {![catch {sysctl hw.memsize} memsize]} {
    if {$build_arch eq "x86_64" || $build_arch eq "ppc64"} {
        incr memsize -4000000000
    } else {
        incr memsize -2000000000
    }
    if {$memsize <= 0} {
        build.jobs 1
    } elseif {${build.jobs} > $memsize / 1000000000 + 1} {
        build.jobs [expr {$memsize / 1000000000 + 1}]
    }
}

# memory usage bug in gcc 4.2 on x86_64
if {$build_arch eq "x86_64"} {
    compiler.blacklist gcc-4.2
}

platform darwin {
    # use arch -foo if available
    if {${os.major} >= 9} {
        build.cmd arch -${build_arch} ${build.cmd}
    }
    # MacOS Sierra patch
    if {${os.major} >= 16} {
        patchfiles-append rtime.py.diff
    }
}
build.env           PYPY_USESSION_DIR=${workpath} PYPY_LOCALBASE=${prefix} \
                    CFLAGS="${configure.cc_archflags} ${configure.cppflags}" \
                    LDFLAGS="${configure.ld_archflags} ${configure.ldflags}" \
                    PYPY_CC="${configure.cc}"

build.dir           ${worksrcpath}/pypy/goal
build.args          --batch --verbose \
                    --cc=${configure.cc} --opt=jit \
                    --make-jobs=${build.jobs}
build.target        ../../rpython/bin/rpython
build.post_args     targetpypystandalone

# JIT is not available on powerpc at present
if {${os.arch} ne "i386"} {
    build.args-delete --opt=jit
    build.args-append --opt=3
}

post-build {
    # some modules have to be compiled in an extra step
    foreach script {_audioop_build.py _curses_build.py _pwdgrp_build.py _sqlite3_build.py _syslog_build.py} {
        system -W ${worksrcpath}/lib_pypy "${build.dir}/pypy-c $script"
    }
}

destroot {
    xinstall -d ${destroot}${prefix}/lib/${name}/lib-python
    copy ${worksrcpath}/lib-python/2.7 ${destroot}${prefix}/lib/${name}/lib-python
    copy ${worksrcpath}/lib_pypy ${worksrcpath}/include \
         ${destroot}${prefix}/lib/${name}
    # library search paths changed quite inconveniently such that the binary
    # won't run if it's in ${prefix}/bin
    xinstall -m 755 ${build.dir}/pypy-c ${destroot}${prefix}/lib/${name}/pypy
    xinstall ${build.dir}/libpypy-c.dylib ${destroot}${prefix}/lib/${name}
    ln -s ${prefix}/lib/${name}/pypy ${destroot}${prefix}/bin

    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -W ${worksrcpath} -m 644 README.rst LICENSE ${destroot}${prefix}/share/doc/${name}
}

variant opt1 description {use optimization level 1 for faster build (but slower execution)} {
    depends_lib-append port:boehmgc
    build.args-delete  --opt=3 --opt=jit
    build.args-append  --opt=1
}

livecheck.type      regex
livecheck.url       http://pypy.org/download.html
livecheck.regex     pypy2-v(\[0-9.\]+)-src${extract.suffix}