RISC-V를 위한 교차 번역(Cross-Translating)

이 문서는 RPython을 RISC-V 64비트 백엔드로 번역(translation)하는 방법을 설명합니다.

Ubuntu RISC-V 64비트 Chroot 생성하기

이 절에서는 x86 호스트에 RISC-V 64비트 chroot를 설정하는 방법을 설명합니다. RISC-V 64비트 보드에서 직접 개발하려는 경우 이 절을 건너뛰어도 됩니다.

먼저, 호스트에 아래 종속성들을 설치해야 합니다:

  • debootstrap – 디렉터리에 Debian/Ubuntu 루트 파일 시스템을 생성하는 Debian 도구
  • schroot– chroot 간 전환을 도와주는 chroot 관리 데몬입니다.
  • qemu-user-static– x86-64에서 RISC-V 64비트 실행 파일을 실행할 수 있게 해주는 바이너리 변환기입니다.
  • binfmt-support – Linux 커널이 RISC-V 64비트 실행 파일에 대해 qemu-user-static을 호출하도록 돕는 유틸리티 패키지입니다.
  • ubuntu-keyring – Ubuntu 아카이브용 공개 키입니다.

아래 명령을 실행하여 모두 설치하십시오:

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

# For non-Ubuntu host:
sudo apt-get install ubuntu-keyring

둘째, chroot를 어디에 설정할지 결정해야 합니다. 아래 예시에서는 /srv/chroot/rv64_ubuntu_24_04를 사용합니다:

이제 다음을 호출하여 루트 파일 시스템을 생성합니다:

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}

셋째, 세마포어와 공유 메모리 사용을 허용하는 default_shmschroot 프로필을 생성합니다:

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

넷째, 방금 생성한 루트 파일 시스템을 위한 schroot 설정 파일을 생성합니다. 아래 명령은 /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

이제 다음 명령으로 chroot를 테스트할 수 있습니다:

schroot -l

다음과 같은 출력이 표시되어야 합니다:

chroot:rv64_ubuntu_24_04

다음 명령으로 chroot에 진입할 수 있습니다:

schroot -c rv64_ubuntu_24_04

chroot 내부에서 uname -m을 실행하면 riscv64가 표시되어야 합니다:

$ uname -m
riscv64

-u root 옵션을 사용하면 root 사용자로 chroot에 들어갈 수 있습니다:

schroot -c rv64_ubuntu_24_04 -u root

chroot에 데비안 패키지를 설치하고 싶을 때 이것이 필요할 수도 있습니다.

부트스트래핑을 위한 CPython 2.7 빌드

RPython 툴체인을 실행하려면 Python 2.7 구현체가 필요합니다. 이 절에서는 소스 코드로부터 CPython 2.7을 빌드하는 방법을 설명합니다. 이미 python2.7이 있다면 이 절을 건너뛰어도 됩니다.

Note

CPython 2.7은 더 이상 지원되거나 유지 관리되지 않습니다. 아래 안내는 2024년 초 저의 실험을 바탕으로 합니다. 필요하다면 조정하시기 바랍니다.

먼저, 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

둘째, CPython의 최종 설치 디렉터리를 생성합니다:

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

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

셋째, 패치된 CPython 2.7 저장소를 클론하십시오:

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

cd cpython27-deprecated

넷째, chroot 안에서 CPython 2.7을 빌드하십시오:

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

다섯째, Python 패키지를 설정합니다:

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

$ python2.7 -mensurepip

$ python2.7 -mpip install -U pip wheel

이제 RPython 번역(translation)에 적합한 CPython 2.7이 준비되었을 것입니다.

RPython 툴체인 사용하기

먼저, PyPy 개발을 위한 the dependencies를 설치하십시오:

schroot -c rv64_ubuntu_24_04 -u root -- apt-get install \
    build-essential pkg-config libbz2-dev libexpat1-dev libffi-dev \
    libgc-dev libgdbm-dev liblzma-dev libncurses5-dev libncursesw5-dev \
    libsqlite3-dev libssl-dev tk-dev zlib1g-dev

추가로, 모든 테스트 스위트를 통과시키려면 git을 사용해 PyPy를 빌드해야 합니다:

schroot -c rv64_ubuntu_24_04 -u root -- apt-get install git

둘째, PyPy 개발을 위한 Python 패키지를 설치하십시오:

schroot -c rv64_ubuntu_24_04

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

$ cd /path/to/pypy/source/tree

$ python2.7 -mpip install -r requirements.txt

Hello World 예제 번역(translation)하기

다음 내용으로 target.py 파일을 생성하십시오:

def main(args):
    print "Hello World"
    return 0

def target(*args):
    return main, None

그리고 번역기(translator)를 호출합니다:

$ python2.7 rpython/bin/rpython -O2 target.py

모든 것이 올바르게 작동했다면, 이는 RISC-V 64비트 바이너리를 생성해야 합니다. RISC-V 64비트에서 이 바이너리를 실행하면 Hello World 출력이 생성되어야 합니다.

PyPy 인터프리터 번역(translation)

아래 명령을 실행하여 JIT 컴파일러가 포함된 전체 PyPy 인터프리터를 번역(translation)하십시오:

$ cd pypy/goal

$ python2.7 ../../rpython/bin/rpython --opt=jit targetpypystandalone.py

$ PYTHONPATH=../.. ./pypy-c ../../lib_pypy/pypy_tools/build_cffi_imports.py

$ cd ../..

$ python2.7 pypy/tool/release/package.py --archive-name=pypy-VER-PLATFORM