PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/rpython/doc/arm.rst

https://bitbucket.org/pypy/pypy/
ReStructuredText | 164 lines | 109 code | 55 blank | 0 comment | 0 complexity | b746de67e8e131942d14859af7c10d9a MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. .. _arm:
  2. Cross-translating for ARM
  3. =========================
  4. Here we describe the setup required and the steps needed to follow to translate
  5. an interpreter using the RPython translator to target ARM using a cross
  6. compilation toolchain.
  7. To translate an RPython program for ARM we can either
  8. translate directly on an ARM device following the normal translation steps. Unfortunately this is not really feasible on most ARM machines. The alternative is to cross-translate using a cross-compilation toolchain.
  9. To cross-translate we run the translation on a more powerful (usually
  10. x86) machine and generate a binary for ARM using a cross-compiler to compile
  11. the generated C code. There are several constraints when doing this. In
  12. particular we currently only support Linux as translation host and target
  13. platforms (tested on Ubuntu). Also we need a 32-bit environment to run the
  14. translation. This can be done either on a 32bit host or in 32bit chroot.
  15. Requirements
  16. ------------
  17. The tools required to cross translate from a Linux based host to an ARM based Linux target are:
  18. - A checkout of PyPy (default branch).
  19. - The GCC ARM cross compiler (on Ubuntu it is the ``gcc-arm-linux-gnueabi package``) but other toolchains should also work.
  20. - Scratchbox 2, a cross-compilation engine (``scratchbox2`` Ubuntu package).
  21. - A 32-bit PyPy or Python.
  22. - And the following (or corresponding) packages need to be installed to create an ARM based chroot:
  23. * ``debootstrap``
  24. * ``schroot``
  25. * ``binfmt-support``
  26. * ``qemu-system``
  27. * ``qemu-user-static``
  28. - The dependencies above are in addition to the ones needed for a regular
  29. translation, `listed here`_.
  30. .. _`listed here`: http://pypy.readthedocs.org/en/latest/build.html#install-build-time-dependencies
  31. Creating a Qemu based ARM chroot
  32. --------------------------------
  33. First we will need to create a rootfs containing the packages and dependencies
  34. required in order to translate PyPy or other interpreters. We are going to
  35. assume, that the files will be placed in ``/srv/chroot/precise_arm``.
  36. Create the rootfs by calling:
  37. ::
  38. mkdir -p /srv/chroot/precise_arm
  39. qemu-debootstrap --variant=buildd --arch=armel precise /srv/chroot/precise_arm/ http://ports.ubuntu.com/ubuntu-ports/
  40. Next, copy the qemu-arm-static binary to the rootfs.
  41. ::
  42. cp /usr/bin/qemu-arm-static /srv/chroot/precise_arm/usr/bin/qemu-arm-static
  43. For easier configuration and management we will create a schroot pointing to
  44. the rootfs. We need to add a configuration block (like the one below) to the
  45. schroot configuration file in /etc/schroot/schroot.conf.
  46. ::
  47. [precise_arm]
  48. directory=/srv/chroot/precise_arm
  49. users=USERNAME
  50. root-users=USERNAME
  51. groups=users
  52. aliases=default
  53. type=directory
  54. To verify that everything is working in the chroot, running ``schroot -c
  55. precise_arm`` should start a shell running in the schroot environment using
  56. qemu-arm to execute the ARM binaries. Running ``uname -m`` in the chroot should
  57. yield a result like ``armv7l``. Showing that we are emulating an ARM system.
  58. Start the schroot as the user root in order to configure the apt sources and
  59. to install the following packages:
  60. ::
  61. schroot -c precise_arm -u root
  62. echo "deb http://ports.ubuntu.com/ubuntu-ports/ precise main universe restricted" > /etc/apt/sources.list
  63. apt-get update
  64. apt-get install libffi-dev libgc-dev python-dev build-essential libncurses5-dev libbz2-dev
  65. Now all dependencies should be in place and we can exit the schroot environment.
  66. Configuring scratchbox2
  67. -----------------------
  68. To configure the scratchbox we need to cd into the root directory of the rootfs
  69. we created before. From there we can call the sb2 configuration tools which
  70. will take the current directory as the base directory for the scratchbox2
  71. environment.
  72. ::
  73. cd /srv/chroot/precise_arm
  74. sb2-init -c `which qemu-arm` ARM `which arm-linux-gnueabi-gcc`
  75. This will create a scratchbox2 based environment called ARM that maps calls to
  76. gcc done within the scratchbox to the arm-linux-gnueabi-gcc outside the
  77. scratchbox. Now we should have a working cross compilation toolchain in place
  78. and can start cross-translating programs for ARM.
  79. Translation
  80. -----------
  81. Having performed all the preliminary steps we should now be able to cross
  82. translate a program for ARM. You can use this minimal
  83. target to test your setup before applying it to a larger project.
  84. Before starting the translator we need to set two environment variables, so the
  85. translator knows how to use the scratchbox environment. We need to set the
  86. **SB2** environment variable to point to the rootfs and the **SB2OPT** should
  87. contain the command line options for the sb2 command. If our rootfs is in the
  88. folder /srv/chroot/precise_arm and the scratchbox environment is called "ARM",
  89. the variables would be defined as follows.
  90. ::
  91. export SB2=/srv/chroot/precise_arm
  92. export SB2OPT='-t ARM'
  93. Once this is set, you can call the translator. For example save this file
  94. ::
  95. def main(args):
  96. print "Hello World"
  97. return 0
  98. def target(*args):
  99. return main, None
  100. and call the translator
  101. ::
  102. pypy ~/path_to_pypy_checkout/rpython/bin/rpython -O2 --platform=arm target.py
  103. If everything worked correctly this should yield an ARM binary. Running this binary in the ARM chroot or on an ARM device should produce the output ``"Hello World"``.
  104. To translate the full python pypy interpreter with a jit, you can cd into pypy/goal and call
  105. ::
  106. pypy <path to rpython>/rpython/bin/rpython -Ojit --platform=arm --gcrootfinder=shadowstack --jit-backend=arm targetpypystandalone.py
  107. The gcrootfinder option is needed to work around `issue 1377`_ and the jit-backend works around `issue 1376`_
  108. .. _issue 1377: https://bitbucket.org/pypy/pypy/issue/1377
  109. .. _issue 1376: https://bitbucket.org/pypy/pypy/issue/1376