# -*- 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: Portfile 119980 2014-05-12 15:55:20Z jmr@macports.org $

PortSystem          1.0

name                pypy
version             2.3
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}-${version}-src
worksrcdir          pypy-pypy-394146e9bb67

checksums           md5 c93a8e47f3b3109af2f66d2bd766eb97 \
                    sha1 8d29b5948510fd0d4fd9d4376e41c48440cade4c \
                    rmd160 3587130f649a95fe42d4ecc3a48af53c76860d57

depends_build       port:pkgconfig
depends_lib         port:libffi \
                    port:sqlite3 \
                    port:bzip2 \
                    port:openssl \
                    port:gettext

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

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 {
    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}
    }
}
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 --translation-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
}

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
    ln -s ${prefix}/lib/${name}/pypy ${destroot}${prefix}/bin
    # some modules have to be compiled at this point
    system "${destroot}${prefix}/lib/${name}/pypy -c 'import _sqlite3; import _curses; import syslog'"

    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     pypy-(\[0-9.\]+)-src${extract.suffix}
