From b4e7f7128e66d83ab40b2f2266e73a9480ae5df5 Mon Sep 17 00:00:00 2001 From: Itamar Oren Date: Wed, 21 Oct 2026 18:00:08 -0700 Subject: [PATCH] gh-144942: Link `$(MODLIBS)` into the macOS shared library and framework (GH-159966) The rules that link `libpython$(LDVERSION).dylib` and the macOS and iOS frameworks don't pass `$(MODLIBS)`, unlike the `libpython$(LDVERSION).so` rule. When a module is built in, for example via `Modules/Setup.local`, the libraries that module needs are left out: - **`--enable-framework`:** the build fails with undefined symbols. - **`--enable-shared`:** the dylib is linked with `-undefined dynamic_lookup`, so the build succeeds and the failure only shows up at runtime. To reproduce, build on macOS with this `Modules/Setup.local`: ``` *static* _blake2 blake2module.c ``` On `main`: - **`--enable-framework`:** linking `Python.framework` fails with 12 undefined `_Py_LibHacl_*` symbols. - **`--enable-shared`:** `nm -u libpython3.16.dylib` lists the same 12 symbols. After `make install`, `python3 -c "import _blake2; _blake2.blake2b(b'abc')"` aborts with `dyld: missing symbol called`, and so does a C program that embeds Python. With this change, both builds link with no undefined HACL symbols, and `_blake2` works as a built-in module. In default builds `$(MODLIBS)` is empty, so they are unaffected. `test_hashlib`, `test_hmac`, `test_embed` and `test_sysconfig` pass for shared and framework builds, both with and without the `Setup.local` above (macOS 14, arm64). I haven't tested the iOS framework rule, which gets the same one-line change. python-build-standalone already carries the dylib part of this as a patch ([astral-sh/python-build-standalone#870](https://github.com/astral-sh/python-build-standalone/issues/870)). This PR doesn't remove `-undefined dynamic_lookup`, as the earlier gh-145186 did. (cherry picked from commit 03864a26212a80fec120137a6161ff7c859d179c) Co-authored-by: Itamar Oren --- Makefile.pre.in | 6 +++--- .../Build/2026-10-20-20-00-00.gh-issue-144942.MdLibs.rst | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Build/2026-10-20-20-00-00.gh-issue-144942.MdLibs.rst diff --git a/Makefile.pre.in b/Makefile.pre.in index 7b3e508d819459a..ca46f1a89849763 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -1034,7 +1034,7 @@ libpython3.so: libpython$(LDVERSION).so $(BLDSHARED) $(NO_AS_NEEDED) -o $@ -Wl,-h$@ $^ libpython$(LDVERSION).dylib: $(LIBRARY_OBJS) - $(CC) -dynamiclib $(PY_CORE_LDFLAGS) -undefined dynamic_lookup -Wl,-install_name,$(prefix)/lib/libpython$(LDVERSION).dylib -Wl,-compatibility_version,$(VERSION) -Wl,-current_version,$(VERSION) -o $@ $(LIBRARY_OBJS) $(DTRACE_OBJS) $(SHLIBS) $(LIBC) $(LIBM); \ + $(CC) -dynamiclib $(PY_CORE_LDFLAGS) -undefined dynamic_lookup -Wl,-install_name,$(prefix)/lib/libpython$(LDVERSION).dylib -Wl,-compatibility_version,$(VERSION) -Wl,-current_version,$(VERSION) -o $@ $(LIBRARY_OBJS) $(DTRACE_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); \ libpython$(VERSION).sl: $(LIBRARY_OBJS) @@ -1068,7 +1068,7 @@ $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/$(PYTHONFRAMEWORK): \ -install_name $(PYTHONFRAMEWORKINSTALLNAMEPREFIX)/$(PYTHONFRAMEWORK) \ -compatibility_version $(VERSION) \ -current_version $(VERSION) \ - -framework CoreFoundation $(LIBS); + -framework CoreFoundation $(MODLIBS) $(LIBS); $(INSTALL) -d -m $(DIRMODE) \ $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)/Resources/English.lproj $(INSTALL_DATA) $(RESSRCDIR)/Info.plist \ @@ -1089,7 +1089,7 @@ $(PYTHONFRAMEWORKDIR)/$(PYTHONFRAMEWORK): \ -install_name $(PYTHONFRAMEWORKINSTALLNAMEPREFIX)/$(PYTHONFRAMEWORK) \ -compatibility_version $(VERSION) \ -current_version $(VERSION) \ - -framework CoreFoundation $(LIBS); + -framework CoreFoundation $(MODLIBS) $(LIBS); $(INSTALL_DATA) $(RESSRCDIR)/Info.plist $(PYTHONFRAMEWORKDIR)/Info.plist # This rule builds the Cygwin Python DLL and import library if configured diff --git a/Misc/NEWS.d/next/Build/2026-10-20-20-00-00.gh-issue-144942.MdLibs.rst b/Misc/NEWS.d/next/Build/2026-10-20-20-00-00.gh-issue-144942.MdLibs.rst new file mode 100644 index 000000000000000..94b25d67af2fe46 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-10-20-20-00-00.gh-issue-144942.MdLibs.rst @@ -0,0 +1,3 @@ +Fix the macOS shared library and framework builds not linking the libraries +required by built-in modules, such as the HACL* objects needed by ``_blake2`` +when it is built in via :file:`Modules/Setup.local`.