2022-07-14 13:36:57 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2021-01-22 09:33:22 -05:00
|
|
|
import functools
|
|
|
|
|
import platform as platform_module
|
|
|
|
|
import re
|
2025-02-12 16:54:44 +01:00
|
|
|
import shutil
|
|
|
|
|
import subprocess
|
2022-09-12 23:01:56 -04:00
|
|
|
import sys
|
2023-04-18 12:38:21 -04:00
|
|
|
from collections.abc import Set
|
2021-01-22 09:33:22 -05:00
|
|
|
from enum import Enum
|
2025-01-16 16:45:18 +01:00
|
|
|
from typing import Final, Literal, assert_never
|
2021-01-22 09:33:22 -05:00
|
|
|
|
2023-04-18 23:05:34 -04:00
|
|
|
from .typing import PlatformName
|
2021-01-22 09:33:22 -05:00
|
|
|
|
2023-05-08 21:05:24 +01:00
|
|
|
PRETTY_NAMES: Final[dict[PlatformName, str]] = {
|
|
|
|
|
"linux": "Linux",
|
|
|
|
|
"macos": "macOS",
|
|
|
|
|
"windows": "Windows",
|
2024-05-28 05:31:36 -07:00
|
|
|
"pyodide": "Pyodide",
|
2023-05-08 21:05:24 +01:00
|
|
|
}
|
2021-01-22 09:33:22 -05:00
|
|
|
|
2022-09-24 23:04:46 +01:00
|
|
|
ARCH_SYNONYMS: Final[list[dict[PlatformName, str | None]]] = [
|
|
|
|
|
{"linux": "x86_64", "macos": "x86_64", "windows": "AMD64"},
|
|
|
|
|
{"linux": "i686", "macos": None, "windows": "x86"},
|
|
|
|
|
{"linux": "aarch64", "macos": "arm64", "windows": "ARM64"},
|
|
|
|
|
]
|
2022-09-12 23:01:56 -04:00
|
|
|
|
2021-01-22 09:33:22 -05:00
|
|
|
|
2025-02-12 16:54:44 +01:00
|
|
|
def _check_aarch32_el0() -> bool:
|
|
|
|
|
"""Check if running armv7l natively on aarch64 is supported"""
|
|
|
|
|
if not sys.platform.startswith("linux"):
|
|
|
|
|
return False
|
|
|
|
|
if platform_module.machine() != "aarch64":
|
|
|
|
|
return False
|
|
|
|
|
executable = shutil.which("linux32")
|
|
|
|
|
if executable is None:
|
|
|
|
|
return False
|
|
|
|
|
check = subprocess.run([executable, "uname", "-m"], check=False, capture_output=True, text=True)
|
|
|
|
|
return check.returncode == 0 and check.stdout.startswith("armv")
|
|
|
|
|
|
|
|
|
|
|
2021-01-22 09:33:22 -05:00
|
|
|
@functools.total_ordering
|
|
|
|
|
class Architecture(Enum):
|
|
|
|
|
value: str
|
|
|
|
|
|
|
|
|
|
# mac/linux archs
|
2021-05-03 11:45:43 -04:00
|
|
|
x86_64 = "x86_64"
|
2021-01-23 13:28:06 +00:00
|
|
|
|
|
|
|
|
# linux archs
|
2021-05-03 11:45:43 -04:00
|
|
|
i686 = "i686"
|
|
|
|
|
aarch64 = "aarch64"
|
|
|
|
|
ppc64le = "ppc64le"
|
|
|
|
|
s390x = "s390x"
|
2024-10-01 16:33:57 +02:00
|
|
|
armv7l = "armv7l"
|
2021-01-22 09:33:22 -05:00
|
|
|
|
2021-01-23 13:28:06 +00:00
|
|
|
# mac archs
|
2021-05-03 11:45:43 -04:00
|
|
|
universal2 = "universal2"
|
|
|
|
|
arm64 = "arm64"
|
2021-01-23 13:28:06 +00:00
|
|
|
|
2021-01-22 09:33:22 -05:00
|
|
|
# windows archs
|
2021-05-03 11:45:43 -04:00
|
|
|
x86 = "x86"
|
|
|
|
|
AMD64 = "AMD64"
|
2021-11-24 10:50:49 -08:00
|
|
|
ARM64 = "ARM64"
|
2021-01-22 09:33:22 -05:00
|
|
|
|
2024-05-28 05:31:36 -07:00
|
|
|
# WebAssembly
|
|
|
|
|
wasm32 = "wasm32"
|
|
|
|
|
|
2021-01-22 09:33:22 -05:00
|
|
|
# Allow this to be sorted
|
2022-07-14 13:36:57 +02:00
|
|
|
def __lt__(self, other: Architecture) -> bool:
|
2021-01-22 09:33:22 -05:00
|
|
|
return self.value < other.value
|
|
|
|
|
|
2022-11-26 15:54:08 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
|
return self.name
|
|
|
|
|
|
2021-01-22 09:33:22 -05:00
|
|
|
@staticmethod
|
2022-07-14 13:36:57 +02:00
|
|
|
def parse_config(config: str, platform: PlatformName) -> set[Architecture]:
|
2021-01-22 09:33:22 -05:00
|
|
|
result = set()
|
2021-05-03 11:45:43 -04:00
|
|
|
for arch_str in re.split(r"[\s,]+", config):
|
|
|
|
|
if arch_str == "auto":
|
2021-01-22 09:33:22 -05:00
|
|
|
result |= Architecture.auto_archs(platform=platform)
|
2021-05-03 11:45:43 -04:00
|
|
|
elif arch_str == "native":
|
2024-09-12 21:32:43 +02:00
|
|
|
native_arch = Architecture.native_arch(platform=platform)
|
|
|
|
|
if native_arch:
|
|
|
|
|
result.add(native_arch)
|
2021-05-03 11:45:43 -04:00
|
|
|
elif arch_str == "all":
|
2021-01-22 09:33:22 -05:00
|
|
|
result |= Architecture.all_archs(platform=platform)
|
2021-05-03 11:45:43 -04:00
|
|
|
elif arch_str == "auto64":
|
2021-01-31 20:41:18 -05:00
|
|
|
result |= Architecture.bitness_archs(platform=platform, bitness="64")
|
2021-05-03 11:45:43 -04:00
|
|
|
elif arch_str == "auto32":
|
2021-01-31 20:41:18 -05:00
|
|
|
result |= Architecture.bitness_archs(platform=platform, bitness="32")
|
2021-01-22 09:33:22 -05:00
|
|
|
else:
|
|
|
|
|
result.add(Architecture(arch_str))
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-05-28 05:31:36 -07:00
|
|
|
def native_arch(platform: PlatformName) -> Architecture | None:
|
|
|
|
|
if platform == "pyodide":
|
|
|
|
|
return Architecture.wasm32
|
2021-01-23 13:28:06 +00:00
|
|
|
|
2022-09-12 23:01:56 -04:00
|
|
|
# Cross-platform support. Used for --print-build-identifiers or docker builds.
|
2022-09-24 23:04:46 +01:00
|
|
|
host_platform: PlatformName = (
|
2022-09-12 23:01:56 -04:00
|
|
|
"windows"
|
|
|
|
|
if sys.platform.startswith("win")
|
|
|
|
|
else ("macos" if sys.platform.startswith("darwin") else "linux")
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-28 05:31:36 -07:00
|
|
|
native_machine = platform_module.machine()
|
2022-09-24 23:04:46 +01:00
|
|
|
native_architecture = Architecture(native_machine)
|
2022-09-12 23:01:56 -04:00
|
|
|
|
2022-09-24 23:04:46 +01:00
|
|
|
# we might need to rename the native arch to the machine we're running
|
|
|
|
|
# on, as the same arch can have different names on different platforms
|
|
|
|
|
if host_platform != platform:
|
|
|
|
|
for arch_synonym in ARCH_SYNONYMS:
|
|
|
|
|
if native_machine == arch_synonym.get(host_platform):
|
|
|
|
|
synonym = arch_synonym[platform]
|
|
|
|
|
|
|
|
|
|
if synonym is None:
|
|
|
|
|
# can't build anything on this platform
|
2024-05-28 05:31:36 -07:00
|
|
|
return None
|
2022-09-24 23:04:46 +01:00
|
|
|
|
|
|
|
|
native_architecture = Architecture(synonym)
|
|
|
|
|
|
2024-05-28 05:31:36 -07:00
|
|
|
return native_architecture
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def auto_archs(platform: PlatformName) -> set[Architecture]:
|
|
|
|
|
native_arch = Architecture.native_arch(platform)
|
|
|
|
|
if native_arch is None:
|
|
|
|
|
return set() # can't build anything on this platform
|
|
|
|
|
result = {native_arch}
|
2022-09-12 23:01:56 -04:00
|
|
|
|
2025-02-12 16:54:44 +01:00
|
|
|
if platform == "linux":
|
|
|
|
|
if Architecture.x86_64 in result:
|
|
|
|
|
# x86_64 machines can run i686 containers
|
|
|
|
|
result.add(Architecture.i686)
|
|
|
|
|
elif Architecture.aarch64 in result and _check_aarch32_el0():
|
|
|
|
|
result.add(Architecture.armv7l)
|
2021-01-23 13:28:06 +00:00
|
|
|
|
2022-09-12 23:01:56 -04:00
|
|
|
if platform == "windows" and Architecture.AMD64 in result:
|
2021-01-22 09:33:22 -05:00
|
|
|
result.add(Architecture.x86)
|
2021-01-23 13:28:06 +00:00
|
|
|
|
2021-01-22 09:33:22 -05:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2022-07-14 13:36:57 +02:00
|
|
|
def all_archs(platform: PlatformName) -> set[Architecture]:
|
2022-02-27 14:16:37 -05:00
|
|
|
all_archs_map = {
|
2022-09-24 23:04:46 +01:00
|
|
|
"linux": {
|
|
|
|
|
Architecture.x86_64,
|
|
|
|
|
Architecture.i686,
|
|
|
|
|
Architecture.aarch64,
|
|
|
|
|
Architecture.ppc64le,
|
|
|
|
|
Architecture.s390x,
|
2024-10-01 16:33:57 +02:00
|
|
|
Architecture.armv7l,
|
2022-09-24 23:04:46 +01:00
|
|
|
},
|
|
|
|
|
"macos": {Architecture.x86_64, Architecture.arm64, Architecture.universal2},
|
|
|
|
|
"windows": {Architecture.x86, Architecture.AMD64, Architecture.ARM64},
|
2024-05-28 05:31:36 -07:00
|
|
|
"pyodide": {Architecture.wasm32},
|
2022-02-27 14:16:37 -05:00
|
|
|
}
|
|
|
|
|
return all_archs_map[platform]
|
2021-01-22 09:33:22 -05:00
|
|
|
|
2021-01-31 20:41:18 -05:00
|
|
|
@staticmethod
|
2022-07-14 13:36:57 +02:00
|
|
|
def bitness_archs(platform: PlatformName, bitness: Literal["64", "32"]) -> set[Architecture]:
|
2024-10-01 16:33:57 +02:00
|
|
|
archs_32 = {Architecture.i686, Architecture.x86, Architecture.armv7l}
|
2021-01-31 20:41:18 -05:00
|
|
|
auto_archs = Architecture.auto_archs(platform)
|
|
|
|
|
|
2021-05-03 11:45:43 -04:00
|
|
|
if bitness == "64":
|
2021-01-31 20:41:18 -05:00
|
|
|
return auto_archs - archs_32
|
2024-10-01 16:33:57 +02:00
|
|
|
if bitness == "32":
|
2021-01-31 20:41:18 -05:00
|
|
|
return auto_archs & archs_32
|
2024-10-01 16:33:57 +02:00
|
|
|
assert_never(bitness)
|
2021-01-31 20:41:18 -05:00
|
|
|
|
2021-01-22 09:33:22 -05:00
|
|
|
|
|
|
|
|
def allowed_architectures_check(
|
|
|
|
|
platform: PlatformName,
|
2023-04-18 12:38:21 -04:00
|
|
|
architectures: Set[Architecture],
|
2021-01-22 09:33:22 -05:00
|
|
|
) -> None:
|
|
|
|
|
allowed_architectures = Architecture.all_archs(platform)
|
|
|
|
|
|
2021-05-03 11:45:43 -04:00
|
|
|
msg = f"{PRETTY_NAMES[platform]} only supports {sorted(allowed_architectures)} at the moment."
|
2021-01-22 09:33:22 -05:00
|
|
|
|
2021-05-03 11:45:43 -04:00
|
|
|
if platform != "linux":
|
|
|
|
|
msg += " If you want to set emulation architectures on Linux, use CIBW_ARCHS_LINUX instead."
|
2021-01-22 09:33:22 -05:00
|
|
|
|
|
|
|
|
if not architectures <= allowed_architectures:
|
2021-05-03 11:45:43 -04:00
|
|
|
msg = f"Invalid archs option {architectures}. " + msg
|
2021-01-22 09:33:22 -05:00
|
|
|
raise ValueError(msg)
|
|
|
|
|
|
|
|
|
|
if not architectures:
|
2021-05-03 11:45:43 -04:00
|
|
|
msg = "Empty archs option set. " + msg
|
2021-01-22 09:33:22 -05:00
|
|
|
raise ValueError(msg)
|