core: Arch: add method to map to Go arch (MR 2474)

Map architectures to the strings used for GOARCH for cross compiling Go
applications.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2025-01-25 19:33:35 +01:00 committed by Oliver Smith
parent 801437e681
commit 8f0971eb92
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
2 changed files with 26 additions and 0 deletions

View file

@ -182,6 +182,21 @@ class Arch(enum.Enum):
f"Can not map Alpine architecture '{self}' to the right hostspec value"
)
def go(self) -> str:
match self:
case Arch.armhf | Arch.armv7:
return "arm"
case Arch.aarch64:
return "arm64"
case Arch.riscv64 | Arch.ppc64le:
return str(self)
case Arch.x86:
return "386"
case Arch.x86_64:
return "amd64"
case _:
raise ValueError(f"Can not map architecture '{self}' to Go arch")
def cpu_emulation_required(self) -> bool:
# Obvious case: host arch is target arch
if self == Arch.native():

View file

@ -49,6 +49,17 @@ def test_valid_arches():
assert Arch.ppc64.qemu() == "ppc64"
assert Arch.ppc64le.qemu() == "ppc64"
# Go arch
assert Arch.armhf.go() == "arm"
assert Arch.armv7.go() == "arm"
assert Arch.aarch64.go() == "arm64"
assert Arch.riscv64.go() == "riscv64"
assert Arch.ppc64le.go() == "ppc64le"
assert Arch.x86_64.go() == "amd64"
with pytest.raises(ValueError) as excinfo:
Arch.mips64.go()
assert "Can not map architecture 'mips64' to Go arch" in str(excinfo.value)
# Check that Arch.cpu_emulation_required() works
assert Arch.native() == Arch.x86_64 or Arch.x86_64.cpu_emulation_required()
assert Arch.native() == Arch.aarch64 or Arch.aarch64.cpu_emulation_required()