-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc][hdrgen] Allow to treat hdrgen Python code as a Python module. #128955
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| # See https://llvm.org/LICENSE.txt for license information. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
File renamed without changes.
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
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
File renamed without changes.
File renamed without changes.
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 |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # ===- Generate headers for libc functions ------------------*- python -*--==# | ||
| # | ||
| # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| # See https://llvm.org/LICENSE.txt for license information. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| # | ||
| # ==------------------------------------------------------------------------==# | ||
|
|
||
| import argparse | ||
| import json | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from hdrgen.header import HeaderFile | ||
| from hdrgen.yaml_to_classes import load_yaml_file, fill_public_api | ||
|
|
||
|
|
||
| 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", | ||
| metavar="FILE", | ||
| type=Path, | ||
| nargs="+", | ||
| ) | ||
| parser.add_argument( | ||
| "-o", | ||
| "--output", | ||
| help="Path to write generated header file", | ||
| type=Path, | ||
| required=True, | ||
| ) | ||
| parser.add_argument( | ||
| "--json", | ||
| help="Write JSON instead of a header, can use multiple YAML files", | ||
| action="store_true", | ||
| ) | ||
| parser.add_argument( | ||
| "--depfile", | ||
| help="Path to write a depfile", | ||
| type=Path, | ||
| ) | ||
| parser.add_argument( | ||
| "--write-if-changed", | ||
| help="Write the output file only if its contents have changed", | ||
| action="store_true", | ||
| default=False, | ||
| ) | ||
| parser.add_argument( | ||
| "-e", | ||
| "--entry-point", | ||
| help="Entry point to include; may be given many times", | ||
| metavar="SYMBOL", | ||
| action="append", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| if not args.json and len(args.yaml_file) != 1: | ||
| print("Only one YAML file at a time without --json", file=sys.stderr) | ||
| parser.print_usage(sys.stderr) | ||
| return 2 | ||
|
|
||
| files_read = set() | ||
|
|
||
| def write_depfile(): | ||
| if not args.depfile: | ||
| return | ||
| deps = " ".join(str(f) for f in sorted(files_read)) | ||
| args.depfile.parent.mkdir(parents=True, exist_ok=True) | ||
| with open(args.depfile, "w") as depfile: | ||
| depfile.write(f"{args.output}: {deps}\n") | ||
|
|
||
| def load_yaml(path): | ||
| files_read.add(path) | ||
| return load_yaml_file(path, HeaderFile, args.entry_point) | ||
|
|
||
| def load_header(yaml_file): | ||
| merge_from_files = dict() | ||
|
|
||
| def merge_from(paths): | ||
| for path in paths: | ||
| # Load each file exactly once, in case of redundant merges. | ||
| if path in merge_from_files: | ||
| continue | ||
| header = load_yaml(path) | ||
| merge_from_files[path] = header | ||
| merge_from(path.parent / f for f in header.merge_yaml_files) | ||
|
|
||
| # Load the main file first. | ||
| header = load_yaml(yaml_file) | ||
|
|
||
| # Now load all the merge_yaml_files, and transitive merge_yaml_files. | ||
| merge_from(yaml_file.parent / f for f in header.merge_yaml_files) | ||
|
|
||
| # Merge in all those files' contents. | ||
| for merge_from_path, merge_from_header in merge_from_files.items(): | ||
| if merge_from_header.name is not None: | ||
| print( | ||
| f"{merge_from_path!s}: Merge file cannot have header field", | ||
| file=sys.stderr, | ||
| ) | ||
| return 2 | ||
| header.merge(merge_from_header) | ||
|
|
||
| return header | ||
|
|
||
| if args.json: | ||
| contents = json.dumps( | ||
| [load_header(file).json_data() for file in args.yaml_file], | ||
| indent=2, | ||
| ) | ||
| else: | ||
| [yaml_file] = args.yaml_file | ||
| header = load_header(yaml_file) | ||
| # The header_template path is relative to the containing YAML file. | ||
| template = header.template(yaml_file.parent, files_read) | ||
| contents = fill_public_api(header.public_api(), template) | ||
|
|
||
| write_depfile() | ||
|
|
||
| if ( | ||
| not args.write_if_changed | ||
| or not args.output.exists() | ||
| or args.output.read_text() != contents | ||
| ): | ||
| args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| args.output.write_text(contents) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.