1#!/usr/bin/env python 2 3import contextlib 4import glob 5import io 6import os 7import pathlib 8import re 9 10header_restrictions = { 11 "barrier": "!defined(_LIBCPP_HAS_NO_THREADS)", 12 "future": "!defined(_LIBCPP_HAS_NO_THREADS)", 13 "latch": "!defined(_LIBCPP_HAS_NO_THREADS)", 14 "mutex": "!defined(_LIBCPP_HAS_NO_THREADS)", 15 "semaphore": "!defined(_LIBCPP_HAS_NO_THREADS)", 16 "shared_mutex": "!defined(_LIBCPP_HAS_NO_THREADS)", 17 "thread": "!defined(_LIBCPP_HAS_NO_THREADS)", 18 19 "filesystem": "!defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)", 20 21 "clocale": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 22 "codecvt": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 23 "fstream": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 24 "iomanip": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 25 "ios": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 26 "iostream": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 27 "istream": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 28 "locale.h": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 29 "locale": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 30 "ostream": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 31 "regex": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 32 "sstream": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 33 "streambuf": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 34 "strstream": "!defined(_LIBCPP_HAS_NO_LOCALIZATION)", 35 36 "wctype.h": "!defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)", 37 "cwctype": "!defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)", 38 "cwchar": "!defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)", 39 "wchar.h": "!defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)", 40 41 "experimental/coroutine": "!defined(_LIBCPP_HAS_NO_EXPERIMENTAL_COROUTINES)", 42 43 "experimental/regex": "!defined(_LIBCPP_HAS_NO_LOCALIZATION) && __cplusplus >= 201103L", 44 "experimental/deque": "__cplusplus >= 201103L", 45 "experimental/map": "__cplusplus >= 201103L", 46 "experimental/memory_resource": "__cplusplus >= 201103L", 47 "experimental/forward_list": "__cplusplus >= 201103L", 48 "experimental/list": "__cplusplus >= 201103L", 49 "experimental/set": "__cplusplus >= 201103L", 50 "experimental/string": "__cplusplus >= 201103L", 51 "experimental/unordered_map": "__cplusplus >= 201103L", 52 "experimental/unordered_set": "__cplusplus >= 201103L", 53 "experimental/vector": "__cplusplus >= 201103L", 54} 55 56private_headers_still_public_in_modules = [ 57 '__assert', '__bsd_locale_defaults.h', '__bsd_locale_fallbacks.h', '__config', 58 '__config_site.in', '__debug', '__hash_table', 59 '__threading_support', '__tree', '__undef_macros' 60] 61 62def find_script(file): 63 """Finds the script used to generate a file inside the file itself. The script is delimited by 64 BEGIN-SCRIPT and END-SCRIPT markers. 65 """ 66 with open(file, 'r') as f: 67 content = f.read() 68 69 match = re.search(r'^BEGIN-SCRIPT$(.+)^END-SCRIPT$', content, flags=re.MULTILINE | re.DOTALL) 70 if not match: 71 raise RuntimeError("Was unable to find a script delimited with BEGIN-SCRIPT/END-SCRIPT markers in {}".format(test_file)) 72 return match.group(1) 73 74def execute_script(script, variables): 75 """Executes the provided Mako template with the given variables available during the 76 evaluation of the script, and returns the result. 77 """ 78 code = compile(script, 'fake-filename', 'exec') 79 output = io.StringIO() 80 with contextlib.redirect_stdout(output): 81 exec(code, variables) 82 output = output.getvalue() 83 return output 84 85def generate_new_file(file, new_content): 86 """Generates the new content of the file by inserting the new content in-between 87 two '// GENERATED-MARKER' markers located in the file. 88 """ 89 with open(file, 'r') as f: 90 old_content = f.read() 91 92 try: 93 before, begin_marker, _, end_marker, after = re.split(r'(// GENERATED-MARKER\n)', old_content, flags=re.MULTILINE | re.DOTALL) 94 except ValueError: 95 raise RuntimeError("Failed to split {} based on markers, please make sure the file has exactly two '// GENERATED-MARKER' occurrences".format(file)) 96 97 return before + begin_marker + new_content + end_marker + after 98 99def produce(test_file, variables): 100 script = find_script(test_file) 101 result = execute_script(script, variables) 102 new_content = generate_new_file(test_file, result) 103 with open(test_file, 'w', newline='\n') as f: 104 f.write(new_content) 105 106def is_header(file): 107 """Returns whether the given file is a header (i.e. not a directory or the modulemap file).""" 108 return not file.is_dir() and not file.name == 'module.modulemap' 109 110def main(): 111 monorepo_root = pathlib.Path(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) 112 include = pathlib.Path(os.path.join(monorepo_root, 'libcxx', 'include')) 113 test = pathlib.Path(os.path.join(monorepo_root, 'libcxx', 'test')) 114 assert(monorepo_root.exists()) 115 116 toplevel_headers = sorted(str(p.relative_to(include)) for p in include.glob('[a-z]*') if is_header(p)) 117 experimental_headers = sorted(str(p.relative_to(include)) for p in include.glob('experimental/[a-z]*') if is_header(p)) 118 extended_headers = sorted(str(p.relative_to(include)) for p in include.glob('ext/[a-z]*') if is_header(p)) 119 public_headers = toplevel_headers + experimental_headers + extended_headers 120 private_headers = sorted(str(p.relative_to(include)) for p in include.rglob('*') if is_header(p) and str(p.relative_to(include)).startswith('__')) 121 variables = { 122 'toplevel_headers': toplevel_headers, 123 'experimental_headers': experimental_headers, 124 'extended_headers': extended_headers, 125 'public_headers': public_headers, 126 'private_headers': private_headers, 127 'header_restrictions': header_restrictions, 128 'private_headers_still_public_in_modules': private_headers_still_public_in_modules 129 } 130 131 produce(test.joinpath('libcxx/assertions/headers_declare_assertion_handler.sh.cpp'), variables) 132 produce(test.joinpath('libcxx/clang_tidy.sh.cpp'), variables) 133 produce(test.joinpath('libcxx/double_include.sh.cpp'), variables) 134 produce(test.joinpath('libcxx/min_max_macros.compile.pass.cpp'), variables) 135 produce(test.joinpath('libcxx/nasty_macros.compile.pass.cpp'), variables) 136 produce(test.joinpath('libcxx/no_assert_include.compile.pass.cpp'), variables) 137 produce(test.joinpath('libcxx/private_headers.verify.cpp'), variables) 138 139 140if __name__ == '__main__': 141 main() 142