blob: 7bc022720439106dcd814a2578d531f04793ac77 [file] [log] [blame]
Ali Saeede9474632022-10-04 12:13:22 -07001#!/usr/bin/env python3
Ben Olmsteadc0d77842019-07-31 17:34:05 -07002
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 Olmstead79dde142019-09-23 18:37:26 -070017"""Main driver program for the Emboss compiler."""
18
Ben Olmsteadc0d77842019-07-31 17:34:05 -070019import argparse
20import os
Ben Olmsteadc0d77842019-07-31 17:34:05 -070021import sys
22
Eric Rahmcbea20f2024-04-08 15:07:04 -070023
Ben Olmsteadc0d77842019-07-31 17:34:05 -070024def _parse_args(argv):
Ben Olmstead79dde142019-09-23 18:37:26 -070025 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 Rees621896d2024-02-27 16:18:24 -080047 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 Rahmcbea20f2024-04-08 15:07:04 -070053 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 Olmstead79dde142019-09-23 18:37:26 -070058 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 Olmsteadc0d77842019-07-31 17:34:05 -070063
64
65def main(argv):
Ben Olmstead79dde142019-09-23 18:37:26 -070066 flags = _parse_args(argv)
Faraaz Sareshwala1c7f19e2022-07-07 11:43:48 -070067 base_path = os.path.dirname(__file__) or "."
Eric Rahm35bf3a02024-04-03 10:49:18 -070068 sys.path.append(base_path)
Faraaz Sareshwala17874c02022-07-07 11:48:17 -070069
Eric Rahm35bf3a02024-04-03 10:49:18 -070070 from compiler.back_end.cpp import ( # pylint:disable=import-outside-toplevel
Eric Rahmcbea20f2024-04-08 15:07:04 -070071 emboss_codegen_cpp, header_generator
Eric Rahm35bf3a02024-04-03 10:49:18 -070072 )
73 from compiler.front_end import ( # pylint:disable=import-outside-toplevel
74 emboss_front_end
Faraaz Sareshwala17874c02022-07-07 11:48:17 -070075 )
76
Eric Rahm35bf3a02024-04-03 10:49:18 -070077 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 Rahmcbea20f2024-04-08 15:07:04 -070083 config = header_generator.Config(include_enum_traits=flags.cc_enum_traits)
Eric Rahm35bf3a02024-04-03 10:49:18 -070084 header, errors = emboss_codegen_cpp.generate_headers_and_log_errors(
Eric Rahmcbea20f2024-04-08 15:07:04 -070085 ir, flags.color_output, config)
Eric Rahm35bf3a02024-04-03 10:49:18 -070086
87 if errors:
88 return 1
Faraaz Sareshwala17874c02022-07-07 11:48:17 -070089
David Rees621896d2024-02-27 16:18:24 -080090 if flags.output_file:
91 output_file = flags.output_file[0]
92 else:
93 output_file = flags.input_file[0] + ".h"
Faraaz Sareshwala17874c02022-07-07 11:48:17 -070094
David Rees621896d2024-02-27 16:18:24 -080095 output_filepath = os.path.join(flags.output_path[0], output_file)
96 os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
97
Eric Rahm35bf3a02024-04-03 10:49:18 -070098 with open(output_filepath, "w") as output:
99 output.write(header)
Ben Olmstead79dde142019-09-23 18:37:26 -0700100 return 0
Ben Olmsteadc0d77842019-07-31 17:34:05 -0700101
102
103if __name__ == "__main__":
Ben Olmstead79dde142019-09-23 18:37:26 -0700104 sys.exit(main(sys.argv))