Skip to content

Commit 2e6d451

Browse files
authored
[libc] Use idiomatic main() function in newhdrgen/yaml_to_classes.py (llvm#113419)
This changes the entry-point Python script of newhdrgen to use the idiomatic main() function of no arguments that does the whole job of the script. This replaces the unusual pattern of having an idiosyncratic main(...) signature and having the script's direct code do argument parsing outside the main function. The idiomatic pattern makes it possible to usefully wrap the script in Python and still use its full command-line functionality.
1 parent a9d51f0 commit 2e6d451

File tree

1 file changed

+29
-54
lines changed

1 file changed

+29
-54
lines changed

libc/newhdrgen/yaml_to_classes.py

+29-54
Original file line numberDiff line numberDiff line change
@@ -216,52 +216,7 @@ def increase_indent(self, flow=False, indentless=False):
216216
print(f"Added function {new_function.name} to {yaml_file}")
217217

218218

219-
def main(
220-
yaml_file,
221-
output_dir=None,
222-
h_def_file=None,
223-
add_function=None,
224-
entry_points=None,
225-
export_decls=False,
226-
):
227-
"""
228-
Main function to generate header files from YAML and .h.def templates.
229-
230-
Args:
231-
yaml_file: Path to the YAML file containing header specification.
232-
h_def_file: Path to the .h.def template file.
233-
output_dir: Directory to output the generated header file.
234-
add_function: Details of the function to be added to the YAML file (if any).
235-
entry_points: A list of specific function names to include in the header.
236-
export_decls: Flag to use GpuHeader for exporting declarations.
237-
"""
238-
if add_function:
239-
add_function_to_yaml(yaml_file, add_function)
240-
241-
header_class = GpuHeader if export_decls else HeaderFile
242-
header = load_yaml_file(yaml_file, header_class, entry_points)
243-
244-
header_str = str(header)
245-
246-
if output_dir:
247-
output_file_path = Path(output_dir)
248-
if output_file_path.is_dir():
249-
output_file_path /= f"{Path(yaml_file).stem}.h"
250-
else:
251-
output_file_path = Path(f"{Path(yaml_file).stem}.h")
252-
253-
if not export_decls and h_def_file:
254-
with open(h_def_file, "r") as f:
255-
h_def_content = f.read()
256-
final_header_content = fill_public_api(header_str, h_def_content)
257-
with open(output_file_path, "w") as f:
258-
f.write(final_header_content)
259-
else:
260-
with open(output_file_path, "w") as f:
261-
f.write(header_str)
262-
263-
264-
if __name__ == "__main__":
219+
def main():
265220
parser = argparse.ArgumentParser(description="Generate header files from YAML")
266221
parser.add_argument(
267222
"yaml_file", help="Path to the YAML file containing header specification"
@@ -297,11 +252,31 @@ def main(
297252
)
298253
args = parser.parse_args()
299254

300-
main(
301-
args.yaml_file,
302-
args.output_dir,
303-
args.h_def_file,
304-
args.add_function,
305-
args.entry_points,
306-
args.export_decls,
307-
)
255+
if args.add_function:
256+
add_function_to_yaml(yaml_file, args.add_function)
257+
258+
header_class = GpuHeader if args.export_decls else HeaderFile
259+
header = load_yaml_file(args.yaml_file, header_class, args.entry_points)
260+
261+
header_str = str(header)
262+
263+
if args.output_dir:
264+
output_file_path = Path(args.output_dir)
265+
if output_file_path.is_dir():
266+
output_file_path /= f"{Path(args.yaml_file).stem}.h"
267+
else:
268+
output_file_path = Path(f"{Path(args.yaml_file).stem}.h")
269+
270+
if not args.export_decls and args.h_def_file:
271+
with open(args.h_def_file, "r") as f:
272+
h_def_content = f.read()
273+
final_header_content = fill_public_api(header_str, h_def_content)
274+
with open(output_file_path, "w") as f:
275+
f.write(final_header_content)
276+
else:
277+
with open(output_file_path, "w") as f:
278+
f.write(header_str)
279+
280+
281+
if __name__ == "__main__":
282+
main()

0 commit comments

Comments
 (0)