Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow linking to zlib import library on Windows #8519

Merged
merged 2 commits into from
Nov 9, 2024

Conversation

cubanpit
Copy link
Contributor

@cubanpit cubanpit commented Nov 1, 2024

This allows to link to zlib when used as shared library, in my environment I do not activate all of the external libraries so that part might influence the result, but I think it can still be valuable for others to try the same.

If compiled as shared library zlib produces `zlib1.dll` and `zdll.lib`.
@cubanpit cubanpit changed the title Allow linking to shared zlib Allow linking to shared zlib on Windows Nov 1, 2024
@radarhere
Copy link
Member

I do not activate all of the external libraries

Could you provide some more information about why your environment might be different to other environments that have used Pillow previously?

@cubanpit
Copy link
Contributor Author

cubanpit commented Nov 2, 2024

Sorry, I might have used a confusing phrasing. I compile Pillow with only a few external libraries, I do not include TIFF support for example, this is what I meant. I use a standard environment to compile Pillow, a Windows 10 container with MSVC 2022 build tools.

@radarhere
Copy link
Member

Thanks. Perhaps I needed to phrase my question better - Pillow has been around for a while, and no one else has suggested supporting 'zdll' as far as I can see. Do you have any thoughts about what you are doing differently that means that you're the first? I'm not trying to say that this change isn't necessary, I'd just like to get a better understanding of the need.

Just to confirm - you've tested this locally and it works for you?

@nulano
Copy link
Contributor

nulano commented Nov 2, 2024

It seems to be the default name for the dll import library when building zlib from source: https://github.com/madler/zlib/blob/develop/win32/Makefile.msc#L18C14-L18C21

setup.py Outdated
@@ -690,6 +690,8 @@ def build_extensions(self) -> None:
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") # different name if shared
Copy link
Member

Choose a reason for hiding this comment

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

So then the different name is not because it is shared?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I understand it is, the .dll library is the shared library and zdll.lib helps linking to it, if zlib is built as static library I think only zlib.lib is generated on Windows.
The reason nobody tried this is that zlib as shared library is not a common use case, most people use static libraries on Windows, in particular for smaller ones.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point. Perhaps

Suggested change
feature.set("zlib", "zdll") # different name if shared
feature.set("zlib", "zdll") # dll import library

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I understand it is, the .dll library is the shared library and zdll.lib helps linking to it, if zlib is built as static library I think only zlib.lib is generated on Windows.

Correct, see Microsoft Docs : https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation#creating-an-import-library

Co-authored-by: Ondrej Baranovič <[email protected]>
@radarhere
Copy link
Member

Just to confirm - you've tested this locally and it works for you?

@cubanpit
Copy link
Contributor Author

cubanpit commented Nov 6, 2024

Yes, I tested this change (with necessary adaptation) on versions 9.4.0, 10.3.0 and 11.0.0.

@radarhere radarhere changed the title Allow linking to shared zlib on Windows Allow linking to zlib import library on Windows Nov 8, 2024
@radarhere radarhere merged commit a01c31a into python-pillow:main Nov 9, 2024
67 checks passed
@cubanpit cubanpit deleted the patch-1 branch November 11, 2024 07:13
@kmilos
Copy link
Contributor

kmilos commented Nov 13, 2024

So if a user has both a static zlib.lib and the zdll.lib implib the static one will be preferred anyway and there is no way to link to the shared one?

A better "fix" would be to ask/patch upstream to align their win32/Makefile.msc w/ CMakeLists.txt to produce zlib.lib (implib, as already expected and supported) and explicit zlibstatic.lib instead...

Cf. https://gitlab.kitware.com/cmake/cmake/-/blob/894ac85d6ba202093601d2aca4aceaeefbe6c98e/Modules/FindZLIB.cmake#L115-L121 (search order differs depending on the intent; however, as the search order here matches the shared branch, one could leave this change as is I suppose...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants