#! /usr/bin/env python3

from configure import run_cmake, rewrite_ninja
import subprocess
import sys
import os

usage_string = f"""
usage: {sys.argv[0]} <opt passes...>

Configures a test suite to run with the provided opt passes, geenrating a
suitable testsuite scripts and providing default names. Compiles the test
suite and configures a perf run.

Example:
  % cd /testsuite
  % ./configure-fast.py -licm -loop-unswitch
  This will output in licm_loop-unswitch/ and configure perf to run.
""".strip()

testsuite_script = """
#! /bin/bash

set -e
. $(dirname $0)/toolbox.sh

box_compile
box_opt {}
box_asm
""".strip()

passes = sys.argv[1:]
name   = "_".join(p[1:] for p in passes)

# Generate the testsuite script

path = f"../scripts/{name}.sh"

with open(path, "w") as fp:
    fp.write(testsuite_script.format(" ".join(passes)))

os.chmod(path, 0o755)

# Configure and rewrite the test suite
run_cmake(name, None, None, None)
rewrite_ninja(os.path.join(name, "rules.ninja"), f"{name}.sh")

# Build the test suite
subprocess.run([ "ninja" ], cwd=name)

# Configure a perf run
subprocess.run([ "../configure-perf.py", "-p", "--table -r 5", "-o",
                 "perf-run5-1" ], cwd=name)
