python3: fix host python extension compilation on macOS
authorJosef Schlehofer <redacted>
Mon, 8 Jun 2026 07:05:18 +0000 (09:05 +0200)
committerAlexandru Ardelean <redacted>
Mon, 8 Jun 2026 11:34:58 +0000 (14:34 +0300)
On macOS (Darwin) hosts, building host Python C extensions (such as Cython) using the '-shared' flag and linking against '-lpython3.x' causes the host Python interpreter to load a duplicate copy of the Python runtime. This leads to type checking mismatches and segmentation faults (SIGSEGV) when importing the compiled extension.

For example, running:
    ./staging_dir/hostpkg/bin/python3 -c "import Cython.Utils"
crashes with:
    Segmentation fault: 11

To build shared modules correctly on macOS, they must be compiled as bundles using the '-bundle -undefined dynamic_lookup' flags instead of '-shared', and they should not link against the Python library (no '-lpython3.x' in LDFLAGS).

Fix this by dynamically adjusting LDSHARED and LDFLAGS in python3-host.mk when the host OS is Darwin.

Signed-off-by: Josef Schlehofer <redacted>
lang/python/python3-host.mk

index 64d4e373e86877f4721fbcebc9d8bc38544b70c5..7c16703ee97880f41bb1d71d8babd5ea0c0fd497 100644 (file)
@@ -74,10 +74,10 @@ HOST_PYTHON3_VARS = \
        CCSHARED="$(HOSTCC) $(HOST_FPIC)" \
        CXX="$(HOSTCXX)" \
        LD="$(HOSTCC)" \
-       LDSHARED="$(HOSTCC) -shared" \
+       LDSHARED="$(HOSTCC) $(if $(findstring Darwin,$(HOST_OS)),-bundle -undefined dynamic_lookup,-shared)" \
        CFLAGS="$(HOST_CFLAGS)" \
        CPPFLAGS="$(HOST_CPPFLAGS) -I$(HOST_PYTHON3_INC_DIR)" \
-       LDFLAGS="$(HOST_LDFLAGS) -lpython$(PYTHON3_VERSION)" \
+       LDFLAGS="$(HOST_LDFLAGS)$(if $(findstring Darwin,$(HOST_OS)),, -lpython$(PYTHON3_VERSION))" \
        $(CARGO_HOST_CONFIG_VARS) \
        SETUPTOOLS_RUST_CARGO_PROFILE="$(CARGO_HOST_PROFILE)"
 
git clone https://git.99rst.org/PROJECT