#!/usr/bin/env python3
import sys
import os
import traceback
import logging
import profile
from subprocess import call, check_call, check_output

ROOT_DIR = './'
TEST_DIR = ROOT_DIR+'tests'
TMP_DIR  = ROOT_DIR+'.tmp'
sys.path.append(ROOT_DIR)
from polyphony.compiler.__main__ import compile_main, logging_setting
from polyphony.compiler.env import env


def exec_test(test, output=True, compile_only=False):
    casefile = os.path.basename(test)
    casename, _ = os.path.splitext(casefile)
    try:
        compile_main(test, casename, TMP_DIR, debug_mode=output)
    except Exception as e:
        print('[COMPILE PYTHON] FAILED:'+test)
        if env.dev_debug_mode:
            traceback.print_exc()
        print(e)
        return
    if compile_only:
        return

    hdl_files = ['{}/{}.v'.format(TMP_DIR, casename), '{}/{}_test.v'.format(TMP_DIR, casename)]
    exec_name = '{}/test'.format(TMP_DIR)
    args = ('iverilog -I {} -W all -o {} -s test'.format(TMP_DIR, exec_name)).split(' ')
    args += hdl_files
    try:
        check_call(args)
    except Exception as e:
        print('[COMPILE HDL] FAILED:'+test)
        return

    try:
        out = check_output([exec_name])
        lines = out.decode('utf-8').split('\n')
        for line in lines:
            if output:
                print(line)
            if 'FAILED' in line:
                raise Exception()
    except Exception as e:
        print('[SIMULATION] FAILED:'+test)
        print(e)


if __name__ == '__main__':
    if not os.path.exists(TMP_DIR):
        os.mkdir(TMP_DIR)
    if len(sys.argv) > 1:
        #profile.run("exec_test(sys.argv[1])")
        exec_test(sys.argv[1])

#!/usr/bin/env python3
import sys
import os
import traceback
import subprocess

IVERILOG_PATH = 'iverilog'
ROOT_DIR = '.' + os.path.sep
TEST_DIR = ROOT_DIR + 'tests'
TMP_DIR  = ROOT_DIR + '.tmp'
sys.path.append(ROOT_DIR)

from polyphony.compiler.__main__ import compile_main, logging_setting
from polyphony.compiler.env import env


def exec_test(casefile_path, output=True, compile_only=False):
    casefile = os.path.basename(casefile_path)
    casename, _ = os.path.splitext(casefile)
    try:
        compile_main(casefile_path, casename, TMP_DIR, debug_mode=output)
    except Exception as e:
        print('[COMPILE PYTHON] FAILED:' + casefile_path)
        if env.dev_debug_mode:
            traceback.print_exc()
        print(e)
        return
    if compile_only:
        return
    for testbench in env.testbenches:
        simulate_verilog(testbench.orig_name, casename, casefile_path, output)


def simulate_verilog(testname, casename, casefile_path, output):
    hdl_files = ['{}{}{}.v'.format(TMP_DIR, os.path.sep, casename), '{}{}{}.v'.format(TMP_DIR, os.path.sep, testname)]
    exec_name = '{}{}{}'.format(TMP_DIR, os.path.sep, testname)
    args = ('{} -I {} -W all -o {} -s {}'.format(IVERILOG_PATH, TMP_DIR, exec_name, testname)).split(' ')
    args += hdl_files
    try:
        subprocess.check_call(args)
    except Exception as e:
        print('[COMPILE HDL] FAILED:' + casefile_path)
        return

    try:
        out = subprocess.check_output([exec_name])
        lines = out.decode('utf-8').split('\n')
        for line in lines:
            if output:
                print(line)
            if 'FAILED' in line:
                raise Exception()
    except Exception as e:
        print('[SIMULATION] FAILED:' + casefile_path)
        print(e)


if __name__ == '__main__':
    if not os.path.exists(TMP_DIR):
        os.mkdir(TMP_DIR)
    if len(sys.argv) > 1:
        # import profile
        # profile.run("exec_test(sys.argv[1])")
        exec_test(sys.argv[1])

update simu.py for 0.3.0.