Debian Codegraph @

rpython/doc/riscv.rst

raw
1 .. _riscv:
2
3 Cross-Translating for RISC-V
4 ============================
5
6 This document describes how to translate RPython to RISC-V 64-bit backend.
7
8
9 Creating a Ubuntu RISC-V 64-bit Chroot
10 --------------------------------------
11
12 This section describes how to set up RISC-V 64-bit chroot on a x86 host. You
13 can skip this section if you would like to develop on a RISC-V 64-bit board
14 directly.
15
16 First, we must install dependencies below on the host:
17
18 * ``debootstrap`` -- Debian tool to create a Debian/Ubuntu root file system in
19 a directory.
20 * ``schroot`` -- A chroot management daemon that helps us switch between
21 chroots.
22 * ``qemu-user-static`` -- The binary translator that allow us to run RISC-V
23 64-bit executables on x86-64.
24 * ``binfmt-support`` -- A utility package that helps the Linux kernel to invoke
25 ``qemu-user-static`` for RISC-V 64-bit executables.
26 * ``ubuntu-keyring`` -- The public key for Ubuntu archive.
27
28 Run the command below to install all of them:
29
30 ::
31
32 sudo apt-get install debootstrap qemu-user-static binfmt-support schroot
33
34 # For non-Ubuntu host:
35 sudo apt-get install ubuntu-keyring
36
37 Second, we must decide where we would like to set up the chroot. In the
38 example below, ``/srv/chroot/rv64_ubuntu_24_04`` will be used:
39
40 Now, we create the root file system by calling:
41
42 ::
43
44 sudo mkdir -p /srv/chroot
45
46 sudo debootstrap --arch=riscv64 \
47 --keyring /usr/share/keyrings/ubuntu-archive-keyring.gpg \
48 --include=ubuntu-keyring \
49 noble \
50 /srv/chroot/rv64_ubuntu_24_04 \
51 http://ports.ubuntu.com/ubuntu-ports
52
53 # Rename /etc/resolv.conf so that schroot can copy the host resolv.conf
54 # into chroot.
55 sudo mv /srv/chroot/rv64_ubuntu_24_04/etc/resolv.conf{,.bak}
56
57 Third, create a ``default_shm`` schroot profile, which allows the usage of
58 semaphore and shared memory:
59
60 ::
61
62 sudo cp /etc/schroot/default /etc/schroot/default_shm
63
64 # Uncomment shm fstab lines
65 sudo sed -i 's_#/run_/run_g' /etc/schroot/default_shm/fstab
66 sudo sed -i 's_#/dev/shm_/dev/shm_g' /etc/schroot/default_shm/fstab
67
68 Fourth, we create a ``schroot`` configuration file for the root file system
69 we've just created. The command below creates a configuration file at
70 ``/etc/schroot/chroot.d/rv64_ubuntu_24_04``:
71
72 ::
73
74 echo "[rv64_ubuntu_24_04]
75 description=Ubuntu Noble (24.04) RISC-V
76 directory=/srv/chroot/rv64_ubuntu_24_04
77 root-users=$(whoami)
78 users=$(whoami)
79 type=directory
80 profile=default_shm" | sudo tee /etc/schroot/chroot.d/rv64_ubuntu_24_04
81
82 Now, you can test the chroot with:
83
84 ::
85
86 schroot -l
87
88 You should see the output:
89
90 ::
91
92 chroot:rv64_ubuntu_24_04
93
94 You can enter the chroot with:
95
96 ::
97
98 schroot -c rv64_ubuntu_24_04
99
100 Inside the chroot, if you run ``uname -m``, you should see ``riscv64``:
101
102 ::
103
104 $ uname -m
105 riscv64
106
107 You can enter the chroot as the ``root`` user with the ``-u root`` option:
108
109 ::
110
111 schroot -c rv64_ubuntu_24_04 -u root
112
113 You may sometimes need this when you want to install Debian packages to the
114 chroot.
115
116
117 Build CPython 2.7 for Bootstrapping
118 -----------------------------------
119
120 To run the RPython toolchain, we need a Python 2.7 implementation. This
121 section describes how to build a CPython 2.7 from its source code. You can
122 skip this section if you already have ``python2.7``.
123
124 .. note::
125
126 CPython 2.7 is no longer supported nor maintained. The instructions below
127 is based on my experiment around early 2024. Please adjust them if needed.
128
129
130 First, install the build dependencies for CPython:
131
132 ::
133
134 schroot -c rv64_ubuntu_24_04 -u root -- apt-get install \
135 build-essential gcc gdb g++ \
136 libbz2-dev libdb-dev libexpat1-dev libffi-dev libgdbm-dev \
137 libncursesw5-dev libreadline-dev libsqlite3-dev libssl-dev \
138 libtinfo-dev tk-dev zlib1g-dev
139
140 Secoond, create the final installation directory for CPython:
141
142 ::
143
144 schroot -c rv64_ubuntu_24_04 -u root -- mkdir /opt/python2
145
146 schroot -c rv64_ubuntu_24_04 -u root -- \
147 chown $(whoami):$(whoami) /opt/python2
148
149 Third, clone the patched CPython 2.7 repository:
150
151 ::
152
153 git clone https://github.com/loganchien/cpython27-deprecated -b release_27
154
155 cd cpython27-deprecated
156
157 Fourth, build CPython 2.7 in the chroot:
158
159 ::
160
161 schroot -c rv64_ubuntu_24_04
162
163 ::
164
165 $ ./configure --prefix=/opt/python2 \
166 --enable-shared \
167 --enable-optimizations \
168 --with-system-ffi LDFLAGS="-Wl,-rpath,/opt/python2/lib"
169
170 $ make -j8
171
172 $ make install -j8
173
174 Fifth, set up Python packages:
175
176 ::
177
178 $ export PATH=/opt/python2/bin:$PATH
179
180 $ python2.7 -mensurepip
181
182 $ python2.7 -mpip install -U pip wheel
183
184 Now, you should have a CPython 2.7 that is good enough for RPython translation.
185
186
187 Using the RPython Toolchain
188 ---------------------------
189
190 First, install `the dependencies`_ for PyPy development:
191
192 .. _`the dependencies`:
193 https://doc.pypy.org/build.html#install-build-time-dependencies
194
195 ::
196
197 schroot -c rv64_ubuntu_24_04 -u root -- apt-get install \
198 build-essential pkg-config libbz2-dev libexpat1-dev libffi-dev \
199 libgc-dev libgdbm-dev liblzma-dev libncurses5-dev libncursesw5-dev \
200 libsqlite3-dev libssl-dev tk-dev zlib1g-dev
201
202 In addition, to pass all test suites, you will have to build PyPy with git:
203
204 ::
205
206 schroot -c rv64_ubuntu_24_04 -u root -- apt-get install git
207
208
209 Second, install Python packages for PyPy development:
210
211 ::
212
213 schroot -c rv64_ubuntu_24_04
214
215 $ export PATH=/opt/python2/bin:$PATH
216
217 $ cd /path/to/pypy/source/tree
218
219 $ python2.7 -mpip install -r requirements.txt
220
221
222 Translate a Hello World Example
223 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224
225 Create a ``target.py`` file with the following content:
226
227 ::
228
229 def main(args):
230 print "Hello World"
231 return 0
232
233 def target(*args):
234 return main, None
235
236 and call the translator:
237
238 ::
239
240 $ python2.7 rpython/bin/rpython -O2 target.py
241
242
243 If everything worked correctly, this should yield an RISC-V 64-bit binary.
244 Running this binary on RISC-V 64-bit should produce the output
245 ``Hello World``.
246
247
248 Translate PyPy Interpreter
249 ~~~~~~~~~~~~~~~~~~~~~~~~~~
250
251 Run the command below to translate the full PyPy interpreter with a JIT
252 compiler:
253
254 ::
255
256 $ cd pypy/goal
257
258 $ python2.7 ../../rpython/bin/rpython --opt=jit targetpypystandalone.py
259
260 $ PYTHONPATH=../.. ./pypy-c ../../lib_pypy/pypy_tools/build_cffi_imports.py
261
262 $ cd ../..
263
264 $ python2.7 pypy/tool/release/package.py --archive-name=pypy-VER-PLATFORM