Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-90905: Allow cross-compilation on macOS #128385

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zanieb
Copy link
Contributor

@zanieb zanieb commented Dec 31, 2024

Adds support for cross-compiling x86_64 from arm64 macOS. This is particularly useful due to the decreasing availability of x86_64 machines to perform builds on.

The changes here are loosely based on the patch that python-build-standalone uses for cross-compiling.

I tested compiling x86_64 from an arm64 machine with the following script:

#!/usr/bin/env bash

set -ex

ROOT=$(pwd)
OUT=${ROOT}/../cpython-out

# Assume that x86_64 Homebrew is installed
ibrew() { arch -x86_64 /usr/local/bin/brew "$@"; }
export -f ibrew

mkdir -p ${OUT}

# Compile the "build" aarch64 Python

make clean
rm -rf ${OUT}/cpython-aarch64

# Exclude modules we don't need that require system dependencies
./configure \
    py_cv_module__openssl=n/a \
    py_cv_module__hashlib=n/a \
    py_cv_module__gdbm=n/a \
    --without-ensurepip \
    --prefix ${OUT}/cpython-aarch64

make -j8
make -j sharedinstall
make -j install

# Build the "host" x86_64 Python

make clean
rm -rf ${OUT}/cpython-x86_64

CFLAGS="-arch x86_64" \
LDFLAGS="-arch x86_64" \
MACOSX_DEPLOYMENT_TARGET=10.15 \
GDBM_CFLAGS="-I$(ibrew --prefix gdbm)/include" \
GDBM_LIBS="-L$(ibrew --prefix gdbm)/lib -lgdbm" \
PKG_CONFIG="$(ibrew --prefix pkg-config)/bin/pkg-config" \
./configure \
    --with-pydebug \
    --with-system-libmpdec \
    --with-openssl="$(ibrew --prefix openssl@3)" \
    --build=aarch64-apple-darwin \
    --host=x86_64-apple-darwin \
    --prefix ${OUT}/cpython-x86_64 \
    --with-build-python=${OUT}/cpython-aarch64/bin/python3 \
    --disable-ipv6 \
    ac_cv_file__dev_ptc=no \
    ac_cv_file__dev_ptmx=no \
    ac_cv_func_sendfile=no

make -j8
make -j sharedinstall
make -j install

${OUT}/cpython-x86_64/bin/python3 -c "import platform; print(platform.machine())"

@zanieb
Copy link
Contributor Author

zanieb commented Jan 1, 2025

This seems a bit too easy. I'm curious if I'm missing something?

The justification for the configure flags are as follows:

--disable-ipv6

configure: error: You must get working getaddrinfo() function or pass the "--disable-ipv6" option to configure.

ac_cv_file__dev_ptmx=no

checking for /dev/ptmx... not set
configure: error: set ac_cv_file__dev_ptmx to yes/no in your CONFIG_SITE file when cross compiling

ac_cv_file__dev_ptc=no

checking for /dev/ptc... not set
configure: error: set ac_cv_file__dev_ptc to yes/no in your CONFIG_SITE file when cross compiling

ac_cv_func_sendfile=no

./Modules/posixmodule.c:11871:15: error: call to undeclared function 'sendfile'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags);
            ^

It's unclear to me if it's expected that the -arch needs to be added to CFLAGS and LDFLAGS explicitly.

I also checked the Platform in the sysconfig, as I know we have an extra patch for this in python-build-standalone but it doesn't look necessary here:

❯ ../cpython-out/cpython-x86_64/bin/python3 -m sysconfig | grep Platform
Platform: "macosx-10.15-x86_64"

@zanieb zanieb marked this pull request as ready for review January 1, 2025 04:59
@corona10 corona10 requested a review from a team January 1, 2025 09:11
@corona10
Copy link
Member

corona10 commented Jan 1, 2025

I think that @ned-deily and @ronaldoussoren have interests with this patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants