Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | # Copyright 2019 Google LLC |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 17 | """Main driver program for the Emboss compiler.""" |
| 18 | |
Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 19 | import argparse |
| 20 | import os |
| 21 | from os import path |
| 22 | import subprocess |
| 23 | import sys |
| 24 | |
| 25 | def _parse_args(argv): |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 26 | parser = argparse.ArgumentParser(description="Emboss compiler") |
| 27 | parser.add_argument("--color-output", |
| 28 | default="if_tty", |
| 29 | choices=["always", "never", "if_tty", "auto"], |
| 30 | help="Print error messages using color. 'auto' is a " |
| 31 | "synonym for 'if_tty'.") |
| 32 | parser.add_argument("--import-dir", "-I", |
| 33 | dest="import_dirs", |
| 34 | action="append", |
| 35 | default=["."], |
| 36 | help="A directory to use when searching for imported " |
| 37 | "embs. If no import-dirs are specified, the " |
| 38 | "current directory will be used.") |
| 39 | parser.add_argument("--generate", |
| 40 | nargs=1, |
| 41 | choices=["cc"], |
| 42 | default="cc", |
| 43 | help="Which back end to use. Currently only C++ is " |
| 44 | "supported.") |
| 45 | parser.add_argument("--output-path", |
| 46 | nargs=1, |
| 47 | default=".", |
| 48 | help="Prefix to use for the generated output file.") |
| 49 | parser.add_argument("input_file", |
| 50 | type=str, |
| 51 | nargs=1, |
| 52 | help=".emb file to compile.") |
| 53 | return parser.parse_args(argv[1:]) |
Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def main(argv): |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 57 | flags = _parse_args(argv) |
| 58 | base_path = path.dirname(__file__) or "." |
| 59 | subprocess_environment = os.environ.copy() |
| 60 | if subprocess_environment.get("PYTHONPATH"): |
| 61 | subprocess_environment["PYTHONPATH"] = ( |
| 62 | base_path + ":" + subprocess_environment.get("PYTHONPATH")) |
| 63 | else: |
| 64 | subprocess_environment["PYTHONPATH"] = base_path |
| 65 | front_end_args = [ |
| 66 | sys.executable, |
reventlov | 6731fc4 | 2019-10-03 15:23:13 -0700 | [diff] [blame] | 67 | path.join(base_path, "compiler", "front_end", "emboss_front_end.py"), |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 68 | "--output-ir-to-stdout", |
| 69 | "--color-output", flags.color_output, |
| 70 | ] |
| 71 | for import_dir in flags.import_dirs: |
| 72 | front_end_args.extend([ |
| 73 | "--import-dir", |
| 74 | import_dir |
| 75 | ]) |
| 76 | front_end_args.append(flags.input_file[0]) |
| 77 | front_end_status = subprocess.run(front_end_args, |
| 78 | stdout=subprocess.PIPE, |
| 79 | env=subprocess_environment) |
| 80 | if front_end_status.returncode != 0: |
| 81 | return front_end_status.returncode |
| 82 | back_end_status = subprocess.run( |
| 83 | [ |
| 84 | sys.executable, |
reventlov | 6731fc4 | 2019-10-03 15:23:13 -0700 | [diff] [blame] | 85 | path.join(base_path, "compiler", "back_end", "cpp", |
| 86 | "emboss_codegen_cpp.py"), |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 87 | ], |
| 88 | input=front_end_status.stdout, |
| 89 | stdout=subprocess.PIPE, |
| 90 | env=subprocess_environment) |
| 91 | if back_end_status.returncode != 0: |
| 92 | return back_end_status.returncode |
| 93 | output_file = path.join(flags.output_path[0], flags.input_file[0]) + ".h" |
| 94 | os.makedirs(path.dirname(output_file), exist_ok=True) |
| 95 | with open(output_file, "wb") as output: |
| 96 | output.write(back_end_status.stdout) |
| 97 | return 0 |
Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 101 | sys.exit(main(sys.argv)) |