-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
[libc] newheadergen: script adjusted for cmake #98825
Changes from all commits
be35f7d
37a73ec
2fe3f1e
17eb4f6
d1733b9
0a25e49
0d98c8e
0e4947a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python | ||
# | ||
# ===- GPU HeaderFile Class for --export-decls version --------*- 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 | ||
# | ||
# ==-------------------------------------------------------------------------==# | ||
|
||
|
||
class GpuHeaderFile: | ||
def __init__(self, name): | ||
self.name = name | ||
self.macros = [] | ||
self.types = [] | ||
self.enumerations = [] | ||
self.objects = [] | ||
self.functions = [] | ||
self.includes = [] | ||
|
||
def add_macro(self, macro): | ||
self.macros.append(macro) | ||
|
||
def add_type(self, type_): | ||
self.types.append(type_) | ||
|
||
def add_enumeration(self, enumeration): | ||
self.enumerations.append(enumeration) | ||
|
||
def add_object(self, object): | ||
self.objects.append(object) | ||
|
||
def add_function(self, function): | ||
self.functions.append(function) | ||
|
||
def __str__(self): | ||
content = [] | ||
|
||
content.append( | ||
f"//===-- C standard declarations for {self.name} ------------------------------===//" | ||
) | ||
content.append("//") | ||
content.append( | ||
"// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions." | ||
) | ||
content.append("// See https://llvm.org/LICENSE.txt for license information.") | ||
content.append("// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception") | ||
content.append("//") | ||
content.append( | ||
"//===----------------------------------------------------------------------===//\n" | ||
) | ||
|
||
header_guard = f"__LLVM_LIBC_DECLARATIONS_{self.name.upper()[:-2]}_H" | ||
content.append(f"#ifndef {header_guard}") | ||
content.append(f"#define {header_guard}\n") | ||
|
||
content.append("#ifndef __LIBC_ATTRS") | ||
content.append("#define __LIBC_ATTRS") | ||
content.append("#endif\n") | ||
|
||
content.append("#ifdef __cplusplus") | ||
content.append('extern "C" {') | ||
content.append("#endif\n") | ||
|
||
for function in self.functions: | ||
content.append(f"{function} __LIBC_ATTRS;\n") | ||
|
||
for object in self.objects: | ||
content.append(f"{object} __LIBC_ATTRS;\n") | ||
|
||
content.append("#ifdef __cplusplus") | ||
content.append("}") | ||
content.append("#endif\n") | ||
|
||
content.append(f"#endif") | ||
|
||
return "\n".join(content) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
header: features.h | ||
standards: | ||
- stdc | ||
macros: [] | ||
types: [] | ||
enums: [] | ||
objects: [] | ||
functions: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,11 @@ | |
# | ||
# ==-------------------------------------------------------------------------==# | ||
|
||
|
||
import yaml | ||
import argparse | ||
|
||
from pathlib import Path | ||
from header import HeaderFile | ||
from gpu_headers import GpuHeaderFile as GpuHeader | ||
from class_implementation.classes.macro import Macro | ||
from class_implementation.classes.type import Type | ||
from class_implementation.classes.function import Function | ||
|
@@ -22,18 +21,20 @@ | |
from class_implementation.classes.object import Object | ||
|
||
|
||
def yaml_to_classes(yaml_data): | ||
def yaml_to_classes(yaml_data, header_class, entry_points=None): | ||
""" | ||
Convert YAML data to header classes. | ||
|
||
Args: | ||
yaml_data: The YAML data containing header specifications. | ||
header_class: The class to use for creating the header. | ||
entry_points: A list of specific function names to include in the header. | ||
|
||
Returns: | ||
HeaderFile: An instance of HeaderFile populated with the data. | ||
""" | ||
header_name = yaml_data.get("header") | ||
header = HeaderFile(header_name) | ||
header = header_class(header_name) | ||
|
||
for macro_data in yaml_data.get("macros", []): | ||
header.add_macro(Macro(macro_data["macro_name"], macro_data["macro_value"])) | ||
|
@@ -49,12 +50,15 @@ def yaml_to_classes(yaml_data): | |
) | ||
|
||
functions = yaml_data.get("functions", []) | ||
if entry_points: | ||
entry_points_set = set(entry_points) | ||
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. why is this set made? 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. The line after when checking for the names, sets have a faster operation time for checking because we are just seeing if the func name is in the set rather than scanning through all of entry_points everytime |
||
functions = [f for f in functions if f["name"] in entry_points_set] | ||
sorted_functions = sorted(functions, key=lambda x: x["name"]) | ||
guards = [] | ||
guarded_function_dict = {} | ||
for function_data in sorted_functions: | ||
guard = function_data.get("guard", None) | ||
if guard == None: | ||
if guard is None: | ||
arguments = [arg["type"] for arg in function_data["arguments"]] | ||
attributes = function_data.get("attributes", None) | ||
standards = function_data.get("standards", None) | ||
|
@@ -105,19 +109,21 @@ def yaml_to_classes(yaml_data): | |
return header | ||
|
||
|
||
def load_yaml_file(yaml_file): | ||
def load_yaml_file(yaml_file, header_class, entry_points): | ||
""" | ||
Load YAML file and convert it to header classes. | ||
|
||
Args: | ||
yaml_file: The path to the YAML file. | ||
yaml_file: Path to the YAML file. | ||
header_class: The class to use for creating the header (HeaderFile or GpuHeader). | ||
entry_points: A list of specific function names to include in the header. | ||
|
||
Returns: | ||
HeaderFile: An instance of HeaderFile populated with the data from the YAML file. | ||
HeaderFile: An instance of HeaderFile populated with the data. | ||
""" | ||
with open(yaml_file, "r") as f: | ||
yaml_data = yaml.safe_load(f) | ||
return yaml_to_classes(yaml_data) | ||
return yaml_to_classes(yaml_data, header_class, entry_points) | ||
|
||
|
||
def fill_public_api(header_str, h_def_content): | ||
|
@@ -207,7 +213,14 @@ def increase_indent(self, flow=False, indentless=False): | |
print(f"Added function {new_function.name} to {yaml_file}") | ||
|
||
|
||
def main(yaml_file, h_def_file, output_dir, add_function=None): | ||
def main( | ||
yaml_file, | ||
output_dir=None, | ||
h_def_file=None, | ||
add_function=None, | ||
entry_points=None, | ||
export_decls=False, | ||
): | ||
""" | ||
Main function to generate header files from YAML and .h.def templates. | ||
|
||
|
@@ -216,41 +229,50 @@ def main(yaml_file, h_def_file, output_dir, add_function=None): | |
h_def_file: Path to the .h.def template file. | ||
output_dir: Directory to output the generated header file. | ||
add_function: Details of the function to be added to the YAML file (if any). | ||
entry_points: A list of specific function names to include in the header. | ||
export_decls: Flag to use GpuHeader for exporting declarations. | ||
""" | ||
|
||
if add_function: | ||
add_function_to_yaml(yaml_file, add_function) | ||
|
||
header = load_yaml_file(yaml_file) | ||
|
||
with open(h_def_file, "r") as f: | ||
h_def_content = f.read() | ||
header_class = GpuHeader if export_decls else HeaderFile | ||
header = load_yaml_file(yaml_file, header_class, entry_points) | ||
|
||
header_str = str(header) | ||
final_header_content = fill_public_api(header_str, h_def_content) | ||
|
||
output_file_name = Path(h_def_file).stem | ||
output_file_path = Path(output_dir) / output_file_name | ||
|
||
with open(output_file_path, "w") as f: | ||
f.write(final_header_content) | ||
if output_dir: | ||
output_file_path = Path(output_dir) | ||
if output_file_path.is_dir(): | ||
output_file_path /= f"{Path(yaml_file).stem}.h" | ||
else: | ||
output_file_path = Path(f"{Path(yaml_file).stem}.h") | ||
|
||
if not export_decls and h_def_file: | ||
with open(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) | ||
|
||
print(f"Generated header file: {output_file_path}") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Generate header files from YAML and .h.def templates" | ||
) | ||
parser = argparse.ArgumentParser(description="Generate header files from YAML") | ||
parser.add_argument( | ||
"yaml_file", help="Path to the YAML file containing header specification" | ||
) | ||
parser.add_argument("h_def_file", help="Path to the .h.def template file") | ||
parser.add_argument( | ||
"--output_dir", | ||
default=".", | ||
help="Directory to output the generated header file", | ||
) | ||
parser.add_argument( | ||
"--h_def_file", | ||
help="Path to the .h.def template file (required if not using --export_decls)", | ||
) | ||
parser.add_argument( | ||
"--add_function", | ||
nargs=6, | ||
|
@@ -264,6 +286,21 @@ def main(yaml_file, h_def_file, output_dir, add_function=None): | |
), | ||
help="Add a function to the YAML file", | ||
) | ||
parser.add_argument( | ||
"--e", action="append", help="Entry point to include", dest="entry_points" | ||
) | ||
parser.add_argument( | ||
"--export-decls", | ||
action="store_true", | ||
help="Flag to use GpuHeader for exporting declarations", | ||
) | ||
args = parser.parse_args() | ||
|
||
main(args.yaml_file, args.h_def_file, args.output_dir, args.add_function) | ||
main( | ||
args.yaml_file, | ||
args.output_dir, | ||
args.h_def_file, | ||
args.add_function, | ||
args.entry_points, | ||
args.export_decls, | ||
) |
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.
why did these get removed?
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.
Was already in the h.def prior so we were duplicating enums.