| 1 |
//! PyPI compatibility checking for platform tags. |
| 2 |
//! |
| 3 |
//! This module implements PyPI's platform tag validation rules to ensure wheels |
| 4 |
//! are compatible with PyPI's upload requirements. The validation logic is based on |
| 5 |
//! warehouse: <https://github.com/pypi/warehouse/blob/main/warehouse/forklift/legacy.py> |
| 6 |
//! |
| 7 |
//! Differences between PyPI's support and Rust: |
| 8 |
//! - Windows: PyPI allows ia64 (win_ia64) but Rust doesn't support IA64/Itanium |
| 9 |
//! - macOS: PyPI allows legacy PPC and fat binaries but these are either unsupported by Rust |
| 10 |
//! or created post-build with lipo rather than being direct Rust targets. This is supported by |
| 11 |
//! the virtual universal2-apple-darwin target instead. |
| 12 |
//! |
| 13 |
//! Supported architectures by platform (intersection of PyPI and Rust/maturin support): |
| 14 |
//! - Linux (manylinux): x86_64, i686, aarch64, armv7l, ppc64le, s390x, ppc64 |
| 15 |
//! - Linux (musllinux): x86_64, i686, aarch64, armv7l, ppc64le, s390x |
| 16 |
//! - Windows: x86_64, i686, aarch64 |
| 17 |
//! - macOS: x86_64, arm64, i686 (Tier 3), universal2 (maturin special target) |
| 18 |
//! - iOS: arm64, x86_64 (simulator) |
| 19 |
//! - Android: armeabi_v7a (armv7l), arm64_v8a (aarch64), x86 (i686), x86_64 |
| 20 |
|
| 21 |
use crate::target::legacy_py::{ |
| 22 |
ALLOWED_PLATFORMS, ANDROID_ARCHES, ANDROID_PLATFORM_RE, IOS_ARCHES, IOS_PLATFORM_RE, |
| 23 |
LINUX_PLATFORM_RE, MACOS_ARCHES, MACOS_MAJOR_VERSIONS, MACOS_PLATFORM_RE, MANYLINUX_ARCHES, |
| 24 |
MUSLLINUX_ARCHES, WINDOWS_ARCHES, |
| 25 |
}; |
| 26 |
use crate::target::{Os, Target}; |
| 27 |
use anyhow::{anyhow, bail, Result}; |
| 28 |
use target_lexicon::Environment; |
| 29 |
|
| 30 |
/// Check for target architectures that we know aren't supported by PyPI to error early. |
| 31 |
pub fn is_arch_supported_by_pypi(target: &Target) -> bool { |
| 32 |
let arch = target.target_arch().to_string(); |
| 33 |
match target.target_os() { |
| 34 |
Os::Windows => WINDOWS_ARCHES.contains(&arch.as_str()), |
| 35 |
Os::Macos => { |
| 36 |
// macOS uses arm64 in platform tags, but target triple uses aarch64 |
| 37 |
let normalized_arch = if arch == "aarch64" { "arm64" } else { &arch }; |
| 38 |
MACOS_ARCHES.contains(&normalized_arch) |
| 39 |
} |
| 40 |
Os::Ios => { |
| 41 |
// iOS uses arm64 in platform tags, but target triple uses aarch64 |
| 42 |
let normalized_arch = if arch == "aarch64" { "arm64" } else { &arch }; |
| 43 |
// PyPI allows iOS with arm64 and x86_64 (simulator) |
| 44 |
matches!(normalized_arch, "arm64" | "x86_64") |
| 45 |
} |
| 46 |
Os::Linux if target.target_triple().contains("android") => { |
| 47 |
// Android target triples map to specific platform tag architectures |
| 48 |
let android_arch = match arch.as_str() { |
| 49 |
"armv7l" => "armeabi_v7a", // armv7 little-endian |
| 50 |
"aarch64" => "arm64_v8a", |
| 51 |
"i686" => "x86", |
| 52 |
"x86_64" => "x86_64", |
| 53 |
_ => return false, |
| 54 |
}; |
| 55 |
ANDROID_ARCHES.contains(&android_arch) |
| 56 |
} |
| 57 |
Os::Linux => match target.target_env() { |
| 58 |
Environment::Gnu |
| 59 |
| Environment::Gnuabi64 |
| 60 |
| Environment::Gnueabi |
| 61 |
| Environment::Gnueabihf => { |
| 62 |
let arch1 = arch.as_str(); |
| 63 |
MANYLINUX_ARCHES.contains(&arch1) |
| 64 |
} |
| 65 |
Environment::Musl |
| 66 |
| Environment::Musleabi |
| 67 |
| Environment::Musleabihf |
| 68 |
| Environment::Muslabi64 => { |
| 69 |
let arch1 = arch.as_str(); |
| 70 |
MUSLLINUX_ARCHES.contains(&arch1) |
| 71 |
} |
| 72 |
_ => false, |
| 73 |
}, |
| 74 |
_ => false, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/// Validates that a wheel platform tag is allowed by PyPI. |
| 79 |
/// |
| 80 |
/// Based on PyPI warehouse platform tag validation logic. |
| 81 |
/// |
| 82 |
fn is_platform_tag_allowed_by_pypi(platform_tag: &str) -> bool { |
| 83 |
// Covers old Windows and old manylinux tags. |
| 84 |
if ALLOWED_PLATFORMS.contains(&platform_tag) { |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
// macOS |
| 89 |
if let Some(captures) = MACOS_PLATFORM_RE.captures(platform_tag) { |
| 90 |
let major = captures.name("major").unwrap().as_str(); |
| 91 |
let arch = captures.name("arch").unwrap().as_str(); |
| 92 |
|
| 93 |
return MACOS_MAJOR_VERSIONS.contains(&major) && MACOS_ARCHES.contains(&arch); |
| 94 |
} |
| 95 |
|
| 96 |
// manylinux/musllinux |
| 97 |
if let Some(captures) = LINUX_PLATFORM_RE.captures(platform_tag) { |
| 98 |
let libc = captures.name("libc").unwrap().as_str(); |
| 99 |
let arch = captures.name("arch").unwrap().as_str(); |
| 100 |
|
| 101 |
return match libc { |
| 102 |
"musl" => MUSLLINUX_ARCHES.contains(&arch), |
| 103 |
"many" => MANYLINUX_ARCHES.contains(&arch), |
| 104 |
_ => false, |
| 105 |
}; |
| 106 |
} |
| 107 |
|
| 108 |
// iOS |
| 109 |
if let Some(captures) = IOS_PLATFORM_RE.captures(platform_tag) { |
| 110 |
let arch = captures.name("arch").unwrap().as_str(); |
| 111 |
return IOS_ARCHES.contains(&arch); |
| 112 |
} |
| 113 |
|
| 114 |
// Android |
| 115 |
if let Some(captures) = ANDROID_PLATFORM_RE.captures(platform_tag) { |
| 116 |
let arch = captures.name("arch").unwrap().as_str(); |
| 117 |
return ANDROID_ARCHES.contains(&arch); |
| 118 |
} |
| 119 |
|
| 120 |
false |
| 121 |
} |
| 122 |
|
| 123 |
/// Validates a wheel filename against PyPI platform tag rules. |
| 124 |
pub fn validate_wheel_filename_for_pypi(filename: &str) -> Result<()> { |
| 125 |
// Parse wheel filename to extract platform tags |
| 126 |
let platform_tags = extract_platform_tags_from_wheel_filename(filename)?; |
| 127 |
|
| 128 |
for platform_tag in platform_tags { |
| 129 |
if !is_platform_tag_allowed_by_pypi(&platform_tag) { |
| 130 |
bail!("Platform tag '{platform_tag}' in wheel '{filename}' is not allowed by PyPI"); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
Ok(()) |
| 135 |
} |
| 136 |
|
| 137 |
/// Extracts platform tags from a wheel filename. |
| 138 |
/// |
| 139 |
/// Wheel filename format: `{name}-{version}-{python_tag}-{abi_tag}-{platform_tag}.whl`. |
| 140 |
fn extract_platform_tags_from_wheel_filename(filename: &str) -> Result<Vec<String>> { |
| 141 |
let name_without_ext = filename |
| 142 |
.strip_suffix(".whl") |
| 143 |
.ok_or_else(|| anyhow!("Not a wheel file: {filename}"))?; |
| 144 |
|
| 145 |
let parts: Vec<&str> = name_without_ext.split('-').collect(); |
| 146 |
|
| 147 |
if parts.len() < 5 { |
| 148 |
bail!("Invalid wheel filename format: {filename}"); |
| 149 |
} |
| 150 |
|
| 151 |
// Platform tag is the last part, and can contain multiple tags separated by '.' |
| 152 |
let platform_part = parts[parts.len() - 1]; |
| 153 |
let platform_tags: Vec<String> = platform_part.split('.').map(|s| s.to_string()).collect(); |
| 154 |
|
| 155 |
Ok(platform_tags) |
| 156 |
} |
| 157 |
|
| 158 |
#[cfg(test)] |
| 159 |
mod tests { |
| 160 |
use super::*; |
| 161 |
use crate::build_options::TargetTriple; |
| 162 |
use crate::target::Target; |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn test_platform_tag_validation() { |
| 166 |
let tags = [ |
| 167 |
// Simple platforms |
| 168 |
("win32", true), |
| 169 |
("win_amd64", true), |
| 170 |
("any", true), |
| 171 |
// manylinux platforms |
| 172 |
("manylinux2014_x86_64", true), |
| 173 |
("manylinux_2_17_aarch64", true), |
| 174 |
("manylinux_2_17_riscv64", true), |
| 175 |
("manylinux_2_39_riscv64", true), |
| 176 |
// musllinux platforms |
| 177 |
("musllinux_1_1_x86_64", true), |
| 178 |
("musllinux_1_1_riscv64", false), |
| 179 |
// macOS platforms |
| 180 |
("macosx_9_0_x86_64", false), // Invalid major version |
| 181 |
("macosx_10_9_x86_64", true), |
| 182 |
("macosx_11_0_arm64", true), |
| 183 |
// iOS platforms |
| 184 |
("ios_14_0_arm64_iphoneos", true), |
| 185 |
("ios_14_0_x86_64_iphonesimulator", true), |
| 186 |
("ios_14_0_i686_iphoneos", false), // Unsupported architecture |
| 187 |
// Android platforms |
| 188 |
("android_21_armeabi_v7a", true), |
| 189 |
("android_21_arm64_v8a", true), |
| 190 |
("android_21_x86", true), |
| 191 |
("android_21_x86_64", true), |
| 192 |
("android_21_mips", false), // Unsupported architecture |
| 193 |
]; |
| 194 |
|
| 195 |
for (platform_tag, expected) in tags { |
| 196 |
assert_eq!( |
| 197 |
is_platform_tag_allowed_by_pypi(platform_tag), |
| 198 |
expected, |
| 199 |
"{platform_tag}" |
| 200 |
); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn test_wheel_filename_parsing() { |
| 206 |
let wheel_filenames = [ |
| 207 |
("test-1.0.0-py3-none-win_amd64.whl", true), |
| 208 |
("test-1.0.0-py3-none-manylinux2014_x86_64.whl", true), |
| 209 |
("test-1.0.0-py3-none-any.whl", true), |
| 210 |
("test-1.0.0-py3-none-linux_riscv64.whl", false), |
| 211 |
]; |
| 212 |
|
| 213 |
for (filename, expect) in wheel_filenames { |
| 214 |
let actual = validate_wheel_filename_for_pypi(filename).is_ok(); |
| 215 |
assert_eq!(actual, expect, "{filename}"); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn test_target_arch_validation() { |
| 221 |
let targets = [ |
| 222 |
("x86_64-pc-windows-msvc", true), |
| 223 |
("aarch64-apple-darwin", true), |
| 224 |
("x86_64-unknown-linux-gnu", true), |
| 225 |
("aarch64-linux-android", true), |
| 226 |
("armv7-linux-androideabi", true), |
| 227 |
("riscv64gc-unknown-linux-gnu", true), |
| 228 |
("x86_64-unknown-freebsd", false), // Now unsupported (no lazy validation) |
| 229 |
("powerpc64-unknown-linux-gnu", true), // PPC64 on Linux is supported |
| 230 |
("s390x-unknown-linux-gnu", true), // s390x on Linux is supported |
| 231 |
("wasm32-unknown-emscripten", false), // Emscripten is unsupported |
| 232 |
("i686-pc-windows-msvc", true), // i686 Windows is supported |
| 233 |
]; |
| 234 |
|
| 235 |
for (triple, expected) in targets { |
| 236 |
let target = |
| 237 |
Target::from_target_triple(Some(&TargetTriple::Regular(triple.to_string()))) |
| 238 |
.unwrap(); |
| 239 |
assert_eq!(is_arch_supported_by_pypi(&target), expected, "{triple}"); |
| 240 |
} |
| 241 |
} |
| 242 |
} |