#!/usr/bin/python
import subprocess
import pprint
import os
from benchpress.version import get_paths, implementations

def compile(search_path=None, languages=['c','cpp']):
    """Compile benchmarks of the given languages."""

    paths = get_paths()
    if not search_path:
        search_path = paths["benchmarks"]

    benchmarks = implementations(search_path)
    for lang in languages:
        print("# Compiling %s benchmarks")
        impls = benchmarks["impls"]
        for bench_lbl in impls:
            bench = impls[bench_lbl]
            if lang not in bench:
                continue
            for tool in bench[lang]:
                target = os.sep.join([bench_lbl, tool])
                path = os.sep.join([search_path, bench_lbl, tool])

                if bench[lang][tool]["issues"]:
                    print("!! Skipping %s, it has known issues" % target)
                    continue

                print("## Compiling %s" % target)
                process = subprocess.Popen(
                    "make",
                    cwd=path,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE
                )
                out, err = process.communicate()
                if out:
                    print(out)
                if err:
                    print(err)

if __name__ == "__main__":
    compile("benchmarks", ['c', 'cpp'])
