Skip to content

Commit c88c27b

Browse files
authored
gh-133312: configure: add --enable-static-libpython-for-interpreter (#133313)
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 e0b56f0 commit c88c27b

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

Doc/using/configure.rst

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

10131013
.. versionadded:: 3.10
10141014

1015+
.. option:: --enable-static-libpython-for-interpreter
1016+
1017+
Do not link the Python interpreter binary (``python3``) against the
1018+
shared Python library; instead, statically link the interpreter
1019+
against ``libpython`` as if ``--enable-shared`` had not been used,
1020+
but continue to build the shared ``libpython`` (for use by other
1021+
programs).
1022+
1023+
This option does nothing if ``--enable-shared`` is not used.
1024+
1025+
The default (when ``-enable-shared`` is used) is to link the Python
1026+
interpreter against the built shared library.
1027+
1028+
.. versionadded:: next
1029+
10151030

10161031
Libraries options
10171032
-----------------
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

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,17 @@ fi],
15261526
[AC_MSG_RESULT([yes])])
15271527
AC_SUBST([STATIC_LIBPYTHON])
15281528

1529+
AC_MSG_CHECKING([for --enable-static-libpython-for-interpreter])
1530+
AC_ARG_ENABLE([static-libpython-for-interpreter],
1531+
AS_HELP_STRING([--enable-static-libpython-for-interpreter],
1532+
[even with --enable-shared, statically link libpython into the interpreter (default is to use the shared library)]))
1533+
1534+
if test -z "$enable_static_libpython_for_interpreter"
1535+
then
1536+
enable_static_libpython_for_interpreter="no"
1537+
fi
1538+
AC_MSG_RESULT([$enable_static_libpython_for_interpreter])
1539+
15291540
AC_MSG_CHECKING([for --enable-profiling])
15301541
AC_ARG_ENABLE([profiling],
15311542
AS_HELP_STRING([--enable-profiling], [enable C-level code profiling with gprof (default is no)]))
@@ -1679,7 +1690,11 @@ if test "$PY_ENABLE_SHARED" = 1 || test "$enable_framework" ; then
16791690
LIBRARY_DEPS="\$(LIBRARY) $LIBRARY_DEPS"
16801691
fi
16811692
# Link Python program to the shared library
1682-
LINK_PYTHON_OBJS='$(BLDLIBRARY)'
1693+
if test "$enable_static_libpython_for_interpreter" = "yes"; then
1694+
LINK_PYTHON_OBJS='$(LIBRARY_OBJS)'
1695+
else
1696+
LINK_PYTHON_OBJS='$(BLDLIBRARY)'
1697+
fi
16831698
else
16841699
if test "$STATIC_LIBPYTHON" = 0; then
16851700
# Build Python needs object files but don't need to build
@@ -2161,11 +2176,14 @@ if test "$Py_BOLT" = 'true' ; then
21612176
fi
21622177
fi
21632178

2164-
dnl Enable BOLT of libpython if built.
2179+
dnl Enable BOLT of libpython if built and used by the python3 binary.
2180+
dnl (If it is built but not used, we cannot profile it.)
21652181
AC_SUBST([BOLT_BINARIES])
21662182
BOLT_BINARIES='$(BUILDPYTHON)'
21672183
AS_VAR_IF([enable_shared], [yes], [
2168-
BOLT_BINARIES="${BOLT_BINARIES} \$(INSTSONAME)"
2184+
AS_VAR_IF([enable_static_libpython_for_interpreter], [no], [
2185+
BOLT_BINARIES="${BOLT_BINARIES} \$(INSTSONAME)"
2186+
])
21692187
])
21702188

21712189
AC_ARG_VAR(

0 commit comments

Comments
 (0)