Skip to content

Commit e982394

Browse files
committed
make static linking unconditional
1 parent 985ffc6 commit e982394

8 files changed

Lines changed: 206 additions & 234 deletions

‎CHANGELOG.md‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
### 0.3.34.237.1 (2026-10-09)
44

5-
- Build every manylinux and musllinux wheel without a runtime dependency on
6-
libgfortran, and therefore without libquadmath, which is only ever pulled in
7-
as a dependency of libgfortran itself. This drops ~1MB (compressed) from the
8-
wheels that bundled both and, since libquadmath is LGPL-2.1-or-later with no
9-
GCC Runtime Library Exception, removes an LGPL obligation along with it. The
10-
two `x**n` entry points are taken as-is out of the toolchain's libgfortran.a
11-
so results stay bit-for-bit identical; see tools/gfortran_compat.sh. macOS
12-
is unchanged for now (ld64 has no -Wl,--exclude-libs), and Windows already
13-
static-links libgfortran. A toolchain that cannot support it falls back to
14-
linking libgfortran as before.
15-
= Update windows devstudio 2022 to 2026
5+
- Build every manylinux, musllinux and macOS wheel without a runtime
6+
dependency on libgfortran, and therefore without libquadmath, which is only
7+
ever pulled in as a dependency of libgfortran itself. This shrinks the
8+
wheels and simplifies licensing.
9+
10+
- Update windows devstudio 2022 to 2026
1611

1712

1813
### 0.3.34.237.0 (2026-09-06)

‎ci-repair-wheel.sh‎

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,8 @@ else
1919
# rm dist/scipy_openblas*-none-any.whl
2020
# rm {dest_dir}/*.whl
2121

22-
# Add an RPATH to libgfortran:
23-
# https://github.com/pypa/auditwheel/issues/451
24-
# Use zipfile since the manylinux images do not have `zip`
25-
#
26-
# Platforms built with NO_LIBGFORTRAN=1 bundle no libgfortran (and so no
27-
# libquadmath, which is the only reason this fixup is needed at all), so
28-
# there is nothing to repair there. Count what we extracted and skip the
29-
# rest when the wheel is already clean.
30-
n_gfortran=$(python3 -c "
31-
import re, sys, zipfile, pathlib
32-
whl = next(pathlib.Path(sys.argv[1]).glob('*.whl'))
33-
with zipfile.ZipFile(whl, 'a') as z:
34-
members = [m for m in z.namelist() if re.search(r'libgfortran', m)]
35-
z.extractall(members=members)
36-
print(len(members))
37-
" "$1")
38-
if [ "$n_gfortran" = "0" ]; then
39-
echo "wheel bundles no libgfortran; skipping the auditwheel #451 rpath fixup"
40-
else
41-
patchelf --force-rpath --set-rpath '$ORIGIN' */lib/libgfortran*
42-
python3 -c "
43-
import sys, zipfile, pathlib, glob
44-
whl = next(pathlib.Path(sys.argv[1]).glob('*.whl'))
45-
patched = {f: pathlib.Path(f).read_bytes() for f in glob.glob('*/lib/libgfortran*')}
46-
47-
# Read all original entries, replacing patched one
48-
entries = {}
49-
with zipfile.ZipFile(whl, 'r') as z:
50-
for item in z.infolist():
51-
entries[item] = patched.get(item.filename, z.read(item.filename))
52-
53-
# Rewrite the archive
54-
with zipfile.ZipFile(whl, 'w', compression=zipfile.ZIP_DEFLATED) as z:
55-
for item, data in entries.items():
56-
z.writestr(item, data)
57-
" "$1"
58-
fi
22+
# No libgfortran rpath fixup (auditwheel #451) any more: the wheels
23+
# bundle no libgfortran, which is the only reason it was ever needed.
5924
mkdir -p /output
6025
# copy libs/openblas*.tar.gz to dist/
6126
cp libs/openblas*.tar.gz /output/

‎ci-test.sh‎

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@ fi
3131
$PYTHON -m pip install pkgconf
3232
$PYTHON -m pkgconf scipy-openblas --cflags
3333

34-
# Check the Fortran-runtime story of the installed wheel.
35-
#
36-
# Note this asks the *library* what happened rather than asking
37-
# want_no_libgfortran what we intended: build_gfortran_compat falls back to
38-
# linking libgfortran on toolchains it cannot support (no static
39-
# libgfortran.a on riscv64, for instance), and such a wheel legitimately
40-
# still bundles the runtime. The build already asserts the clean case on the
41-
# library it produces; repeating it here catches anything
42-
# auditwheel/delocate might reintroduce while repairing the wheel.
34+
# Check that the installed wheel carries no Fortran runtime.
4335
source tools/gfortran_compat.sh
4436

4537
if [ "${INTERFACE64}" != "1" ]; then
@@ -55,18 +47,11 @@ if [ -z "$openblas_lib" ]; then
5547
exit 1
5648
fi
5749

58-
if [ -n "$(fortran_runtime_deps "$openblas_lib")" ]; then
59-
echo "NOTE: $(basename "$openblas_lib") links the Fortran runtime, so this"
60-
echo " platform took the fallback path in build_gfortran_compat and the"
61-
echo " wheel is expected to bundle it. Nothing to assert."
62-
else
63-
# The library needs no Fortran runtime, so the wheel must not ship one.
64-
found=$(find "$pkgdir" \( -name 'libgfortran*' -o -name 'libquadmath*' \) -print)
65-
if [ -n "$found" ]; then
66-
echo "FAIL: $(basename "$openblas_lib") needs no Fortran runtime, yet the"
67-
echo " wheel still bundles it:"
68-
echo "$found"
69-
exit 1
70-
fi
71-
assert_no_fortran_runtime "$openblas_lib"
50+
# The library needs no Fortran runtime, so the wheel must not ship one.
51+
found=$(find "$pkgdir" \( -name 'libgfortran*' -o -name 'libquadmath*' \) -print)
52+
if [ -n "$found" ]; then
53+
echo "FAIL: the wheel still bundles the Fortran runtime:"
54+
echo "$found"
55+
exit 1
7256
fi
57+
assert_no_fortran_runtime "$openblas_lib"

‎patches/0001-allow-building-without-libgfortran.patch‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/Makefile.system b/Makefile.system
2-
index e8e732e9c..90c814ebd 100644
2+
index e8e732e9c..9b05ed213 100644
33
--- a/Makefile.system
44
+++ b/Makefile.system
55
@@ -1207,15 +1207,17 @@ FCOMMON_OPT += -Wall
@@ -21,7 +21,7 @@ index e8e732e9c..90c814ebd 100644
2121
ifdef NO_BINARY_MODE
2222
ifeq ($(ARCH), $(filter $(ARCH),mips64))
2323
ifdef BINARY64
24-
@@ -1917,6 +1919,31 @@ export CROSS
24+
@@ -1917,6 +1919,43 @@ export CROSS
2525
export CROSS_SUFFIX
2626
export NOFORTRAN
2727
export C_LAPACK
@@ -42,7 +42,19 @@ index e8e732e9c..90c814ebd 100644
4242
+ifeq ($(NO_LIBGFORTRAN), 1)
4343
+FEXTRALIB := $(filter-out -lgfortran -lquadmath,$(FEXTRALIB))
4444
+ifneq ($(LIBGFORTRAN_COMPAT), )
45+
+ifeq ($(OSNAME), Darwin)
46+
+# ld64 has no --exclude-libs and needs none: the dylib link passes
47+
+# -Wl,-exported_symbols_list,osx.def, which holds only the BLAS and LAPACK
48+
+# names, so whatever the compat archive brings in is unexported already.
49+
+# -dead_strip_dylibs is what macOS needs instead. The dylib is linked by the
50+
+# gfortran driver, which appends -lgfortran itself whatever FEXTRALIB says;
51+
+# this drops the load command of a dylib no symbol resolves to, taking
52+
+# libgfortran -- and with it libgfortran's own libquadmath dependency -- out
53+
+# of the result.
54+
+LIBGFORTRAN_COMPAT_LINK = $(LIBGFORTRAN_COMPAT) -Wl,-dead_strip_dylibs
55+
+else
4556
+LIBGFORTRAN_COMPAT_LINK = -Wl,--exclude-libs,$(notdir $(LIBGFORTRAN_COMPAT)) $(LIBGFORTRAN_COMPAT)
57+
+endif
4658
+FEXTRALIB += $(LIBGFORTRAN_COMPAT_LINK)
4759
+endif
4860
+endif

‎tools/LICENSE_linux.txt‎

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
This binary distribution of NumPy also bundles the following software:
55

6-
76
Name: OpenBLAS
8-
Files: numpy.libs/libscipy_openblas*.so
7+
Files: lib/libscipy_openblas*.so
98
Description: bundled as a dynamically linked library
109
Availability: https://github.com/OpenMathLib/OpenBLAS/
1110
License: BSD-3-Clause-Attribution
@@ -41,7 +40,7 @@ License: BSD-3-Clause-Attribution
4140

4241

4342
Name: LAPACK
44-
Files: numpy.libs/libscipy_openblas*.so
43+
Files: lib/libscipy_openblas*.so
4544
Description: bundled in OpenBLAS
4645
Availability: https://github.com/OpenMathLib/OpenBLAS/
4746
License: BSD-3-Clause-Attribution
@@ -96,8 +95,8 @@ License: BSD-3-Clause-Attribution
9695

9796

9897
Name: GCC runtime library
99-
Files: numpy.libs/libgfortran*.so
100-
Description: dynamically linked to files compiled with gcc
98+
Files: scipy_openblas*/lib/libscipy_openblas*
99+
Description: libgfortran routines statically linked into libscipy_openblas
101100
Availability: https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=libgfortran
102101
License: GPL-3.0-with-GCC-exception
103102
Copyright (C) 2002-2017 Free Software Foundation, Inc.
@@ -878,25 +877,3 @@ may consider it more useful to permit linking proprietary applications with
878877
the library. If this is what you want to do, use the GNU Lesser General
879878
Public License instead of this License. But first, please read
880879
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
881-
882-
Name: libquadmath
883-
Files: numpy.libs/libquadmath*.so
884-
Description: dynamically linked to files compiled with gcc
885-
Availability: https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=libquadmath
886-
License: LGPL-2.1-or-later
887-
888-
GCC Quad-Precision Math Library
889-
Copyright (C) 2010-2019 Free Software Foundation, Inc.
890-
Written by Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
891-
892-
This file is part of the libquadmath library.
893-
Libquadmath is free software; you can redistribute it and/or
894-
modify it under the terms of the GNU Library General Public
895-
License as published by the Free Software Foundation; either
896-
version 2.1 of the License, or (at your option) any later version.
897-
898-
Libquadmath is distributed in the hope that it will be useful,
899-
but WITHOUT ANY WARRANTY; without even the implied warranty of
900-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
901-
Lesser General Public License for more details.
902-
https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html

‎tools/LICENSE_osx.txt‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ License: BSD-3-Clause-Attribution
9595

9696

9797
Name: GCC runtime library
98-
Files: numpy/.dylibs/libgfortran*, numpy/.dylibs/libgcc*
99-
Description: dynamically linked to files compiled with gcc
98+
Files: scipy_openblas*/lib/libscipy_openblas*
99+
Description: libgfortran routines statically linked into libscipy_openblas
100100
Availability: https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=libgfortran
101101
License: GPL-3.0-with-GCC-exception
102102
Copyright (C) 2002-2017 Free Software Foundation, Inc.
@@ -877,25 +877,3 @@ may consider it more useful to permit linking proprietary applications with
877877
the library. If this is what you want to do, use the GNU Lesser General
878878
Public License instead of this License. But first, please read
879879
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
880-
881-
Name: libquadmath
882-
Files: numpy/.dylibs/libquadmath*.so
883-
Description: dynamically linked to files compiled with gcc
884-
Availability: https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=libquadmath
885-
License: LGPL-2.1-or-later
886-
887-
GCC Quad-Precision Math Library
888-
Copyright (C) 2010-2019 Free Software Foundation, Inc.
889-
Written by Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
890-
891-
This file is part of the libquadmath library.
892-
Libquadmath is free software; you can redistribute it and/or
893-
modify it under the terms of the GNU Library General Public
894-
License as published by the Free Software Foundation; either
895-
version 2.1 of the License, or (at your option) any later version.
896-
897-
Libquadmath is distributed in the hope that it will be useful,
898-
but WITHOUT ANY WARRANTY; without even the implied warranty of
899-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
900-
Lesser General Public License for more details.
901-
https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html

‎tools/build_steps.sh‎

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,6 @@ MB_PYTHON_VERSION=3.9
99

1010
source $(dirname "${BASH_SOURCE[0]}")/gfortran_compat.sh
1111

12-
# Whether to build without a runtime dependency on libgfortran (and so
13-
# without libquadmath, which is only ever a dependency of libgfortran itself).
14-
#
15-
# Enabled for every manylinux and musllinux target. The set of libgfortran
16-
# entry points the Fortran LAPACK sources reference is identical on all of
17-
# them -- _gfortran_concat_string and _gfortran_etime, plus _gfortran_pow_r*_i8
18-
# for INTERFACE64 -- because it comes from the LAPACK Fortran itself
19-
# (`SIDE // TRANS`, `x**n`) rather than from anything architecture specific.
20-
# If a toolchain cannot support it anyway, build_gfortran_compat returns
21-
# non-zero and the build falls back to linking libgfortran as before.
22-
#
23-
# macOS is excluded for now: the link needs -Wl,--exclude-libs to keep the
24-
# compat symbols out of the dylib's export table, and ld64 has no equivalent,
25-
# so it would be a hard link failure rather than a fallback. See
26-
# tools/gfortran_compat.sh. Windows never gets here -- it uses
27-
# tools/build_steps_windows.sh, which already static-links libgfortran and
28-
# asserts that libquadmath is not pulled in.
29-
#
30-
# Set NO_LIBGFORTRAN=0 in a CI matrix row to opt that row out, or
31-
# NO_LIBGFORTRAN=1 to force it on.
32-
function want_no_libgfortran {
33-
# want_no_libgfortran <plat> (plat accepted but not currently needed --
34-
# the decision is per-OS, and callers already have it to hand)
35-
[ -n "$NO_LIBGFORTRAN" ] && { [ "$NO_LIBGFORTRAN" = "1" ]; return $?; }
36-
case "$(uname -s)" in
37-
Linux) return 0 ;;
38-
*) return 1 ;;
39-
esac
40-
}
41-
4212
function before_build {
4313
# install gfortran, objconv on macOS
4414
if [ "$(uname -s)" == "Darwin" ]; then
@@ -72,10 +42,30 @@ function before_build {
7242
# Deployment target set by gfortran_utils
7343
echo "Deployment target $MACOSX_DEPLOYMENT_TARGET"
7444

45+
# The tarball ships libgfortran as a dylib only; fetch the static
46+
# archive of the same gcc release (tools/gfortran_compat.sh).
47+
local libgfortran_a
48+
if ! libgfortran_a=$(download_macos_libgfortran_a "${PLAT}"); then
49+
echo "ERROR: cannot fetch the static libgfortran for ${PLAT}" >&2
50+
exit 1
51+
fi
52+
export LIBGFORTRAN_A="$libgfortran_a"
53+
7554
# Build the objconv tool
7655
if [[ ! -x objconv/objconv ]]; then
7756
(cd ${ROOT_DIR}/objconv && bash ../tools/build_objconv.sh)
7857
fi
58+
else
59+
# Red Hat keeps static libraries in separate *-static subpackages, so
60+
# the images on a system gcc rather than a gcc-toolset (manylinux_2_39,
61+
# i.e. riscv64) have no libgfortran.a. The archive is in the CRB repo,
62+
# which they already enable. A no-op on gcc-toolset images and Alpine.
63+
if ! find_libgfortran_a > /dev/null; then
64+
echo "no static libgfortran; installing libgfortran-static"
65+
{ dnf install -y libgfortran-static \
66+
|| yum install -y libgfortran-static; } \
67+
|| echo "WARNING: could not install libgfortran-static"
68+
fi
7969
fi
8070
}
8171

@@ -202,23 +192,17 @@ function build_lib {
202192
esac
203193
interface_flags="$interface_flags SYMBOLPREFIX=scipy_ LIBNAMEPREFIX=scipy_ FIXED_LIBNAME=1"
204194

205-
# Drop the libgfortran/libquadmath runtime dependency where we can. If
206-
# the toolchain cannot support it (no static libgfortran.a, or the
207-
# objects we need are no longer self-contained) fall back to the old
208-
# behaviour rather than failing the build -- see tools/gfortran_compat.sh.
209-
local compat_lib=""
210-
if want_no_libgfortran "$plat"; then
211-
if compat_lib=$(build_gfortran_compat "$PWD/build/gfortran_compat" "$interface64"); then
212-
# TIMER=NONE selects LAPACK's second_NONE.f/dsecnd_NONE.f, which
213-
# removes the only reference to _gfortran_etime. SECOND/DSECND
214-
# then return 0.0, as they already do in a C_LAPACK build.
215-
interface_flags="$interface_flags NO_LIBGFORTRAN=1 LIBGFORTRAN_COMPAT=$compat_lib TIMER=NONE"
216-
echo "building without a libgfortran runtime dependency"
217-
else
218-
compat_lib=""
219-
echo "WARNING: cannot drop libgfortran on ${MB_ML_LIBC:-manylinux}-$plat, linking it as usual"
220-
fi
195+
# Replace the libgfortran/libquadmath runtime dependency with the compat
196+
# archive; a toolchain that cannot supply it fails the build. TIMER=NONE
197+
# selects LAPACK's second_NONE.f/dsecnd_NONE.f, removing the only
198+
# reference to _gfortran_etime. See tools/gfortran_compat.sh.
199+
local compat_lib
200+
if ! compat_lib=$(build_gfortran_compat "$PWD/build/gfortran_compat" "$interface64"); then
201+
echo "ERROR: cannot build the libgfortran compat archive for" \
202+
"${MB_ML_LIBC:-manylinux}-$plat" >&2
203+
exit 1
221204
fi
205+
interface_flags="$interface_flags NO_LIBGFORTRAN=1 LIBGFORTRAN_COMPAT=$compat_lib TIMER=NONE"
222206

223207
mkdir -p libs
224208
set -x
@@ -269,25 +253,20 @@ function build_lib {
269253
rm $BUILD_PREFIX/lib/pkgconfig/scipy-openblas.pc.bak
270254
fi
271255

272-
if [ -n "$compat_lib" ]; then
273-
# The shared library picked these up from the link line (see
274-
# LIBGFORTRAN_COMPAT_LINK in the patch), but the static library is
275-
# assembled from OpenBLAS's own objects only. Add them so
276-
# that linking libscipy_openblas*.a does not need libgfortran either
277-
# -- the .pc file no longer advertises -lgfortran in Libs.private.
278-
# Done after the INTERFACE64 branch above, which overwrites the
279-
# installed archive with the symbol-renamed one. The renaming does
280-
# not touch _gfortran_* names, so order is safe either way.
281-
local compat_dir=$(dirname "$compat_lib")
282-
for static_lib in $BUILD_PREFIX/lib/libscipy_openblas*.a; do
283-
# An unmatched glob arrives as a literal, and `ar crs` would
284-
# happily create an archive at that bogus path.
285-
[ -f "$static_lib" ] || continue
286-
ar crs "$static_lib" "$compat_dir"/*.o
287-
done
288-
assert_no_fortran_runtime $BUILD_PREFIX/lib/libscipy_openblas*.so \
289-
$BUILD_PREFIX/lib/libscipy_openblas*.dylib
290-
fi
256+
# The shared library picked these up from the link line (see
257+
# LIBGFORTRAN_COMPAT_LINK in the patch), but the static one is assembled
258+
# from OpenBLAS's own objects only; add them there too, so that linking
259+
# libscipy_openblas*.a does not need libgfortran either. Must come after
260+
# the INTERFACE64 branch above, which overwrites the installed archive.
261+
local compat_dir=$(dirname "$compat_lib")
262+
for static_lib in $BUILD_PREFIX/lib/libscipy_openblas*.a; do
263+
# An unmatched glob arrives as a literal, and `ar crs` would happily
264+
# create an archive at that bogus path.
265+
[ -f "$static_lib" ] || continue
266+
ar crs "$static_lib" "$compat_dir"/*.o
267+
done
268+
assert_no_fortran_runtime $BUILD_PREFIX/lib/libscipy_openblas*.so \
269+
$BUILD_PREFIX/lib/libscipy_openblas*.dylib
291270

292271
local out_name="openblas.tar.gz"
293272
tar zcvf libs/$out_name \

0 commit comments

Comments
 (0)