Repository: python-pillow/Pillow

Issue:
Allow setting specific SDK on macOS
Hey all

I help package things for conda-forge, and pillow 8.4 recently got stuck in limbo due to https://github.com/python-pillow/Pillow/pull/5624 breaking the build we (need to) use there. We rely on the fact that we can control the MacOS SDK being used (which is explicitly set up by conda-build), but after #5624, the sdk from the system gets preference and ends up breaking (mismatched compiler vs. lib).

I reverted it to get https://github.com/conda-forge/pillow-feedstock/pull/101 out the door, but carrying patches indefinitely is not great.

I haven't studied #5622, but I hope we can find a solution that suits everyone. This could (for example) work with an environment variable that gets precedence if non-empty (but many other solutions would likely be possible).


Discussion:
Hi. Could you point me to exactly where conda-forge sets the SDK being used?
Have a look at this [CI run](https://dev.azure.com/conda-forge/feedstock-builds/_build/results?buildId=408603&view=logs&j=385a3385-ae8b-54d1-9abe-3c614f74cb73&t=a5f9d343-149e-5621-6ab9-f0278009be04&l=772), for example
Sorry, azure-links to individual lines are pretty broken. Try looking for "echo 'Downloading 10.9 sdk'" and surrounding lines.
The CONDA_BUILD_SYSROOT then obviously goes together with `CONDA_TOOLCHAIN_BUILD=x86_64-apple-darwin13.4.0` (conda-forge generally uses oldest-reasonable compilers to also support older OSes - due to ABI concerns). That toolchain is being set independently of the VM/image where the built-in SDK might be much newer.

AFAICT, the pillow-code post-#5624 then causes the code to refer to the standard library in the system-SDK, which however cannot be processed by the old toolchain. Here's a [failing](https://dev.azure.com/conda-forge/feedstock-builds/_build/results?buildId=394168&view=results) CI run with exactly that.
Ok, what do you think of this idea - we only prefer the command line tools when `xcrun --show-sdk-path` returns the default setting - https://github.com/radarhere/Pillow/commit/58feec8cdaa858fb09ff23b7af608474f29f4416?

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -404,6 +404,27 @@ def _remove_extension(self, name):
                 self.extensions.remove(extension)
                 break
 
+    def get_macos_sdk_path(self):
+        try:
+            sdk_path = (
+                subprocess.check_output(["xcrun", "--show-sdk-path"])
+                .strip()
+                .decode("latin1")
+            )
+        except Exception:
+            sdk_path = None
+        if (
+            not sdk_path
+            or sdk_path == "/Applications/Xcode.app/Contents/Developer"
+            "/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
+        ):
+            commandlinetools_sdk_path = (
+                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
+            )
+            if os.path.exists(commandlinetools_sdk_path):
+                sdk_path = commandlinetools_sdk_path
+        return sdk_path
+
     def build_extensions(self):
 
         library_dirs = []
@@ -531,17 +552,7 @@ def build_extensions(self):
                 _add_directory(library_dirs, "/usr/X11/lib")
                 _add_directory(include_dirs, "/usr/X11/include")
 
-            # SDK install path
-            sdk_path = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
-            if not os.path.exists(sdk_path):
-                try:
-                    sdk_path = (
-                        subprocess.check_output(["xcrun", "--show-sdk-path"])
-                        .strip()
-                        .decode("latin1")
-                    )
-                except Exception:
-                    sdk_path = None
+            sdk_path = self.get_macos_sdk_path()
             if sdk_path:
                 _add_directory(library_dirs, os.path.join(sdk_path, "usr", "lib"))
                 _add_directory(include_dirs, os.path.join(sdk_path, "usr", "include"))


Test patch: