From 6c123e98eedde1fd947909ef80295cb0dd636670 Mon Sep 17 00:00:00 2001 From: Gregory Szorc Date: Sun, 18 May 2025 15:21:03 -0700 Subject: [PATCH] Rework host platform naming "host platform" is this concept I invented very early in the project to encode the build machine os and architecture. I chose "linux64" to represent Linux x86-64. And for reasons I can't recall, I never qualified the macOS architecture into the name like I did for Linux. This commit refactors the string so that it is consistently `os_arch`. `linux64` becomes `linux_x86_64`. `macos` becomes `macos_x86_64` and `macos_arm64`. I'm doing this cleanup in service of native ARM Linux builds. `linux64` is ambiguous. And macOS not behaving the same was always wonky. Let's impose some consistency instead of layering hacks upon hacks. --- cpython-unix/Makefile | 2 +- cpython-unix/build-cpython-host.sh | 2 +- cpython-unix/build-cpython.sh | 32 ++++++++--------- cpython-unix/build-main.py | 22 +++--------- cpython-unix/build-ncurses.sh | 6 ++-- cpython-unix/build-tix.sh | 2 +- cpython-unix/build-tk.sh | 4 +-- cpython-unix/build.py | 25 +++++++------ cpython-unix/targets.yml | 58 +++++++++++++++++------------- pythonbuild/utils.py | 50 +++++++++++++++++++++----- 10 files changed, 117 insertions(+), 86 deletions(-) diff --git a/cpython-unix/Makefile b/cpython-unix/Makefile index 196b6bbfb..2413d7a15 100644 --- a/cpython-unix/Makefile +++ b/cpython-unix/Makefile @@ -86,7 +86,7 @@ $(OUTDIR)/$(CLANG_FILENAME): $(OUTDIR)/musl-$(MUSL_VERSION)-$(HOST_PLATFORM).tar: $(BASE_TOOLCHAIN_DEPENDS) $(HERE)/build-musl.sh $(RUN_BUILD) --toolchain musl -ifeq ($(HOST_PLATFORM),linux64) +ifeq ($(HOST_PLATFORM),linux_x86_64) TOOLCHAIN_TARGET := $(OUTDIR)/musl-$(MUSL_VERSION)-$(HOST_PLATFORM).tar else TOOLCHAIN_TARGET := diff --git a/cpython-unix/build-cpython-host.sh b/cpython-unix/build-cpython-host.sh index 3cfa3ac59..99b021d77 100755 --- a/cpython-unix/build-cpython-host.sh +++ b/cpython-unix/build-cpython-host.sh @@ -22,7 +22,7 @@ export trailer_m4=${TOOLS_PATH}/host/share/autoconf/autoconf/trailer.m4 # The share/autoconf/autom4te.cfg file also hard-codes some paths. Rewrite # those to the real tools path. -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then sed_args="-i '' -e" else sed_args="-i" diff --git a/cpython-unix/build-cpython.sh b/cpython-unix/build-cpython.sh index 39d810e3c..b7ba3855f 100755 --- a/cpython-unix/build-cpython.sh +++ b/cpython-unix/build-cpython.sh @@ -33,7 +33,7 @@ export trailer_m4=${TOOLS_PATH}/host/share/autoconf/autoconf/trailer.m4 # The share/autoconf/autom4te.cfg file also hard-codes some paths. Rewrite # those to the real tools path. -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then sed_args=(-i '' -e) else sed_args=(-i) @@ -59,7 +59,7 @@ cat Makefile.extra pushd Python-${PYTHON_VERSION} # configure doesn't support cross-compiling on Apple. Teach it. -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_13}" ]; then patch -p1 -i ${ROOT}/patch-apple-cross-3.13.patch elif [ "${PYTHON_MAJMIN_VERSION}" = "3.12" ]; then @@ -159,7 +159,7 @@ fi # linked modules. But those libraries should only get linked into libpython, not the # executable. This behavior is kinda suspect on all platforms, as it could be adding # library dependencies that shouldn't need to be there. -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then if [ "${PYTHON_MAJMIN_VERSION}" = "3.9" ]; then patch -p1 -i ${ROOT}/patch-python-link-modules-3.9.patch elif [ "${PYTHON_MAJMIN_VERSION}" = "3.10" ]; then @@ -214,7 +214,7 @@ fi # macOS. On older versions, we need to hack up readline.c to build against # libedit. This patch breaks older libedit (as seen on macOS) so don't apply # on macOS. -if [[ -n "${PYTHON_MEETS_MAXIMUM_VERSION_3_9}" && "${PYBUILD_PLATFORM}" != "macos" ]]; then +if [[ -n "${PYTHON_MEETS_MAXIMUM_VERSION_3_9}" && "${PYBUILD_PLATFORM}" != macos* ]]; then # readline.c assumes that a modern readline API version has a free_history_entry(). # but libedit does not. Change the #ifdef accordingly. # @@ -317,13 +317,13 @@ CFLAGS=${CFLAGS//-fvisibility=hidden/} # But some symbols from some dependency libraries are still non-hidden for some # reason. We force the linker to do our bidding. -if [ "${PYBUILD_PLATFORM}" != "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" != macos* ]]; then LDFLAGS="${LDFLAGS} -Wl,--exclude-libs,ALL" fi EXTRA_CONFIGURE_FLAGS= -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then CFLAGS="${CFLAGS} -I${TOOLS_PATH}/deps/include/uuid" # Prevent using symbols not supported by current macOS SDK target. @@ -332,7 +332,7 @@ fi # Always build against libedit instead of the default of readline. # macOS always uses the system libedit, so no tweaks are needed. -if [ "${PYBUILD_PLATFORM}" != "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" != macos* ]]; then # CPython 3.10 introduced proper configure support for libedit, so add configure # flag there. if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_10}" ]; then @@ -381,7 +381,7 @@ CONFIGURE_FLAGS=" # this patch mildly conflicts with the macos-only patch-python-link-modules # applied above, so you will need to resolve that conflict if you re-enable # this for macos. -if [ "${PYBUILD_PLATFORM}" != "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" != macos* ]]; then if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_12}" ]; then patch -p1 -i "${ROOT}/patch-python-configure-add-enable-static-libpython-for-interpreter.patch" else @@ -493,7 +493,7 @@ if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_11}" ]; then CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --with-build-python=${TOOLS_PATH}/host/bin/python${PYTHON_MAJMIN_VERSION}" fi -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then # Configure may detect libintl from non-system sources, such # as Homebrew or MacPorts. So nerf the check to prevent this. CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_lib_intl_textdomain=no" @@ -563,7 +563,7 @@ if [ "${PYBUILD_PLATFORM}" = "macos" ]; then fi # ptsrname_r is only available in SDK 13.4+, but we target a lower version for compatibility. -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_func_ptsname_r=no" fi @@ -575,12 +575,12 @@ fi # On 3.14+ `test_strftime_y2k` fails when cross-compiling for `x86_64_v2` and `x86_64_v3` targets on # Linux, so we ignore it. See https://github.com/python/cpython/issues/128104 -if [[ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_14}" && -n "${CROSS_COMPILING}" && "${PYBUILD_PLATFORM}" != "macos" ]]; then +if [[ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_14}" && -n "${CROSS_COMPILING}" && "${PYBUILD_PLATFORM}" != macos* ]]; then export PROFILE_TASK='-m test --pgo --ignore test_strftime_y2k' fi # We use ndbm on macOS and BerkeleyDB elsewhere. -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --with-dbmliborder=ndbm" else CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --with-dbmliborder=bdb" @@ -608,7 +608,7 @@ if [ -n "${CROSS_COMPILING}" ]; then # python will end up with the time.tzset function or not. All linux targets, # however, should have a working tzset function via libc. So we manually # indicate this to the configure script. - if [ "${PYBUILD_PLATFORM}" != "macos" ]; then + if [[ "${PYBUILD_PLATFORM}" != macos* ]]; then CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_working_tzset=yes" fi @@ -660,7 +660,7 @@ fi # This ensures we can run the binary in any location without # LD_LIBRARY_PATH pointing to the directory containing libpython. if [ "${PYBUILD_SHARED}" = "1" ]; then - if [ "${PYBUILD_PLATFORM}" = "macos" ]; then + if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then # There's only 1 dylib produced on macOS and it has the binary suffix. LIBPYTHON_SHARED_LIBRARY_BASENAME=libpython${PYTHON_MAJMIN_VERSION}${PYTHON_BINARY_SUFFIX}.dylib LIBPYTHON_SHARED_LIBRARY=${ROOT}/out/python/install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME} @@ -1143,7 +1143,7 @@ cp -av ${TOOLS_PATH}/deps/lib/*.a ${ROOT}/out/python/build/lib/ # # We copy the libclang_rt..a library from our clang into the # distribution so it is available. See documentation in quirks.rst for more. -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then cp -av $(dirname $(which clang))/../lib/clang/*/lib/darwin/libclang_rt.osx.a ${ROOT}/out/python/build/lib/ fi @@ -1158,7 +1158,7 @@ if [ -d "${TOOLS_PATH}/deps/lib/tcl8" ]; then cp -av $source ${ROOT}/out/python/install/lib/ done - if [ "${PYBUILD_PLATFORM}" != "macos" ]; then + if [[ "${PYBUILD_PLATFORM}" != macos* ]]; then cp -av ${TOOLS_PATH}/deps/lib/Tix8.4.3 ${ROOT}/out/python/install/lib/ fi fi diff --git a/cpython-unix/build-main.py b/cpython-unix/build-main.py index 6bf91b49b..a353daf27 100755 --- a/cpython-unix/build-main.py +++ b/cpython-unix/build-main.py @@ -7,7 +7,6 @@ import multiprocessing import os import pathlib -import platform import subprocess import sys @@ -15,6 +14,8 @@ from pythonbuild.downloads import DOWNLOADS from pythonbuild.utils import ( compress_python_archive, + current_host_platform, + default_target_triple, get_target_settings, release_tag_from_git, supported_targets, @@ -28,29 +29,14 @@ def main(): - if sys.platform == "linux": - host_platform = "linux64" - default_target_triple = "x86_64-unknown-linux-gnu" - elif sys.platform == "darwin": - host_platform = "macos" - machine = platform.machine() - - if machine == "arm64": - default_target_triple = "aarch64-apple-darwin" - elif machine == "x86_64": - default_target_triple = "x86_64-apple-darwin" - else: - raise Exception("unhandled macOS machine value: %s" % machine) - else: - print("Unsupported build platform: %s" % sys.platform) - return 1 + host_platform = current_host_platform() # Note these arguments must be synced with `build.py` parser = argparse.ArgumentParser() parser.add_argument( "--target-triple", - default=default_target_triple, + default=default_target_triple(), choices=supported_targets(TARGETS_CONFIG), help="Target host triple to build for", ) diff --git a/cpython-unix/build-ncurses.sh b/cpython-unix/build-ncurses.sh index b1a568bc4..25df7e227 100755 --- a/cpython-unix/build-ncurses.sh +++ b/cpython-unix/build-ncurses.sh @@ -16,7 +16,7 @@ tar -xf ncurses-${NCURSES_VERSION}.tar.gz # ncurses version. Our workaround is to build ncurses for the host when # cross-compiling then make its `tic` available to the target ncurses # build. -if [[ -n "${CROSS_COMPILING}" && "${PYBUILD_PLATFORM}" != "macos" ]]; then +if [[ -n "${CROSS_COMPILING}" && "${PYBUILD_PLATFORM}" != macos* ]]; then echo "building host ncurses to provide modern tic for cross-compile" pushd ncurses-${NCURSES_VERSION} @@ -65,7 +65,7 @@ CONFIGURE_FLAGS=" # ncurses wants --with-build-cc when cross-compiling. But it insists on CC # and this value not being equal, even though using the same binary with # different compiler flags is doable! -if [[ -n "${CROSS_COMPILING}" && "${PYBUILD_PLATFORM}" != "macos" ]]; then +if [[ -n "${CROSS_COMPILING}" && "${PYBUILD_PLATFORM}" != macos* ]]; then CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --with-build-cc=$(which "${HOST_CC}")" fi @@ -91,7 +91,7 @@ fi # binary. So we provide a suitable runtime value and then move files at install # time. -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --datadir=/usr/share --sysconfdir=/etc diff --git a/cpython-unix/build-tix.sh b/cpython-unix/build-tix.sh index a894f5280..c1d5fae3d 100755 --- a/cpython-unix/build-tix.sh +++ b/cpython-unix/build-tix.sh @@ -28,7 +28,7 @@ if [ "${CC}" = "clang" ]; then CFLAGS="${CFLAGS} -Wno-error=implicit-function-declaration -Wno-error=incompatible-function-pointer-types" fi -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then CFLAGS="${CFLAGS} -I${TOOLS_PATH}/deps/include" EXTRA_CONFIGURE_FLAGS="--without-x" else diff --git a/cpython-unix/build-tk.sh b/cpython-unix/build-tk.sh index 0dca35165..2769a631d 100755 --- a/cpython-unix/build-tk.sh +++ b/cpython-unix/build-tk.sh @@ -17,7 +17,7 @@ pushd tk*/unix CFLAGS="${EXTRA_TARGET_CFLAGS} -fPIC" LDFLAGS="${EXTRA_TARGET_LDFLAGS}" -if [ "${PYBUILD_PLATFORM}" = "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" = macos* ]]; then CFLAGS="${CFLAGS} -I${TOOLS_PATH}/deps/include -Wno-availability" CFLAGS="${CFLAGS} -Wno-deprecated-declarations -Wno-unknown-attributes -Wno-typedef-redefinition" LDFLAGS="-L${TOOLS_PATH}/deps/lib" @@ -36,7 +36,7 @@ CFLAGS="${CFLAGS}" CPPFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" ./configure \ ${EXTRA_CONFIGURE_FLAGS} # Remove wish, since we don't need it. -if [ "${PYBUILD_PLATFORM}" != "macos" ]; then +if [[ "${PYBUILD_PLATFORM}" != macos* ]]; then sed -i 's/all: binaries libraries doc/all: libraries/' Makefile sed -i 's/install-binaries: $(TK_STUB_LIB_FILE) $(TK_LIB_FILE) ${WISH_EXE}/install-binaries: $(TK_STUB_LIB_FILE) $(TK_LIB_FILE)/' Makefile fi diff --git a/cpython-unix/build.py b/cpython-unix/build.py index f058e93a4..05783fe82 100755 --- a/cpython-unix/build.py +++ b/cpython-unix/build.py @@ -116,7 +116,7 @@ def add_target_env(env, build_platform, target_triple, build_env): extra_host_cflags = [] extra_host_ldflags = [] - if build_platform == "linux64": + if build_platform == "linux_x86_64": env["BUILD_TRIPLE"] = "x86_64-unknown-linux-gnu" env["TARGET_TRIPLE"] = ( @@ -133,7 +133,7 @@ def add_target_env(env, build_platform, target_triple, build_env): ): env["CROSS_COMPILING"] = "1" - if build_platform == "macos": + if build_platform.startswith("macos_"): machine = platform.machine() if machine == "arm64": @@ -232,7 +232,7 @@ def toolchain_archive_path(package_name, host_platform): def install_binutils(platform): - return platform != "macos" + return not platform.startswith("macos_") def simple_build( @@ -410,7 +410,7 @@ def build_tix( ) depends = {"tcl", "tk"} - if host_platform != "macos": + if not host_platform.startswith("macos_"): depends |= {"libX11", "xorgproto"} for p in sorted(depends): @@ -516,7 +516,7 @@ def python_build_info( binary_suffix = "" - if platform == "linux64": + if platform == "linux_x86_64": bi["core"]["static_lib"] = ( "install/lib/python{version}/config-{version}{binary_suffix}-x86_64-linux-gnu/libpython{version}{binary_suffix}.a".format( version=version, binary_suffix=binary_suffix @@ -539,7 +539,7 @@ def python_build_info( object_file_format = f"llvm-bitcode:%{llvm_version}" else: object_file_format = "elf" - elif platform == "macos": + elif platform.startswith("macos_"): bi["core"]["static_lib"] = ( "install/lib/python{version}/config-{version}{binary_suffix}-darwin/libpython{version}{binary_suffix}.a".format( version=version, binary_suffix=binary_suffix @@ -599,9 +599,12 @@ def python_build_info( if lib.startswith("-l"): lib = lib[2:] - if platform == "linux64" and lib not in linux_allowed_system_libraries: + if platform == "linux_x86_64" and lib not in linux_allowed_system_libraries: raise Exception("unexpected library in LIBS (%s): %s" % (libs, lib)) - elif platform == "macos" and lib not in MACOS_ALLOW_SYSTEM_LIBRARIES: + elif ( + platform.startswith("macos_") + and lib not in MACOS_ALLOW_SYSTEM_LIBRARIES + ): raise Exception("unexpected library in LIBS (%s): %s" % (libs, lib)) log("adding core system link library: %s" % lib) @@ -867,7 +870,7 @@ def build_cpython( extension_module_loading = ["builtin"] crt_features = [] - if host_platform == "linux64": + if host_platform == "linux_x86_64": if "static" in parsed_build_options: crt_features.append("static") else: @@ -893,7 +896,7 @@ def build_cpython( python_symbol_visibility = "global-default" - elif host_platform == "macos": + elif host_platform.startswith("macos_"): python_symbol_visibility = "global-default" extension_module_loading.append("shared-library") crt_features.append("libSystem") @@ -1238,7 +1241,7 @@ def main(): elif action == "tk": extra_archives = {"tcl"} - if host_platform != "macos": + if not host_platform.startswith("macos_"): extra_archives |= { "libX11", "libXau", diff --git a/cpython-unix/targets.yml b/cpython-unix/targets.yml index 1b1797686..46a92e31e 100644 --- a/cpython-unix/targets.yml +++ b/cpython-unix/targets.yml @@ -61,7 +61,8 @@ # 11.0+. aarch64-apple-darwin: host_platforms: - - macos + - macos_arm64 + - macos_x86_64 pythons_supported: - '3.9' - '3.10' @@ -111,7 +112,8 @@ aarch64-apple-darwin: aarch64-apple-ios: host_platforms: - - macos + - macos_arm64 + - macos_x86_64 pythons_supported: - '3.9' needs_toolchain: true @@ -153,7 +155,7 @@ aarch64-apple-ios: aarch64-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -194,7 +196,8 @@ aarch64-unknown-linux-gnu: arm64-apple-tvos: host_platforms: - - macos + - macos_arm64 + - macos_x86_64 pythons_supported: - '3.9' needs_toolchain: true @@ -235,7 +238,7 @@ arm64-apple-tvos: armv7-unknown-linux-gnueabi: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -276,7 +279,7 @@ armv7-unknown-linux-gnueabi: armv7-unknown-linux-gnueabihf: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -317,7 +320,7 @@ armv7-unknown-linux-gnueabihf: i686-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -363,7 +366,7 @@ i686-unknown-linux-gnu: mips-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -404,7 +407,7 @@ mips-unknown-linux-gnu: mipsel-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -445,7 +448,7 @@ mipsel-unknown-linux-gnu: ppc64le-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -486,7 +489,7 @@ ppc64le-unknown-linux-gnu: riscv64-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -527,7 +530,7 @@ riscv64-unknown-linux-gnu: s390x-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -568,7 +571,8 @@ s390x-unknown-linux-gnu: thumb7k-apple-watchos: host_platforms: - - macos + - macos_arm64 + - macos_x86_64 pythons_supported: - '3.9' needs_toolchain: true @@ -613,7 +617,8 @@ thumb7k-apple-watchos: # machines. x86_64-apple-darwin: host_platforms: - - macos + - macos_arm64 + - macos_x86_64 pythons_supported: - '3.9' - '3.10' @@ -663,7 +668,8 @@ x86_64-apple-darwin: x86_64-apple-ios: host_platforms: - - macos + - macos_arm64 + - macos_x86_64 pythons_supported: - '3.9' needs_toolchain: true @@ -705,7 +711,8 @@ x86_64-apple-ios: x86_64-apple-tvos: host_platforms: - - macos + - macos_arm64 + - macos_x86_64 pythons_supported: - '3.9' needs_toolchain: true @@ -746,7 +753,8 @@ x86_64-apple-tvos: x86_64-apple-watchos: host_platforms: - - macos + - macos_arm64 + - macos_x86_64 pythons_supported: - '3.9' needs_toolchain: true @@ -787,7 +795,7 @@ x86_64-apple-watchos: x86_64-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -833,7 +841,7 @@ x86_64-unknown-linux-gnu: x86_64_v2-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -880,7 +888,7 @@ x86_64_v2-unknown-linux-gnu: x86_64_v3-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -927,7 +935,7 @@ x86_64_v3-unknown-linux-gnu: x86_64_v4-unknown-linux-gnu: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -974,7 +982,7 @@ x86_64_v4-unknown-linux-gnu: x86_64-unknown-linux-musl: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -1018,7 +1026,7 @@ x86_64-unknown-linux-musl: x86_64_v2-unknown-linux-musl: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -1063,7 +1071,7 @@ x86_64_v2-unknown-linux-musl: x86_64_v3-unknown-linux-musl: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' @@ -1108,7 +1116,7 @@ x86_64_v3-unknown-linux-musl: x86_64_v4-unknown-linux-musl: host_platforms: - - linux64 + - linux_x86_64 pythons_supported: - '3.9' - '3.10' diff --git a/pythonbuild/utils.py b/pythonbuild/utils.py index 14d9a4743..1c4f4ffc4 100644 --- a/pythonbuild/utils.py +++ b/pythonbuild/utils.py @@ -30,6 +30,41 @@ from .logging import log +def current_host_platform() -> str: + """Resolve the name of the current machine's host platform. + + This is conceptually a simplified machine triple. + """ + machine = platform.machine() + if sys.platform == "linux": + if machine == "x86_64": + return "linux_x86_64" + else: + raise Exception(f"unsupported Linux host platform: {machine}") + elif sys.platform == "darwin": + if machine == "arm64": + return "macos_arm64" + elif machine == "x86_64": + return "macos_x86_64" + else: + raise Exception(f"unhanded macOS machine type: {machine}") + else: + raise Exception(f"unsupported host platform: {sys.platform}") + + +def default_target_triple() -> str: + """Resolve the default target triple to build for.""" + host = current_host_platform() + if host == "linux_x86_64": + return "x86_64-unknown-linux-gnu" + elif host == "macos_arm64": + return "aarch64-apple-darwin" + elif host == "macos_x86_64": + return "x86_64-apple-darwin" + else: + raise Exception(f"unrecognized host platform: {host}") + + def get_targets(yaml_path: pathlib.Path): """Obtain the parsed targets YAML file.""" with yaml_path.open("rb") as fh: @@ -47,9 +82,9 @@ def supported_targets(yaml_path: pathlib.Path): for target, settings in get_targets(yaml_path).items(): for host_platform in settings["host_platforms"]: - if sys.platform == "linux" and host_platform == "linux64": + if sys.platform == "linux" and host_platform == "linux_x86_64": targets.add(target) - elif sys.platform == "darwin" and host_platform == "macos": + elif sys.platform == "darwin" and host_platform.startswith("macos_"): targets.add(target) return targets @@ -430,17 +465,16 @@ def sort_key(v): def clang_toolchain(host_platform: str, target_triple: str) -> str: - if host_platform == "linux64": + if host_platform == "linux_x86_64": # musl currently has issues with LLVM 15+. if "musl" in target_triple: return "llvm-14-x86_64-linux" else: return "llvm-20-x86_64-linux" - elif host_platform == "macos": - if platform.mac_ver()[2] == "arm64": - return "llvm-aarch64-macos" - else: - return "llvm-x86_64-macos" + elif host_platform == "macos_arm64": + return "llvm-aarch64-macos" + elif host_platform == "macos_x86_64": + return "llvm-x86_64-macos" else: raise Exception("unhandled host platform")