Created
April 2, 2024 18:50
-
-
Save EricRahm/f1dbeb48ae43b3b869d85d0a70bbd3d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright 2024 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. | |
"""Script that runs the Emboss compiler in a way that's easy to profile.""" | |
import argparse | |
import os | |
import sys | |
from compiler.front_end import emboss_front_end | |
from compiler.back_end.cpp import emboss_codegen_cpp | |
def _parse_args(argv): | |
parser = argparse.ArgumentParser(description="Emboss compiler") | |
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("--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.") | |
parser.add_argument("--front-end-only", action="store_true") | |
parser.add_argument("--back-end-only", action="store_true") | |
return parser.parse_args(argv[1:]) | |
def main(argv): | |
status = 0 | |
flags = _parse_args(argv) | |
base_path = os.path.dirname(__file__) or "." | |
sys.path.append(base_path) | |
sys.path.append(os.path.join(base_path, "compiler", "util")) | |
ir_file = os.path.join(flags.output_path[0], flags.input_file[0]) + ".ir" | |
os.makedirs(os.path.dirname(ir_file), exist_ok=True) | |
if not flags.back_end_only: | |
front_end_args = [ | |
os.path.join(base_path, "compiler", "front_end", "emboss_front_end.py"), | |
"--output-file", ir_file | |
] | |
for import_dir in flags.import_dirs: | |
front_end_args.extend(["--import-dir", import_dir]) | |
front_end_args.append(flags.input_file[0]) | |
print(front_end_args) | |
status = emboss_front_end.main( | |
emboss_front_end._parse_command_line(front_end_args)) | |
if status != 0: | |
return status | |
if not flags.front_end_only: | |
output_file = os.path.join(flags.output_path[0], flags.input_file[0]) + ".h" | |
back_end_args = [ | |
os.path.join(base_path, "compiler", "back_end", "cpp", | |
"emboss_codegen_cpp.py"), | |
"--input-file", ir_file, | |
"--output-file", output_file | |
] | |
status = emboss_codegen_cpp.main( | |
emboss_codegen_cpp._parse_command_line(back_end_args)) | |
return status | |
if __name__ == "__main__": | |
sys.exit(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment