1#!/usr/bin/env python
2
3import glob
4import os
5import posixpath
6import re
7
8
9def get_libcxx_paths():
10    utils_path = os.path.dirname(os.path.abspath(__file__))
11    script_name = os.path.basename(__file__)
12    assert os.path.exists(utils_path)
13    src_root = os.path.dirname(utils_path)
14    include_path = os.path.join(src_root, 'include')
15    assert os.path.exists(include_path)
16    libcxx_test_path = os.path.join(src_root, 'test', 'libcxx')
17    assert os.path.exists(libcxx_test_path)
18    return script_name, src_root, include_path, libcxx_test_path
19
20
21script_name, source_root, include_path, libcxx_test_path = get_libcxx_paths()
22
23header_markup = {
24    "atomic": ["ifndef _LIBCPP_HAS_NO_THREADS"],
25    "barrier": ["ifndef _LIBCPP_HAS_NO_THREADS"],
26    "future": ["ifndef _LIBCPP_HAS_NO_THREADS"],
27    "latch": ["ifndef _LIBCPP_HAS_NO_THREADS"],
28    "mutex": ["ifndef _LIBCPP_HAS_NO_THREADS"],
29    "shared_mutex": ["ifndef _LIBCPP_HAS_NO_THREADS"],
30    "semaphore": ["ifndef _LIBCPP_HAS_NO_THREADS"],
31    "thread": ["ifndef _LIBCPP_HAS_NO_THREADS"],
32
33    "filesystem": ["ifndef _LIBCPP_HAS_NO_FILESYSTEM_LIBRARY"],
34    "experimental/filesystem": ["ifndef _LIBCPP_HAS_NO_FILESYSTEM_LIBRARY"],
35
36    "clocale": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
37    "codecvt": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
38    "fstream": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
39    "iomanip": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
40    "ios": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
41    "iostream": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
42    "istream": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
43    "locale": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
44    "locale.h": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
45    "ostream": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
46    "regex": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
47    "sstream": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
48    "streambuf": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
49    "strstream": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
50
51    "experimental/coroutine": ["if defined(__cpp_coroutines)"],
52    "experimental/regex": ["ifndef _LIBCPP_HAS_NO_LOCALIZATION"],
53}
54
55allowed_extensions = ['', '.h']
56indent_width = 4
57
58
59begin_pattern = """\
60////////////////////////////////////////////////////////////////////////////////
61// BEGIN-GENERATED-HEADERS
62////////////////////////////////////////////////////////////////////////////////
63"""
64
65warning_note = """\
66// WARNING: This test was generated by {script_name}
67// and should not be edited manually.
68
69""".format(script_name=script_name)
70
71end_pattern = """\
72////////////////////////////////////////////////////////////////////////////////
73// END-GENERATED-HEADERS
74////////////////////////////////////////////////////////////////////////////////
75"""
76
77generated_part_pattern = re.compile(re.escape(begin_pattern) + ".*" + re.escape(end_pattern),
78                                    re.MULTILINE | re.DOTALL)
79
80headers_template = """\
81// Top level headers
82{top_level_headers}
83
84// experimental headers
85#if __cplusplus >= 201103L
86{experimental_headers}
87#endif // __cplusplus >= 201103L
88
89// extended headers
90{extended_headers}
91"""
92
93
94def should_keep_header(p, exclusions=None):
95    if os.path.isdir(p):
96        return False
97
98    if exclusions:
99        relpath = os.path.relpath(p, include_path)
100        relpath = posixpath.join(*os.path.split(relpath))
101        if relpath in exclusions:
102            print('Excluded file:', relpath)
103            return False
104
105    return os.path.splitext(p)[1] in allowed_extensions
106
107
108def produce_include(relpath, indent_level, post_include=None):
109    relpath = posixpath.join(*os.path.split(relpath))
110    template = "{preambule}#{indentation}include <{include}>{post_include}{postambule}"
111
112    base_indentation = ' '*(indent_width * indent_level)
113    next_indentation = base_indentation + ' '*(indent_width)
114    post_include = "\n{}".format(post_include) if post_include else ''
115
116    markup = header_markup.get(relpath, None)
117    if markup:
118        preambule = '#{indentation}{directive}\n'.format(
119            directive=markup[0],
120            indentation=base_indentation,
121        )
122        postambule = '\n#{indentation}endif'.format(
123            indentation=base_indentation,
124        )
125        indentation = next_indentation
126    else:
127        preambule = ''
128        postambule = ''
129        indentation = base_indentation
130
131    return template.format(
132        include=relpath,
133        post_include=post_include,
134        preambule=preambule,
135        postambule=postambule,
136        indentation=indentation,
137    )
138
139
140def produce_headers(path_parts, indent_level, post_include=None, exclusions=None):
141    pattern = os.path.join(*path_parts, '[a-z]*')
142
143    files = sorted(glob.glob(pattern, recursive=False))
144
145    include_headers = [
146        produce_include(os.path.relpath(p, include_path),
147                        indent_level, post_include=post_include)
148        for p in files
149        if should_keep_header(p, exclusions)
150    ]
151
152    return '\n'.join(include_headers)
153
154
155def produce_top_level_headers(post_include=None, exclusions=None):
156    return produce_headers([include_path], 0, post_include=post_include, exclusions=exclusions)
157
158
159def produce_experimental_headers(post_include=None, exclusions=None):
160    return produce_headers([include_path, 'experimental'], 1, post_include=post_include, exclusions=exclusions)
161
162
163def produce_extended_headers(post_include=None, exclusions=None):
164    return produce_headers([include_path, 'ext'], 0, post_include=post_include, exclusions=exclusions)
165
166
167def replace_generated_headers(test_path, test_str):
168    with open(test_path, 'r') as f:
169        content = f.read()
170
171    preambule = begin_pattern + '\n// clang-format off\n\n' + warning_note
172    postambule = '\n// clang-format on\n\n' + end_pattern
173    content = generated_part_pattern.sub(
174        preambule + test_str + postambule, content)
175
176    with open(test_path, 'w', newline='\n') as f:
177        f.write(content)
178
179
180def produce_test(test_filename, exclusions=None, post_include=None):
181    test_str = headers_template.format(
182        top_level_headers=produce_top_level_headers(
183            post_include=post_include,
184            exclusions=exclusions,
185        ),
186        experimental_headers=produce_experimental_headers(
187            post_include=post_include,
188        ),
189        extended_headers=produce_extended_headers(
190            post_include=post_include,
191        ),
192    )
193
194    replace_generated_headers(os.path.join(
195        libcxx_test_path, test_filename), test_str)
196
197
198def main():
199    produce_test('double_include.sh.cpp')
200    produce_test('min_max_macros.compile.pass.cpp',
201                 post_include='TEST_MACROS();')
202    produce_test('no_assert_include.compile.pass.cpp',
203                 exclusions=['cassert'])
204
205
206if __name__ == '__main__':
207    main()
208