xref: /aosp_15_r20/external/cronet/third_party/libc++/src/utils/generate_libcxx_cppm_in.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# ===----------------------------------------------------------------------===##
2#
3# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#
7# ===----------------------------------------------------------------------===##
8
9import os.path
10import sys
11
12from libcxx.header_information import module_c_headers
13from libcxx.header_information import module_headers
14from libcxx.header_information import header_restrictions
15from libcxx.header_information import headers_not_available
16
17
18def write_file(module):
19    libcxx_module_directory = os.path.join(
20        os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "modules"
21    )
22    with open(
23        os.path.join(libcxx_module_directory, f"{module}.cppm.in"), "w"
24    ) as module_cpp_in:
25        module_cpp_in.write(
26            """\
27// -*- C++ -*-
28//===----------------------------------------------------------------------===//
29//
30// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
31// See https://llvm.org/LICENSE.txt for license information.
32// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33//
34//===----------------------------------------------------------------------===//
35
36// WARNING, this entire header is generated by
37// utils/generate_libcxx_cppm_in.py
38// DO NOT MODIFY!
39
40module;
41
42#include <__config>
43
44// The headers of Table 24: C++ library headers [tab:headers.cpp]
45// and the headers of Table 25: C++ headers for C library facilities [tab:headers.cpp.c]
46"""
47        )
48        for header in module_headers if module == "std" else module_c_headers:
49            if header in header_restrictions:
50                module_cpp_in.write(
51                    f"""\
52#if {header_restrictions[header]}
53#  include <{header}>
54#endif
55"""
56                )
57            else:
58                module_cpp_in.write(f"#include <{header}>\n")
59
60        module_cpp_in.write(
61            """
62// *** Headers not yet available ***
63//
64// This validation is mainly to catch when a new header is added but adding the
65// corresponding .inc file is forgotten. However, the check based on __has_include
66// alone doesn't work on Windows because the Windows SDK is on the include path,
67// and that means the MSVC STL headers can be found as well, tricking __has_include
68// into thinking that libc++ provides the header.
69//
70#ifndef _WIN32
71"""
72        )
73        for header in sorted(headers_not_available):
74            module_cpp_in.write(
75                f"""\
76#  if __has_include(<{header}>)
77#    error "please update the header information for <{header}> in headers_not_available in utils/libcxx/header_information.py"
78#  endif // __has_include(<{header}>)
79"""
80            )
81
82        module_cpp_in.write(
83            f"""#endif // _WIN32
84
85export module {module};
86{'export import std;' if module == 'std.compat' else ''}
87
88{'@LIBCXX_MODULE_STD_INCLUDE_SOURCES@' if module == 'std' else ''}
89{'@LIBCXX_MODULE_STD_COMPAT_INCLUDE_SOURCES@' if module == 'std.compat' else ''}"""
90        )
91
92
93if __name__ == "__main__":
94    if len(sys.argv) != 2 or (sys.argv[1] != "std" and sys.argv[1] != "std.compat"):
95        sys.stderr.write(
96            f"""\
97Usage:
98{os.path.basename(__file__)} (std|std.compat)
99"""
100        )
101        sys.exit(1)
102
103    write_file(sys.argv[1])
104