From d1aa29cbeb7241520daec3b7fe5550bfaae6c03e Mon Sep 17 00:00:00 2001 From: "Jonathan J. Helmus" Date: Tue, 20 Oct 2026 11:37:35 -0500 Subject: [PATCH] Cross-compile LoongArch with LLVM Use Clang and lld with a Debian-provided sysroot and GCC runtime objects. Install libcrypt into the sysroot as this was split from glibc. --- cpython-unix/build-libxcb.sh | 6 ------ cpython-unix/build-xorgproto.sh | 6 ------ .../build.cross-loongarch64.Dockerfile | 18 ++++++++++++------ cpython-unix/targets.yml | 18 ++++++++++++++---- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/cpython-unix/build-libxcb.sh b/cpython-unix/build-libxcb.sh index d99a235b4..b7935c151 100755 --- a/cpython-unix/build-libxcb.sh +++ b/cpython-unix/build-libxcb.sh @@ -13,12 +13,6 @@ export PKG_CONFIG_PATH=/tools/deps/share/pkgconfig:/tools/deps/lib/pkgconfig tar -xf "libxcb-${LIBXCB_VERSION}.tar.gz" pushd "libxcb-${LIBXCB_VERSION}" -if [[ "${TARGET_TRIPLE}" = loongarch64* ]]; then - rm -f build-aux/config.guess build-aux/config.sub - curl -sSL -o build-aux/config.guess 'https://git.savannah.gnu.org/cgit/config.git/plain/config.guess' - curl -sSL -o build-aux/config.sub 'https://git.savannah.gnu.org/cgit/config.git/plain/config.sub' -fi - if [ "${CC}" = "musl-clang" ]; then EXTRA_FLAGS="--disable-shared" fi diff --git a/cpython-unix/build-xorgproto.sh b/cpython-unix/build-xorgproto.sh index 5eae50f3f..3f102be72 100755 --- a/cpython-unix/build-xorgproto.sh +++ b/cpython-unix/build-xorgproto.sh @@ -15,12 +15,6 @@ export PKG_CONFIG_PATH=/tools/deps/share/pkgconfig tar -xf "xorgproto-${XORGPROTO_VERSION}.tar.gz" pushd "xorgproto-${XORGPROTO_VERSION}" -if [[ "${TARGET_TRIPLE}" = loongarch64* ]]; then - rm -f config.guess.sub config.sub - curl -sSL -o config.guess 'https://git.savannah.gnu.org/cgit/config.git/plain/config.guess' - curl -sSL -o config.sub 'https://git.savannah.gnu.org/cgit/config.git/plain/config.sub' -fi - CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" ./configure \ --build="${BUILD_TRIPLE}" \ --host="${TARGET_TRIPLE}" \ diff --git a/cpython-unix/build.cross-loongarch64.Dockerfile b/cpython-unix/build.cross-loongarch64.Dockerfile index 2e78250ef..e95fbffa9 100644 --- a/cpython-unix/build.cross-loongarch64.Dockerfile +++ b/cpython-unix/build.cross-loongarch64.Dockerfile @@ -52,14 +52,20 @@ RUN apt-get install \ zip \ zlib1g-dev +# Clang and lld use the Debian glibc sysroot and GCC startup/runtime objects. RUN apt-get install \ - g++-loongarch64-linux-gnu \ - gcc-loongarch64-linux-gnu \ - libc6-dev-loong64-cross + libc6-dev-loong64-cross \ + libgcc-14-dev-loong64-cross \ + linux-libc-dev-loong64-cross RUN cd /tmp && \ curl -LO https://snapshot.debian.org/archive/debian-ports/20240812T192057Z/pool-loong64/main/libx/libxcrypt/libcrypt-dev_4.4.36-4_loong64.deb && \ curl -LO https://snapshot.debian.org/archive/debian-ports/20240812T192057Z/pool-loong64/main/libx/libxcrypt/libcrypt1_4.4.36-4_loong64.deb && \ - dpkg -x libcrypt-dev_4.4.36-4_loong64.deb / && \ - dpkg -x libcrypt1_4.4.36-4_loong64.deb / && \ - rm -f /tmp/*.deb + dpkg -x libcrypt-dev_4.4.36-4_loong64.deb /tmp/crypt && \ + dpkg -x libcrypt1_4.4.36-4_loong64.deb /tmp/crypt && \ + cp -a /tmp/crypt/usr/lib/loongarch64-linux-gnu/. /usr/loongarch64-linux-gnu/lib/ && \ + cp -a /tmp/crypt/usr/include/. /usr/loongarch64-linux-gnu/include/ && \ + rm -rf /tmp/crypt /tmp/*.deb + +# Rewrite libc's lib and lib64 paths to be relative to the sysroot for LLD. +RUN sed -i 's@/usr/loongarch64-linux-gnu/@/@g' /usr/loongarch64-linux-gnu/lib/libc.so diff --git a/cpython-unix/targets.yml b/cpython-unix/targets.yml index e8e814dc7..c3fb76b75 100644 --- a/cpython-unix/targets.yml +++ b/cpython-unix/targets.yml @@ -349,16 +349,26 @@ loongarch64-unknown-linux-gnu: - '3.15' - '3.16' docker_image_suffix: .cross-loongarch64 - host_cc: /usr/bin/x86_64-linux-gnu-gcc - host_cxx: /usr/bin/x86_64-linux-gnu-g++ - target_cc: /usr/bin/loongarch64-linux-gnu-gcc - target_cxx: /usr/bin/loongarch64-linux-gnu-g++ + needs_toolchain: true + host_cc: clang + host_cxx: clang++ + target_cc: clang + target_cxx: clang++ target_cflags: + - '--target=loongarch64-unknown-linux-gnu' + - '--sysroot=/usr/loongarch64-linux-gnu' + - '--gcc-install-dir=/usr/lib/gcc-cross/loongarch64-linux-gnu/14' + - '-fuse-ld=lld' + - '-Wno-unused-command-line-argument' # Hardening - '-fstack-protector' - '-U_FORTIFY_SOURCE' - '-D_FORTIFY_SOURCE=2' target_ldflags: + - '--target=loongarch64-unknown-linux-gnu' + - '--sysroot=/usr/loongarch64-linux-gnu' + - '--gcc-install-dir=/usr/lib/gcc-cross/loongarch64-linux-gnu/14' + - '-fuse-ld=lld' # Hardening - '-Wl,-z,noexecstack' - '-Wl,-z,relro'