Skip to content

Commit 6d3bfdd

Browse files
authored
[libc] Make hdrgen support macro_header YAML field. (llvm#123265)
A macro can specify macro_header instead of macro_value to indicate that an llvm-libc-macros/ header file is supposed to define this macro. This is used for dlfcn.h, which previously bogusly redefined the RTLD_* macros to empty.
1 parent 3c2ba68 commit 6d3bfdd

File tree

9 files changed

+50
-17
lines changed

9 files changed

+50
-17
lines changed

libc/include/dlfcn.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ header: dlfcn.h
22
header_template: dlfcn.h.def
33
macros:
44
- macro_name: RTLD_LAZY
5-
macro_value: null
5+
macro_header: dlfcn-macros.h
66
- macro_name: RTLD_NOW
7-
macro_value: null
7+
macro_header: dlfcn-macros.h
88
- macro_name: RTLD_GLOBAL
9-
macro_value: null
9+
macro_header: dlfcn-macros.h
1010
- macro_name: RTLD_LOCAL
11-
macro_value: null
11+
macro_header: dlfcn-macros.h
1212
types: []
1313
enums: []
1414
objects: []

libc/utils/hdrgen/gpu_headers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from header import HeaderFile
1010

11+
1112
class GpuHeaderFile(HeaderFile):
1213
def __str__(self):
1314
content = []

libc/utils/hdrgen/header.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#
77
# ==-------------------------------------------------------------------------==#
88

9+
from pathlib import PurePath
10+
911

1012
class HeaderFile:
1113
def __init__(self, name):
@@ -32,11 +34,26 @@ def add_object(self, object):
3234
def add_function(self, function):
3335
self.functions.append(function)
3436

37+
def includes(self):
38+
return sorted(
39+
{
40+
PurePath("llvm-libc-macros") / macro.header
41+
for macro in self.macros
42+
if macro.header is not None
43+
}
44+
)
45+
3546
def public_api(self):
36-
content = [""]
47+
header_dir = PurePath(self.name).parent
48+
content = [
49+
f'#include "{file.relative_to(header_dir)}"' for file in self.includes()
50+
] + [""]
3751

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

4158
for type_ in self.types:
4259
content.append(f"{type_}")
@@ -76,11 +93,9 @@ def public_api(self):
7693
content.append(f"#endif // {current_guard}")
7794
content.append("")
7895

79-
for object in self.objects:
80-
content.append(str(object))
96+
content.extend(str(object) for object in self.objects)
8197
if self.objects:
82-
content.append("\n__END_C_DECLS")
83-
else:
84-
content.append("__END_C_DECLS")
98+
content.append("")
99+
content.append("__END_C_DECLS")
85100

86101
return "\n".join(content)

libc/utils/hdrgen/macro.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99

1010
class Macro:
11-
def __init__(self, name, value=None):
11+
def __init__(self, name, value=None, header=None):
1212
self.name = name
1313
self.value = value
14+
self.header = header
1415

1516
def __str__(self):
17+
if self.header != None:
18+
return ""
1619
if self.value != None:
1720
return f"#define {self.name} {self.value}"
18-
else:
19-
return f"#define {self.name}"
21+
return f"#define {self.name}"

libc/utils/hdrgen/tests/expected_output/test_header.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
#include "__llvm-libc-common.h"
1313
#include "llvm-libc-macros/float16-macros.h"
14-
#include "llvm-libc-macros/test_small-macros.h"
1514
#include "llvm-libc-types/float128.h"
1615

16+
#include "llvm-libc-macros/test_more-macros.h"
17+
#include "llvm-libc-macros/test_small-macros.h"
18+
1719
#define MACRO_A 1
1820

1921
#define MACRO_B 2
2022

23+
#define MACRO_C
24+
2125
#include <llvm-libc-types/type_a.h>
2226
#include <llvm-libc-types/type_b.h>
2327

libc/utils/hdrgen/tests/input/test_small.h.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "__llvm-libc-common.h"
1313
#include "llvm-libc-macros/float16-macros.h"
14-
#include "llvm-libc-macros/test_small-macros.h"
1514
#include "llvm-libc-types/float128.h"
1615

1716
%%public_api()

libc/utils/hdrgen/tests/input/test_small.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ macros:
55
macro_value: 1
66
- macro_name: MACRO_B
77
macro_value: 2
8+
- macro_name: MACRO_C
9+
- macro_name: MACRO_D
10+
macro_header: test_small-macros.h
11+
- macro_name: MACRO_E
12+
macro_header: test_more-macros.h
813
types:
914
- type_name: type_a
1015
- type_name: type_b

libc/utils/hdrgen/tests/test_integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def setUp(self):
1010
self.output_dir = TestHeaderGenIntegration.output_dir
1111
self.source_dir = Path(__file__).parent
1212
self.main_script = self.source_dir.parent / "main.py"
13+
self.maxDiff = 80 * 100
1314

1415
def run_script(self, yaml_file, output_file, entry_points):
1516
command = [

libc/utils/hdrgen/yaml_to_classes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ def yaml_to_classes(yaml_data, header_class, entry_points=None):
3838
header.template_file = yaml_data.get("header_template")
3939

4040
for macro_data in yaml_data.get("macros", []):
41-
header.add_macro(Macro(macro_data["macro_name"], macro_data["macro_value"]))
41+
header.add_macro(
42+
Macro(
43+
macro_data["macro_name"],
44+
macro_data.get("macro_value"),
45+
macro_data.get("macro_header"),
46+
)
47+
)
4248

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

0 commit comments

Comments
 (0)