Cross-Translating for RISC-V — RPython Documentation (original) (raw)

This document describes how to translate RPython to RISC-V 64-bit backend.

Creating a Ubuntu RISC-V 64-bit Chroot

This section describes how to set up RISC-V 64-bit chroot on a x86 host. You can skip this section if you would like to develop on a RISC-V 64-bit board directly.

First, we must install dependencies below on the host:

Run the command below to install all of them:

sudo apt-get install debootstrap qemu-user-static binfmt-support schroot

For non-Ubuntu host:

sudo apt-get install ubuntu-keyring

Second, we must decide where we would like to set up the chroot. In the example below, /srv/chroot/rv64_ubuntu_24_04 will be used:

Now, we create the root file system by calling:

sudo mkdir -p /srv/chroot

sudo debootstrap --arch=riscv64
--keyring /usr/share/keyrings/ubuntu-archive-keyring.gpg
--include=ubuntu-keyring
noble
/srv/chroot/rv64_ubuntu_24_04
http://ports.ubuntu.com/ubuntu-ports

Rename /etc/resolv.conf so that schroot can copy the host resolv.conf

into chroot.

sudo mv /srv/chroot/rv64_ubuntu_24_04/etc/resolv.conf{,.bak}

Third, create a default_shm schroot profile, which allows the usage of semaphore and shared memory:

sudo cp /etc/schroot/default /etc/schroot/default_shm

Uncomment shm fstab lines

sudo sed -i 's_#/run_/run_g' /etc/schroot/default_shm/fstab sudo sed -i 's_#/dev/shm_/dev/shm_g' /etc/schroot/default_shm/fstab

Fourth, we create a schroot configuration file for the root file system we’ve just created. The command below creates a configuration file at/etc/schroot/chroot.d/rv64_ubuntu_24_04:

echo "[rv64_ubuntu_24_04] description=Ubuntu Noble (24.04) RISC-V directory=/srv/chroot/rv64_ubuntu_24_04 root-users=$(whoami) users=$(whoami) type=directory profile=default_shm" | sudo tee /etc/schroot/chroot.d/rv64_ubuntu_24_04

Now, you can test the chroot with:

You should see the output:

You can enter the chroot with:

schroot -c rv64_ubuntu_24_04

Inside the chroot, if you run uname -m, you should see riscv64:

You can enter the chroot as the root user with the -u root option:

schroot -c rv64_ubuntu_24_04 -u root

You may sometimes need this when you want to install Debian packages to the chroot.

Build CPython 2.7 for Bootstrapping

To run the RPython toolchain, we need a Python 2.7 implementation. This section describes how to build a CPython 2.7 from its source code. You can skip this section if you already have python2.7.

Note

CPython 2.7 is no longer supported nor maintained. The instructions below is based on my experiment around early 2024. Please adjust them if needed.

First, install the build dependencies for CPython:

schroot -c rv64_ubuntu_24_04 -u root -- apt-get install
build-essential gcc gdb g++
libbz2-dev libdb-dev libexpat1-dev libffi-dev libgdbm-dev
libncursesw5-dev libreadline-dev libsqlite3-dev libssl-dev
libtinfo-dev tk-dev zlib1g-dev

Secoond, create the final installation directory for CPython:

schroot -c rv64_ubuntu_24_04 -u root -- mkdir /opt/python2

schroot -c rv64_ubuntu_24_04 -u root --
chown (whoami):(whoami):(whoami):(whoami) /opt/python2

Third, clone the patched CPython 2.7 repository:

git clone https://github.com/loganchien/cpython27-deprecated -b release_27

cd cpython27-deprecated

Fourth, build CPython 2.7 in the chroot:

schroot -c rv64_ubuntu_24_04

$ ./configure --prefix=/opt/python2
--enable-shared
--enable-optimizations
--with-system-ffi LDFLAGS="-Wl,-rpath,/opt/python2/lib"

$ make -j8

$ make install -j8

Fifth, set up Python packages:

$ export PATH=/opt/python2/bin:$PATH

$ python2.7 -mensurepip

$ python2.7 -mpip install -U pip wheel

Now, you should have a CPython 2.7 that is good enough for RPython translation.