RPython Compilation Quick Reference
Table of Contents
1. How to compile python into native binaries
Well first things first, this is way more ram hungry than you'd expect, and you're restricted to python 2. BUT, you do end up with nice fast binaries at the end. So let's proceed!
I never remember the incantations you need to compile the code, and both pypy's and rpython's docs have the examples fairly buried under assumptions that you're really just looking to debug the pypy interpreter, if you aren't just looking for a readymade binary of pypy itself. From what I can tell, the main build command that generates pypy has rpython call what they dub a "translation goal". RPython has much more about these translations and how you can perform them interactively with an rpython shell (which is neat), but for our purposes: rpython ≅ compiler, and it just needs to be pointed at a valid translation goal file. Which is luckily just an ordinary python 2 file with a couple specific functions defined like `entrypoint`. The nop (no operation) program for testing that rpython provides is as follows:
""" A simple standalone target. The target below specifies None as the argument types list. This is a case treated specially in driver.py . If the list of input types is empty, it is meant to be a list of strings, actually implementing argv of the executable. """ def debug(msg): print "debug:", msg # __________ Entry point __________ def entry_point(argv): debug("hello world") return 0 # _____ Define and setup target ___ def target(*args): return entry_point
1.1. Compiling
So we've got the targetnopstandalone.py, we've got the pypy3 source code downloaded, how do you actually compile this thing? Short version:
# where cwd is pypy[version stuff]-src/ cd rpython ./bin/rpython translator/goal/targetnopstandalone.py
I'm not sure yet how to just get rpython working outside of the pypy src dir, so for now just make a new dir in pypy3.[python version]-[pypy version]-src/rpython/ (I'm calling mine `myprojects` so it's obvious). For convenience you can make a symlink, but whatever. In `myprojects/testapp/` I copied targetnopstandalone.py as targetapp.py and wrote the following Makefile
BIN := app SRC := ./targetapp.py PYC := ~/Apps/pypy3.10-v7.3.17-src/rpython/bin/rpython .PHONY: all all: $(PYC) $(SRC) .PHONY: clean clean: rm target$(BIN)-c *.pyc