xref: /llvm-project/libcxx/utils/generate_libcxx_cppm_in.py (revision 04757337cdc8ba2da9bc3303fb62af514d5452c5)
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("\n// *** Headers not yet available ***\n")
61        for header in sorted(headers_not_available):
62            module_cpp_in.write(
63                f"""\
64#if __has_include(<{header}>)
65#  error "please update the header information for <{header}> in headers_not_available in utils/libcxx/header_information.py"
66#endif // __has_include(<{header}>)
67"""
68            )
69
70        module_cpp_in.write(
71            f"""
72export module {module};
73{'export import std;' if module == 'std.compat' else ''}
74
75{'@LIBCXX_MODULE_STD_INCLUDE_SOURCES@' if module == 'std' else ''}
76{'@LIBCXX_MODULE_STD_COMPAT_INCLUDE_SOURCES@' if module == 'std.compat' else ''}"""
77        )
78
79
80if __name__ == "__main__":
81    if len(sys.argv) != 2 or (sys.argv[1] != "std" and sys.argv[1] != "std.compat"):
82        sys.stderr.write(
83            f"""\
84Usage:
85{os.path.basename(__file__)} (std|std.compat)
86"""
87        )
88        sys.exit(1)
89
90    write_file(sys.argv[1])
91