1#!/usr/bin/env python3 2 3# 4# Copyright (C) 2020 The Android Open Source Project 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import argparse 20import collections 21import os 22import pathlib 23import sys 24 25class RustModule: 26 def __init__(self): 27 self.files = [] 28 self.nested = collections.defaultdict(RustModule) 29 30 def emit(self, output_file, indent=""): 31 for (input_name, input_path) in self.files: 32 output_file.write(indent) 33 output_file.write("pub mod %s {\n" % input_name) 34 # Copy the contents of the input file into the output 35 with open(input_path, "r") as input_file: 36 for l in input_file: 37 output_file.write(indent) 38 output_file.write(" ") 39 output_file.write(l) 40 41 output_file.write(indent) 42 output_file.write("}\n") 43 44 for name, mod in self.nested.items(): 45 output_file.write(indent) 46 output_file.write("pub mod %s {\n" % name) 47 mod.emit(output_file, indent + " ") 48 output_file.write(indent) 49 output_file.write("}\n") 50 51 def emit_mangled(self, output_file, indent="", prefix=""): 52 for (input_name, _) in self.files: 53 output_file.write(indent) 54 output_file.write("pub use %s::%s::mangled::*;\n" % (prefix, input_name)) 55 for name, mod in self.nested.items(): 56 new_prefix = prefix + "::" + name 57 mod.emit_mangled(output_file, indent, prefix=new_prefix) 58 59def main(output, root, inputs, imports): 60 root_module = RustModule() 61 for inp in inputs: 62 in_rel = os.path.relpath(inp, root) 63 in_path = pathlib.PurePath(in_rel) 64 65 node = root_module 66 for part in in_path.parts[:-1]: 67 node = node.nested[part] 68 69 if os.path.isfile(inp): 70 in_name, in_ext = os.path.splitext(in_path.parts[-1]) 71 node.files.append((in_name, inp)) 72 73 with open(output, "w") as lib_rs_file: 74 lib_rs_file.write("#![allow(non_snake_case)]\n") 75 lib_rs_file.write("#![allow(missing_docs)]\n") 76 lib_rs_file.write("#[deprecated(note = \"Please access via libbinder_rs binder::\")]\n") 77 lib_rs_file.write("pub use binder;\n") 78 79 lib_rs_file.write("pub mod aidl {\n") 80 root_module.emit(lib_rs_file, indent=" ") 81 lib_rs_file.write("}\n") 82 83 lib_rs_file.write("pub mod mangled {\n") 84 root_module.emit_mangled(lib_rs_file, indent=" ", prefix="super::aidl") 85 for imp in imports: 86 lib_rs_file.write(" pub(crate) use %s::mangled::*;\n" % imp) 87 lib_rs_file.write("}\n") 88 89def execute(): 90 parser = argparse.ArgumentParser(description='Generate the top-level lib.rs.', 91 fromfile_prefix_chars='@') 92 parser.add_argument('output', help='Path to output .rs file') 93 parser.add_argument('root', help='Common ancestor of all input files') 94 parser.add_argument('inputs', nargs='+', help='Input .rs files') 95 parser.add_argument('-I', '--import', action='append', dest='imports', 96 default=[], help='Crates to import') 97 98 args = parser.parse_args() 99 if args is None: 100 sys.exit(1) 101 102 sys.exit(main(args.output, args.root, args.inputs, args.imports)) 103 104if __name__ == "__main__": 105 execute() 106