blob: c32cd5809260a1d4a1a5bf30d24ee7f692ad5149 [file] [log] [blame]
Ben Olmsteadc0d77842019-07-31 17:34:05 -07001#!/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 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
21from os import path
22import subprocess
23import sys
24
25def _parse_args(argv):
Ben Olmstead79dde142019-09-23 18:37:26 -070026 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 Olmsteadc0d77842019-07-31 17:34:05 -070054
55
56def main(argv):
Ben Olmstead79dde142019-09-23 18:37:26 -070057 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,
reventlov6731fc42019-10-03 15:23:13 -070067 path.join(base_path, "compiler", "front_end", "emboss_front_end.py"),
Ben Olmstead79dde142019-09-23 18:37:26 -070068 "--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,
reventlov6731fc42019-10-03 15:23:13 -070085 path.join(base_path, "compiler", "back_end", "cpp",
86 "emboss_codegen_cpp.py"),
Ben Olmstead79dde142019-09-23 18:37:26 -070087 ],
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 Olmsteadc0d77842019-07-31 17:34:05 -070098
99
100if __name__ == "__main__":
Ben Olmstead79dde142019-09-23 18:37:26 -0700101 sys.exit(main(sys.argv))