Skip to content

[libc] Make hdrgen emit correct relative paths for llvm-libc-types #127150

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 3 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
25 changes: 16 additions & 9 deletions libc/utils/hdrgen/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# ==-------------------------------------------------------------------------==#

from pathlib import PurePath
from pathlib import PurePosixPath


class HeaderFile:
Expand Down Expand Up @@ -37,26 +37,33 @@ def add_function(self, function):
def includes(self):
return sorted(
{
PurePath("llvm-libc-macros") / macro.header
PurePosixPath("llvm-libc-macros") / macro.header
for macro in self.macros
if macro.header is not None
}
| {
PurePosixPath("llvm-libc-types") / f"{typ.type_name}.h"
for typ in self.types
}
)

def public_api(self):
header_dir = PurePath(self.name).parent
# Python 3.12 has .relative_to(dir, walk_up=True) for this.
path_prefix = PurePosixPath("../" * (len(PurePosixPath(self.name).parents) - 1))

def relpath(file):
return path_prefix / file

content = [
f'#include "{file.relative_to(header_dir)}"' for file in self.includes()
] + [""]
f"#include {file}"
for file in sorted(f'"{relpath(file)!s}"' for file in self.includes())
]

for macro in self.macros:
# 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_}")
content.extend(["", f"{macro}"])

if self.enumerations:
combined_enum_content = ",\n ".join(
Expand Down
23 changes: 23 additions & 0 deletions libc/utils/hdrgen/tests/expected_output/subdir/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- C standard library header subdir/test.h --------------------------===//
//
// 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
//
//===--------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SUBDIR_TEST_H
#define LLVM_LIBC_SUBDIR_TEST_H

#include "../__llvm-libc-common.h"

#include "../llvm-libc-types/type_a.h"
#include "../llvm-libc-types/type_b.h"

__BEGIN_C_DECLS

type_a func(type_b) __NOEXCEPT;

__END_C_DECLS

#endif // LLVM_LIBC_SUBDIR_TEST_H
5 changes: 2 additions & 3 deletions libc/utils/hdrgen/tests/expected_output/test_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@

#include "llvm-libc-macros/test_more-macros.h"
#include "llvm-libc-macros/test_small-macros.h"
#include "llvm-libc-types/type_a.h"
#include "llvm-libc-types/type_b.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>

enum {
enum_a = value_1,
enum_b = value_2,
Expand Down
16 changes: 16 additions & 0 deletions libc/utils/hdrgen/tests/input/subdir/test.h.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===-- C standard library header subdir/test.h --------------------------===//
//
// 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
//
//===--------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SUBDIR_TEST_H
#define LLVM_LIBC_SUBDIR_TEST_H

#include "../__llvm-libc-common.h"

%%public_api()

#endif // LLVM_LIBC_SUBDIR_TEST_H
12 changes: 12 additions & 0 deletions libc/utils/hdrgen/tests/input/subdir/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
header: subdir/test.h
header_template: test.h.def
types:
- type_name: type_a
- type_name: type_b
functions:
- name: func
return_type: type_a
arguments:
- type: type_b
standards:
- stdc
9 changes: 8 additions & 1 deletion libc/utils/hdrgen/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setUp(self):
self.main_script = self.source_dir.parent / "main.py"
self.maxDiff = 80 * 100

def run_script(self, yaml_file, output_file, entry_points):
def run_script(self, yaml_file, output_file, entry_points=[]):
command = [
"python3",
str(self.main_script),
Expand Down Expand Up @@ -52,6 +52,13 @@ def test_generate_header(self):

self.compare_files(output_file, expected_output_file)

def test_generate_subdir_header(self):
yaml_file = self.source_dir / "input" / "subdir" / "test.yaml"
expected_output_file = self.source_dir / "expected_output" / "subdir" / "test.h"
output_file = self.output_dir / "subdir" / "test.h"
self.run_script(yaml_file, output_file)
self.compare_files(output_file, expected_output_file)


def main():
parser = argparse.ArgumentParser(description="TestHeaderGenIntegration arguments")
Expand Down
3 changes: 0 additions & 3 deletions libc/utils/hdrgen/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@
class Type:
def __init__(self, type_name):
self.type_name = type_name

def __str__(self):
return f"#include <llvm-libc-types/{self.type_name}.h>"