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

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 

8 

9"""Definition of underlying platforms supported by Apio.""" 

10 

11import re 

12import platform 

13from dataclasses import dataclass 

14from apio.utils import env_options 

15from apio.common.apio_console import fatal_error 

16 

17 

18@dataclass(frozen=True) 

19class ApioPlatform: 

20 """A class with Apio supported platforms and their attributes.""" 

21 

22 # -- Platform id. 

23 id: str 

24 

25 # -- Human readable general platform type. 

26 type: str 

27 

28 # -- Human readable variant of the platform type. 

29 variant: str 

30 

31 # -- Set to True exactly one of these 

32 is_darwin: bool = False 

33 is_linux: bool = False 

34 is_windows: bool = False 

35 

36 def __post_init__(self): 

37 """Post init validation""" 

38 

39 # -- Constraint chars in platform id. 

40 assert re.fullmatch(r"[a-z0-9-]+", self.id), self.id 

41 

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 

46 

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 

51 

52 

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} 

74 

75 

76def _determine_system_platform_id() -> str: 

77 """Return a String with the current platform: 

78 ex. linux-x86-64 

79 ex. windows-amd64""" 

80 

81 # -- Get the platform: linux, windows, darwin 

82 type_ = platform.system().lower() 

83 platform_str = f"{type_}" 

84 

85 # -- Get the architecture 

86 arch = platform.machine().lower() 

87 

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" 

92 

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}" 

96 

97 # -- Return the full platform 

98 return platform_str 

99 

100 

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() 

110 

111 # Stick to the naming conventions we use for boards, fpgas, etc. 

112 platform_id = platform_id.replace("_", "-") 

113 

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 ) 

121 

122 # -- All done ok. 

123 return _APIO_PLATFORMS_DICT[platform_id] 

124 

125 

126def get_apio_platforms() -> dict[str, ApioPlatform]: 

127 """Return a dict with all supported platforms.""" 

128 return _APIO_PLATFORMS_DICT.copy() 

129 

130 

131def get_system_info() -> str: 

132 """Return a short string with additional platform info such as version.""" 

133 return platform.platform()