Coverage for apio/common/apio_platforms.py: 85%
39 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
1# This file is part of the Apio project
2# (C) 2016-present FPGAwars
3# Project authors:
4# * Jesús Arroyo (Jesus89) (2016-2019)
5# * Juan Gonzalez (obijuan) (2019-2024)
6# * The Apio Team (2024-present)
7# License: GPLv2
9"""Definition of underlying platforms supported by Apio."""
11import re
12import platform
13from dataclasses import dataclass
14from apio.utils import env_options
15from apio.common.apio_console import fatal_error
18@dataclass(frozen=True)
19class ApioPlatform:
20 """A class with Apio supported platforms and their attributes."""
22 # -- Platform id.
23 id: str
25 # -- Human readable general platform type.
26 type: str
28 # -- Human readable variant of the platform type.
29 variant: str
31 # -- Set to True exactly one of these
32 is_darwin: bool = False
33 is_linux: bool = False
34 is_windows: bool = False
36 def __post_init__(self):
37 """Post init validation"""
39 # -- Constraint chars in platform id.
40 assert re.fullmatch(r"[a-z0-9-]+", self.id), self.id
42 # -- Exactly one of the platform types should be true.
43 assert (
44 sum([self.is_linux, self.is_windows, self.is_darwin]) == 1
45 ), self.id
47 # -- Sanity check the matching between names and types.
48 assert ("darwin" in self.id) == self.is_darwin, self.id
49 assert ("linux" in self.id) == self.is_linux, self.id
50 assert ("windows" in self.id) == self.is_windows, self.id
53# -- The list of supported platforms and their attribute.
54_APIO_PLATFORMS_DICT = {
55 "darwin-arm64": ApioPlatform(
56 id="darwin-arm64",
57 type="Mac OSX",
58 variant="ARM 64 bit (Apple Silicon)",
59 is_darwin=True,
60 ),
61 "linux-x86-64": ApioPlatform(
62 id="linux-x86-64",
63 type="Linux",
64 variant="X86 64 bit",
65 is_linux=True,
66 ),
67 "windows-amd64": ApioPlatform(
68 id="windows-amd64",
69 type="Windows",
70 variant="x86 64 bit",
71 is_windows=True,
72 ),
73}
76def _determine_system_platform_id() -> str:
77 """Return a String with the current platform:
78 ex. linux-x86-64
79 ex. windows-amd64"""
81 # -- Get the platform: linux, windows, darwin
82 type_ = platform.system().lower()
83 platform_str = f"{type_}"
85 # -- Get the architecture
86 arch = platform.machine().lower()
88 # -- Special case for windows
89 if type_ == "windows": 89 ↛ 91line 89 didn't jump to line 91 because the condition on line 89 was never true
90 # -- Assume all the windows to be 64-bits
91 arch = "amd64"
93 # -- Add the architecture, if it exists
94 if arch: 94 ↛ 98line 94 didn't jump to line 98 because the condition on line 94 was always true
95 platform_str += f"_{arch}"
97 # -- Return the full platform
98 return platform_str
101def get_apio_platform() -> ApioPlatform:
102 """Determines and returns the platform id based on system info and
103 optional override."""
104 # -- Use override and get from the underlying system.
105 platform_id_override = env_options.get(env_options.APIO_PLATFORM)
106 if platform_id_override: 106 ↛ 107line 106 didn't jump to line 107 because the condition on line 106 was never true
107 platform_id = platform_id_override
108 else:
109 platform_id = _determine_system_platform_id()
111 # Stick to the naming conventions we use for boards, fpgas, etc.
112 platform_id = platform_id.replace("_", "-")
114 # -- Verify it's valid. This can be a user error if the override
115 # -- is invalid.
116 if platform_id not in _APIO_PLATFORMS_DICT: 116 ↛ 117line 116 didn't jump to line 117 because the condition on line 116 was never true
117 fatal_error(
118 f"Unknown platform id: [{platform_id}]",
119 info="See Apio's documentation for supported platforms.",
120 )
122 # -- All done ok.
123 return _APIO_PLATFORMS_DICT[platform_id]
126def get_apio_platforms() -> dict[str, ApioPlatform]:
127 """Return a dict with all supported platforms."""
128 return _APIO_PLATFORMS_DICT.copy()
131def get_system_info() -> str:
132 """Return a short string with additional platform info such as version."""
133 return platform.platform()