Repository navigation
Expand file tree
/
Copy pathgen_exports.py
More file actions
106 lines (89 loc) · 4.1 KB
/
Copy pathgen_exports.py
File metadata and controls
106 lines (89 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""Write src/harfbuzz.cppm and src/ft.cppm from the pinned HarfBuzz headers.
src/harfbuzz.cppm exports, by `export using`, every type, enumerator and
function that <hb.h>, <hb-ot.h> and <hb-aat.h> declare (the upstream globals,
unchanged) and the C++ helpers of <hb-cplusplus.hh> (namespace hb). It
re-exports mcpplibs.mwrap.harfbuzz.ft and mcpplibs.mwrap.harfbuzz.macros.
src/ft.cppm exports the functions of <hb-ft.h>, the FreeType integration.
Their FreeType parameter types (FT_Face …) belong to FreeType, exported by
whatever brings FreeType in: mcpplibs.mwrap.freetype, or <ft2build.h>.
With --check, nothing is written and a difference fails.
"""
import argparse
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent))
from inventory import fetch, surface, CPLUSPLUS # noqa: E402
ROOT = '''// Generated by tools/gen_exports.py from HarfBuzz 14.3.0's headers — do not
// edit by hand.
//
// mcpplibs.mwrap.harfbuzz — HarfBuzz, the OpenType text shaping engine, as a
// C++ module: the API of <hb.h>, <hb-ot.h> and <hb-aat.h> and the C++ helpers
// of <hb-cplusplus.hh>. Every exported name is the upstream entity, unchanged.
// The root re-exports the FreeType integration (<hb-ft.h>) and the macro
// mappings. The library is compat.harfbuzz, built from the same release.
module;
#include <hb.h>
#include <hb-ot.h>
#include <hb-aat.h>
#include <hb-cplusplus.hh>
export module mcpplibs.mwrap.harfbuzz;
export import mcpplibs.mwrap.harfbuzz.ft;
export import mcpplibs.mwrap.harfbuzz.macros;
// A using-declaration of a deprecated function is not a use of it; calling
// one through the module warns as calling it through the header does.
#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
'''
FT = '''// Generated by tools/gen_exports.py from HarfBuzz 14.3.0's <hb-ft.h> — do not
// edit by hand.
//
// mcpplibs.mwrap.harfbuzz.ft — HarfBuzz's FreeType integration: hb_face_t and
// hb_font_t over an FT_Face, and FreeType font functions for any hb_font_t.
// compat.harfbuzz always builds it (HAVE_FREETYPE over compat.freetype); the
// root module re-exports it. FT_Face and the other FreeType types are
// FreeType's own: import mcpplibs.mwrap.freetype, or include <ft2build.h>.
module;
#include <hb-ft.h>
export module mcpplibs.mwrap.harfbuzz.ft;
#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
'''
def render(sf):
out = [ROOT]
out.append(f'// types: typedef names and struct/union tags ({len(sf.types)})')
out += [f'export using ::{n};' for n in sf.types]
out.append(f'\n// enumerators ({len(sf.enumerators)})')
out += [f'export using ::{n};' for n in sf.enumerators]
out.append(f'\n// functions ({len(sf.functions)})')
out += [f'export using ::{n};' for n in sf.functions]
out.append(f'\n// {CPLUSPLUS}: hb::shared_ptr / hb::unique_ptr over the vtable of each object type')
out.append('export namespace hb {')
out += [f'using hb::{n};' for n in sf.cplusplus]
out.append('} // namespace hb\n')
root = '\n'.join(out)
ft = '\n'.join([FT, f'// functions ({len(sf.ft_functions)})'] + [f'export using ::{n};' for n in sf.ft_functions]) + '\n'
return root, ft
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--check', action='store_true')
args = parser.parse_args()
root = Path(__file__).resolve().parents[1]
texts = render(surface(fetch(root)))
stale = []
for path, text in zip((root / 'src/harfbuzz.cppm', root / 'src/ft.cppm'), texts):
current = path.read_text(encoding='utf-8') if path.exists() else None
if current == text:
continue
if args.check:
stale.append(path.relative_to(root).as_posix())
else:
path.parent.mkdir(exist_ok=True)
path.write_text(text, encoding='utf-8', newline='\n')
print('wrote', path.relative_to(root).as_posix())
if stale:
sys.exit('not what tools/gen_exports.py writes: ' + ', '.join(stale))
if __name__ == '__main__':
main()