| 1 |
.. _building-from-source: |
| 2 |
|
| 3 |
Building PyPy from Source |
| 4 |
========================= |
| 5 |
|
| 6 |
Building PyPy requires a Python2 interpreter since RPython is a Python2 |
| 7 |
dialect. For building PyPy, we recommend installing a pre-built PyPy2.7 first |
| 8 |
(see :doc:`install`). It is possible to build PyPy with CPython2.7, but it will |
| 9 |
take a lot longer to run -- depending on your architecture, between two and |
| 10 |
three times as long. |
| 11 |
|
| 12 |
Even when using PyPy2.7 to build PyPy, translation is time-consuming -- 20 |
| 13 |
minutes on a fast machine -- and RAM-hungry. You will need **at least** 3 GB |
| 14 |
of memory on a 32-bit machine and 6GB on a 64-bit machine. |
| 15 |
|
| 16 |
|
| 17 |
Before you start |
| 18 |
---------------- |
| 19 |
|
| 20 |
Our normal development workflow avoids a full translation by using test-driven |
| 21 |
development. You can read more about how to develop PyPy here_, and latest |
| 22 |
translated (hopefully functional) binary packages are available on our |
| 23 |
buildbot's `nightly builds`_. |
| 24 |
|
| 25 |
.. _here: contributing.html |
| 26 |
.. _`nightly builds`: https://buildbot.pypy.org/nightly |
| 27 |
|
| 28 |
You will need the build dependencies below to run the tests. |
| 29 |
|
| 30 |
|
| 31 |
Clone the repository |
| 32 |
-------------------- |
| 33 |
|
| 34 |
If you prefer to compile your own PyPy, or if you want to modify it, you |
| 35 |
will need to obtain a copy of the sources. This can be done either by |
| 36 |
`downloading them from the download page`_ or by checking them out from the |
| 37 |
repository using git. We suggest using git if you want to access |
| 38 |
the current development. |
| 39 |
|
| 40 |
.. _downloading them from the download page: https://www.pypy.org/download.html |
| 41 |
|
| 42 |
You must issue the following command on your |
| 43 |
command line, DOS box, or terminal:: |
| 44 |
|
| 45 |
git clone https://github.com/pypy/pypy.git |
| 46 |
|
| 47 |
This will clone the repository and place it into a directory |
| 48 |
named ``pypy``, and will get you the PyPy source in ``pypy/pypy`` and |
| 49 |
documentation files in ``pypy/pypy/doc``. |
| 50 |
We try to ensure that the tip is always stable, but it might |
| 51 |
occasionally be broken. You may want to check out `our nightly tests`_: |
| 52 |
find a revision hash, e.g. "963e808156b3", that passed at least the |
| 53 |
``{linux64}`` tests (corresponding to a ``+`` sign on the |
| 54 |
line ``success``) and then, in your cloned repository, switch to this revision |
| 55 |
using:: |
| 56 |
|
| 57 |
git checkout XXXXX |
| 58 |
|
| 59 |
where ``XXXXX`` is the revision hash. |
| 60 |
|
| 61 |
.. _our nightly tests: https://buildbot.pypy.org/summary?branch=main |
| 62 |
|
| 63 |
|
| 64 |
Install build-time dependencies |
| 65 |
------------------------------- |
| 66 |
|
| 67 |
.. note:: |
| 68 |
|
| 69 |
For some hints on how to translate the Python interpreter under |
| 70 |
Windows, see the :ref:`Windows document <translating-on-windows>`. |
| 71 |
|
| 72 |
The host Python2 needs to have CFFI installed. If translating on PyPy, CFFI |
| 73 |
is already installed. If translating on CPython, you need to install it, e.g. |
| 74 |
using ``python2.7 -mpip install cffi``. |
| 75 |
|
| 76 |
To build PyPy on Unix using the C translation backend, you need at least a C |
| 77 |
compiler and ``make`` installed. Further, some optional modules have additional |
| 78 |
dependencies: |
| 79 |
|
| 80 |
cffi, ctypes |
| 81 |
libffi, pkg-config |
| 82 |
|
| 83 |
zlib |
| 84 |
libz |
| 85 |
|
| 86 |
bz2 |
| 87 |
libbz2 |
| 88 |
|
| 89 |
pyexpat |
| 90 |
libexpat1 |
| 91 |
|
| 92 |
_vmprof |
| 93 |
libunwind (optional, loaded dynamically at runtime) |
| 94 |
|
| 95 |
Make sure to have these libraries (with development headers) installed |
| 96 |
before building PyPy, otherwise the resulting binary will not contain |
| 97 |
these modules. Furthermore, the following libraries should be present |
| 98 |
after building PyPy, otherwise the corresponding CFFI modules are not |
| 99 |
built (you can run or re-run :source:`lib_pypy/pypy_tools/build_cffi_imports.py` |
| 100 |
to build them; you don't need to re-translate the whole PyPy): |
| 101 |
|
| 102 |
sqlite3 |
| 103 |
libsqlite3 |
| 104 |
|
| 105 |
_ssl, _hashlib |
| 106 |
libssl |
| 107 |
|
| 108 |
curses |
| 109 |
libncurses-dev (for PyPy2) |
| 110 |
libncursesw-dev (for PyPy3) |
| 111 |
|
| 112 |
gdbm |
| 113 |
libgdbm-dev |
| 114 |
|
| 115 |
tk |
| 116 |
tk-dev |
| 117 |
|
| 118 |
lzma (PyPy3 only) |
| 119 |
liblzma or libxz, version 5 and up |
| 120 |
|
| 121 |
To run untranslated tests, you need the Boehm garbage collector libgc, version |
| 122 |
7.4 and later. |
| 123 |
|
| 124 |
On Debian and Ubuntu (16.04 onwards), this is the command to install |
| 125 |
all build-time dependencies:: |
| 126 |
|
| 127 |
apt-get install gcc make libffi-dev pkg-config zlib1g-dev libbz2-dev \ |
| 128 |
libsqlite3-dev libncurses5-dev libexpat1-dev libssl-dev libgdbm-dev \ |
| 129 |
tk-dev libgc-dev \ |
| 130 |
liblzma-dev libncursesw5-dev # These two are only needed on PyPy3 |
| 131 |
|
| 132 |
On Fedora:: |
| 133 |
|
| 134 |
dnf install gcc make libffi-devel pkgconfig zlib-devel bzip2-devel \ |
| 135 |
sqlite-devel ncurses-devel expat-devel openssl-devel tk-devel \ |
| 136 |
gdbm-devel gc-devel \ |
| 137 |
xz-devel # For lzma on PyPy3. |
| 138 |
|
| 139 |
On SLES11:: |
| 140 |
|
| 141 |
zypper install gcc make python-devel pkg-config \ |
| 142 |
zlib-devel libopenssl-devel libbz2-devel sqlite3-devel \ |
| 143 |
libexpat-devel libffi-devel \ |
| 144 |
xz-devel # For lzma on PyPy3. |
| 145 |
(XXX plus the SLES11 version of libgdbm-dev and tk-dev) |
| 146 |
|
| 147 |
On Mac OS X: |
| 148 |
|
| 149 |
Currently PyPy supports both building on both Apple Silicon (M1, Arm64) and |
| 150 |
X86_64. You must use an appropriate toolchain for building: either ``arm64`` |
| 151 |
or ``x86_64``. "Fat" universal2 builds are not supported. |
| 152 |
|
| 153 |
Most of the build-time dependencies are installed alongside the Developer |
| 154 |
Tools. ``libx11`` is needed for ``tkinter``. ``openssl`` needs to be |
| 155 |
installed for tests, and a brew-provided pypy will speed up translation. Note |
| 156 |
that you must use the architecture-appropriate x86_64 or arm64 ``brew`` |
| 157 |
command: |
| 158 |
|
| 159 |
.. code-block:: shell |
| 160 |
|
| 161 |
xcode-select --install |
| 162 |
brew install openssl pypy pkg-config libx11 |
| 163 |
# expose openssl in the cffi _ssl_build script |
| 164 |
export CPPFLAGS=$(pkg-config openssl --cflags-only-I) |
| 165 |
export LDFLAGS=$(pkg-config openssl --libs-only-L) |
| 166 |
|
| 167 |
|
| 168 |
Set environment variables that will affect translation |
| 169 |
------------------------------------------------------ |
| 170 |
|
| 171 |
The following environment variables can be used to tweak the result: |
| 172 |
|
| 173 |
+------------------------+-----------------------------------------------------------+ |
| 174 |
| value | result | |
| 175 |
+------------------------+-----------------------------------------------------------+ |
| 176 |
| CC | compiler to use | |
| 177 |
+------------------------+-----------------------------------------------------------+ |
| 178 |
| PYPY_MULTIARCH | pypy 3.7+: ends up in ``sys.platform._multiarch`` | |
| 179 |
| | on posix, defaults to ``x86_64-linux-gnu`` | |
| 180 |
+------------------------+-----------------------------------------------------------+ |
| 181 |
| PYPY_USESSION_DIR | base directory for temporary files, usually ``$TMP`` | |
| 182 |
+------------------------+-----------------------------------------------------------+ |
| 183 |
| PYPY_USESSION_BASENAME | each call to ``from rpython.tools import udir`` will get | |
| 184 |
| | a temporary directory | |
| 185 |
| | ``$PYPY_USESSION_DIR/usession-$PYPY_USESSION_BASENAME-N`` | |
| 186 |
| | where ``N`` increments on each call | |
| 187 |
+------------------------+-----------------------------------------------------------+ |
| 188 |
| PYPY_USESSION_KEEP | how many old temporary directories to keep, any older | |
| 189 |
| | ones will be deleted. Defaults to 3 | |
| 190 |
+------------------------+-----------------------------------------------------------+ |
| 191 |
|
| 192 |
|
| 193 |
Run the translation |
| 194 |
------------------- |
| 195 |
|
| 196 |
Since the translating Python needs CFFI, it is best to create a virtualenv and |
| 197 |
then ``pip install cffi`` there. |
| 198 |
|
| 199 |
We usually translate in the ``pypy/goal`` directory, so all the following |
| 200 |
commands assume your ``$pwd`` is there. |
| 201 |
|
| 202 |
Translate with JIT:: |
| 203 |
|
| 204 |
pypy2.7 ../../rpython/bin/rpython --opt=jit |
| 205 |
|
| 206 |
Translate without JIT:: |
| 207 |
|
| 208 |
pypy2.7 ../../rpython/bin/rpython --opt=2 |
| 209 |
|
| 210 |
Note this translates pypy via the ``targetpypystandalone.py`` file, so these |
| 211 |
are shorthand for:: |
| 212 |
|
| 213 |
pypy2.7 ../../rpython/bin/rpython <rpython options> targetpypystandalone.py <pypy options> |
| 214 |
|
| 215 |
More help is available via ``--help`` at either option position, and more info |
| 216 |
can be found in the :doc:`config/index` section. |
| 217 |
|
| 218 |
(You can use ``python2`` instead of ``pypy2.7`` here, which will take longer |
| 219 |
but works too.) |
| 220 |
|
| 221 |
If everything works correctly this will: |
| 222 |
|
| 223 |
1. Run the rpython `translation chain`_, producing a database of the |
| 224 |
entire pypy interpreter. This step is currently single threaded, and RAM |
| 225 |
hungry. As part of this step, the chain creates a large number of C code |
| 226 |
files and a Makefile to compile them in a |
| 227 |
directory controlled by the ``PYPY_USESSION_DIR`` environment variable. |
| 228 |
2. Create an executable ``pypy-c`` or ``pypy3.XX-c`` by running the Makefile. |
| 229 |
This step can utilize all possible cores on the machine. |
| 230 |
3. Copy the needed binaries to the current directory. |
| 231 |
4. Generate c-extension modules for any cffi-based stdlib modules. |
| 232 |
|
| 233 |
The resulting executable behaves mostly like a normal Python |
| 234 |
interpreter (see :doc:`cpython_differences`), and is ready for testing, for |
| 235 |
use as a base interpreter for a new virtualenv, or for packaging into a binary |
| 236 |
suitable for installation on another machine running the same OS as the build |
| 237 |
machine. |
| 238 |
|
| 239 |
Note that step 4 is merely done as a convenience, any of the steps may be rerun |
| 240 |
without rerunning the previous steps. |
| 241 |
|
| 242 |
.. _`translation chain`: https://rpython.readthedocs.io/en/latest/translation.html |
| 243 |
|
| 244 |
|
| 245 |
Making a debug build of PyPy |
| 246 |
---------------------------- |
| 247 |
|
| 248 |
Rerun the ``Makefile`` with the ``make lldebug`` or ``make lldebug0`` target, |
| 249 |
which will build in a way that running under a debugger makes sense. |
| 250 |
Appropriate compilation flags are added to add debug info, and for ``lldebug0`` |
| 251 |
compiler optimizations are fully disabled. If you stop in a debugger, you will |
| 252 |
see the very wordy machine-generated C code from the rpython translation step, |
| 253 |
which takes a little bit of reading to relate back to the rpython code. |
| 254 |
|
| 255 |
|
| 256 |
Build cffi import libraries for the stdlib |
| 257 |
------------------------------------------ |
| 258 |
|
| 259 |
Various stdlib modules require a separate build step to create the cffi |
| 260 |
import libraries in the :ref:`out-of-line API mode <performance>`. This is done by the following |
| 261 |
command:: |
| 262 |
|
| 263 |
cd pypy/goal |
| 264 |
PYTHONPATH=../.. ./pypy-c ../../lib_pypy/pypy_tools/build_cffi_imports.py |
| 265 |
|
| 266 |
|
| 267 |
Packaging (preparing for installation) |
| 268 |
-------------------------------------- |
| 269 |
|
| 270 |
Packaging is required if you want to install PyPy system-wide, even to |
| 271 |
install on the same machine. The reason is that doing so prepares a |
| 272 |
number of extra features that cannot be done lazily on a root-installed |
| 273 |
PyPy, because the normal users don't have write access. This concerns |
| 274 |
mostly libraries that would normally be compiled if and when they are |
| 275 |
imported the first time. |
| 276 |
|
| 277 |
:: |
| 278 |
|
| 279 |
python pypy/tool/release/package.py --archive-name=pypy-VER-PLATFORM |
| 280 |
|
| 281 |
This creates a clean and prepared hierarchy, as well as a ``.tar.bz2`` |
| 282 |
with the same content; the directory to find these will be printed out. You |
| 283 |
can then either move the file hierarchy or unpack the ``.tar.bz2`` at the |
| 284 |
correct place. |
| 285 |
|
| 286 |
It is recommended to use package.py because custom scripts will |
| 287 |
invariably become out-of-date. If you want to write custom scripts |
| 288 |
anyway, note an easy-to-miss point: some modules are written with CFFI, |
| 289 |
and require some compilation. If you install PyPy as root without |
| 290 |
pre-compiling them, normal users will get errors. |
| 291 |
|
| 292 |
|
| 293 |
Installation |
| 294 |
------------ |
| 295 |
|
| 296 |
PyPy dynamically finds the location of its libraries depending on the location |
| 297 |
of the executable. The directory hierarchy of a typical PyPy2 installation |
| 298 |
looks like this:: |
| 299 |
|
| 300 |
./bin/pypy |
| 301 |
./include/ |
| 302 |
./lib_pypy/ |
| 303 |
./lib-python/2.7 |
| 304 |
./site-packages/ |
| 305 |
|
| 306 |
A PyPy3.8+ installation will match the CPython layout:: |
| 307 |
|
| 308 |
./bin/ |
| 309 |
./include/pypy3.8/include |
| 310 |
./lib/pypy3.8 |
| 311 |
|
| 312 |
The hierarchy shown above is relative to a PREFIX directory. PREFIX is |
| 313 |
computed by starting from the directory where the executable resides, and |
| 314 |
"walking up" the filesystem until we find a directory containing ``lib_pypy`` |
| 315 |
and ``lib-python/2.7`` (on pypy2). |
| 316 |
|
| 317 |
To install PyPy system wide on unix-like systems, it is recommended to put the |
| 318 |
whole hierarchy alone (e.g. in ``/opt/pypy``) and put a symlink to the |
| 319 |
``pypy`` executable into ``/usr/bin`` or ``/usr/local/bin``. |
| 320 |
|
| 321 |
If the executable fails to find suitable libraries, it will report ``debug: |
| 322 |
WARNING: library path not found, using compiled-in sys.path`` and then attempt |
| 323 |
to continue normally. If the default path is usable, most code will be fine. |
| 324 |
However, the ``sys.prefix`` will be unset and some existing libraries assume |
| 325 |
that this is never the case. |