Refactor a little to minimise changes and clarify naming
This commit is contained in:
@@ -10,17 +10,11 @@ from .typing import Final, Literal, PlatformName, assert_never
|
|||||||
|
|
||||||
PRETTY_NAMES: Final = {"linux": "Linux", "macos": "macOS", "windows": "Windows"}
|
PRETTY_NAMES: Final = {"linux": "Linux", "macos": "macOS", "windows": "Windows"}
|
||||||
|
|
||||||
ARCH_CATEGORIES: Final[dict[str, set[str]]] = {
|
ARCH_SYNONYMS: Final[list[dict[PlatformName, str | None]]] = [
|
||||||
"32": {"i686", "x86"},
|
{"linux": "x86_64", "macos": "x86_64", "windows": "AMD64"},
|
||||||
"64": {"x86_64", "AMD64"},
|
{"linux": "i686", "macos": None, "windows": "x86"},
|
||||||
"arm": {"ARM64", "aarch64", "arm64"},
|
{"linux": "aarch64", "macos": "arm64", "windows": "ARM64"},
|
||||||
}
|
]
|
||||||
|
|
||||||
PLATFORM_CATEGORIES: Final[dict[PlatformName, set[str]]] = {
|
|
||||||
"linux": {"i686", "x86_64", "aarch64", "ppc64le", "s390x"},
|
|
||||||
"macos": {"x86_64", "arm64", "universal2"},
|
|
||||||
"windows": {"x86", "AMD64", "ARM64"},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@functools.total_ordering
|
@functools.total_ordering
|
||||||
@@ -72,25 +66,28 @@ class Architecture(Enum):
|
|||||||
native_machine = platform_module.machine()
|
native_machine = platform_module.machine()
|
||||||
|
|
||||||
# Cross-platform support. Used for --print-build-identifiers or docker builds.
|
# Cross-platform support. Used for --print-build-identifiers or docker builds.
|
||||||
host_platform = (
|
host_platform: PlatformName = (
|
||||||
"windows"
|
"windows"
|
||||||
if sys.platform.startswith("win")
|
if sys.platform.startswith("win")
|
||||||
else ("macos" if sys.platform.startswith("darwin") else "linux")
|
else ("macos" if sys.platform.startswith("darwin") else "linux")
|
||||||
)
|
)
|
||||||
|
|
||||||
result = set()
|
native_architecture = Architecture(native_machine)
|
||||||
|
|
||||||
# Replace native_machine with the matching machine for intel or arm
|
# we might need to rename the native arch to the machine we're running
|
||||||
if host_platform == platform:
|
# on, as the same arch can have different names on different platforms
|
||||||
native_architecture = Architecture(native_machine)
|
if host_platform != platform:
|
||||||
result.add(native_architecture)
|
for arch_synonym in ARCH_SYNONYMS:
|
||||||
else:
|
if native_machine == arch_synonym.get(host_platform):
|
||||||
for arch_group in ARCH_CATEGORIES.values():
|
synonym = arch_synonym[platform]
|
||||||
if native_machine in arch_group:
|
|
||||||
possible_archs = arch_group & PLATFORM_CATEGORIES[platform]
|
if synonym is None:
|
||||||
if len(possible_archs) == 1:
|
# can't build anything on this platform
|
||||||
(cross_machine,) = possible_archs
|
return set()
|
||||||
result.add(Architecture(cross_machine))
|
|
||||||
|
native_architecture = Architecture(synonym)
|
||||||
|
|
||||||
|
result = {native_architecture}
|
||||||
|
|
||||||
if platform == "linux" and Architecture.x86_64 in result:
|
if platform == "linux" and Architecture.x86_64 in result:
|
||||||
# x86_64 machines can run i686 containers
|
# x86_64 machines can run i686 containers
|
||||||
@@ -104,15 +101,21 @@ class Architecture(Enum):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def all_archs(platform: PlatformName) -> set[Architecture]:
|
def all_archs(platform: PlatformName) -> set[Architecture]:
|
||||||
all_archs_map = {
|
all_archs_map = {
|
||||||
"linux": {Architecture[item] for item in PLATFORM_CATEGORIES["linux"]},
|
"linux": {
|
||||||
"macos": {Architecture[item] for item in PLATFORM_CATEGORIES["macos"]},
|
Architecture.x86_64,
|
||||||
"windows": {Architecture[item] for item in PLATFORM_CATEGORIES["windows"]},
|
Architecture.i686,
|
||||||
|
Architecture.aarch64,
|
||||||
|
Architecture.ppc64le,
|
||||||
|
Architecture.s390x,
|
||||||
|
},
|
||||||
|
"macos": {Architecture.x86_64, Architecture.arm64, Architecture.universal2},
|
||||||
|
"windows": {Architecture.x86, Architecture.AMD64, Architecture.ARM64},
|
||||||
}
|
}
|
||||||
return all_archs_map[platform]
|
return all_archs_map[platform]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def bitness_archs(platform: PlatformName, bitness: Literal["64", "32"]) -> set[Architecture]:
|
def bitness_archs(platform: PlatformName, bitness: Literal["64", "32"]) -> set[Architecture]:
|
||||||
archs_32 = {Architecture[item] for item in ARCH_CATEGORIES["32"]}
|
archs_32 = {Architecture.i686, Architecture.x86}
|
||||||
auto_archs = Architecture.auto_archs(platform)
|
auto_archs = Architecture.auto_archs(platform)
|
||||||
|
|
||||||
if bitness == "64":
|
if bitness == "64":
|
||||||
|
|||||||
Reference in New Issue
Block a user