Coverage for tests/unit_tests/managers/test_scons_manager.py: 100%

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

10Tests of the scons manager scons.py 

11""" 

12 

13from google.protobuf import text_format 

14from tests.conftest import ApioRunner 

15from apio.common.proto.stubs.apio_scons_pb2 import ( 

16 SconsParams, 

17 Verbosity, 

18 TargetParams, 

19 LintParams, 

20) 

21from apio.apio_context import ( 

22 ApioContext, 

23 PackagesPolicy, 

24 ProjectPolicy, 

25 RemoteConfigPolicy, 

26) 

27from apio.managers.scons_manager import SConsManager 

28 

29TEST_APIO_INI_DICT = { 

30 "[env:default]": { 

31 # -- Required. 

32 "board": "alhambra-ii", 

33 # -- Optional. 

34 "top-module": "my_module", 

35 "format-verible-options": "\n --aaa bbb\n --ccc ddd", 

36 "yosys-extra-options": "-dsp -xyz", 

37 "nextpnr-extra-options": "--freq 13", 

38 "gtkwave-extra-options": "--rcvar=do_initial_zoom_fit 1", 

39 "verilator-extra-options": "-Wno-fatal", 

40 } 

41} 

42 

43# -- The non determinisitc values marked with TBD are patched by the test 

44# -- at runtime. 

45EXPECTED1 = """ 

46timestamp: "TBD" 

47fpga_definition { 

48 arch: ice40 

49 vendor_part: "ICE40HX4K-TQ144" 

50 size: "8k" 

51 ice40_params { 

52 nextpnr_type: "hx8k" 

53 nextpnr_package: "tq144:4k" 

54 } 

55} 

56environment { 

57 platform_id: "TBD" 

58 terminal_mode: FORCE_TERMINAL 

59 theme_name: "light" 

60 yosys_path: "TBD" 

61 trellis_path: "TBD" 

62 scons_shell_id: "" 

63 xilinx_prjxray_db_path: "TBD" 

64 tools_env_mutations { 

65 pyinstaller_linux_fix: false 

66 } 

67} 

68apio_env_params { 

69 env_name: "default" 

70 board_id: "alhambra-ii" 

71 top_module: "my_module" 

72 yosys_extra_options: "-dsp -xyz" 

73 nextpnr_extra_options: "--freq 13" 

74 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1' 

75 verilator_extra_options: "-Wno-fatal", 

76} 

77""" 

78 

79EXPECTED2 = """ 

80timestamp: "TBD" 

81fpga_definition { 

82 arch: ice40 

83 vendor_part: "ICE40HX4K-TQ144" 

84 size: "8k" 

85 ice40_params { 

86 nextpnr_type: "hx8k" 

87 nextpnr_package: "tq144:4k" 

88 } 

89} 

90verbosity { 

91 all: true 

92 synth: true 

93 pnr: true 

94} 

95environment { 

96 platform_id: "TBD" 

97 terminal_mode: FORCE_TERMINAL 

98 theme_name: "light" 

99 yosys_path: "TBD" 

100 trellis_path: "TBD" 

101 scons_shell_id: "" 

102 tools_env_mutations { 

103 pyinstaller_linux_fix: false 

104 } 

105} 

106apio_env_params { 

107 env_name: "default" 

108 board_id: "alhambra-ii" 

109 top_module: "my_module" 

110 yosys_extra_options: "-dsp -xyz" 

111 nextpnr_extra_options: "--freq 13" 

112 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1' 

113 verilator_extra_options: "-Wno-fatal" 

114} 

115target { 

116 lint { 

117 top_module: "my_module" 

118 } 

119} 

120""" 

121 

122 

123def test_default_params(apio_runner: ApioRunner): 

124 """Tests the construct_scons_params() with default values.""" 

125 

126 with apio_runner.in_sandbox() as sb: 

127 

128 # -- Setup a Scons object. 

129 sb.write_apio_ini(TEST_APIO_INI_DICT) 

130 apio_ctx = ApioContext( 

131 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

132 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

133 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

134 ) 

135 scons = SConsManager(apio_ctx) 

136 

137 # -- Get the actual value. 

138 scons_params = scons.construct_scons_params() 

139 

140 # -- Construct the expected value. We fill in non deterministic values. 

141 expected = text_format.Parse(EXPECTED1, SconsParams()) 

142 expected.timestamp = scons_params.timestamp 

143 expected.environment.platform_id = apio_ctx.platform.id 

144 # expected.environment.is_windows = apio_ctx.is_windows 

145 expected.environment.yosys_path = str( 

146 sb.packages_dir / "oss-cad-suite/share/yosys" 

147 ) 

148 expected.environment.trellis_path = str( 

149 sb.packages_dir / "oss-cad-suite/share/trellis" 

150 ) 

151 expected.environment.scons_shell_id = apio_ctx.scons_shell_id() 

152 

153 expected.environment.xilinx_prjxray_db_path = str( 

154 sb.packages_dir / "openxc7/share/nextpnr/external/prjxray-db" 

155 ) 

156 expected.environment.tools_env_mutations.CopyFrom( 

157 apio_ctx.get_env_mutations_for_subprocess( 

158 include_apio_packages=True 

159 ) 

160 ) 

161 

162 # -- Compare actual to expected values. 

163 assert str(scons_params) == str(expected) 

164 

165 

166def test_explicit_params(apio_runner: ApioRunner): 

167 """Tests the construct_scons_params() method with values override..""" 

168 

169 with apio_runner.in_sandbox() as sb: 

170 

171 # -- Setup a Scons object. 

172 sb.write_apio_ini(TEST_APIO_INI_DICT) 

173 apio_ctx = ApioContext( 

174 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

175 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

176 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

177 ) 

178 scons = SConsManager(apio_ctx) 

179 

180 # -- Get the actual value. 

181 target_params = TargetParams( 

182 lint=LintParams( 

183 top_module="my_module", 

184 ) 

185 ) 

186 verbosity = Verbosity(all=True, synth=True, pnr=True) 

187 scons_params = scons.construct_scons_params( 

188 verbosity=verbosity, target_params=target_params 

189 ) 

190 

191 # -- Construct the expected value. We fill in non deterministic values. 

192 expected = text_format.Parse(EXPECTED2, SconsParams()) 

193 expected.timestamp = scons_params.timestamp 

194 expected.environment.platform_id = apio_ctx.platform.id 

195 # expected.environment.is_windows = apio_ctx.is_windows 

196 expected.environment.yosys_path = str( 

197 sb.packages_dir / "oss-cad-suite/share/yosys" 

198 ) 

199 expected.environment.trellis_path = str( 

200 sb.packages_dir / "oss-cad-suite/share/trellis" 

201 ) 

202 expected.environment.scons_shell_id = apio_ctx.scons_shell_id() 

203 

204 expected.environment.xilinx_prjxray_db_path = str( 

205 sb.packages_dir / "openxc7/share/nextpnr/external/prjxray-db" 

206 ) 

207 

208 expected.environment.tools_env_mutations.CopyFrom( 

209 apio_ctx.get_env_mutations_for_subprocess( 

210 include_apio_packages=True 

211 ) 

212 ) 

213 

214 # -- Compare actual to expected values. 

215 assert str(scons_params) == str(expected)