22import json
33import platform as _platform_module
44from collections import defaultdict
5+ from pathlib import Path
56from typing import Dict , List , Optional , Set
67from os .path import join , dirname , abspath
78import logging
89
10+ from ..libc import get_libc , Libc
11+ from ..musllinux import find_musl_libc , get_musl_version
12+
13+
14+ _HERE = Path (__file__ ).parent
915
1016logger = logging .getLogger (__name__ )
1117
@@ -22,6 +28,7 @@ def get_arch_name() -> str:
2228
2329
2430_ARCH_NAME = get_arch_name ()
31+ _LIBC = get_libc ()
2532
2633
2734def _validate_pep600_compliance (policies ) -> None :
@@ -56,18 +63,64 @@ def _validate_pep600_compliance(policies) -> None:
5663 symbol_versions [arch ] = symbol_versions_arch
5764
5865
59- with open (join (dirname (abspath (__file__ )), 'manylinux-policy.json' )) as f :
66+ _POLICY_JSON_MAP = {
67+ Libc .GLIBC : _HERE / 'manylinux-policy.json' ,
68+ Libc .MUSL : _HERE / 'musllinux-policy.json' ,
69+ }
70+
71+
72+ def _get_musl_policy ():
73+ if _LIBC != Libc .MUSL :
74+ return None
75+ musl_version = get_musl_version (find_musl_libc ())
76+ return f'musllinux_{ musl_version .major } _{ musl_version .minor } '
77+
78+
79+ _MUSL_POLICY = _get_musl_policy ()
80+
81+
82+ def _fixup_musl_libc_soname (whitelist ):
83+ if _LIBC != Libc .MUSL :
84+ return whitelist
85+ soname_map = {
86+ "libc.so" : {
87+ "x86_64" : "libc.musl-x86_64.so.1" ,
88+ "i686" : "libc.musl-x86.so.1" ,
89+ "aarch64" : "libc.musl-aarch64.so.1" ,
90+ "s390x" : "libc.musl-s390x.so.1" ,
91+ "ppc64le" : "libc.musl-ppc64le.so.1" ,
92+ "armv7l" : "libc.musl-armv7.so.1" ,
93+ }
94+ }
95+ new_whitelist = []
96+ for soname in whitelist :
97+ if soname in soname_map :
98+ new_soname = soname_map [soname ][_ARCH_NAME ]
99+ logger .debug (f"Replacing whitelisted '{ soname } ' by '{ new_soname } '" )
100+ new_whitelist .append (new_soname )
101+ else :
102+ new_whitelist .append (soname )
103+ return new_whitelist
104+
105+
106+ with _POLICY_JSON_MAP [_LIBC ].open () as f :
60107 _POLICIES = []
61108 _policies_temp = json .load (f )
62109 _validate_pep600_compliance (_policies_temp )
63110 for _p in _policies_temp :
111+ if _MUSL_POLICY is not None and \
112+ _p ['name' ] not in {'linux' , _MUSL_POLICY }:
113+ continue
64114 if _ARCH_NAME in _p ['symbol_versions' ].keys () or _p ['name' ] == 'linux' :
65115 if _p ['name' ] != 'linux' :
66116 _p ['symbol_versions' ] = _p ['symbol_versions' ][_ARCH_NAME ]
67117 _p ['name' ] = _p ['name' ] + '_' + _ARCH_NAME
68118 _p ['aliases' ] = [alias + '_' + _ARCH_NAME
69119 for alias in _p ['aliases' ]]
120+ _p ['lib_whitelist' ] = _fixup_musl_libc_soname (_p ['lib_whitelist' ])
70121 _POLICIES .append (_p )
122+ if _LIBC == Libc .MUSL :
123+ assert len (_POLICIES ) == 2 , _POLICIES
71124
72125POLICY_PRIORITY_HIGHEST = max (p ['priority' ] for p in _POLICIES )
73126POLICY_PRIORITY_LOWEST = min (p ['priority' ] for p in _POLICIES )
@@ -78,8 +131,8 @@ def load_policies():
78131
79132
80133def _load_policy_schema ():
81- with open (join (dirname (abspath (__file__ )), 'policy-schema.json' )) as f :
82- schema = json .load (f )
134+ with open (join (dirname (abspath (__file__ )), 'policy-schema.json' )) as f_ :
135+ schema = json .load (f_ )
83136 return schema
84137
85138
@@ -124,6 +177,8 @@ def get_replace_platforms(name: str) -> List[str]:
124177 return []
125178 if name .startswith ('manylinux_' ):
126179 return ['linux_' + '_' .join (name .split ('_' )[3 :])]
180+ if name .startswith ('musllinux_' ):
181+ return ['linux_' + '_' .join (name .split ('_' )[3 :])]
127182 return ['linux_' + '_' .join (name .split ('_' )[1 :])]
128183
129184
0 commit comments