| #!/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. |
| |
| import argparse |
| import os |
| from os import path |
| 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 = path.dirname(__file__) or "." |
| subprocess_environment = {k: v for k, v in os.environ.items()} |
| 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, |
| path.join(base_path, "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, |
| path.join(base_path, "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 = path.join(flags.output_path[0], flags.input_file[0]) + ".h" |
| os.makedirs(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)) |