Skip to content

Commit e1a50ae

Browse files
committed
Merge branch 'main' into indygreg/toolchain-update
2 parents e591651 + 46a3737 commit e1a50ae

7 files changed

+390
-33
lines changed

cpython-unix/build-cpython.sh

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,6 @@ if [ -n "${PYTHON_MEETS_MAXIMUM_VERSION_3_10}" ]; then
298298
patch -p1 -i ${ROOT}/patch-configure-crypt-no-modify-libs.patch
299299
fi
300300

301-
# We patched configure.ac above. Reflect those changes.
302-
autoconf
303-
304-
# configure assumes cross compiling when host != target and doesn't provide a way to
305-
# override. Our target triple normalization may lead configure into thinking we
306-
# aren't cross-compiling when we are. So force a static "yes" value when our
307-
# build system says we are cross-compiling.
308-
if [ -n "${CROSS_COMPILING}" ]; then
309-
patch -p1 -i ${ROOT}/patch-force-cross-compile.patch
310-
fi
311-
312301
# BOLT instrumented binaries segfault in some test_embed tests for unknown reasons.
313302
# On 3.12 (minimum BOLT version), the segfault causes the test harness to
314303
# abort and BOLT optimization uses the partial test results. On 3.13, the segfault
@@ -385,6 +374,22 @@ CONFIGURE_FLAGS="
385374
${EXTRA_CONFIGURE_FLAGS}"
386375

387376

377+
# Build a libpython3.x.so, but statically link the interpreter against
378+
# libpython.
379+
#
380+
# For now skip this on macos, because it causes some linker failures. Note that
381+
# this patch mildly conflicts with the macos-only patch-python-link-modules
382+
# applied above, so you will need to resolve that conflict if you re-enable
383+
# this for macos.
384+
if [ "${PYBUILD_PLATFORM}" != "macos" ]; then
385+
if [ -n "${PYTHON_MEETS_MINIMUM_VERSION_3_12}" ]; then
386+
patch -p1 -i "${ROOT}/patch-python-configure-add-enable-static-libpython-for-interpreter.patch"
387+
else
388+
patch -p1 -i "${ROOT}/patch-python-configure-add-enable-static-libpython-for-interpreter-${PYTHON_MAJMIN_VERSION}.patch"
389+
fi
390+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --enable-static-libpython-for-interpreter"
391+
fi
392+
388393
if [ "${CC}" = "musl-clang" ]; then
389394
# In order to build the _blake2 extension module with SSE3+ instructions, we need
390395
# musl-clang to find headers that provide access to the intrinsics, as they are not
@@ -573,6 +578,14 @@ else
573578
fi
574579

575580
if [ -n "${CROSS_COMPILING}" ]; then
581+
# configure assumes cross compiling when host != target and doesn't
582+
# provide a way to override. Our target triple normalization may
583+
# lead configure into thinking we aren't cross-compiling when we
584+
# are. So force a static "yes" value when our build system says we
585+
# are cross-compiling.
586+
# See also https://savannah.gnu.org/support/?110348
587+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} cross_compiling=yes"
588+
576589
# configure doesn't like a handful of scenarios when cross-compiling.
577590
#
578591
# getaddrinfo buggy test fails for some reason. So we short-circuit it.
@@ -589,8 +602,18 @@ if [ -n "${CROSS_COMPILING}" ]; then
589602
if [ "${PYBUILD_PLATFORM}" != "macos" ]; then
590603
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_working_tzset=yes"
591604
fi
605+
606+
# Also, it cannot detect whether the compiler supports -pthread or
607+
# not, and conservatively defaults to no, which is not the right
608+
# default on relatively modern compilers.
609+
CONFIGURE_FLAGS="${CONFIGURE_FLAGS} ac_cv_pthread=yes"
610+
611+
# TODO: There are probably more of these, see #399.
592612
fi
593613

614+
# We patched configure.ac above. Reflect those changes.
615+
autoconf
616+
594617
CFLAGS=$CFLAGS CPPFLAGS=$CFLAGS LDFLAGS=$LDFLAGS \
595618
./configure ${CONFIGURE_FLAGS}
596619

cpython-unix/build.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,21 +562,34 @@ def python_build_info(
562562
bi["object_file_format"] = object_file_format
563563

564564
# Determine allowed libaries on Linux
565+
libs = extra_metadata["python_config_vars"].get("LIBS", "").split()
565566
mips = target_triple.split("-")[0] in {"mips", "mipsel"}
566567
linux_allowed_system_libraries = LINUX_ALLOW_SYSTEM_LIBRARIES.copy()
567568
if mips and version == "3.13":
568569
# See https://github.com/astral-sh/python-build-standalone/issues/410
569570
linux_allowed_system_libraries.add("atomic")
570571
riscv = target_triple.split("-")[0] in {"riscv64"}
571572
if riscv:
572-
# RISC-V binary often comes with libatomic on old GCC versions
573+
# On older GCC versions, RISC-V sub-word atomic operations require a
574+
# helper function found in libatomic. To facilitate this, GCC <15 adds
575+
# "-latomic" to the definition of "-pthread". We think it's generally
576+
# reasonable on RISC-V systems (but not all Linux systems in general)
577+
# to expect a libatomic system library is installed.
578+
#
579+
# Because "-latomic" is implicitly added by "-pthread", it may not be
580+
# found in the LIBS sysconfig variable, but we need to pretend it is so
581+
# that it gets into PYTHON.json (in particular, so that the validation
582+
# script accepts this dependency).
583+
#
573584
# See https://github.com/riscvarchive/riscv-gcc/issues/12
574585
# https://github.com/riscvarchive/riscv-gcc/issues/337
575586
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86005
587+
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104338
588+
# https://github.com/gcc-mirror/gcc/commit/203f3060dd363361b172f7295f42bb6bf5ac0b3b
576589
linux_allowed_system_libraries.add("atomic")
590+
libs.append("-latomic")
577591

578592
# Add in core linking annotations.
579-
libs = extra_metadata["python_config_vars"].get("LIBS", "").split()
580593
skip = False
581594
for i, lib in enumerate(libs):
582595
if skip:

cpython-unix/patch-force-cross-compile.patch

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
From 579a7cf9498ccfa656dd720a5db8dd6e04e97150 Mon Sep 17 00:00:00 2001
2+
From: Geoffrey Thomas <[email protected]>
3+
Date: Sat, 19 Apr 2025 11:13:40 -0400
4+
Subject: [PATCH 1/1] configure: add --enable-static-libpython-for-interpreter
5+
6+
This option changes the behavior of --enable-shared to continue to build
7+
the libpython3.x.so shared library, but not use it for linking the
8+
python3 interpreter executable. Instead, the executable is linked
9+
directly against the libpython .o files as it would be with
10+
--disable-shared [in newer versions of Python].
11+
12+
There are two benefits of this change. First, libpython uses
13+
thread-local storage, which is noticeably slower when used in a loaded
14+
module instead of in the main program, because the main program can take
15+
advantage of constant offsets from the thread state pointer but loaded
16+
modules have to dynamically call a function __tls_get_addr() to
17+
potentially allocate their thread-local storage area. (There is another
18+
thread-local storage model for dynamic libraries which mitigates most of
19+
this performance hit, but it comes at the cost of preventing
20+
dlopen("libpython3.x.so"), which is a use case we want to preserve.)
21+
22+
Second, this improves the user experience around relocatable Python a
23+
little bit, in that we don't need to use an $ORIGIN-relative path to
24+
locate libpython3.x.so, which has some mild benefits around musl (which
25+
does not support $ORIGIN-relative DT_NEEDED, only $ORIGIN-relative
26+
DT_RPATH/DT_RUNPATH), users who want to make the interpreter setuid or
27+
setcap (which prevents processing $ORIGIN), etc.
28+
---
29+
Makefile.pre.in | 4 +++-
30+
configure.ac | 18 ++++++++++++++++++
31+
2 files changed, 21 insertions(+), 1 deletion(-)
32+
33+
diff --git a/Makefile.pre.in b/Makefile.pre.in
34+
index fa99dd86c41..84c00a5c071 100644
35+
--- a/Makefile.pre.in
36+
+++ b/Makefile.pre.in
37+
@@ -458,6 +458,8 @@ LIBRARY_OBJS= \
38+
$(LIBRARY_OBJS_OMIT_FROZEN) \
39+
Python/frozen.o
40+
41+
+LINK_PYTHON_OBJS=@LINK_PYTHON_OBJS@
42+
+
43+
##########################################################################
44+
# DTrace
45+
46+
@@ -586,7 +588,7 @@ clinic: check-clean-src $(srcdir)/Modules/_blake2/blake2s_impl.c
47+
48+
# Build the interpreter
49+
$(BUILDPYTHON): Programs/python.o $(LIBRARY_DEPS)
50+
- $(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS)
51+
+ $(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
52+
53+
platform: $(BUILDPYTHON) pybuilddir.txt
54+
$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform
55+
diff --git a/configure.ac b/configure.ac
56+
index ac3be3850a9..a07003a24ed 100644
57+
--- a/configure.ac
58+
+++ b/configure.ac
59+
@@ -1093,6 +1093,17 @@ then
60+
fi
61+
AC_MSG_RESULT($enable_shared)
62+
63+
+AC_MSG_CHECKING([for --enable-static-libpython-for-interpreter])
64+
+AC_ARG_ENABLE([static-libpython-for-interpreter],
65+
+ AS_HELP_STRING([--enable-static-libpython-for-interpreter],
66+
+ [even with --enable-shared, statically link libpython into the interpreter (default is to use the shared library)]))
67+
+
68+
+if test -z "$enable_static_libpython_for_interpreter"
69+
+then
70+
+ enable_static_libpython_for_interpreter="no"
71+
+fi
72+
+AC_MSG_RESULT([$enable_static_libpython_for_interpreter])
73+
+
74+
AC_MSG_CHECKING(for --enable-profiling)
75+
AC_ARG_ENABLE(profiling,
76+
AS_HELP_STRING([--enable-profiling], [enable C-level code profiling with gprof (default is no)]))
77+
@@ -1198,6 +1209,13 @@ fi
78+
79+
AC_MSG_RESULT($LDLIBRARY)
80+
81+
+if test "$enable_static_libpython_for_interpreter" = "yes"; then
82+
+ LINK_PYTHON_OBJS='$(LIBRARY_OBJS)'
83+
+else
84+
+ LINK_PYTHON_OBJS='$(BLDLIBRARY)'
85+
+fi
86+
+AC_SUBST(LINK_PYTHON_OBJS)
87+
+
88+
AC_SUBST(AR)
89+
AC_CHECK_TOOLS(AR, ar aal, ar)
90+
91+
--
92+
2.39.5 (Apple Git-154)
93+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
From a5182aec2c0597adb8a01298af120809fcf3187b Mon Sep 17 00:00:00 2001
2+
From: Geoffrey Thomas <[email protected]>
3+
Date: Sat, 19 Apr 2025 11:13:40 -0400
4+
Subject: [PATCH 1/1] configure: add --enable-static-libpython-for-interpreter
5+
6+
This option changes the behavior of --enable-shared to continue to build
7+
the libpython3.x.so shared library, but not use it for linking the
8+
python3 interpreter executable. Instead, the executable is linked
9+
directly against the libpython .o files as it would be with
10+
--disable-shared.
11+
12+
There are two benefits of this change. First, libpython uses
13+
thread-local storage, which is noticeably slower when used in a loaded
14+
module instead of in the main program, because the main program can take
15+
advantage of constant offsets from the thread state pointer but loaded
16+
modules have to dynamically call a function __tls_get_addr() to
17+
potentially allocate their thread-local storage area. (There is another
18+
thread-local storage model for dynamic libraries which mitigates most of
19+
this performance hit, but it comes at the cost of preventing
20+
dlopen("libpython3.x.so"), which is a use case we want to preserve.)
21+
22+
Second, this improves the user experience around relocatable Python a
23+
little bit, in that we don't need to use an $ORIGIN-relative path to
24+
locate libpython3.x.so, which has some mild benefits around musl (which
25+
does not support $ORIGIN-relative DT_NEEDED, only $ORIGIN-relative
26+
DT_RPATH/DT_RUNPATH), users who want to make the interpreter setuid or
27+
setcap (which prevents processing $ORIGIN), etc.
28+
---
29+
configure.ac | 17 ++++++++++++++++-
30+
1 file changed, 16 insertions(+), 1 deletion(-)
31+
32+
diff --git a/configure.ac b/configure.ac
33+
index ab5e1de6fab..6783c36da4d 100644
34+
--- a/configure.ac
35+
+++ b/configure.ac
36+
@@ -1419,6 +1419,17 @@ fi],
37+
[AC_MSG_RESULT(yes)])
38+
AC_SUBST(STATIC_LIBPYTHON)
39+
40+
+AC_MSG_CHECKING([for --enable-static-libpython-for-interpreter])
41+
+AC_ARG_ENABLE([static-libpython-for-interpreter],
42+
+ AS_HELP_STRING([--enable-static-libpython-for-interpreter],
43+
+ [even with --enable-shared, statically link libpython into the interpreter (default is to use the shared library)]))
44+
+
45+
+if test -z "$enable_static_libpython_for_interpreter"
46+
+then
47+
+ enable_static_libpython_for_interpreter="no"
48+
+fi
49+
+AC_MSG_RESULT([$enable_static_libpython_for_interpreter])
50+
+
51+
AC_MSG_CHECKING(for --enable-profiling)
52+
AC_ARG_ENABLE(profiling,
53+
AS_HELP_STRING([--enable-profiling], [enable C-level code profiling with gprof (default is no)]))
54+
@@ -1563,7 +1574,11 @@ if test "$PY_ENABLE_SHARED" = 1 || test "$enable_framework" ; then
55+
LIBRARY_DEPS="\$(LIBRARY) $LIBRARY_DEPS"
56+
fi
57+
# Link Python program to the shared library
58+
- LINK_PYTHON_OBJS='$(BLDLIBRARY)'
59+
+ if test "$enable_static_libpython_for_interpreter" = "yes"; then
60+
+ LINK_PYTHON_OBJS='$(LIBRARY_OBJS)'
61+
+ else
62+
+ LINK_PYTHON_OBJS='$(BLDLIBRARY)'
63+
+ fi
64+
else
65+
if test "$STATIC_LIBPYTHON" = 0; then
66+
# Build Python needs object files but don't need to build
67+
--
68+
2.39.5 (Apple Git-154)
69+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
From 5ae9112a87d45c3aff5ee269ff8e2e49ca278ed3 Mon Sep 17 00:00:00 2001
2+
From: Geoffrey Thomas <[email protected]>
3+
Date: Sat, 19 Apr 2025 11:13:40 -0400
4+
Subject: [PATCH 1/1] configure: add --enable-static-libpython-for-interpreter
5+
6+
This option changes the behavior of --enable-shared to continue to build
7+
the libpython3.x.so shared library, but not use it for linking the
8+
python3 interpreter executable. Instead, the executable is linked
9+
directly against the libpython .o files as it would be with
10+
--disable-shared [in newer versions of Python].
11+
12+
There are two benefits of this change. First, libpython uses
13+
thread-local storage, which is noticeably slower when used in a loaded
14+
module instead of in the main program, because the main program can take
15+
advantage of constant offsets from the thread state pointer but loaded
16+
modules have to dynamically call a function __tls_get_addr() to
17+
potentially allocate their thread-local storage area. (There is another
18+
thread-local storage model for dynamic libraries which mitigates most of
19+
this performance hit, but it comes at the cost of preventing
20+
dlopen("libpython3.x.so"), which is a use case we want to preserve.)
21+
22+
Second, this improves the user experience around relocatable Python a
23+
little bit, in that we don't need to use an $ORIGIN-relative path to
24+
locate libpython3.x.so, which has some mild benefits around musl (which
25+
does not support $ORIGIN-relative DT_NEEDED, only $ORIGIN-relative
26+
DT_RPATH/DT_RUNPATH), users who want to make the interpreter setuid or
27+
setcap (which prevents processing $ORIGIN), etc.
28+
---
29+
Makefile.pre.in | 4 +++-
30+
configure.ac | 18 ++++++++++++++++++
31+
2 files changed, 21 insertions(+), 1 deletion(-)
32+
33+
diff --git a/Makefile.pre.in b/Makefile.pre.in
34+
index a276d535c7f..193439aa73e 100644
35+
--- a/Makefile.pre.in
36+
+++ b/Makefile.pre.in
37+
@@ -460,6 +460,8 @@ LIBRARY_OBJS= \
38+
$(LIBRARY_OBJS_OMIT_FROZEN) \
39+
Python/frozen.o
40+
41+
+LINK_PYTHON_OBJS=@LINK_PYTHON_OBJS@
42+
+
43+
##########################################################################
44+
# DTrace
45+
46+
@@ -589,7 +591,7 @@ clinic: check-clean-src $(srcdir)/Modules/_blake2/blake2s_impl.c
47+
48+
# Build the interpreter
49+
$(BUILDPYTHON): Programs/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY) $(EXPORTSYMS)
50+
- $(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS)
51+
+ $(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
52+
53+
platform: $(BUILDPYTHON) pybuilddir.txt
54+
$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform
55+
diff --git a/configure.ac b/configure.ac
56+
index aa515da4655..122b11def62 100644
57+
--- a/configure.ac
58+
+++ b/configure.ac
59+
@@ -1106,6 +1106,17 @@ then
60+
fi
61+
AC_MSG_RESULT($enable_shared)
62+
63+
+AC_MSG_CHECKING([for --enable-static-libpython-for-interpreter])
64+
+AC_ARG_ENABLE([static-libpython-for-interpreter],
65+
+ AS_HELP_STRING([--enable-static-libpython-for-interpreter],
66+
+ [even with --enable-shared, statically link libpython into the interpreter (default is to use the shared library)]))
67+
+
68+
+if test -z "$enable_static_libpython_for_interpreter"
69+
+then
70+
+ enable_static_libpython_for_interpreter="no"
71+
+fi
72+
+AC_MSG_RESULT([$enable_static_libpython_for_interpreter])
73+
+
74+
AC_MSG_CHECKING(for --enable-profiling)
75+
AC_ARG_ENABLE(profiling,
76+
AS_HELP_STRING([--enable-profiling], [enable C-level code profiling with gprof (default is no)]))
77+
@@ -1211,6 +1222,13 @@ fi
78+
79+
AC_MSG_RESULT($LDLIBRARY)
80+
81+
+if test "$enable_static_libpython_for_interpreter" = "yes"; then
82+
+ LINK_PYTHON_OBJS='$(LIBRARY_OBJS)'
83+
+else
84+
+ LINK_PYTHON_OBJS='$(BLDLIBRARY)'
85+
+fi
86+
+AC_SUBST(LINK_PYTHON_OBJS)
87+
+
88+
AC_SUBST(AR)
89+
AC_CHECK_TOOLS(AR, ar aal, ar)
90+
91+
--
92+
2.39.5 (Apple Git-154)
93+

0 commit comments

Comments
 (0)