-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc] Add some integration features into newhdrgen #114272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e5a892
[libc] Add some integration features into newhdrgen
frobtech 712bbb8
Restore old behavior for test suite with explicit YAML file arg
frobtech 15c13eb
python formatting
frobtech 1f75c67
Merge branch 'main' into p/libc-newhdrgen
frobtech 8475db2
Merge remote-tracking branch 'origin/main' into p/libc-newhdrgen
frobtech 44040ef
Merge remote-tracking branch 'fork/p/libc-newhdrgen' into p/libc-newh…
frobtech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,15 +219,36 @@ def increase_indent(self, flow=False, indentless=False): | |
| def main(): | ||
| parser = argparse.ArgumentParser(description="Generate header files from YAML") | ||
| parser.add_argument( | ||
| "yaml_file", help="Path to the YAML file containing header specification" | ||
| "yaml_file", | ||
| help="Path to the YAML file containing header specification", | ||
| type=Path, | ||
| nargs="?", | ||
| ) | ||
| parser.add_argument( | ||
| "--output_dir", | ||
| help="Directory to output the generated header file", | ||
| type=Path, | ||
| ) | ||
| parser.add_argument( | ||
| "--h_def_file", | ||
| help="Path to the .h.def template file (required if not using --export_decls)", | ||
| type=Path, | ||
| ) | ||
| parser.add_argument( | ||
| "--libc-dir", | ||
| help="Path to llvm-libc source tree", | ||
| type=Path, | ||
| default=Path(__file__).parents[1], | ||
| ) | ||
| parser.add_argument( | ||
| "--depfile", | ||
| help="Path to write a depfile", | ||
| type=argparse.FileType("w"), | ||
| ) | ||
| parser.add_argument( | ||
| "--write-if-changed", | ||
| help="Don't touch the output .h file if it's unchanged", | ||
| action="store_true", | ||
| ) | ||
| parser.add_argument( | ||
| "--add_function", | ||
|
|
@@ -252,30 +273,62 @@ def main(): | |
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| def write_to_file(path, contents): | ||
| if ( | ||
| not args.write_if_changed | ||
| or not path.exists() | ||
| or path.read_text() != contents | ||
| ): | ||
| path.write_text(contents) | ||
|
|
||
| yaml_file = args.yaml_file | ||
| if args.h_def_file and not yaml_file: | ||
| libc_include_dir = args.libc_dir / "include" | ||
| libc_yaml_dir = args.libc_dir / "newhdrgen" / "yaml" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to be updated since headergen has been moved to the hdrgen directory. |
||
| yaml_file = libc_yaml_dir / args.h_def_file.with_suffix("").with_suffix( | ||
| ".yaml" | ||
| ).relative_to(libc_include_dir) | ||
|
|
||
| if args.output_dir: | ||
| output_file_path = args.output_dir | ||
| if output_file_path.is_dir(): | ||
| if not args.yaml_file: | ||
| libc_yaml_dir = args.libc_dir / "newhdrgen" / "yaml" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also needs to be updated. |
||
| output_file_path /= args.yaml_file.relative_to( | ||
| libc_yaml_dir | ||
| ).with_suffix(".h") | ||
| else: | ||
| output_file_path /= f"{Path(args.yaml_file).stem}.h" | ||
| output_file_path.parent.mkdir(parents=True, exist_ok=True) | ||
| else: | ||
| output_file_path = args.yaml_file.with_suffix(".h") | ||
|
|
||
| if args.depfile and args.h_def_file: | ||
| args.depfile.write(f"{output_file_path}: {args.h_def_file} {args.yaml_file}\n") | ||
| args.depfile.close() | ||
|
|
||
| if args.add_function: | ||
| add_function_to_yaml(args.yaml_file, args.add_function) | ||
|
|
||
| header_class = GpuHeader if args.export_decls else HeaderFile | ||
| header = load_yaml_file(args.yaml_file, header_class, args.entry_points) | ||
|
|
||
| if args.add_function: | ||
| add_function_to_yaml(yaml_file, args.add_function) | ||
|
|
||
| header_str = str(header) | ||
| header_class = GpuHeader if args.export_decls else HeaderFile | ||
| header = load_yaml_file(args.yaml_file, header_class, args.entry_points) | ||
|
|
||
| if args.output_dir: | ||
| output_file_path = Path(args.output_dir) | ||
| if output_file_path.is_dir(): | ||
| output_file_path /= f"{Path(args.yaml_file).stem}.h" | ||
| else: | ||
| output_file_path = Path(f"{Path(args.yaml_file).stem}.h") | ||
| header_str = str(header) | ||
|
|
||
| if not args.export_decls and args.h_def_file: | ||
| with open(args.h_def_file, "r") as f: | ||
| h_def_content = f.read() | ||
| final_header_content = fill_public_api(header_str, h_def_content) | ||
| with open(output_file_path, "w") as f: | ||
| f.write(final_header_content) | ||
| else: | ||
| with open(output_file_path, "w") as f: | ||
| f.write(header_str) | ||
| final_header_content = str | ||
| write_to_file(output_file_path, final_header_content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given that this is only used in one place at the moment, would it make more sense to inline this function?