Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 65 additions & 50 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,18 @@ def _find_include_file(self: pil_build_ext, include: str) -> str | None:


def _find_library_file(self: pil_build_ext, library: str) -> str | None:
ret = self.compiler.find_library_file(self.compiler.library_dirs, library)
ret = self.compiler.find_library_file(
self.compiler.library_dirs, library, debug=debug_build()
)
if ret:
_dbg("Found library %s at %s", (library, ret))
else:
_dbg("Couldn't find library %s in %s", (library, self.compiler.library_dirs))
# we are only interested in the library name including a possible debug suffix
lib_name_base = os.path.basename(ret).split(".")[0]
# as the library prefix differs depending on library type and platform,
# we ignore it by looking for the actual library name
start_index = lib_name_base.find(library)
return lib_name_base[start_index:]

@mrbean-bremen mrbean-bremen Apr 25, 2024

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hacky, but I didn't find a better way to get the library name from the file name. I could not use splitext, as that would not handle something like libfoo.so.6.
As this is an internal function only, I hope it is ok.

_dbg("Couldn't find library %s in %s", (library, self.compiler.library_dirs))
return ret


Expand Down Expand Up @@ -691,20 +698,23 @@ def build_extensions(self) -> None:
if feature.want("zlib"):
_dbg("Looking for zlib")
if _find_include_file(self, "zlib.h"):
if _find_library_file(self, "z"):
feature.set("zlib", "z")
elif sys.platform == "win32" and _find_library_file(self, "zlib"):
feature.set("zlib", "zlib") # alternative name
elif sys.platform == "win32" and _find_library_file(self, "zdll"):
feature.set("zlib", "zdll") # dll import library
lib_zlib = _find_library_file(self, "z")
if lib_zlib is None and sys.platform == "win32":
# alternative name
lib_zlib = _find_library_file(self, "zlib")
if lib_zlib is None:
# dll import library
lib_zlib = _find_library_file(self, "zdll")
feature.set("zlib", lib_zlib)

if feature.want("jpeg"):
_dbg("Looking for jpeg")
if _find_include_file(self, "jpeglib.h"):
if _find_library_file(self, "jpeg"):
feature.set("jpeg", "jpeg")
elif sys.platform == "win32" and _find_library_file(self, "libjpeg"):
feature.set("jpeg", "libjpeg") # alternative name
lib_jpeg = _find_library_file(self, "jpeg")
if lib_jpeg is None and sys.platform == "win32":
# alternative name
lib_jpeg = _find_library_file(self, "libjpeg")
feature.set("jpeg", lib_jpeg)

feature.set("openjpeg_version", None)
if feature.want("jpeg2000"):
Expand Down Expand Up @@ -734,35 +744,38 @@ def build_extensions(self) -> None:
(str(best_version), best_path),
)

if best_version and _find_library_file(self, "openjp2"):
# Add the directory to the include path so we can include
# <openjpeg.h> rather than having to cope with the versioned
# include path
_add_directory(self.compiler.include_dirs, best_path, 0)
feature.set("jpeg2000", "openjp2")
if best_version:
if lib_jpeg2k := _find_library_file(self, "openjp2"):
# Add the directory to the include path so we can include
# <openjpeg.h> rather than having to cope with the versioned
# include path
_add_directory(self.compiler.include_dirs, best_path, 0)
feature.set("jpeg2000", lib_jpeg2k)
feature.set("openjpeg_version", ".".join(str(x) for x in best_version))

if feature.want("imagequant"):
_dbg("Looking for imagequant")
if _find_include_file(self, "libimagequant.h"):
if _find_library_file(self, "imagequant"):
feature.set("imagequant", "imagequant")
elif _find_library_file(self, "libimagequant"):
feature.set("imagequant", "libimagequant")
lib_imagequant = _find_library_file(
self, "imagequant"
) or _find_library_file(self, "libimagequant")
if lib_imagequant is not None:
feature.set("imagequant", lib_imagequant)

if feature.want("tiff"):
_dbg("Looking for tiff")
if _find_include_file(self, "tiff.h"):
if sys.platform in ["win32", "darwin"] and _find_library_file(
self, "libtiff"
):
feature.set("tiff", "libtiff")
elif _find_library_file(self, "tiff"):
feature.set("tiff", "tiff")
lib_tiff = None
if sys.platform in ["win32", "darwin"]:
lib_tiff = _find_library_file(self, "libtiff")
if lib_tiff is None:
lib_tiff = _find_library_file(self, "tiff")
if lib_tiff is not None:
feature.set("tiff", lib_tiff)

if feature.want("freetype"):
_dbg("Looking for freetype")
if _find_library_file(self, "freetype"):
if lib_freetype := _find_library_file(self, "freetype"):
# look for freetype2 include files
freetype_version = 0
for subdir in self.compiler.include_dirs:
Expand All @@ -779,27 +792,28 @@ def build_extensions(self) -> None:
freetype_version = 21
break
if freetype_version:
feature.set("freetype", "freetype")
feature.set("freetype", lib_freetype)
if subdir:
_add_directory(self.compiler.include_dirs, subdir, 0)

if feature.get("freetype") and feature.want("raqm"):
if not feature.want_vendor("raqm"): # want system Raqm
_dbg("Looking for Raqm")
if _find_include_file(self, "raqm.h"):
if _find_library_file(self, "raqm"):
feature.set("raqm", "raqm")
elif _find_library_file(self, "libraqm"):
feature.set("raqm", "libraqm")
lib_raqm = _find_library_file(self, "raqm") or _find_library_file(
self, "libraqm"
)
if lib_raqm is not None:
feature.set("raqm", lib_raqm)
else: # want to build Raqm from src/thirdparty
_dbg("Looking for HarfBuzz")
feature.set("harfbuzz", None)
hb_dir = _find_include_dir(self, "harfbuzz", "hb.h")
if hb_dir:
if isinstance(hb_dir, str):
_add_directory(self.compiler.include_dirs, hb_dir, 0)
if _find_library_file(self, "harfbuzz"):
feature.set("harfbuzz", "harfbuzz")
if lib_harfbuzz := _find_library_file(self, "harfbuzz"):
feature.set("harfbuzz", lib_harfbuzz)
if feature.get("harfbuzz"):
if not feature.want_vendor("fribidi"): # want system FriBiDi
_dbg("Looking for FriBiDi")
Expand All @@ -810,20 +824,20 @@ def build_extensions(self) -> None:
_add_directory(
self.compiler.include_dirs, fribidi_dir, 0
)
if _find_library_file(self, "fribidi"):
feature.set("fribidi", "fribidi")
if lib_fribidi := _find_library_file(self, "fribidi"):
feature.set("fribidi", lib_fribidi)
feature.set("raqm", True)
else: # want to build FriBiDi shim from src/thirdparty
feature.set("raqm", True)

if feature.want("lcms"):
_dbg("Looking for lcms")
if _find_include_file(self, "lcms2.h"):
if _find_library_file(self, "lcms2"):
feature.set("lcms", "lcms2")
elif _find_library_file(self, "lcms2_static"):
# alternate Windows name.
feature.set("lcms", "lcms2_static")
lib_lcms2 = _find_library_file(self, "lcms2") or _find_library_file(
self, "lcms2_static" # alternate Windows name
)
if lib_lcms2 is not None:
feature.set("lcms", lib_lcms2)

if feature.want("webp"):
_dbg("Looking for webp")
Expand All @@ -837,14 +851,14 @@ def build_extensions(self) -> None:
_find_library_file(self, prefix + library)
for library in ("webp", "webpmux", "webpdemux")
):
feature.set("webp", prefix + "webp")
feature.set("webp", _find_library_file(self, prefix + "webp"))
break

if feature.want("xcb"):
_dbg("Looking for xcb")
if _find_include_file(self, "xcb/xcb.h"):
if _find_library_file(self, "xcb"):
feature.set("xcb", "xcb")
if lib_xcb := _find_library_file(self, "xcb"):
feature.set("xcb", lib_xcb)

if feature.want("avif"):
_dbg("Looking for avif")
Expand All @@ -853,8 +867,9 @@ def build_extensions(self) -> None:
major_version = int(
fp.read().split(b"#define AVIF_VERSION_MAJOR ")[1].split()[0]
)
if major_version >= 1 and _find_library_file(self, "avif"):
feature.set("avif", "avif")
if major_version >= 1:
if lib_avif := _find_library_file(self, "avif"):
feature.set("avif", lib_avif)

for f in feature:
if not feature.get(f) and feature.require(f):
Expand Down Expand Up @@ -908,7 +923,7 @@ def build_extensions(self) -> None:

if feature.get("freetype"):
srcs = []
libs = ["freetype"]
libs = [feature.get("freetype")]
defs = []
if feature.get("raqm"):
if not feature.want_vendor("raqm"): # using system Raqm
Expand Down