Skip to content

Commit 02e9418

Browse files
boukadisbladis
authored andcommitted
fix: prefer architecture-specific wheels over universal for macOS
This is what poetry also seems to do. It should lead to smaller downloads if both are available and avoiding universal wheels that are actually just arm64 or x86_64, which I've seen in the wild.
1 parent 1ebed90 commit 02e9418

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/pypa.nix

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ let
3232
# PEP-625 only specifies .tar.gz as valid extension but .zip is also fairly widespread.
3333
matchSdistFileName = match "([^-]+)-(.+)(\.tar\.gz|\.zip)";
3434

35+
matchMacosTag = match "macosx_([0-9]+)_([0-9]+)_(.+)";
36+
3537
# Tag normalization documented in
3638
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#details
3739
normalizedImpls = {
@@ -275,7 +277,7 @@ lib.fix (self: {
275277
else if hasPrefix "macosx" platformTag then
276278
(
277279
let
278-
m = match "macosx_([0-9]+)_([0-9]+)_(.+)" platformTag;
280+
m = matchMacosTag platformTag;
279281
major = elemAt m 0;
280282
minor = elemAt m 1;
281283
arch = elemAt m 2;
@@ -476,7 +478,6 @@ lib.fix (self: {
476478
languageTags = filter isPythonTagCompatible file.languageTags;
477479
# Extract the tag as a number. E.g. "37" is `toInt "37"` and "none"/"any" is 0
478480
languageTags' = map (tag: if tag == "none" then 0 else toInt tag.version) languageTags;
479-
480481
in
481482
{
482483
bestLanguageTag = head (sort (x: y: x > y) languageTags');
@@ -485,6 +486,20 @@ lib.fix (self: {
485486
&& length languageTags > 0
486487
&& lib.any (self.isPlatformTagCompatible platform python.stdenv.cc.libc) file.platformTags;
487488
inherit file;
489+
# Prefer macOS wheels with specific architectures over universal2
490+
macArchPreference =
491+
let
492+
macArchs = filter isString (
493+
map (
494+
tag:
495+
let
496+
m = matchMacosTag tag;
497+
in
498+
if m != null then elemAt m 2 else null
499+
) file.platformTags
500+
);
501+
in
502+
if any (arch: arch != "universal2") macArchs then 1 else 0;
488503
}
489504
) files;
490505

@@ -498,6 +513,7 @@ lib.fix (self: {
498513
|| x.file.version > y.file.version
499514
|| (x.file.buildTag != null && (y.file.buildTag == null || x.file.buildTag > y.file.buildTag))
500515
|| x.bestLanguageTag > y.bestLanguageTag
516+
|| x.macArchPreference > y.macArchPreference
501517
) compatibleFiles;
502518

503519
in

lib/test_pypa.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ in
512512
testZmqCPythonDarwin311 = mkTest {
513513
input = zmqWheels;
514514
output = [
515-
"pyzmq-24.0.1-cp311-cp311-macosx_10_15_universal2.whl"
516515
"pyzmq-24.0.1-cp311-cp311-macosx_10_9_x86_64.whl"
516+
"pyzmq-24.0.1-cp311-cp311-macosx_10_15_universal2.whl"
517517
];
518518
python = mocks.cpythonDarwin311;
519519
};

0 commit comments

Comments
 (0)