Skip to content

Commit 68b08bb

Browse files
committed
pythongh-133312: configure: add --enable-static-libpython-for-interpreter
This option changes the behavior of --enable-shared to continue to build the libpython3.x.so shared library, but not use it for linking the python3 interpreter executable. Instead, the executable is linked directly against the libpython .o files as it would be with --disable-shared. There are two benefits of this change. First, libpython uses thread-local storage, which is noticeably slower when used in a loaded module instead of in the main program, because the main program can take advantage of constant offsets from the thread state pointer but loaded modules have to dynamically call a function __tls_get_addr() to potentially allocate their thread-local storage area. (There is another thread-local storage model for dynamic libraries which mitigates most of this performance hit, but it comes at the cost of preventing dlopen("libpython3.x.so"), which is a use case we want to preserve.) Second, this improves the user experience around relocatable Python a little bit, in that we don't need to use an $ORIGIN-relative path to locate libpython3.x.so, which has some mild benefits around musl (which does not support $ORIGIN-relative DT_NEEDED, only $ORIGIN-relative DT_RPATH/DT_RUNPATH), users who want to make the interpreter setuid or setcap (which prevents processing $ORIGIN), etc.
1 parent 102f825 commit 68b08bb

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

Doc/using/configure.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,21 @@ Linker options
815815

816816
.. versionadded:: 3.10
817817

818+
.. option:: --enable-static-libpython-for-interpreter
819+
820+
Do not link the Python interpreter binary (``python3``) against the
821+
shared Python library; instead, statically link the interpreter
822+
against ``libpython`` as if ``--enable-shared`` had not been used,
823+
but continue to build the shared ``libpython`` (for use by other
824+
programs).
825+
826+
This option does nothing if ``--enable-shared`` is not used.
827+
828+
The default (when ``-enable-shared`` is used) is to link the Python
829+
interpreter against the built shared library.
830+
831+
.. versionadded:: 3.14
832+
818833

819834
Libraries options
820835
-----------------
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Add a new ``./configure`` option
2+
:option:`--enable-static-libpython-for-interpreter` which, when used
3+
with :option:`--enable-shared`, continues to build the shared library
4+
but does not use it for the interpreter. Instead, libpython is
5+
statically linked into the interpreter, as if :option:`--enable-shared`
6+
had not been used. This allows you to do a single build and get a Python
7+
interpreter binary that does not use a shared library but also get a
8+
shared library for use by other programs.

configure.ac

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,17 @@ fi],
15021502
[AC_MSG_RESULT([yes])])
15031503
AC_SUBST([STATIC_LIBPYTHON])
15041504

1505+
AC_MSG_CHECKING([for --enable-static-libpython-for-interpreter])
1506+
AC_ARG_ENABLE([static-libpython-for-interpreter],
1507+
AS_HELP_STRING([--enable-static-libpython-for-interpreter],
1508+
[even with --enable-shared, statically link libpython into the interpreter (default is to use the shared library)]))
1509+
1510+
if test -z "$enable_static_libpython_for_interpreter"
1511+
then
1512+
enable_static_libpython_for_interpreter="no"
1513+
fi
1514+
AC_MSG_RESULT([$enable_static_libpython_for_interpreter])
1515+
15051516
AC_MSG_CHECKING([for --enable-profiling])
15061517
AC_ARG_ENABLE([profiling],
15071518
AS_HELP_STRING([--enable-profiling], [enable C-level code profiling with gprof (default is no)]))
@@ -1660,7 +1671,11 @@ if test "$PY_ENABLE_SHARED" = 1 || test "$enable_framework" ; then
16601671
LIBRARY_DEPS="\$(LIBRARY) $LIBRARY_DEPS"
16611672
fi
16621673
# Link Python program to the shared library
1663-
LINK_PYTHON_OBJS='$(BLDLIBRARY)'
1674+
if test "$enable_static_libpython_for_interpreter" = "yes"; then
1675+
LINK_PYTHON_OBJS='$(LIBRARY_OBJS)'
1676+
else
1677+
LINK_PYTHON_OBJS='$(BLDLIBRARY)'
1678+
fi
16641679
else
16651680
if test "$STATIC_LIBPYTHON" = 0; then
16661681
# Build Python needs object files but don't need to build
@@ -2166,11 +2181,14 @@ if test "$Py_BOLT" = 'true' ; then
21662181
fi
21672182
fi
21682183

2169-
dnl Enable BOLT of libpython if built.
2184+
dnl Enable BOLT of libpython if built and used by the python3 binary.
2185+
dnl (If it is built but not used, we cannot profile it.)
21702186
AC_SUBST([BOLT_BINARIES])
21712187
BOLT_BINARIES='$(BUILDPYTHON)'
21722188
AS_VAR_IF([enable_shared], [yes], [
2173-
BOLT_BINARIES="${BOLT_BINARIES} \$(INSTSONAME)"
2189+
AS_VAR_IF([enable_static_libpython_for_interpreter], [no], [
2190+
BOLT_BINARIES="${BOLT_BINARIES} \$(INSTSONAME)"
2191+
])
21742192
])
21752193

21762194
AC_ARG_VAR(

0 commit comments

Comments
 (0)