From a421c8401edd277688504880c86790bca2d47436 Mon Sep 17 00:00:00 2001 From: Zhuo Zhi Date: Fri, 10 Jan 2025 05:46:01 +0100 Subject: [PATCH] Add riscv64-linux support Added separate Dockerfiles for riscv64 as suggested by https://github.com/astral-sh/python-build-standalone/issues/219#issuecomment-1962751095. RISC-V needs `atomic` linked which is added to `LINUX_ALLOW_SYSTEM_LIBRARIES`. Tested locally with `./build-linux.py --target-triple riscv64-unknown-linux-gnu --python cpython-{3.9,3.10,3.11,3.12,3.13,3.14}` and all build fine. --- .github/workflows/linux.yml | 2 + ci-targets.yaml | 21 +++++++ cpython-unix/build-cpython.sh | 3 + cpython-unix/build-libX11.sh | 3 + cpython-unix/build-main.py | 2 + cpython-unix/build.cross-riscv64.Dockerfile | 68 +++++++++++++++++++++ cpython-unix/build.py | 1 + cpython-unix/extension-modules.yml | 1 + cpython-unix/targets.yml | 40 ++++++++++++ cpython-unix/xcb.cross-riscv64.Dockerfile | 3 + docs/building.rst | 1 + src/release.rs | 13 ++++ src/validation.rs | 8 +++ 13 files changed, 166 insertions(+) create mode 100644 cpython-unix/build.cross-riscv64.Dockerfile create mode 100644 cpython-unix/xcb.cross-riscv64.Dockerfile diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index faab0ccb..b3f33282 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -58,9 +58,11 @@ jobs: image: - build - build.cross + - build.cross-riscv64 - gcc - xcb - xcb.cross + - xcb.cross-riscv64 name: ${{ matrix.image }} runs-on: depot-ubuntu-22.04 permissions: diff --git a/ci-targets.yaml b/ci-targets.yaml index cd6d2916..0ecc662d 100644 --- a/ci-targets.yaml +++ b/ci-targets.yaml @@ -143,6 +143,27 @@ linux: - freethreaded+lto minimum-python-version: "3.13" + riscv64-unknown-linux-gnu: + arch: riscv64 + libc: gnu + python_versions: + - "3.9" + - "3.10" + - "3.11" + - "3.12" + - "3.13" + - "3.14" + build_options: + - debug + - noopt + - lto + build_options_conditional: + - options: + - freethreaded+debug + - freethreaded+noopt + - freethreaded+lto + minimum-python-version: "3.13" + x86_64-unknown-linux-gnu: arch: x86_64 libc: gnu diff --git a/cpython-unix/build-cpython.sh b/cpython-unix/build-cpython.sh index bd9c7bc7..edf68527 100755 --- a/cpython-unix/build-cpython.sh +++ b/cpython-unix/build-cpython.sh @@ -936,6 +936,9 @@ mips64el-unknown-linux-gnuabi64) ppc64le-unknown-linux-gnu) PYTHON_ARCH="powerpc64le-linux-gnu" ;; +riscv64-unknown-linux-gnu) + PYTHON_ARCH="riscv64-linux-gnu" + ;; s390x-unknown-linux-gnu) PYTHON_ARCH="s390x-linux-gnu" ;; diff --git a/cpython-unix/build-libX11.sh b/cpython-unix/build-libX11.sh index 019b2c07..6870514c 100755 --- a/cpython-unix/build-libX11.sh +++ b/cpython-unix/build-libX11.sh @@ -69,6 +69,9 @@ if [ -n "${CROSS_COMPILING}" ]; then ppc64le-unknown-linux-gnu) EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull" ;; + riscv64-unknown-linux-gnu) + EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull" + ;; s390x-unknown-linux-gnu) EXTRA_FLAGS="${EXTRA_FLAGS} --enable-malloc0returnsnull" ;; diff --git a/cpython-unix/build-main.py b/cpython-unix/build-main.py index a41e333b..dd4d2d7c 100755 --- a/cpython-unix/build-main.py +++ b/cpython-unix/build-main.py @@ -103,9 +103,11 @@ def main(): "toolchain", "toolchain-image-build", "toolchain-image-build.cross", + "toolchain-image-build.cross-riscv64", "toolchain-image-gcc", "toolchain-image-xcb", "toolchain-image-xcb.cross", + "toolchain-image-xcb.cross-riscv64", }, default="default", help="The make target to evaluate", diff --git a/cpython-unix/build.cross-riscv64.Dockerfile b/cpython-unix/build.cross-riscv64.Dockerfile new file mode 100644 index 00000000..740a098b --- /dev/null +++ b/cpython-unix/build.cross-riscv64.Dockerfile @@ -0,0 +1,68 @@ +# Debian Buster. +FROM debian@sha256:2a0c1b9175adf759420fe0fbd7f5b449038319171eb76554bb76cbe172b62b42 +MAINTAINER Gregory Szorc + +RUN groupadd -g 1000 build && \ + useradd -u 1000 -g 1000 -d /build -s /bin/bash -m build && \ + mkdir /tools && \ + chown -R build:build /build /tools + +ENV HOME=/build \ + SHELL=/bin/bash \ + USER=build \ + LOGNAME=build \ + HOSTNAME=builder \ + DEBIAN_FRONTEND=noninteractive + +CMD ["/bin/bash", "--login"] +WORKDIR '/build' + +RUN for s in debian_buster debian_buster-updates debian-security_buster/updates; do \ + echo "deb http://snapshot.debian.org/archive/${s%_*}/20250109T084424Z/ ${s#*_} main"; \ + done > /etc/apt/sources.list && \ + ( echo 'quiet "true";'; \ + echo 'APT::Get::Assume-Yes "true";'; \ + echo 'APT::Install-Recommends "false";'; \ + echo 'Acquire::Check-Valid-Until "false";'; \ + echo 'Acquire::Retries "5";'; \ + ) > /etc/apt/apt.conf.d/99cpython-portable + +RUN apt-get update + +# Host building. +RUN apt-get install \ + bzip2 \ + gcc \ + g++ \ + libc6-dev \ + libffi-dev \ + make \ + patch \ + perl \ + pkg-config \ + tar \ + xz-utils \ + unzip \ + zip \ + zlib1g-dev + +# Cross-building. +RUN apt-get install \ + gcc-aarch64-linux-gnu \ + gcc-arm-linux-gnueabi \ + gcc-arm-linux-gnueabihf \ + gcc-mips-linux-gnu \ + gcc-mips64el-linux-gnuabi64 \ + gcc-mipsel-linux-gnu \ + gcc-powerpc64le-linux-gnu \ + gcc-riscv64-linux-gnu \ + gcc-s390x-linux-gnu \ + libc6-dev-arm64-cross \ + libc6-dev-armel-cross \ + libc6-dev-armhf-cross \ + libc6-dev-mips-cross \ + libc6-dev-mips64el-cross \ + libc6-dev-mipsel-cross \ + libc6-dev-ppc64el-cross \ + libc6-dev-riscv64-cross \ + libc6-dev-s390x-cross diff --git a/cpython-unix/build.py b/cpython-unix/build.py index 8122dd9a..b0a3bfad 100755 --- a/cpython-unix/build.py +++ b/cpython-unix/build.py @@ -52,6 +52,7 @@ TARGETS_CONFIG = SUPPORT / "targets.yml" LINUX_ALLOW_SYSTEM_LIBRARIES = { + "atomic", "c", "crypt", "dl", diff --git a/cpython-unix/extension-modules.yml b/cpython-unix/extension-modules.yml index 6a17cb87..1a195b04 100644 --- a/cpython-unix/extension-modules.yml +++ b/cpython-unix/extension-modules.yml @@ -259,6 +259,7 @@ _decimal: targets: - aarch64-.* - ppc64le-unknown-linux.* + - riscv64-unknown-linux.* - s390x-unknown-linux-.* - x86_64.* links: diff --git a/cpython-unix/targets.yml b/cpython-unix/targets.yml index 64683ec6..d3285891 100644 --- a/cpython-unix/targets.yml +++ b/cpython-unix/targets.yml @@ -470,6 +470,46 @@ ppc64le-unknown-linux-gnu: - zlib openssl_target: linux-ppc64le +riscv64-unknown-linux-gnu: + host_platforms: + - linux64 + pythons_supported: + - '3.9' + - '3.10' + - '3.11' + - '3.12' + - '3.13' + - '3.14' + docker_image_suffix: .cross-riscv64 + host_cc: /usr/bin/x86_64-linux-gnu-gcc + host_cxx: /usr/bin/x86_64-linux-gnu-g++ + target_cc: /usr/bin/riscv64-linux-gnu-gcc + needs: + - autoconf + - bdb + - binutils + - bzip2 + - expat + - libedit + - libffi + - libX11 + - libXau + - libxcb + - m4 + - mpdecimal + - ncurses + - openssl-3.0 + - patchelf + - sqlite + - tcl + - tk + - tix + - uuid + - xorgproto + - xz + - zlib + openssl_target: linux64-riscv64 + s390x-unknown-linux-gnu: host_platforms: - linux64 diff --git a/cpython-unix/xcb.cross-riscv64.Dockerfile b/cpython-unix/xcb.cross-riscv64.Dockerfile new file mode 100644 index 00000000..260aa7d7 --- /dev/null +++ b/cpython-unix/xcb.cross-riscv64.Dockerfile @@ -0,0 +1,3 @@ +{% include 'build.cross-riscv64.Dockerfile' %} +RUN apt-get install \ + python diff --git a/docs/building.rst b/docs/building.rst index cadd0c49..dc91dea2 100644 --- a/docs/building.rst +++ b/docs/building.rst @@ -42,6 +42,7 @@ As are various other targets:: $ ./build-linux.py --target mips-unknown-linux-gnu $ ./build-linux.py --target mipsel-unknown-linux-gnu $ ./build-linux.py --target ppc64le-unknown-linux-gnu + $ ./build-linux.py --target riscv64-unknown-linux-gnu $ ./build-linux.py --target s390x-unknown-linux-gnu macOS diff --git a/src/release.rs b/src/release.rs index e67a114f..62eb76bf 100644 --- a/src/release.rs +++ b/src/release.rs @@ -194,6 +194,19 @@ pub static RELEASE_TRIPLES: Lazy> = Lazy:: }, ); + h.insert( + "riscv64-unknown-linux-gnu", + TripleRelease { + suffixes: linux_suffixes_nopgo.clone(), + install_only_suffix: "lto", + python_version_requirement: Some(VersionSpecifier::from_str(">=3.9").unwrap()), + conditional_suffixes: vec![ConditionalSuffixes { + python_version_requirement: VersionSpecifier::from_str(">=3.13").unwrap(), + suffixes: linux_suffixes_nopgo_freethreaded.clone(), + }], + }, + ); + h.insert( "s390x-unknown-linux-gnu", TripleRelease { diff --git a/src/validation.rs b/src/validation.rs index fa57aec6..4c67a312 100644 --- a/src/validation.rs +++ b/src/validation.rs @@ -45,6 +45,7 @@ const RECOGNIZED_TRIPLES: &[&str] = &[ "mipsel-unknown-linux-gnu", "mips64el-unknown-linux-gnuabi64", "ppc64le-unknown-linux-gnu", + "riscv64-unknown-linux-gnu", "s390x-unknown-linux-gnu", "thumbv7k-apple-watchos", "x86_64-apple-darwin", @@ -172,6 +173,10 @@ static GLIBC_MAX_VERSION_BY_TRIPLE: Lazy> = Lazy: ("mipsel-unknown-linux-gnu", "linux-mipsel"), ("mips64el-unknown-linux-gnuabi64", "todo"), ("ppc64le-unknown-linux-gnu", "linux-powerpc64le"), + ("riscv64-unknown-linux-gnu", "linux-riscv64"), ("s390x-unknown-linux-gnu", "linux-s390x"), ("x86_64-apple-darwin", "macosx-10.15-x86_64"), ("x86_64-apple-ios", "iOS-x86_64"), @@ -866,6 +873,7 @@ fn validate_elf>( "mipsel-unknown-linux-gnu" => object::elf::EM_MIPS, "mips64el-unknown-linux-gnuabi64" => 0, "ppc64le-unknown-linux-gnu" => object::elf::EM_PPC64, + "riscv64-unknown-linux-gnu" => object::elf::EM_RISCV, "s390x-unknown-linux-gnu" => object::elf::EM_S390, "x86_64-unknown-linux-gnu" => object::elf::EM_X86_64, "x86_64_v2-unknown-linux-gnu" => object::elf::EM_X86_64,