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