Ali Saeed | e947463 | 2022-10-04 12:13:22 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 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 |
Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 21 | import sys |
| 22 | |
Eric Rahm | cbea20f | 2024-04-08 15:07:04 -0700 | [diff] [blame] | 23 | |
Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 24 | def _parse_args(argv): |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 25 | parser = argparse.ArgumentParser(description="Emboss compiler") |
| 26 | parser.add_argument("--color-output", |
| 27 | default="if_tty", |
| 28 | choices=["always", "never", "if_tty", "auto"], |
| 29 | help="Print error messages using color. 'auto' is a " |
| 30 | "synonym for 'if_tty'.") |
| 31 | parser.add_argument("--import-dir", "-I", |
| 32 | dest="import_dirs", |
| 33 | action="append", |
| 34 | default=["."], |
| 35 | help="A directory to use when searching for imported " |
| 36 | "embs. If no import-dirs are specified, the " |
| 37 | "current directory will be used.") |
| 38 | parser.add_argument("--generate", |
| 39 | nargs=1, |
| 40 | choices=["cc"], |
| 41 | default="cc", |
| 42 | help="Which back end to use. Currently only C++ is " |
| 43 | "supported.") |
| 44 | parser.add_argument("--output-path", |
| 45 | nargs=1, |
| 46 | default=".", |
David Rees | 621896d | 2024-02-27 16:18:24 -0800 | [diff] [blame] | 47 | help="""Prefix path to use for the generated output file. |
| 48 | Defaults to '.'""") |
| 49 | parser.add_argument("--output-file", |
| 50 | nargs=1, |
| 51 | help="""File name to be used for the generated output |
| 52 | file. Defaults to input_file suffixed by '.h'""") |
Eric Rahm | cbea20f | 2024-04-08 15:07:04 -0700 | [diff] [blame] | 53 | parser.add_argument("--cc-enum-traits", |
| 54 | action=argparse.BooleanOptionalAction, |
| 55 | default=True, |
| 56 | help="""Controls generation of EnumTraits by the C++ |
| 57 | backend""") |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 58 | parser.add_argument("input_file", |
| 59 | type=str, |
| 60 | nargs=1, |
| 61 | help=".emb file to compile.") |
| 62 | return parser.parse_args(argv[1:]) |
Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def main(argv): |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 66 | flags = _parse_args(argv) |
Faraaz Sareshwala | 1c7f19e | 2022-07-07 11:43:48 -0700 | [diff] [blame] | 67 | base_path = os.path.dirname(__file__) or "." |
Eric Rahm | 35bf3a0 | 2024-04-03 10:49:18 -0700 | [diff] [blame] | 68 | sys.path.append(base_path) |
Faraaz Sareshwala | 17874c0 | 2022-07-07 11:48:17 -0700 | [diff] [blame] | 69 | |
Eric Rahm | 35bf3a0 | 2024-04-03 10:49:18 -0700 | [diff] [blame] | 70 | from compiler.back_end.cpp import ( # pylint:disable=import-outside-toplevel |
Eric Rahm | cbea20f | 2024-04-08 15:07:04 -0700 | [diff] [blame] | 71 | emboss_codegen_cpp, header_generator |
Eric Rahm | 35bf3a0 | 2024-04-03 10:49:18 -0700 | [diff] [blame] | 72 | ) |
| 73 | from compiler.front_end import ( # pylint:disable=import-outside-toplevel |
| 74 | emboss_front_end |
Faraaz Sareshwala | 17874c0 | 2022-07-07 11:48:17 -0700 | [diff] [blame] | 75 | ) |
| 76 | |
Eric Rahm | 35bf3a0 | 2024-04-03 10:49:18 -0700 | [diff] [blame] | 77 | ir, _, errors = emboss_front_end.parse_and_log_errors( |
| 78 | flags.input_file[0], flags.import_dirs, flags.color_output) |
| 79 | |
| 80 | if errors: |
| 81 | return 1 |
| 82 | |
Eric Rahm | cbea20f | 2024-04-08 15:07:04 -0700 | [diff] [blame] | 83 | config = header_generator.Config(include_enum_traits=flags.cc_enum_traits) |
Eric Rahm | 35bf3a0 | 2024-04-03 10:49:18 -0700 | [diff] [blame] | 84 | header, errors = emboss_codegen_cpp.generate_headers_and_log_errors( |
Eric Rahm | cbea20f | 2024-04-08 15:07:04 -0700 | [diff] [blame] | 85 | ir, flags.color_output, config) |
Eric Rahm | 35bf3a0 | 2024-04-03 10:49:18 -0700 | [diff] [blame] | 86 | |
| 87 | if errors: |
| 88 | return 1 |
Faraaz Sareshwala | 17874c0 | 2022-07-07 11:48:17 -0700 | [diff] [blame] | 89 | |
David Rees | 621896d | 2024-02-27 16:18:24 -0800 | [diff] [blame] | 90 | if flags.output_file: |
| 91 | output_file = flags.output_file[0] |
| 92 | else: |
| 93 | output_file = flags.input_file[0] + ".h" |
Faraaz Sareshwala | 17874c0 | 2022-07-07 11:48:17 -0700 | [diff] [blame] | 94 | |
David Rees | 621896d | 2024-02-27 16:18:24 -0800 | [diff] [blame] | 95 | output_filepath = os.path.join(flags.output_path[0], output_file) |
| 96 | os.makedirs(os.path.dirname(output_filepath), exist_ok=True) |
| 97 | |
Eric Rahm | 35bf3a0 | 2024-04-03 10:49:18 -0700 | [diff] [blame] | 98 | with open(output_filepath, "w") as output: |
| 99 | output.write(header) |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 100 | return 0 |
Ben Olmstead | c0d7784 | 2019-07-31 17:34:05 -0700 | [diff] [blame] | 101 | |
| 102 | |
| 103 | if __name__ == "__main__": |
Ben Olmstead | 79dde14 | 2019-09-23 18:37:26 -0700 | [diff] [blame] | 104 | sys.exit(main(sys.argv)) |