|
4 | 4 | # found in the LICENSE file. |
5 | 5 |
|
6 | 6 | import argparse |
7 | | -import os |
8 | | -import string |
9 | | -import sys |
10 | | - |
11 | | - |
12 | | -def read_libraries(libraries_path): |
13 | | - with open(libraries_path) as f: |
14 | | - lines = f.readlines() |
15 | | - return [l.rstrip("\n") for l in lines] |
16 | | - |
17 | | - |
18 | | -def write_libraries(libraries_path, libraries): |
19 | | - directory = os.path.dirname(libraries_path) |
20 | | - if not os.path.exists(directory): |
21 | | - os.makedirs(directory) |
22 | | - with open(libraries_path, "w+") as f: |
23 | | - for library in libraries: |
24 | | - f.write(library) |
25 | | - f.write("\n") |
| 7 | +from pathlib import Path |
26 | 8 |
|
27 | 9 |
|
28 | 10 | def main(): |
29 | 11 | parser = argparse.ArgumentParser( |
30 | | - description="Generate response file for FIDL frontend" |
| 12 | + description="Generate response file for FIDL frontend. " |
| 13 | + "Arguments not mentioned here are forwarded as is to fidlc." |
31 | 14 | ) |
32 | 15 | parser.add_argument( |
33 | 16 | "--out-response-file", |
34 | | - help="The path for the response file to generate", |
| 17 | + help="The path for for the response file to generate", |
| 18 | + type=Path, |
35 | 19 | required=True |
36 | 20 | ) |
37 | 21 | parser.add_argument( |
38 | 22 | "--out-libraries", |
39 | | - help="The path for the libraries file to generate", |
| 23 | + help="The path for for the libraries file to generate", |
| 24 | + type=Path, |
40 | 25 | required=True |
41 | 26 | ) |
42 | 27 | parser.add_argument( |
43 | | - "--json", help="The path for the JSON file to generate, if any" |
44 | | - ) |
45 | | - parser.add_argument( |
46 | | - "--tables", help="The path for the tables file to generate, if any" |
47 | | - ) |
48 | | - parser.add_argument( |
49 | | - "--deprecated-fuchsia-only-c-client", |
50 | | - help="The path for the C simple client file to generate, if any" |
| 28 | + "--sources", help="List of FIDL source files", nargs="+", required=True |
51 | 29 | ) |
52 | | - parser.add_argument( |
53 | | - "--deprecated-fuchsia-only-c-header", |
54 | | - help="The path for the C header file to generate, if any" |
55 | | - ) |
56 | | - parser.add_argument( |
57 | | - "--deprecated-fuchsia-only-c-server", |
58 | | - help="The path for the C simple server file to generate, if any" |
59 | | - ) |
60 | | - parser.add_argument( |
61 | | - "--name", help="The name for the generated FIDL library, if any" |
62 | | - ) |
63 | | - parser.add_argument( |
64 | | - "--depfile", help="The name for the generated depfile, if any" |
65 | | - ) |
66 | | - parser.add_argument("--sources", help="List of FIDL source files", nargs="*") |
67 | 30 | parser.add_argument( |
68 | 31 | "--dep-libraries", help="List of dependent libraries", nargs="*" |
69 | 32 | ) |
70 | | - parser.add_argument( |
71 | | - "--experimental-flag", help="List of experimental flags", action="append" |
72 | | - ) |
73 | | - args = parser.parse_args() |
74 | | - |
75 | | - target_libraries = [] |
76 | | - |
77 | | - for dep_libraries_path in args.dep_libraries or []: |
78 | | - dep_libraries = read_libraries(dep_libraries_path) |
79 | | - for library in dep_libraries: |
80 | | - if library in target_libraries: |
81 | | - continue |
82 | | - target_libraries.append(library) |
83 | | - |
84 | | - target_libraries.append(" ".join(sorted(args.sources))) |
85 | | - write_libraries(args.out_libraries, target_libraries) |
86 | | - |
87 | | - response_file = [] |
88 | | - |
89 | | - if args.json: |
90 | | - response_file.append("--json %s" % args.json) |
91 | | - |
92 | | - if args.tables: |
93 | | - response_file.append("--tables %s" % args.tables) |
94 | | - |
95 | | - if args.deprecated_fuchsia_only_c_client: |
96 | | - response_file.append( |
97 | | - "--deprecated-fuchsia-only-c-client %s" % |
98 | | - args.deprecated_fuchsia_only_c_client |
99 | | - ) |
100 | | - |
101 | | - if args.deprecated_fuchsia_only_c_header: |
102 | | - response_file.append( |
103 | | - "--deprecated-fuchsia-only-c-header %s" % |
104 | | - args.deprecated_fuchsia_only_c_header |
105 | | - ) |
106 | | - |
107 | | - if args.deprecated_fuchsia_only_c_server: |
108 | | - response_file.append( |
109 | | - "--deprecated-fuchsia-only-c-server %s" % |
110 | | - args.deprecated_fuchsia_only_c_server |
111 | | - ) |
112 | | - |
113 | | - if args.name: |
114 | | - response_file.append("--name %s" % args.name) |
115 | | - |
116 | | - if args.depfile: |
117 | | - response_file.append("--depfile %s" % args.depfile) |
| 33 | + args, args_to_forward = parser.parse_known_args() |
118 | 34 |
|
119 | | - if args.experimental_flag: |
120 | | - for experimental_flag in args.experimental_flag: |
121 | | - response_file.append("--experimental %s" % experimental_flag) |
| 35 | + # Each line contains a library's source files separated by spaces. |
| 36 | + # We use a dict instead of a set to maintain insertion order. |
| 37 | + dep_lines = {} |
| 38 | + for path in args.dep_libraries or []: |
| 39 | + with open(path) as f: |
| 40 | + for line in f: |
| 41 | + dep_lines[line.rstrip()] = True |
| 42 | + libraries = list(dep_lines) |
| 43 | + libraries.append(" ".join(sorted(args.sources))) |
122 | 44 |
|
123 | | - response_file.extend(["--files %s" % library for library in target_libraries]) |
| 45 | + args.out_libraries.parent.mkdir(parents=True, exist_ok=True) |
| 46 | + with open(args.out_libraries, "w") as f: |
| 47 | + print("\n".join(libraries), file=f) |
124 | 48 |
|
125 | | - with open(args.out_response_file, "w+") as f: |
126 | | - f.write(" ".join(response_file)) |
127 | | - f.write("\n") |
| 49 | + args.out_response_file.parent.mkdir(parents=True, exist_ok=True) |
| 50 | + with open(args.out_response_file, "w") as f: |
| 51 | + fidlc_args = args_to_forward + ["--files " + line for line in libraries] |
| 52 | + print(" ".join(fidlc_args), file=f) |
128 | 53 |
|
129 | 54 |
|
130 | 55 | if __name__ == "__main__": |
131 | | - sys.exit(main()) |
| 56 | + main() |
0 commit comments