Files
cibuildwheel/cibuildwheel/architecture.py
T

122 lines
3.8 KiB
Python
Raw Normal View History

2021-01-22 09:33:22 -05:00
import functools
import platform as platform_module
import re
from enum import Enum
from typing import Set
2021-01-31 20:41:18 -05:00
from .typing import Literal, PlatformName, assert_never
2021-01-22 09:33:22 -05:00
2021-05-03 11:45:43 -04:00
PRETTY_NAMES = {"linux": "Linux", "macos": "macOS", "windows": "Windows"}
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"
# linux archs
2021-05-03 11:45:43 -04:00
i686 = "i686"
aarch64 = "aarch64"
ppc64le = "ppc64le"
s390x = "s390x"
2021-01-22 09:33:22 -05:00
# mac archs
2021-05-03 11:45:43 -04:00
universal2 = "universal2"
arm64 = "arm64"
2021-01-22 09:33:22 -05:00
# windows archs
2021-05-03 11:45:43 -04:00
x86 = "x86"
AMD64 = "AMD64"
2021-01-22 09:33:22 -05:00
# Allow this to be sorted
def __lt__(self, other: "Architecture") -> bool:
return self.value < other.value
@staticmethod
2021-05-03 11:45:43 -04: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":
2021-01-22 09:33:22 -05:00
result.add(Architecture(platform_module.machine()))
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
2021-05-03 11:45:43 -04:00
def auto_archs(platform: PlatformName) -> "Set[Architecture]":
2021-01-22 09:33:22 -05:00
native_architecture = Architecture(platform_module.machine())
result = {native_architecture}
2021-05-03 11:45:43 -04:00
if platform == "linux" and native_architecture == Architecture.x86_64:
2021-01-22 09:33:22 -05:00
# x86_64 machines can run i686 docker containers
result.add(Architecture.i686)
2021-05-03 11:45:43 -04:00
if platform == "windows" and native_architecture == Architecture.AMD64:
2021-01-22 09:33:22 -05:00
result.add(Architecture.x86)
2021-05-03 11:45:43 -04:00
if platform == "macos" and native_architecture == Architecture.arm64:
# arm64 can build and test both archs of a universal2 wheel.
result.add(Architecture.universal2)
2021-01-22 09:33:22 -05:00
return result
@staticmethod
2021-05-03 11:45:43 -04:00
def all_archs(platform: PlatformName) -> "Set[Architecture]":
if platform == "linux":
2021-04-30 17:56:34 -04:00
return {
Architecture.x86_64,
Architecture.i686,
Architecture.aarch64,
Architecture.ppc64le,
Architecture.s390x,
}
2021-05-03 11:45:43 -04:00
elif platform == "macos":
return {Architecture.x86_64, Architecture.arm64, Architecture.universal2}
2021-05-03 11:45:43 -04:00
elif platform == "windows":
2021-01-22 09:33:22 -05:00
return {Architecture.x86, Architecture.AMD64}
else:
assert_never(platform)
2021-01-31 20:41:18 -05:00
@staticmethod
2021-05-03 11:45:43 -04:00
def bitness_archs(platform: PlatformName, bitness: Literal["64", "32"]) -> "Set[Architecture]":
2021-01-31 20:41:18 -05:00
archs_32 = {Architecture.i686, Architecture.x86}
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
2021-05-03 11:45:43 -04:00
elif bitness == "32":
2021-01-31 20:41:18 -05:00
return auto_archs & archs_32
else:
assert_never(bitness)
2021-01-22 09:33:22 -05:00
def allowed_architectures_check(
platform: PlatformName,
architectures: Set[Architecture],
) -> 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)