xref: /aosp_15_r20/external/emboss/embossc (revision 99e0aae7469b87d12f0ad23e61142c2d74c1ef70)
1#!/usr/bin/env 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
17"""Main driver program for the Emboss compiler."""
18
19import argparse
20import os
21import sys
22
23
24def _parse_args(argv):
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=".",
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'""")
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""")
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:])
63
64
65def main(argv):
66  flags = _parse_args(argv)
67  base_path = os.path.dirname(__file__) or "."
68  sys.path.append(base_path)
69
70  from compiler.back_end.cpp import ( # pylint:disable=import-outside-toplevel
71    emboss_codegen_cpp, header_generator
72  )
73  from compiler.front_end import ( # pylint:disable=import-outside-toplevel
74    emboss_front_end
75  )
76
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
83  config = header_generator.Config(include_enum_traits=flags.cc_enum_traits)
84  header, errors = emboss_codegen_cpp.generate_headers_and_log_errors(
85      ir, flags.color_output, config)
86
87  if errors:
88    return 1
89
90  if flags.output_file:
91    output_file = flags.output_file[0]
92  else:
93    output_file = flags.input_file[0] + ".h"
94
95  output_filepath = os.path.join(flags.output_path[0], output_file)
96  os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
97
98  with open(output_filepath, "w") as output:
99    output.write(header)
100  return 0
101
102
103if __name__ == "__main__":
104  sys.exit(main(sys.argv))
105