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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
#!/usr/bin/env python
#
# Notice: Some parts of this file were copied from PyBOMBS, which has a
# different copyright, and is highlighted appropriately. The following
# copyright notice pertains to all the parts written specifically for this
# script.
#
# Copyright 2016 Ettus Research
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Run Vivado builds
"""
from __future__ import print_function
import os
import sys
import re
import json
from datetime import datetime
import time
import argparse
import subprocess
import threading
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # Py3k
READ_TIMEOUT = 0.1 # s
#############################################################################
# The following functions were copied with minor modifications from PyBOMBS:
def get_console_width():
'''
Returns width of console.
http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
'''
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
return cr[1]
def which(program):
"""
Equivalent to Unix' `which` command.
Returns None if the executable `program` can't be found.
If a full path is given (e.g. /usr/bin/foo), it will return
the path if the executable can be found, or None otherwise.
If no path is given, it will search PATH.
"""
def is_exe(fpath):
" Check fpath is an executable "
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
if os.path.split(program)[0] and is_exe(program):
return program
else:
for path in os.environ.get("PATH", "").split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
#
# End of functions copied from PyBOMBS.
#############################################################################
def print_timer(time_delta):
"""docstring for print_timer"""
hours, secs = divmod(time_delta.seconds, 3600)
mins, secs = divmod(secs, 60)
return "[{h:02}:{m:02}:{s:02}]".format(
h=hours, m=mins, s=secs,
)
def list_search(patterns, string):
" Returns True if string matches any element of pattern "
for pattern in patterns:
if re.search(pattern, string) is not None:
return True
return False
def parse_args():
" Parses args for this script, and for Vivado. "
parser = argparse.ArgumentParser(
description="Run Vivado and parse output.",
)
parser.add_argument(
'--no-color', action="store_true",
help="Don't colorize output.",
)
parser.add_argument(
'--vivado-command', default=None,
help="Vivado command.",
)
parser.add_argument(
'--parse-config', default=None,
help="Additional parser configurations",
)
parser.add_argument(
'-v', '--verbose', default=False,
action='store_true',
help="Print Vivado output")
parser.add_argument(
'--warnings', default=False,
action='store_true',
help="Print Vivado warnings")
our_args, viv_args = parser.parse_known_args()
return our_args, " ".join(viv_args)
class VivadoRunner(object):
" Vivado Runner "
colors = {
'warning': '\033[0;35m',
'critical warning': '\033[33m',
'error': '\033[1;31m',
'fatal': '\033[1;31m',
'task': '\033[32m',
'cmd': '\033[1;34m',
'normal': '\033[0m',
}
# Black 0;30 Dark Gray 1;30
# Blue 0;34 Light Blue 1;34
# Green 0;32 Light Green 1;32
# Cyan 0;36 Light Cyan 1;36
# Red 0;31 Light Red 1;31
# Purple 0;35 Light Purple 1;35
# Brown 0;33 Yellow 1;33
# Light Gray 0;37 White 1;37
viv_tcl_cmds = {
'synth_design' : 'Synthesis',
'opt_design': 'Logic Optimization',
'place_design': 'Placer',
'route_design': 'Routing',
'phys_opt_design': 'Physical Synthesis',
'report_timing' : 'Timing Reporting',
'report_power': 'Power Reporting',
'report_drc': 'DRC',
'write_bitstream': 'Write Bitstream',
}
def __init__(self, args, viv_args):
self.status = ''
self.args = args
self.current_task = "Initialization"
self.current_phase = "Starting"
self.command = args.vivado_command + " " + viv_args
self.notif_queue = Queue()
self.msg_counters = {}
self.fatal_error_found = False
self.line_types = {
'cmd': {
'regexes': [
'^Command: .+',
],
'action': self.show_cmd,
'id': "Command",
},
'task': {
'regexes': [
'^Starting .* Task',
'^.*Translating synthesized netlist.*',
'^\[TEST CASE .*',
],
'action': self.update_task,
'id': "Task",
},
'phase': {
'regexes': [
'^Phase (?P<id>[a-zA-Z0-9/. ]*)$',
'^Start (?P<id>[a-zA-Z0-9/. ]*)$',
'^(?P<id>TESTBENCH STARTED: [\w_]*)$',
],
'action': self.update_phase,
'id': "Phase",
},
'warning': {
'regexes': [
'^WARNING'
],
'action': lambda x: self.act_on_build_msg('warning', x),
'id': "Warning",
'fatal': [
]
},
'critical warning': {
'regexes': [
'^CRITICAL WARNING'
],
'action': lambda x: self.act_on_build_msg('critical warning', x),
'id': "Critical Warning",
'fatal': [
]
},
'error': {
'regexes': [
'^ERROR',
'no such file or directory',
'^Result: FAILED'
],
'action': lambda x: self.act_on_build_msg('error', x),
'id': "Error",
'fatal': [
'.', # All errors are fatal by default
]
},
'test': {
'regexes': [
'^ - T'
'^Result: '
],
'action': self.update_testbench,
'id': "Test"
}
}
self.parse_config = None
if args.parse_config is not None:
try:
args.parse_config = os.path.normpath(args.parse_config)
parse_config = json.load(open(args.parse_config))
self.add_notification(
"Using parser configuration from: {pc}".format(pc=args.parse_config),
color=self.colors.get('normal')
)
loadables = ('regexes', 'ignore', 'fatal')
for line_type in self.line_types:
for loadable in loadables:
self.line_types[line_type][loadable] = \
self.line_types[line_type].get(loadable, []) + \
parse_config.get(line_type, {}).get(loadable, [])
except (IOError, ValueError):
self.add_notification(
"Could not read parser configuration from: {pc}".format(pc=args.parse_config),
color=self.colors.get('warning')
)
self.tty = sys.stdout.isatty()
self.timer = datetime.now() # Make sure this is the last line in ctor
def run(self):
"""
Kick off Vivado build.
Returns True if it all passed.
"""
def enqueue_output(stdout_data, stdout_queue):
" Puts the output from the process into the queue "
for line in iter(stdout_data.readline, b''):
stdout_queue.put(line)
stdout_data.close()
def poll_queue(q):
" Safe polling from queue "
try:
return q.get(timeout=READ_TIMEOUT).decode('utf-8')
except UnicodeDecodeError:
pass
except Empty:
pass
return ""
# Start process
self.add_notification(
"Executing command: {cmd}".format(cmd=self.command), add_time=True, color=self.colors.get('cmd')
)
proc = subprocess.Popen(
self.command,
shell=True, # Yes we run this in a shell. Unsafe but helps with Vivado.
stdout=subprocess.PIPE, stderr=subprocess.STDOUT # Pipe it all out via stdout
)
# Init thread and queue
q_stdout = Queue()
t = threading.Thread(target=enqueue_output, args=(proc.stdout, q_stdout))
# End the thread when the program terminates
t.daemon = True
t.start()
status_line_t = threading.Thread(target=VivadoRunner.run_loop, args=(self.print_status_line, 0.5 if self.tty else 60*10))
status_line_t.daemon = True
status_line_t.start()
# Run loop
while proc.poll() is None or not q_stdout.empty(): # Run while process is alive
line_stdout = poll_queue(q_stdout)
self.update_output(line_stdout)
success = (proc.returncode == 0) and not self.fatal_error_found
self.cleanup_output(success)
return success
def update_output(self, lines):
" Receives a line from Vivado output and acts upon it. "
self.process_line(lines)
@staticmethod
def run_loop(func, delay, *args, **kwargs):
while True:
func(*args, **kwargs)
time.sleep(delay)
def print_status_line(self):
" Prints status on stdout"
old_status_line_len = len(self.status)
self.update_status_line()
sys.stdout.write("\x1b[2K\r") # Scroll cursor back to beginning and clear last line
self.flush_notification_queue(old_status_line_len)
sys.stdout.write(self.status)
sys.stdout.flush()
# Make sure we print enough spaces to clear out all of the previous message
# if not msgs_printed:
# sys.stdout.write(" " * max(0, old_status_line_len - len(self.status)))
def cleanup_output(self, success):
" Run final printery after all is said and done. "
# Check message counts are within limits
self.update_phase("Finished")
self.add_notification(
"Process terminated. Status: {status}".format(status='Success' if success else 'Failure'),
add_time=True,
color=self.colors.get("task" if success else "error")
)
sys.stdout.write("\n")
self.flush_notification_queue(len(self.status))
print("")
print("========================================================")
print("Warnings: ", self.msg_counters.get('warning', 0))
print("Critical Warnings: ", self.msg_counters.get('critical warning', 0))
print("Errors: ", self.msg_counters.get('error', 0))
print("")
sys.stdout.flush()
def process_line(self, lines):
" process line "
for line in [l.rstrip() for l in lines.split("\n") if len(l.strip())]:
line_info, line_data = self.classify_line(line)
if line_info is not None:
self.line_types[line_info]['action'](line_data)
elif self.args.verbose:
print(line)
def classify_line(self, line):
"""
Identify the current line. Return None if the line can't be identified.
"""
for line_type in self.line_types:
for regex in self.line_types[line_type]['regexes']:
re_obj = re.search(regex, line)
if re_obj is not None:
return line_type, re_obj.groupdict().get('id', line)
return None, None
def update_status_line(self):
" Update self.status. Does not print anything! "
status_line = "{timer} Current task: {task} +++ Current Phase: {phase}"
self.status = status_line.format(
timer=print_timer(datetime.now() - self.timer),
task=self.current_task.strip(),
phase=self.current_phase.strip(),
)
def add_notification(self, msg, add_time=False, color=None):
"""
Format msg and add it as a notification to the queue.
"""
if add_time:
msg = print_timer(datetime.now() - self.timer) + " " + msg
if color is not None and not self.args.no_color:
msg = color + msg + self.colors.get('normal')
self.notif_queue.put(msg)
def flush_notification_queue(self, min_len):
" Print all strings in the notification queue. "
msg_printed = False
while not self.notif_queue.empty():
msg = self.notif_queue.get().strip()
print(msg)
msg_printed = True
return msg_printed
def act_on_build_msg(self, msg_type, msg):
"""
Act on a warning, error, critical warning, etc.
"""
if list_search(self.line_types[msg_type].get('fatal', []), msg):
self.add_notification(msg, color=self.colors.get('fatal'))
self.fatal_error_found = True
elif not list_search(self.line_types[msg_type].get('ignore', []), msg):
self.add_notification(msg, color=self.colors.get(msg_type))
self.msg_counters[msg_type] = self.msg_counters.get(msg_type, 0) + 1
def show_cmd(self, tcl_cmd):
" Show the current command "
self.update_phase("Finished")
tcl_cmd = tcl_cmd.replace("Command:", "").strip()
#sys.stdout.write("\n")
self.add_notification("Executing Tcl: " + tcl_cmd,
add_time=True, color=self.colors.get("cmd"))
cmd = tcl_cmd.strip().split()[0];
if cmd in self.viv_tcl_cmds:
cmd = self.viv_tcl_cmds[cmd]
self.update_task("Starting " + cmd + " Command", is_new=False)
#self.flush_notification_queue(len(self.status))
def update_task(self, task, is_new=True):
" Update current task "
# Special case: Treat "translation" as a phase as well
if "Translating synthesized netlist" in task:
task = "Translating Synthesized Netlist"
filtered_task = task.replace("Starting", "").replace("Task", "").replace("Command", "")
if is_new and (filtered_task != self.current_task):
self.update_phase("Finished")
self.current_task = filtered_task
self.current_phase = "Starting"
self.add_notification(task, add_time=True, color=self.colors.get("task"))
sys.stdout.write("\n")
self.print_status_line()
def update_phase(self, phase):
" Update current phase "
self.current_phase = phase.strip()
self.current_task = self.current_task.replace("Phase", "")
sys.stdout.write("\n")
self.print_status_line()
def update_testbench(self, testbench):
pass # Do nothing
def main():
" Go, go, go! "
args, viv_args = parse_args()
if args.vivado_command is None:
if which("vivado"):
args.vivado_command = "vivado"
elif which("vivado_lab"):
args.vivado_command = "vivado_lab"
else:
print("Cannot find Vivado executable!")
return False
try:
return VivadoRunner(args, viv_args).run()
except KeyboardInterrupt:
print("")
print("")
print("Caught Ctrl-C. Exiting.")
print("")
return False
if __name__ == "__main__":
exit(not main())
|