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("pub use binder::public_api as binder;\n")
77
78    lib_rs_file.write("pub mod aidl {\n")
79    root_module.emit(lib_rs_file, indent="  ")
80    lib_rs_file.write("}\n")
81
82    lib_rs_file.write("pub mod mangled {\n")
83    root_module.emit_mangled(lib_rs_file, indent="  ", prefix="super::aidl")
84    for imp in imports:
85      lib_rs_file.write("  pub(crate) use %s::mangled::*;\n" % imp)
86    lib_rs_file.write("}\n")
87
88if __name__ == "__main__":
89  parser = argparse.ArgumentParser(description='Generate the top-level lib.rs.')
90  parser.add_argument('output', help='Path to output .rs file')
91  parser.add_argument('root', help='Common ancestor of all input files')
92  parser.add_argument('inputs', nargs='+', help='Input .rs files')
93  parser.add_argument('-I', '--import', action='append', dest='imports',
94                      default=[], help='Crates to import')
95
96  args = parser.parse_args()
97  if args is None:
98    sys.exit(1)
99
100  sys.exit(main(args.output, args.root, args.inputs, args.imports))
101