Skip to content

Commit 4f27e39

Browse files
authored
Fix musl detection in Python 3.12 and below (#827)
CPython 3.10 and below don't even attempt to fix the target triple for musl. CPython 3.11 and 3.12 do, but in our setup it doesn't work right because of an autoconf bug. CPython 3.13 rewrites the target triple detection logic and works fine. Fixes #724.
1 parent ce3b5ae commit 4f27e39

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

cpython-unix/build-cpython-host.sh

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,12 @@ pushd "Python-${PYTHON_VERSION}"
3737
# Clang 13 actually prints something with --print-multiarch, confusing CPython's
3838
# configure. This is reported as https://bugs.python.org/issue45405. We nerf the
3939
# check since we know what we're doing.
40-
if [ "${CC}" = "clang" ]; then
40+
if [[ "${CC}" = "clang" || "${CC}" = "musl-clang" ]]; then
4141
if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_13}" ]; then
4242
patch -p1 -i ${ROOT}/patch-disable-multiarch-13.patch
4343
else
4444
patch -p1 -i ${ROOT}/patch-disable-multiarch.patch
4545
fi
46-
elif [ "${CC}" = "musl-clang" ]; then
47-
# Similarly, this is a problem for musl Clang on Python 3.13+
48-
if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_13}" ]; then
49-
patch -p1 -i ${ROOT}/patch-disable-multiarch-13.patch
50-
fi
5146
fi
5247

5348
autoconf

cpython-unix/build-cpython.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ if [ -n "${CROSS_COMPILING}" ]; then
115115
fi
116116
fi
117117

118+
# CPython <=3.10 doesn't properly detect musl. CPython <=3.12 tries, but fails
119+
# in our environment because of an autoconf bug. CPython >=3.13 is fine.
120+
if [ -n "${PYTHON_MEETS_MAXIMUM_VERSION_3_10}" ]; then
121+
patch -p1 -i ${ROOT}/patch-cpython-configure-target-triple-musl-3.10.patch
122+
elif [ -n "${PYTHON_MEETS_MAXIMUM_VERSION_3_12}" ]; then
123+
patch -p1 -i ${ROOT}/patch-cpython-configure-target-triple-musl-3.12.patch
124+
fi
125+
118126
# Clang 13 actually prints something with --print-multiarch, confusing CPython's
119127
# configure. This is reported as https://bugs.python.org/issue45405. We nerf the
120128
# check since we know what we're doing.
@@ -1133,11 +1141,10 @@ mkdir -p "${LIB_DYNLOAD}"
11331141
touch "${LIB_DYNLOAD}/.empty"
11341142

11351143
# Symlink libpython so we don't have 2 copies.
1144+
# TODO(geofft): Surely we can get PYTHON_ARCH out of the build?
11361145
case "${TARGET_TRIPLE}" in
11371146
aarch64-unknown-linux-*)
1138-
# In Python 3.13+, the musl target is identified in cross compiles and the output directory
1139-
# is named accordingly.
1140-
if [[ "${CC}" = "musl-clang" && -n "${PYTHON_MEETS_MINIMUM_VERSION_3_13}" ]]; then
1147+
if [[ "${CC}" = "musl-clang" ]]; then
11411148
PYTHON_ARCH="aarch64-linux-musl"
11421149
else
11431150
PYTHON_ARCH="aarch64-linux-gnu"
@@ -1176,9 +1183,7 @@ s390x-unknown-linux-gnu)
11761183
PYTHON_ARCH="s390x-linux-gnu"
11771184
;;
11781185
x86_64-unknown-linux-*)
1179-
# In Python 3.13+, the musl target is identified in cross compiles and the output directory
1180-
# is named accordingly.
1181-
if [[ "${CC}" = "musl-clang" && -n "${PYTHON_MEETS_MINIMUM_VERSION_3_13}" ]]; then
1186+
if [[ "${CC}" = "musl-clang" ]]; then
11821187
PYTHON_ARCH="x86_64-linux-musl"
11831188
else
11841189
PYTHON_ARCH="x86_64-linux-gnu"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
From d7a70fe9ffa0e4e173bae545cdcba62dc7a2a1f1 Mon Sep 17 00:00:00 2001
2+
From: Geoffrey Thomas <geofft@ldpreload.com>
3+
Date: Sat, 25 Oct 2025 18:49:45 -0400
4+
Subject: [PATCH 1/1] LOCAL: configure.ac: Fix musl detection
5+
Forwarded: not-needed
6+
7+
Newer versions of Python rework this code significantly, so this does
8+
not need to be upstreamed.
9+
---
10+
configure.ac | 5 +++++
11+
1 file changed, 5 insertions(+)
12+
13+
diff --git a/configure.ac b/configure.ac
14+
index ac3be3850a9..9c7c02ea6de 100644
15+
--- a/configure.ac
16+
+++ b/configure.ac
17+
@@ -866,6 +866,11 @@ EOF
18+
19+
if $CPP $CPPFLAGS conftest.c >conftest.out 2>/dev/null; then
20+
PLATFORM_TRIPLET=`grep -v '^#' conftest.out | grep -v '^ *$' | tr -d ' '`
21+
+ case "$CC" in
22+
+ musl-*)
23+
+ PLATFORM_TRIPLET=`echo "$PLATFORM_TRIPLET" | sed 's/linux-gnu/linux-musl/'`
24+
+ ;;
25+
+ esac
26+
AC_MSG_RESULT([$PLATFORM_TRIPLET])
27+
else
28+
AC_MSG_RESULT([none])
29+
--
30+
2.39.5 (Apple Git-154)
31+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
From 851c49bafb1fe11c02eafe4850e952cafec548b7 Mon Sep 17 00:00:00 2001
2+
From: Geoffrey Thomas <geofft@ldpreload.com>
3+
Date: Sat, 25 Oct 2025 18:49:45 -0400
4+
Subject: [PATCH 1/1] LOCAL: configure.ac: Fix musl detection
5+
Forwarded: not-needed
6+
7+
We set HOST_CC to regular (glibc) clang and CC to musl-clang.
8+
AC_CANONICAL_HOST sets build_os based on the output of config.guess,
9+
which looks at HOST_CC in preference to CC, and therefore misreports the
10+
target triple as gnu. Directly checking CC works for us.
11+
12+
Newer versions of Python rework this code significantly to not rely on
13+
build_os and do musl detection internally, so this does not need to be
14+
upstreamed.
15+
---
16+
configure.ac | 4 ++--
17+
1 file changed, 2 insertions(+), 2 deletions(-)
18+
19+
diff --git a/configure.ac b/configure.ac
20+
index 1a02d19f1b2..ee743c11aa5 100644
21+
--- a/configure.ac
22+
+++ b/configure.ac
23+
@@ -1108,8 +1108,8 @@ EOF
24+
25+
if $CPP $CPPFLAGS conftest.c >conftest.out 2>/dev/null; then
26+
PLATFORM_TRIPLET=`grep -v '^#' conftest.out | grep -v '^ *$' | tr -d ' '`
27+
- case "$build_os" in
28+
- linux-musl*)
29+
+ case "$CC" in
30+
+ musl-*)
31+
PLATFORM_TRIPLET=`echo "$PLATFORM_TRIPLET" | sed 's/linux-gnu/linux-musl/'`
32+
;;
33+
esac
34+
--
35+
2.39.5 (Apple Git-154)
36+

src/verify_distribution.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5+
import importlib.machinery
56
import os
67
import struct
78
import sys
@@ -255,6 +256,17 @@ def test_hash_algorithm(self):
255256
msg=f"{sys.hash_info.algorithm=!r} is not siphash",
256257
)
257258

259+
def test_libc_identity(self):
260+
def assertLibc(value):
261+
for libc in ("-gnu", "-musl"):
262+
if os.environ["TARGET_TRIPLE"].endswith(libc):
263+
self.assertIn(libc, value)
264+
else:
265+
self.assertNotIn(libc, value)
266+
267+
assertLibc(sys.implementation._multiarch)
268+
assertLibc(importlib.machinery.EXTENSION_SUFFIXES[0])
269+
258270

259271
if __name__ == "__main__":
260272
unittest.main()

0 commit comments

Comments
 (0)