#!/usr/bin/python3

# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Main driver program for the Emboss compiler."""

import argparse
import os
import subprocess
import sys


def _parse_args(argv):
  parser = argparse.ArgumentParser(description="Emboss compiler")
  parser.add_argument("--color-output",
                      default="if_tty",
                      choices=["always", "never", "if_tty", "auto"],
                      help="Print error messages using color.  'auto' is a "
                           "synonym for 'if_tty'.")
  parser.add_argument("--import-dir", "-I",
                      dest="import_dirs",
                      action="append",
                      default=["."],
                      help="A directory to use when searching for imported "
                           "embs.  If no import-dirs are specified, the "
                           "current directory will be used.")
  parser.add_argument("--generate",
                      nargs=1,
                      choices=["cc"],
                      default="cc",
                      help="Which back end to use.  Currently only C++ is "
                           "supported.")
  parser.add_argument("--output-path",
                      nargs=1,
                      default=".",
                      help="Prefix to use for the generated output file.")
  parser.add_argument("input_file",
                      type=str,
                      nargs=1,
                      help=".emb file to compile.")
  return parser.parse_args(argv[1:])


def main(argv):
  flags = _parse_args(argv)
  base_path = os.path.dirname(__file__) or "."
  subprocess_environment = os.environ.copy()

  if subprocess_environment.get("PYTHONPATH"):
    subprocess_environment["PYTHONPATH"] = (
      base_path + ":" + subprocess_environment.get("PYTHONPATH"))
  else:
    subprocess_environment["PYTHONPATH"] = base_path

  front_end_args = [
      sys.executable,
      os.path.join(base_path, "compiler", "front_end", "emboss_front_end.py"),
      "--output-ir-to-stdout",
      "--color-output", flags.color_output,
  ]

  for import_dir in flags.import_dirs:
    front_end_args.extend(["--import-dir", import_dir])

  front_end_args.append(flags.input_file[0])
  front_end_status = subprocess.run(front_end_args,
                                    stdout=subprocess.PIPE,
                                    env=subprocess_environment)

  if front_end_status.returncode != 0:
    return front_end_status.returncode

  back_end_status = subprocess.run(
    [
      sys.executable,
      os.path.join(base_path, "compiler", "back_end", "cpp",
        "emboss_codegen_cpp.py"),
    ],
    input=front_end_status.stdout,
    stdout=subprocess.PIPE,
    env=subprocess_environment
  )

  if back_end_status.returncode != 0:
    return back_end_status.returncode

  output_file = os.path.join(flags.output_path[0], flags.input_file[0]) + ".h"
  os.makedirs(os.path.dirname(output_file), exist_ok=True)

  with open(output_file, "wb") as output:
    output.write(back_end_status.stdout)
  return 0


if __name__ == "__main__":
  sys.exit(main(sys.argv))
