The open source OpenXR runtime
1#!/usr/bin/env python3
2# Copyright 2022-2023, Collabora, Ltd.
3#
4# SPDX-License-Identifier: BSL-1.0
5#
6# Original author: Rylie Pavlik <rylie.pavlik@collabora.com
7"""Script to set up mapping.imp, etc for include-what-you-use."""
8from pathlib import Path
9from typing import List
10
11_REPO = Path(__file__).parent.parent.resolve()
12_SCRIPT_DIR = Path(__file__).parent.resolve()
13_OUTPUT_FILENAME = _SCRIPT_DIR / "mapping.imp"
14
15_FILE = Path(__file__).resolve()
16
17_WRAPPERS = [
18 """{ include: ["@[<\\"]vulkan/.+", "private", "\\"xrt/xrt_vulkan_includes.h\\"", "public"] },""",
19 """{ include: ["\\"EGL/eglplatform.h\\"", "private", "\\"ogl/ogl_api.h\\"", "public"] },""",
20 """{ include: ["<math.h>", "public", "\\"math/m_mathinclude.h\\"", "public"] },""",
21 """{ include: ["<cmath>", "public", "\\"math/m_mathinclude.h\\"", "public"] },""",
22 """{ symbol: ["M_PI", "public", "\\"math/m_mathinclude.h\\"", "public"] },""",
23 """{ symbol: ["M_PIl", "public", "\\"math/m_mathinclude.h\\"", "public"] },""",
24 """{ symbol: ["M_1_PI", "public", "\\"math/m_mathinclude.h\\"", "public"] },""",
25 """{ symbol: ["ssize_t", "public", "\\"xrt/xrt_compiler.h\\"", "public"] },""",
26]
27
28_MISC_ERRORS = [
29 """{ include: ["@[<\\"]bits/exception.h[>\\"]", "private", "<exception>", "public"] },""",
30 """{ include: ["@[<\\"]ext/alloc_traits.h[>\\"]", "private", "<vector>", "public"] },""",
31 """{ include: ["@[<\\"]bits/types/struct_iovec.h.", "private", "<sys/uio.h>", "public"] },""",
32]
33
34_MISC_DEPS = [
35 """{ include: ["@[<\\"]jmoreconfig.h>\\"]", "private", "\\"jpeglib.h\\"", "public"] },""",
36]
37
38_OPENCV_ENTRIES = [
39 """{ include: ["@<opencv2/core/.*.inl.hpp>", "private", "<opencv2/core.hpp>", "public"] },""",
40 """{ include: ["@<opencv2/core/hal/.*", "private", "<opencv2/core.hpp>", "public"] },""",
41]
42
43
44def _generate_openxr_entries():
45 for header in ("openxr_platform.h", "openxr_platform_defines.h"):
46 yield """{ include: ["@[<\\"]openxr/%s.*", "private", "\\"xrt/xrt_openxr_includes.h\\"", "public"] },""" % header
47
48
49def _generate_eigen_entries():
50 for stubborn_header in ("ArrayBase", "MatrixBase", "DenseBase"):
51 yield """{ include: ["@[<\\"]src/Core/%s.h[>\\"]", "private", "<Eigen/Core>", "public"] },""" % (
52 stubborn_header,
53 )
54 for module in ("Core", "Geometry", "LU", "SparseCore", "Sparse"):
55 yield """{ include: ["@[<\\"]Eigen/src/%s/.*", "private", "<Eigen/%s>", "public"] },""" % (
56 module,
57 module,
58 )
59 yield """{ include: ["@[<\\"]src/%s/.*", "private", "<Eigen/%s>", "public"] },""" % (
60 module,
61 module,
62 )
63
64
65_CONFIG_HEADERS = (
66 "xrt_config_android.h",
67 "xrt_config_build.h",
68 "xrt_config_drivers.h",
69 "xrt_config_have.h",
70 "xrt_config_vulkan.h",
71)
72
73
74def _generate_config_header_defines():
75 for header in _CONFIG_HEADERS:
76 input_filename = header + ".cmake_in"
77 input_file = _REPO / "src" / "xrt" / "include" / "xrt" / input_filename
78 with open(str(input_file), "r", encoding="utf-8") as fp:
79 for line in fp:
80 if line.startswith("#cmakedefine"):
81 parts = line.split(" ")
82 symbol = parts[1].strip()
83 yield """{ symbol: ["%s", "public", "\\"xrt/%s\\"", "public"] },""" % (
84 symbol,
85 header,
86 )
87
88
89def get_all_entries():
90 entries = []
91 entries.extend(_WRAPPERS)
92 entries.extend(_MISC_ERRORS)
93 entries.extend(_MISC_DEPS)
94 # entries.extend(_OPENCV_ENTRIES)
95 # entries.extend(_generate_eigen_entries())
96 entries.extend(_generate_openxr_entries())
97 entries.extend(_generate_config_header_defines())
98 return entries
99
100
101# REUSE-IgnoreStart
102
103
104def _find_copyright_lines(fn: Path, *args):
105 with open(fn, encoding="utf-8") as f:
106 copyright_lines = [line.strip() for line in f if line.startswith("# Copyright")]
107
108 known_lines = set(copyright_lines)
109 for alt_fn in args:
110 with open(_SCRIPT_DIR / alt_fn, encoding="utf-8") as f:
111 new_lines = [line.strip() for line in f if line.startswith("# Copyright")]
112 copyright_lines.extend(line for line in new_lines if line not in known_lines)
113 known_lines.update(new_lines)
114
115 return copyright_lines
116
117
118def write_mapping_file(entries: List[str], output_filename: Path, script_name: str):
119 """Write an IWYU mapping file with the given entries."""
120 # Grab all lines from this and our other script.
121 lines = _find_copyright_lines(_FILE, script_name)
122 lines += [
123 """#""",
124 "# SPDX-License" + "-Identifier: BSL-1.0", # split to avoid breaking REUSE tool
125 """#""",
126 """# GENERATED - edit %s instead of this file""" % script_name,
127 "[",
128 ]
129 lines.extend(entries)
130 lines += "]"
131 content = "".join(line + "\n" for line in lines)
132 with open(output_filename, "w", encoding="utf-8") as fp:
133 fp.write(content)
134
135
136# REUSE-IgnoreEnd
137
138
139def write_file():
140 my_script_fn = Path(__file__).resolve().name
141 eigen_path = _SCRIPT_DIR / "eigen.imp"
142 write_mapping_file(list(_generate_eigen_entries()), eigen_path, my_script_fn)
143
144 opencv_path = _SCRIPT_DIR / "opencv.imp"
145 write_mapping_file(_OPENCV_ENTRIES, opencv_path, my_script_fn)
146 entries = get_all_entries()
147 for ref_path in (eigen_path, opencv_path):
148 entries.append("""{ ref: "%s" },""" % ref_path.name)
149 write_mapping_file(entries, _OUTPUT_FILENAME, my_script_fn)
150
151
152if __name__ == "__main__":
153 write_file()