|
| 1 | +From 5ae9112a87d45c3aff5ee269ff8e2e49ca278ed3 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Geoffrey Thomas <geofft@ldpreload.com> |
| 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