Skip to content

Build standard-library extensions as shared modules by default on Linux and macOS - #1448

Draft
jjhelmus wants to merge 7 commits into
mainfrom
jjh/shared_extensions
Draft

jjhelmus wants to merge 7 commits into
mainfrom
jjh/shared_extensions

Conversation

@jjhelmus

Copy link
Copy Markdown
Contributor

Build and install most standard-library extensions as shared libraries on Linux and macOS. Bundled libraries, OpenSSL, sqlite, etc, are statically linked into the appropriate extension. Fully-static distribution continue to statically link extensions into python and libpython.

Validate the link nature using MODSHARED_NAMES from the python/PYTHON.json file within the distribution for Python 3.11+. Use the a hardcoded fallback for 3.10. Test a handful of module as part of disttests.

This significantly reduces the size of python and libpython as the codepaths are moved to external shared libaries. The total distribution size is also reduced because these codepaths are no longer duplicated in python and libpython. In an aarch64 3.15 debug build the install directory was reduced by ~15% although the savings for the .tar.zstd is a more modest 3% and 7% for Linux and macOS.

Use MODSHARED_NAMES in PYTHON.json within the distribution to determine
which extentions should be shared when performing validation.

For Python 3.10 use a hard-coded list as MODSHARED_NAMES does not exist.

Keep existing logic for Windows and static builds.
On macOS, Python 3.12+, apply -hidden-l to the per-module linker
flags as well as MODLIBS.  This keeps shared extensions from
exporting symbols from bundled static libraries.
Shared extensions can require newer glibc symbols than libpython.
Scan both libpython and the shared objects in lib-dynload with
objdump and record the highest GLIBC symbol version found.
Build standard library extensions as shared by default while continuing
to statically link in any external libraries (openssl, zlib, etc).

This reduces the size of python and libpython and is more in line with
upstream defaults.

Static build continue to statically link all extensions.
Test that the _io, math, _ssl and _sqlite modules are loaded in the
manner expected by the extension and built option.
@jjhelmus jjhelmus added platform:darwin Specific to the macOS platform platform:linux Specific to the Linux platform python:all Select all Python versions arch:all Select all architectures build:all labels Nov 23, 2026
@indygreg

Copy link
Copy Markdown
Collaborator

Thank you for undoing this longstanding PyOxidizer kludge!

Backport CPython PR #116294 to Python 3.11 and 3.12. Use
_PYTHON_SYSCONFIGDATA_PATH to load the target configuration
without adding the target build directory to PYTHONPATH.

On macOS, ARM64 and x86-64 extensions have the same filename
suffix. Keeping their import paths separate prevents the ARM64
build interpreter from trying to load x86-64 extensions.
@jjhelmus

Copy link
Copy Markdown
Contributor Author

There is a race condition in the 3.11 builds where two installers write to the same extension file. This only rarely caused builds failures when extensions where statically linked but seems to occur more often with shared extensions. A one-line patch to Makefile can address this.

@jjhelmus

Copy link
Copy Markdown
Contributor Author

There may be an issue with statically linking libcurses into both _curses and _curses_panel as both have a private copy of the screen state.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:all Select all architectures build:all platform:darwin Specific to the macOS platform platform:linux Specific to the Linux platform python:all Select all Python versions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants