From 4de5371e22f2bb0e4dc7c97cffcdf438e431d632 Mon Sep 17 00:00:00 2001 From: "Jonathan J. Helmus" Date: Thu, 23 Jul 2026 18:09:11 -0500 Subject: [PATCH 1/7] build _ctypes as a shared extension --- cpython-unix/extension-modules.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/cpython-unix/extension-modules.yml b/cpython-unix/extension-modules.yml index 05165eb0a..ac4d35aef 100644 --- a/cpython-unix/extension-modules.yml +++ b/cpython-unix/extension-modules.yml @@ -108,6 +108,7 @@ _csv: - _csv.c _ctypes: + build-mode: shared sources: - _ctypes/_ctypes.c - _ctypes/callbacks.c From d3e4aa719b864f74ccf09ab885bd6678bf991a8f Mon Sep 17 00:00:00 2001 From: "Jonathan J. Helmus" Date: Thu, 23 Jul 2026 18:09:47 -0500 Subject: [PATCH 2/7] enable BOLT optimization on Linux aarch64 --- cpython-unix/build-cpython.sh | 8 ++++++++ cpython-unix/targets.yml | 6 +----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cpython-unix/build-cpython.sh b/cpython-unix/build-cpython.sh index c851afc85..d7216c556 100755 --- a/cpython-unix/build-cpython.sh +++ b/cpython-unix/build-cpython.sh @@ -515,6 +515,14 @@ if [ -n "${CPYTHON_OPTIMIZED}" ]; then -indirect-call-promotion=all \ -dyno-stats \ -frame-opt=hot" + if [[ "${TARGET_TRIPLE}" == aarch64* ]]; then + # CDSplit requires the prohibitively slow compact code model on + # AArch64, indirect-call promotion crashes on calls, and frame + # optimization is only supported on x86. + BOLT_APPLY_FLAGS="${BOLT_APPLY_FLAGS/-split-strategy=cdsplit/-split-strategy=profile2}" + BOLT_APPLY_FLAGS="${BOLT_APPLY_FLAGS/-indirect-call-promotion=all/-indirect-call-promotion=jump-tables}" + BOLT_APPLY_FLAGS="${BOLT_APPLY_FLAGS/ -frame-opt=hot/}" + fi fi # Allow users to enable the experimental JIT on 3.13+ diff --git a/cpython-unix/targets.yml b/cpython-unix/targets.yml index 51fdbcd80..e69aa56f9 100644 --- a/cpython-unix/targets.yml +++ b/cpython-unix/targets.yml @@ -178,11 +178,7 @@ aarch64-unknown-linux-gnu: - zlib - zstd openssl_target: linux-aarch64 - # Blocked on: - # BOLT-ERROR: Cannot relax adr in non-simple function - # trampoline_code_table/1. Use --strict option to override - # See https://github.com/llvm/llvm-project/issues/146541 - # bolt_capable: true + bolt_capable: true armv7-unknown-linux-gnueabi: linux_uapi_include_arch: arm-linux-gnueabi From 12cd2256cecdbc0acf8b0a6ece70393fe17a9bfe Mon Sep 17 00:00:00 2001 From: "Jonathan J. Helmus" Date: Fri, 24 Jul 2026 13:18:24 -0500 Subject: [PATCH 3/7] mark _ctypes as a shared extension --- src/validation.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/validation.rs b/src/validation.rs index d130b43e2..3d539b12e 100644 --- a/src/validation.rs +++ b/src/validation.rs @@ -865,6 +865,7 @@ const GLOBAL_EXTENSIONS_WINDOWS_NO_STATIC: &[&str] = &["_testinternalcapi", "_tk /// Extension modules that should be built as shared libraries. const SHARED_LIBRARY_EXTENSIONS: &[&str] = &[ "_crypt", + "_ctypes", "_ctypes_test", "_dbm", "_testbuffer", From a6ff8e11a64ef83903b108fcbd1071a6ba2217a3 Mon Sep 17 00:00:00 2001 From: "Jonathan J. Helmus" Date: Fri, 24 Jul 2026 13:23:28 -0500 Subject: [PATCH 4/7] refactor arch specific BOLT_APPLY_FLAGS --- cpython-unix/build-cpython.sh | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cpython-unix/build-cpython.sh b/cpython-unix/build-cpython.sh index d7216c556..e9d25d5c1 100755 --- a/cpython-unix/build-cpython.sh +++ b/cpython-unix/build-cpython.sh @@ -504,7 +504,6 @@ if [ -n "${CPYTHON_OPTIMIZED}" ]; then -reorder-blocks=ext-tsp \ -reorder-functions=cdsort \ -split-functions \ --split-strategy=cdsplit \ -icf=safe \ -inline-all \ -split-eh \ @@ -512,16 +511,18 @@ if [ -n "${CPYTHON_OPTIMIZED}" ]; then -peepholes=none \ -jump-tables=aggressive \ -inline-ap \ --indirect-call-promotion=all \ --dyno-stats \ --frame-opt=hot" +-dyno-stats" if [[ "${TARGET_TRIPLE}" == aarch64* ]]; then # CDSplit requires the prohibitively slow compact code model on - # AArch64, indirect-call promotion crashes on calls, and frame - # optimization is only supported on x86. - BOLT_APPLY_FLAGS="${BOLT_APPLY_FLAGS/-split-strategy=cdsplit/-split-strategy=profile2}" - BOLT_APPLY_FLAGS="${BOLT_APPLY_FLAGS/-indirect-call-promotion=all/-indirect-call-promotion=jump-tables}" - BOLT_APPLY_FLAGS="${BOLT_APPLY_FLAGS/ -frame-opt=hot/}" + # AArch64, and indirect-call promotion crashes on calls. + BOLT_APPLY_FLAGS="${BOLT_APPLY_FLAGS} \ +-split-strategy=profile2 \ +-indirect-call-promotion=jump-tables" + elif [[ "${TARGET_TRIPLE}" == x86_64* ]]; then + BOLT_APPLY_FLAGS="${BOLT_APPLY_FLAGS} \ +-split-strategy=cdsplit \ +-indirect-call-promotion=all \ +-frame-opt=hot" fi fi From 57204138a7bed7e91258aeab3695682d7002f75d Mon Sep 17 00:00:00 2001 From: "Jonathan J. Helmus" Date: Fri, 24 Jul 2026 14:42:20 -0500 Subject: [PATCH 5/7] _ctypes requires Py_BUILD_CORE_MODULE defined in 3.10 --- cpython-unix/extension-modules.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpython-unix/extension-modules.yml b/cpython-unix/extension-modules.yml index ac4d35aef..0528f59b7 100644 --- a/cpython-unix/extension-modules.yml +++ b/cpython-unix/extension-modules.yml @@ -129,6 +129,8 @@ _ctypes: - HAVE_FFI_PREP_CLOSURE_LOC=1 - HAVE_FFI_CLOSURE_ALLOC=1 defines-conditional: + - define: Py_BUILD_CORE_MODULE + maximum-python-version: "3.10" - define: MACOSX targets: - .*-apple-.* From 9ed7c8f678b101fdd9d7bf862152f3ab97ab1b72 Mon Sep 17 00:00:00 2001 From: "Jonathan J. Helmus" Date: Fri, 24 Jul 2026 14:58:30 -0500 Subject: [PATCH 6/7] Fix compiler definition matching across argument boundaries Prevent the definition-matching regex from consuming whitespace between compiler arguments. Only definitions containing an explicit assignment should be moved into Makefile.extra. Other definitions should remain in Setup.local. --- pythonbuild/cpython.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonbuild/cpython.py b/pythonbuild/cpython.py index 3221760ab..178043da2 100644 --- a/pythonbuild/cpython.py +++ b/pythonbuild/cpython.py @@ -465,7 +465,7 @@ def derive_setup_local( # agrees fully with the distribution's knowledge of extensions. So we can # treat our metadata as canonical. - RE_DEFINE = re.compile(rb"-D[^=]+=[^\s]+") + RE_DEFINE = re.compile(rb"-D[^=\s]+=[^\s]+") # Translate our YAML metadata into Setup lines. From 61eb0ed8678c7c1c69f857be9a9e3401bbb3b8a8 Mon Sep 17 00:00:00 2001 From: "Jonathan J. Helmus" Date: Fri, 24 Jul 2026 15:32:57 -0500 Subject: [PATCH 7/7] Apply extension module definitions through preprocessor flags --- pythonbuild/cpython.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonbuild/cpython.py b/pythonbuild/cpython.py index 178043da2..19b0a153b 100644 --- a/pythonbuild/cpython.py +++ b/pythonbuild/cpython.py @@ -697,7 +697,7 @@ def derive_setup_local( for target in sorted(extra_cflags): make_lines.append( - b"%s: PY_STDMODULE_CFLAGS += %s" % (target, b" ".join(extra_cflags[target])) + b"%s: PY_CPPFLAGS += %s" % (target, b" ".join(extra_cflags[target])) ) return {