diff --git a/setup.py b/setup.py index fbd23a5685a..9d298bdfa20 100644 --- a/setup.py +++ b/setup.py @@ -231,12 +231,19 @@ def _find_include_file(self: pil_build_ext, include: str) -> int: 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)) - return ret + # 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:] + _dbg("Couldn't find library %s in %s", (library, self.compiler.library_dirs)) + return None def _find_include_dir(self: pil_build_ext, dirname: str, include: str) -> bool | str: @@ -687,20 +694,20 @@ 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": + lib_zlib = _find_library_file(self, "zlib") or _find_library_file( + self, "zdll" + ) # alternative name and dll import library + 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": + lib_jpeg = _find_library_file(self, "libjpeg") # alternative name + feature.set("jpeg", lib_jpeg) feature.set("openjpeg_version", None) if feature.want("jpeg2000"): @@ -730,35 +737,43 @@ def build_extensions(self) -> None: (best_version, best_path), ) - if best_version and _find_library_file(self, "openjp2"): - # Add the directory to the include path so we can include - # rather than having to cope with the versioned - # include path - _add_directory(self.compiler.include_dirs, best_path, 0) - feature.set("jpeg2000", "openjp2") - feature.set("openjpeg_version", ".".join(str(x) for x in best_version)) + if best_version: + lib_jpeg2k = _find_library_file(self, "openjp2") + if lib_jpeg2k is not None: + # Add the directory to the include path so we can include + # 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") + feature.set( + "imagequant", + _find_library_file(self, "imagequant") + or _find_library_file(self, "libimagequant"), + ) if feature.want("tiff"): _dbg("Looking for tiff") if _find_include_file(self, "tiff.h"): - if _find_library_file(self, "tiff"): - feature.set("tiff", "tiff") - if sys.platform in ["win32", "darwin"] and _find_library_file( - self, "libtiff" - ): - feature.set("tiff", "libtiff") + feature.set( + "tiff", + ( + sys.platform in ["win32", "darwin"] + and _find_library_file(self, "libtiff") + ) + or _find_library_file(self, "tiff"), + ) if feature.want("freetype"): _dbg("Looking for freetype") - if _find_library_file(self, "freetype"): + lib_freetype = _find_library_file(self, "freetype") + if lib_freetype is not None: # look for freetype2 include files freetype_version = 0 for subdir in self.compiler.include_dirs: @@ -775,7 +790,7 @@ 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) @@ -783,10 +798,11 @@ def build_extensions(self) -> None: 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") + feature.set( + "raqm", + _find_library_file(self, "raqm") + or _find_library_file(self, "libraqm"), + ) else: # want to build Raqm from src/thirdparty _dbg("Looking for HarfBuzz") feature.set("harfbuzz", None) @@ -794,8 +810,7 @@ def build_extensions(self) -> None: 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") + feature.set("harfbuzz", _find_library_file(self, "harfbuzz")) if feature.get("harfbuzz"): if not feature.want_vendor("fribidi"): # want system FriBiDi _dbg("Looking for FriBiDi") @@ -806,8 +821,8 @@ def build_extensions(self) -> None: _add_directory( self.compiler.include_dirs, fribidi_dir, 0 ) - if _find_library_file(self, "fribidi"): - feature.set("fribidi", "fribidi") + feature.set("fribidi", _find_library_file(self, "fribidi")) + if feature.get("fribidi"): feature.set("raqm", True) else: # want to build FriBiDi shim from src/thirdparty feature.set("raqm", True) @@ -815,11 +830,11 @@ def build_extensions(self) -> None: 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") + feature.set( + "lcms", + _find_library_file(self, "lcms2") + or _find_library_file(self, "lcms2_static"), + ) # alternate Windows name if feature.want("webp"): _dbg("Looking for webp") @@ -829,18 +844,18 @@ def build_extensions(self) -> None: ): # In Google's precompiled zip it is called "libwebp" for prefix in ("", "lib"): - if all( + lib_webps = tuple( _find_library_file(self, prefix + library) for library in ("webp", "webpmux", "webpdemux") - ): - feature.set("webp", prefix + "webp") + ) + if all(lib_webps): + feature.set("webp", lib_webps[0]) 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") + feature.set("xcb", _find_library_file(self, "xcb")) for f in feature: if not feature.get(f) and feature.require(f): @@ -894,7 +909,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