Improved Yosys error handling. Signed-off-by: Maciej Kurc <mkurc@antmicro.com>
diff --git a/v2x/yosys/run.py b/v2x/yosys/run.py index 9b30c79..3703eeb 100755 --- a/v2x/yosys/run.py +++ b/v2x/yosys/run.py
@@ -30,13 +30,16 @@ def get_output(params): """Run Yosys with given command line parameters, and return - stdout as a string""" + stdout as a string. Raises CalledProcessError on a non-zero exit code.""" verbose = get_verbose() cmd = [get_yosys()] + get_yosys_common_args() + params if verbose: - print(cmd) + msg = "" + msg += "command".ljust(9).ljust(80, "=") + "\n" + msg += str(cmd) + print(msg) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -47,23 +50,32 @@ retcode = p.wait() - msg = "" - msg += "stdout" + "=" * 75 + "\n" - msg += stdout + "\n" - msg += "stderr" + "-" * 75 + "\n" - msg += stderr + "\n" - msg += "=" * 75 + "\n" - msg += "Return Code: {}".format(retcode) - if verbose: + msg = "" + + if len(stdout): + msg += "stdout".ljust(9).ljust(80, "=") + "\n" + msg += stdout + + if len(stderr): + msg += "stderr".ljust(9).ljust(80, "=") + "\n" + msg += stderr + + msg += "exitcode".ljust(9).ljust(80, "=") + "\n" + msg += "{}\n".format(retcode) + + msg += "=" * 80 + "\n" print(msg) if retcode != 0: emsg = "" - emsg += "Failed to run {}".join(" ".join(cmd)) + "\n" - emsg += msg + emsg += "Yosys failed with exit code {}\n".format(retcode) + emsg += "Command: '{}'\n".format(" ".join(cmd)) + emsg += "Message:\n" + emsg += "\n".join([" " + l for l in stderr.splitlines()]) raise subprocess.CalledProcessError(retcode, cmd, emsg) + return stdout @@ -105,7 +117,7 @@ commands = "read_verilog {} {} {}; ".format( get_defines(), get_includes(), " ".join(infiles) ) + commands - params = ["-q", "-p", commands] + params = ["-p", commands] return get_output(params) @@ -117,7 +129,7 @@ script : path to Yosys script to run infiles : list of input files """ - params = ["-q", "-s", script] + infiles + params = ["-s", script] + infiles return get_output(params) @@ -143,9 +155,13 @@ else: mode_str = "" cmds = "{}prep {}; write_json {}".format(mode_str, prep_opts, json_opts) - j = utils.strip_yosys_json(commands(cmds, infiles)) - """with open('dump.json', 'w') as dbg: - print(j,file=dbg)""" + + try: + j = utils.strip_yosys_json(commands(cmds, infiles)) + except subprocess.CalledProcessError as ex: + print(ex.output) + exit(-1) + return json.loads(j) @@ -198,7 +214,13 @@ outfile = tempfile.mktemp() sel_cmd = "{} cd {}; select -write {} {}".format(p, module, outfile, expr) - commands(sel_cmd, infiles) + + try: + commands(sel_cmd, infiles) + except subprocess.CalledProcessError as ex: + print(ex.output) + exit(-1) + pins = [] with open(outfile, 'r') as f: for net in f: @@ -207,6 +229,7 @@ pin = extract_pin(module, snet) if pin is not None: pins.append(pin) + os.remove(outfile) return pins