-
Notifications
You must be signed in to change notification settings - Fork 68
/
matrix_build.py
370 lines (310 loc) · 10.7 KB
/
matrix_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python3
import copy
import os
import shutil
import signal
import subprocess
import click
import tabulate
from constraint import *
CONTINUE_ON_ERROR = False
BOARDS = [
"mksgenlv21",
"mksgenlv2",
"mksgenlv1",
"esp32",
"ramps",
]
STEPPER_TYPES = [
"STEPPER_TYPE_NONE",
"STEPPER_TYPE_ENABLED",
]
DRIVER_TYPES = [
"DRIVER_TYPE_NONE",
"DRIVER_TYPE_A4988_GENERIC",
"DRIVER_TYPE_TMC2209_STANDALONE",
"DRIVER_TYPE_TMC2209_UART",
]
BOOLEAN_VALUES = [0, 1]
DISPLAY_TYPES = [
"DISPLAY_TYPE_NONE",
"DISPLAY_TYPE_LCD_KEYPAD",
"DISPLAY_TYPE_LCD_KEYPAD_I2C_MCP23008",
"DISPLAY_TYPE_LCD_KEYPAD_I2C_MCP23017",
"DISPLAY_TYPE_LCD_JOY_I2C_SSD1306",
]
BUILD_FLAGS = {
"CONFIG_VERSION": "1",
"RA_STEPPER_TYPE": [x for x in STEPPER_TYPES if x != "STEPPER_TYPE_NONE"],
"RA_DRIVER_TYPE": [x for x in DRIVER_TYPES if x != "DRIVER_TYPE_NONE"],
"DEC_STEPPER_TYPE": STEPPER_TYPES,
"DEC_DRIVER_TYPE": DRIVER_TYPES,
"USE_GPS": BOOLEAN_VALUES,
"USE_GYRO_LEVEL": BOOLEAN_VALUES,
"AZ_STEPPER_TYPE": STEPPER_TYPES,
"AZ_DRIVER_TYPE": DRIVER_TYPES,
"ALT_STEPPER_TYPE": STEPPER_TYPES,
"ALT_DRIVER_TYPE": DRIVER_TYPES,
"FOCUS_STEPPER_TYPE": STEPPER_TYPES,
"FOCUS_DRIVER_TYPE": DRIVER_TYPES,
"DISPLAY_TYPE": DISPLAY_TYPES,
"DEBUG_LEVEL": ["DEBUG_NONE", "DEBUG_ANY"],
"RA_MOTOR_CURRENT_RATING": "1",
"RA_OPERATING_CURRENT_SETTING": "1",
"DEC_MOTOR_CURRENT_RATING": "1",
"DEC_OPERATING_CURRENT_SETTING": "1",
"ALT_MOTOR_CURRENT_RATING": "1",
"ALT_OPERATING_CURRENT_SETTING": "1",
"AZ_MOTOR_CURRENT_RATING": "1",
"AZ_OPERATING_CURRENT_SETTING": "1",
"FOCUS_MOTOR_CURRENT_RATING": "1",
"FOCUS_OPERATING_CURRENT_SETTING": "1",
# Not all boards define the pins, so just hardcode it for compile testing
"RA_HOMING_SENSOR_PIN": "1",
"DEC_HOMING_SENSOR_PIN": "1",
"RA_ENDSWITCH_EAST_SENSOR_PIN": "1",
"RA_ENDSWITCH_WEST_SENSOR_PIN": "1",
"DEC_ENDSWITCH_UP_SENSOR_PIN": "1",
"DEC_ENDSWITCH_DOWN_SENSOR_PIN": "1",
}
STEPPER_SUPPORT = {
"STEPPER_TYPE_NONE": {
"DRIVER_TYPE_NONE"
},
"STEPPER_TYPE_ENABLED": {
"DRIVER_TYPE_A4988_GENERIC",
"DRIVER_TYPE_TMC2209_STANDALONE",
"DRIVER_TYPE_TMC2209_UART",
}
}
def update_dict(orig, patch):
result = copy.deepcopy(orig)
result.update(patch)
return result
BOARD_SUPPORT = {
"esp32": update_dict(BUILD_FLAGS, {
"USE_GPS": [0],
"USE_GYRO_LEVEL": [0],
"DISPLAY_TYPE": [
"DISPLAY_TYPE_NONE",
"DISPLAY_TYPE_LCD_JOY_I2C_SSD1306"
],
"AZ_DRIVER_TYPE": [
"DRIVER_TYPE_NONE"
],
"AZ_STEPPER_TYPE": [
"STEPPER_TYPE_NONE"
],
"ALT_DRIVER_TYPE": [
"DRIVER_TYPE_NONE"
],
"ALT_STEPPER_TYPE": [
"STEPPER_TYPE_NONE"
],
"FOCUS_DRIVER_TYPE": [
"DRIVER_TYPE_NONE"
],
"FOCUS_STEPPER_TYPE": [
"STEPPER_TYPE_NONE"
],
}),
"mksgenlv21": update_dict(BUILD_FLAGS, {
"USE_GPS": [0],
"USE_GYRO_LEVEL": [0],
"DISPLAY_TYPE": [
"DISPLAY_TYPE_NONE",
"DISPLAY_TYPE_LCD_KEYPAD"
],
}),
"mksgenlv2": update_dict(BUILD_FLAGS, {
"USE_GPS": [0],
"USE_GYRO_LEVEL": [0],
"DISPLAY_TYPE": [
"DISPLAY_TYPE_NONE",
"DISPLAY_TYPE_LCD_KEYPAD"
],
}),
"mksgenlv1": update_dict(BUILD_FLAGS, {
"USE_GPS": [0],
"USE_GYRO_LEVEL": [0],
"DISPLAY_TYPE": [
"DISPLAY_TYPE_NONE",
"DISPLAY_TYPE_LCD_KEYPAD"
],
"FOCUS_DRIVER_TYPE": [
"DRIVER_TYPE_NONE"
],
"FOCUS_STEPPER_TYPE": [
"STEPPER_TYPE_NONE"
],
}),
"ramps": update_dict(BUILD_FLAGS, {
"USE_GPS": [0],
"USE_GYRO_LEVEL": [0],
"DISPLAY_TYPE": [
"DISPLAY_TYPE_NONE",
"DISPLAY_TYPE_LCD_KEYPAD"
],
}),
}
SHORT_STRINGS = {
0: "DISABLED",
1: "ENABLED",
"STEPPER_TYPE_NONE": "NONE",
"STEPPER_TYPE_28BYJ48": "28BYJ48",
"STEPPER_TYPE_NEMA17": "NEMA17",
"DRIVER_TYPE_NONE": "NONE",
"DRIVER_TYPE_A4988_GENERIC": "A4988_GENERIC",
"DRIVER_TYPE_TMC2209_STANDALONE": "TMC2209_STANDALONE",
"DRIVER_TYPE_TMC2209_UART": "TMC2209_UART",
"DISPLAY_TYPE_NONE": "NONE",
"DISPLAY_TYPE_LCD_KEYPAD": "LCD_KEYPAD",
"DISPLAY_TYPE_LCD_KEYPAD_I2C_MCP23008": "LCD_KEYPAD_I2C_MCP23008",
"DISPLAY_TYPE_LCD_KEYPAD_I2C_MCP23017": "LCD_KEYPAD_I2C_MCP23017",
"DISPLAY_TYPE_LCD_JOY_I2C_SSD1306": "LCD_JOY_I2C_SSD1306",
"RA_STEPPER_TYPE": "RA_STEPPER",
"RA_DRIVER_TYPE": "RA_DRIVER",
"DEC_STEPPER_TYPE": "DEC_STEPPER",
"DEC_DRIVER_TYPE": "DEC_DRIVER",
"USE_GPS": "GPS",
"USE_GYRO_LEVEL": "GYRO",
"AZ_STEPPER_TYPE": "AZ_STEPPER",
"AZ_DRIVER_TYPE": "AZ_DRIVER",
"ALT_STEPPER_TYPE": "ALT_STEPPER",
"ALT_DRIVER_TYPE": "ALT_DRIVER",
"FOCUS_STEPPER_TYPE": "FOCUS_STEPPER",
"FOCUS_DRIVER_TYPE": "FOCUS_DRIVER",
"DISPLAY_TYPE": "DISPLAY",
}
def shorten(string):
return SHORT_STRINGS[string] if string in SHORT_STRINGS else string
# Define all possible parameters (boards and flags)
def create_problem():
problem = Problem()
problem.addVariable("BOARD", BOARDS)
for key, values in BUILD_FLAGS.items():
problem.addVariable(key, values)
return problem
# Set constraints to the problem based on supported features
def set_support_constraints(problem):
def board_supports_flag_value(b, k, v):
return v in BOARD_SUPPORT[b][k]
def board_support_constraint(expected_board, expected_flag):
return lambda b, v: expected_board != b or board_supports_flag_value(b, expected_flag, v)
# Apply board-feature support constraints
for board, flags in BOARD_SUPPORT.items():
for key, values in flags.items():
constraint = board_support_constraint(board, key)
problem.addConstraint(constraint, ["BOARD", key])
# Apply stepper-driver support constraints
def driver_supports_stepper(d, s):
return d in STEPPER_SUPPORT[s]
problem.addConstraint(driver_supports_stepper, ["RA_DRIVER_TYPE", "RA_STEPPER_TYPE"])
problem.addConstraint(driver_supports_stepper, ["DEC_DRIVER_TYPE", "DEC_STEPPER_TYPE"])
problem.addConstraint(driver_supports_stepper, ["ALT_DRIVER_TYPE", "ALT_STEPPER_TYPE"])
problem.addConstraint(driver_supports_stepper, ["AZ_DRIVER_TYPE", "AZ_STEPPER_TYPE"])
problem.addConstraint(driver_supports_stepper, ["FOCUS_DRIVER_TYPE", "FOCUS_STEPPER_TYPE"])
# Define constraints for excluded tests
def set_test_constraints(problem):
# Reduce amount of boards under test
# problem.addConstraint(InSetConstraint({"mega2560", "esp32", "mksgenlv21"}), ["BOARD"])
problem.addConstraint(AllEqualConstraint(), [
"RA_STEPPER_TYPE",
"DEC_STEPPER_TYPE",
])
problem.addConstraint(AllEqualConstraint(), [
"ALT_STEPPER_TYPE",
"AZ_STEPPER_TYPE",
])
problem.addConstraint(AllEqualConstraint(), [
"RA_DRIVER_TYPE",
"DEC_DRIVER_TYPE",
])
problem.addConstraint(AllEqualConstraint(), [
"ALT_DRIVER_TYPE",
"AZ_DRIVER_TYPE",
])
def set_ci_constraints(problem):
problem.addConstraint(InSetConstraint({"DISPLAY_TYPE_NONE", "DISPLAY_TYPE_LCD_KEYPAD"}), ["DISPLAY_TYPE"])
# problem.addConstraint(InSetConstraint({"DRIVER_TYPE_ULN2003"}), ["ALT_DRIVER_TYPE"])
def print_solutions_matrix(solutions, short_strings=False):
def get_value(vb, vk):
matching_solutions = list(filter(lambda sol: sol["BOARD"] == vb, solutions))
values = set(map(lambda s: s[vk], matching_solutions))
if short_strings:
str_values = {"ALL"} if vk != "BOARD" and values == set(BUILD_FLAGS[vk]) else set(map(shorten, values))
else:
str_values = set(map(shorten, values))
return "\n".join(str_values)
boards = sorted(list(set(map(lambda s: s["BOARD"], solutions))))
keys = list(solutions[0].keys())
rows = [[get_value(board, key) for board in boards] for key in keys]
print(tabulate.tabulate(rows, tablefmt="grid", showindex=map(shorten, keys), colalign=("right",)))
def generate_config_file(flag_values):
content = "#pragma once\n\n"
for key, value in flag_values.items():
content += "#define {} {}\n".format(key, value)
with open("Configuration_local_matrix.hpp", 'w') as f:
f.write(content)
print("Generated local config")
print("Path: {}".format(os.path.abspath(f.name)))
print("Content:")
print(content)
def create_run_environment(flag_values):
build_env = dict(os.environ)
build_flags = " ".join(["-D{}={}".format(key, value) for key, value in flag_values.items()])
build_env["PLATFORMIO_BUILD_FLAGS"] = build_flags
return build_env
def execute(board, flag_values, use_config_file=True):
if use_config_file:
build_env = dict(os.environ)
build_env["PLATFORMIO_BUILD_FLAGS"] = "-DMATRIX_LOCAL_CONFIG=1"
generate_config_file(flag_values)
else:
build_env = create_run_environment(flag_values)
proc = subprocess.Popen(
"pio run -e {}".format(board),
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE,
shell=True,
env=build_env,
)
(stdout, stderr) = proc.communicate()
return stdout, stdout, proc.returncode
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self):
shutil.rmtree('.pio/build/matrix')
self.kill_now = True
@click.command()
@click.option(
'--board',
'-b',
type=click.Choice(BOARDS, case_sensitive=False),
multiple=True,
help="Limit boards under test. Multiple values allowed.")
def solve(board):
# noinspection PyUnusedLocal
killer = GracefulKiller()
problem = create_problem()
set_support_constraints(problem)
if board:
problem.addConstraint(InSetConstraint(board), ["BOARD"])
set_test_constraints(problem)
set_ci_constraints(problem)
solutions = problem.getSolutions()
print_solutions_matrix(solutions, short_strings=False)
print("Testing {} combinations".format(len(solutions)))
for num, solution in enumerate(solutions, start=1):
print("[{}/{}] Building ...".format(num, len(solutions)), flush=True)
print_solutions_matrix([solution])
board = solution.pop("BOARD")
(o, e, c) = execute(board, solution)
if c and not CONTINUE_ON_ERROR:
exit(c)
print(flush=True)
if __name__ == '__main__':
solve()