| 1 |
use crate::auditwheel::PlatformTag; |
| 2 |
use crate::target::Arch; |
| 3 |
use once_cell::sync::Lazy; |
| 4 |
use serde::Deserialize; |
| 5 |
use std::cmp::{Ordering, PartialOrd}; |
| 6 |
use std::collections::{HashMap, HashSet}; |
| 7 |
use std::fmt; |
| 8 |
use std::fmt::{Display, Formatter}; |
| 9 |
|
| 10 |
/// The policies (allowed symbols) for the different manylinux tags, sorted from highest |
| 11 |
/// priority to lowest |
| 12 |
pub static MANYLINUX_POLICIES: Lazy<Vec<Policy>> = Lazy::new(|| { |
| 13 |
// https://github.com/pypa/auditwheel/blob/master/auditwheel/policy/manylinux-policy.json |
| 14 |
let mut policies: Vec<Policy> = serde_json::from_slice(include_bytes!("manylinux-policy.json")) |
| 15 |
.expect("invalid manylinux policy.json file"); |
| 16 |
policies.sort_by_key(|policy| -policy.priority); |
| 17 |
policies |
| 18 |
}); |
| 19 |
|
| 20 |
/// The policies (allowed symbols) for the different musllinux tags, sorted from highest |
| 21 |
/// priority to lowest |
| 22 |
pub static MUSLLINUX_POLICIES: Lazy<Vec<Policy>> = Lazy::new(|| { |
| 23 |
// https://github.com/pypa/auditwheel/blob/master/auditwheel/policy/musllinux-policy.json |
| 24 |
let mut policies: Vec<Policy> = serde_json::from_slice(include_bytes!("musllinux-policy.json")) |
| 25 |
.expect("invalid musllinux policy.json file"); |
| 26 |
policies.sort_by_key(|policy| -policy.priority); |
| 27 |
policies |
| 28 |
}); |
| 29 |
|
| 30 |
/// Manylinux policy |
| 31 |
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] |
| 32 |
pub struct Policy { |
| 33 |
/// platform tag name |
| 34 |
pub name: String, |
| 35 |
/// platform tag aliases |
| 36 |
pub aliases: Vec<String>, |
| 37 |
/// policy priority. Tags supporting more platforms have higher priority |
| 38 |
pub priority: i64, |
| 39 |
/// platform architecture to symbol versions map |
| 40 |
#[serde(rename = "symbol_versions")] |
| 41 |
pub symbol_versions: HashMap<String, HashMap<String, HashSet<String>>>, |
| 42 |
/// whitelisted libraries |
| 43 |
#[serde(rename = "lib_whitelist")] |
| 44 |
pub lib_whitelist: HashSet<String>, |
| 45 |
/// blacklisted symbols of whitelisted libraries |
| 46 |
pub blacklist: HashMap<String, HashSet<String>>, |
| 47 |
} |
| 48 |
|
| 49 |
impl Default for Policy { |
| 50 |
fn default() -> Self { |
| 51 |
// defaults to linux |
| 52 |
Policy::from_tag(&PlatformTag::Linux).unwrap() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl Display for Policy { |
| 57 |
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 58 |
if self.aliases.is_empty() { |
| 59 |
f.write_str(&self.name) |
| 60 |
} else { |
| 61 |
f.write_fmt(format_args!( |
| 62 |
"{} (aka {})", |
| 63 |
&self.name, |
| 64 |
self.aliases.join(",") |
| 65 |
)) |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
impl PartialOrd for Policy { |
| 71 |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 72 |
self.priority.partial_cmp(&other.priority) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
impl Policy { |
| 77 |
/// Get platform tag from this policy |
| 78 |
pub fn platform_tag(&self) -> PlatformTag { |
| 79 |
self.name.parse().expect("unknown platform tag") |
| 80 |
} |
| 81 |
|
| 82 |
/// Get policy by its platform tag |
| 83 |
pub fn from_tag(tag: &PlatformTag) -> Option<Self> { |
| 84 |
match tag { |
| 85 |
PlatformTag::Manylinux { major, minor } => { |
| 86 |
let name = format!("manylinux_{major}_{minor}"); |
| 87 |
MANYLINUX_POLICIES.iter().find(|p| p.name == name).cloned() |
| 88 |
} |
| 89 |
PlatformTag::Musllinux { major, minor } => { |
| 90 |
let name = format!("musllinux_{major}_{minor}"); |
| 91 |
MUSLLINUX_POLICIES.iter().find(|p| p.name == name).cloned() |
| 92 |
} |
| 93 |
PlatformTag::Linux => MANYLINUX_POLICIES |
| 94 |
.iter() |
| 95 |
.find(|p| p.name == "linux") |
| 96 |
.cloned(), |
| 97 |
PlatformTag::Pypi => unreachable!("pypi is not a policy"), |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
pub(crate) fn fixup_musl_libc_so_name(&mut self, target_arch: Arch) { |
| 102 |
// Fixup musl libc lib_whitelist |
| 103 |
if self.name.starts_with("musllinux") && self.lib_whitelist.remove("libc.so") { |
| 104 |
let new_soname = match target_arch { |
| 105 |
Arch::Aarch64 => "libc.musl-aarch64.so.1", |
| 106 |
Arch::Armv6L => "libc.musl-armhf.so.1", |
| 107 |
Arch::Armv7L => "libc.musl-armv7.so.1", |
| 108 |
Arch::Powerpc64Le => "libc.musl-ppc64le.so.1", |
| 109 |
Arch::Powerpc64 => "", // musllinux doesn't support ppc64 |
| 110 |
Arch::X86 => "libc.musl-x86.so.1", |
| 111 |
Arch::X86_64 => "libc.musl-x86_64.so.1", |
| 112 |
Arch::S390X => "libc.musl-s390x.so.1", |
| 113 |
_ => "", |
| 114 |
}; |
| 115 |
if !new_soname.is_empty() { |
| 116 |
self.lib_whitelist.insert(new_soname.to_string()); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#[cfg(test)] |
| 123 |
mod test { |
| 124 |
use super::{Arch, Policy, MANYLINUX_POLICIES, MUSLLINUX_POLICIES}; |
| 125 |
use crate::PlatformTag; |
| 126 |
use pretty_assertions::assert_eq; |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_load_policy() { |
| 130 |
let linux = Policy::from_tag(&PlatformTag::Linux).unwrap(); |
| 131 |
assert!(linux.symbol_versions.is_empty()); |
| 132 |
assert!(linux.lib_whitelist.is_empty()); |
| 133 |
|
| 134 |
let manylinux2010 = Policy::from_tag(&PlatformTag::Manylinux { |
| 135 |
major: 2, |
| 136 |
minor: 12, |
| 137 |
}) |
| 138 |
.unwrap(); |
| 139 |
assert!(manylinux2010.lib_whitelist.contains("libc.so.6")); |
| 140 |
let symbol_version = &manylinux2010.symbol_versions["x86_64"]; |
| 141 |
assert_eq!(symbol_version["CXXABI"].len(), 4); |
| 142 |
let cxxabi = &symbol_version["CXXABI"]; |
| 143 |
for version in &["1.3", "1.3.1", "1.3.2", "1.3.3"] { |
| 144 |
assert!(cxxabi.contains(*version)); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn test_policy_manylinux_tag() { |
| 150 |
for policy in MANYLINUX_POLICIES.iter() { |
| 151 |
let _tag = policy.platform_tag(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn test_policy_musllinux_tag() { |
| 157 |
for policy in MUSLLINUX_POLICIES.iter() { |
| 158 |
let _tag = policy.platform_tag(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn test_policy_musllinux_fixup_libc_so_name() { |
| 164 |
let mut policy = Policy::from_tag(&PlatformTag::Musllinux { major: 1, minor: 1 }).unwrap(); |
| 165 |
policy.fixup_musl_libc_so_name(Arch::Aarch64); |
| 166 |
assert!(policy.lib_whitelist.contains("libc.musl-aarch64.so.1")); |
| 167 |
} |
| 168 |
} |