#!/usr/bin/env python
import os, glob
from os.path import join, exists
from fabricate import main, autoclean, run

BOARD = 'icestick'
BUILD = 'build'
GOLD = 'gold' 

tests = glob.glob('*.py')

def build():
    for test in tests:
        if not test.startswith('_'):
            base, suffix = test.split('.')
            compile(base)

def compile(base):
    magma(base)
    regress(base)

def magma(base):
    run('magma', '-b', BOARD, '-d', BUILD, base)

def regress(base):
    u = base+'.pcf'
    v = base+'.v'
    buildu, goldu = join(BUILD,u), join(GOLD,u)
    buildv, goldv = join(BUILD,v), join(GOLD,v)
    if exists(goldu):
        run('cmp',buildu,goldu)
    if exists(goldv):
        run('cmp',buildv,goldv)

def clean():
    autoclean()

def gold():
    os.system('cp %s/*.{v,pcf} %s' % (BUILD, GOLD))

main()
