Skip to content

[libc] Make hdrgen support macro_header YAML field. #123265

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 6 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions libc/include/dlfcn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ header: dlfcn.h
header_template: dlfcn.h.def
macros:
- macro_name: RTLD_LAZY
macro_value: null
macro_header: dlfcn-macros.h
- macro_name: RTLD_NOW
macro_value: null
macro_header: dlfcn-macros.h
- macro_name: RTLD_GLOBAL
macro_value: null
macro_header: dlfcn-macros.h
- macro_name: RTLD_LOCAL
macro_value: null
macro_header: dlfcn-macros.h
types: []
enums: []
objects: []
Expand Down
1 change: 1 addition & 0 deletions libc/utils/hdrgen/gpu_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from header import HeaderFile


class GpuHeaderFile(HeaderFile):
def __str__(self):
content = []
Expand Down
29 changes: 22 additions & 7 deletions libc/utils/hdrgen/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#
# ==-------------------------------------------------------------------------==#

from pathlib import PurePath


class HeaderFile:
def __init__(self, name):
Expand All @@ -32,11 +34,26 @@ def add_object(self, object):
def add_function(self, function):
self.functions.append(function)

def includes(self):
return sorted(
{
PurePath("llvm-libc-macros") / macro.header
for macro in self.macros
if macro.header is not None
}
)

def public_api(self):
content = [""]
header_dir = PurePath(self.name).parent
content = [
f'#include "{file.relative_to(header_dir)}"' for file in self.includes()
] + [""]

for macro in self.macros:
content.append(f"{macro}\n")
# When there is nothing to define, the Macro object converts to str
# as an empty string. Don't emit a blank line for those cases.
if str(macro):
content.append(f"{macro}\n")

for type_ in self.types:
content.append(f"{type_}")
Expand Down Expand Up @@ -76,11 +93,9 @@ def public_api(self):
content.append(f"#endif // {current_guard}")
content.append("")

for object in self.objects:
content.append(str(object))
content.extend(str(object) for object in self.objects)
if self.objects:
content.append("\n__END_C_DECLS")
else:
content.append("__END_C_DECLS")
content.append("")
content.append("__END_C_DECLS")

return "\n".join(content)
8 changes: 5 additions & 3 deletions libc/utils/hdrgen/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@


class Macro:
def __init__(self, name, value=None):
def __init__(self, name, value=None, header=None):
self.name = name
self.value = value
self.header = header

def __str__(self):
if self.header != None:
return ""
if self.value != None:
return f"#define {self.name} {self.value}"
else:
return f"#define {self.name}"
return f"#define {self.name}"
6 changes: 5 additions & 1 deletion libc/utils/hdrgen/tests/expected_output/test_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

#include "__llvm-libc-common.h"
#include "llvm-libc-macros/float16-macros.h"
#include "llvm-libc-macros/test_small-macros.h"
#include "llvm-libc-types/float128.h"

#include "llvm-libc-macros/test_more-macros.h"
#include "llvm-libc-macros/test_small-macros.h"

#define MACRO_A 1

#define MACRO_B 2

#define MACRO_C

#include <llvm-libc-types/type_a.h>
#include <llvm-libc-types/type_b.h>

Expand Down
1 change: 0 additions & 1 deletion libc/utils/hdrgen/tests/input/test_small.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "__llvm-libc-common.h"
#include "llvm-libc-macros/float16-macros.h"
#include "llvm-libc-macros/test_small-macros.h"
#include "llvm-libc-types/float128.h"

%%public_api()
Expand Down
5 changes: 5 additions & 0 deletions libc/utils/hdrgen/tests/input/test_small.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ macros:
macro_value: 1
- macro_name: MACRO_B
macro_value: 2
- macro_name: MACRO_C
- macro_name: MACRO_D
macro_header: test_small-macros.h
- macro_name: MACRO_E
macro_header: test_more-macros.h
types:
- type_name: type_a
- type_name: type_b
Expand Down
1 change: 1 addition & 0 deletions libc/utils/hdrgen/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def setUp(self):
self.output_dir = TestHeaderGenIntegration.output_dir
self.source_dir = Path(__file__).parent
self.main_script = self.source_dir.parent / "main.py"
self.maxDiff = 80 * 100

def run_script(self, yaml_file, output_file, entry_points):
command = [
Expand Down
8 changes: 7 additions & 1 deletion libc/utils/hdrgen/yaml_to_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ def yaml_to_classes(yaml_data, header_class, entry_points=None):
header.template_file = yaml_data.get("header_template")

for macro_data in yaml_data.get("macros", []):
header.add_macro(Macro(macro_data["macro_name"], macro_data["macro_value"]))
header.add_macro(
Macro(
macro_data["macro_name"],
macro_data.get("macro_value"),
macro_data.get("macro_header"),
)
)

types = yaml_data.get("types", [])
sorted_types = sorted(types, key=lambda x: x["type_name"])
Expand Down