Debian Codegraph @

3rdparty/catch2/tools/scripts/generateAmalgamatedFiles.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import os
4 import re
5 import datetime
6
7 from scriptCommon import catchPath
8 from releaseCommon import Version
9
10 root_path = os.path.join(catchPath, 'src')
11 starting_header = os.path.join(root_path, 'catch2', 'catch_all.hpp')
12 output_header = os.path.join(catchPath, 'extras', 'catch_amalgamated.hpp')
13 output_cpp = os.path.join(catchPath, 'extras', 'catch_amalgamated.cpp')
14
15 # These are the copyright comments in each file, we want to ignore them
16 copyright_lines = [
17 '// Copyright Catch2 Authors\n',
18 '// Distributed under the Boost Software License, Version 1.0.\n',
19 '// (See accompanying file LICENSE.txt or copy at\n',
20 '// https://www.boost.org/LICENSE_1_0.txt)\n',
21 '// SPDX-License-Identifier: BSL-1.0\n',
22 ]
23
24 # The header of the amalgamated file: copyright information + explanation
25 # what this file is.
26 file_header = '''\
27 // Copyright Catch2 Authors
28 // Distributed under the Boost Software License, Version 1.0.
29 // (See accompanying file LICENSE.txt or copy at
30 // https://www.boost.org/LICENSE_1_0.txt)
31
32 // SPDX-License-Identifier: BSL-1.0
33
34 // Catch v{version_string}
35 // Generated: {generation_time}
36 // ----------------------------------------------------------
37 // This file is an amalgamation of multiple different files.
38 // You probably shouldn't edit it directly.
39 // ----------------------------------------------------------
40 '''
41
42 # Returns file header with proper version string and generation time
43 def formatted_file_header(version):
44 return file_header.format(version_string=version.getVersionString(),
45 generation_time=datetime.datetime.now())
46
47 # Which headers were already concatenated (and thus should not be
48 # processed again)
49 concatenated_headers = set()
50
51 internal_include_parser = re.compile(r'\s*#include <(catch2/.*)>.*')
52
53 def concatenate_file(out, filename: str, expand_headers: bool) -> int:
54 # Gathers statistics on how many headers were expanded
55 concatenated = 1
56 with open(filename, mode='r', encoding='utf-8') as input:
57 for line in input:
58 if line in copyright_lines:
59 continue
60 m = internal_include_parser.match(line)
61 # anything that isn't a Catch2 header can just be copied to
62 # the resulting file
63 if not m:
64 out.write(line)
65 continue
66
67 # TBD: We can also strip out include guards from our own
68 # headers, but it wasn't worth the time at the time of writing
69 # this script.
70
71 # We do not want to expand headers for the cpp file
72 # amalgamation but neither do we want to copy them to output
73 if not expand_headers:
74 continue
75
76 next_header = m.group(1)
77 # We have to avoid re-expanding the same header over and
78 # over again, or the header will end up with couple
79 # hundred thousands lines (~300k as of preview3 :-) )
80 if next_header in concatenated_headers:
81 continue
82
83 # Skip including the auto-generated user config file,
84 # because it has not been generated yet at this point.
85 # The code around it should be written so that just not including
86 # it is equivalent with all-default user configuration.
87 if next_header == 'catch2/catch_user_config.hpp':
88 concatenated_headers.add(next_header)
89 continue
90
91 concatenated_headers.add(next_header)
92 concatenated += concatenate_file(out, os.path.join(root_path, next_header), expand_headers)
93
94 return concatenated
95
96
97 def generate_header():
98 with open(output_header, mode='w', encoding='utf-8') as header:
99 header.write(formatted_file_header(Version()))
100 header.write('#ifndef CATCH_AMALGAMATED_HPP_INCLUDED\n')
101 header.write('#define CATCH_AMALGAMATED_HPP_INCLUDED\n')
102 print('Concatenated {} headers'.format(concatenate_file(header, starting_header, True)))
103 header.write('#endif // CATCH_AMALGAMATED_HPP_INCLUDED\n')
104
105 def generate_cpp():
106 from glob import glob
107 cpp_files = sorted(glob(os.path.join(root_path, 'catch2', '**/*.cpp'), recursive=True))
108 with open(output_cpp, mode='w', encoding='utf-8') as cpp:
109 cpp.write(formatted_file_header(Version()))
110 cpp.write('\n#include "catch_amalgamated.hpp"\n')
111 concatenate_file(cpp, os.path.join(root_path, 'catch2/internal/catch_windows_h_proxy.hpp'), False)
112 for file in cpp_files:
113 concatenate_file(cpp, file, False)
114 print('Concatenated {} cpp files'.format(len(cpp_files)))
115
116 if __name__ == "__main__":
117 generate_header()
118 generate_cpp()
119
120
121 # Notes:
122 # * For .cpp files, internal includes have to be stripped and rewritten
123 # * for .hpp files, internal includes have to be resolved and included
124 # * The .cpp file needs to start with `#include "catch_amalgamated.hpp"
125 # * include guards can be left/stripped, doesn't matter
126 # * *.cpp files should be included sorted, to minimize diffs between versions
127 # * *.hpp files should also be somehow sorted -> use catch_all.hpp as the
128 # * entrypoint
129 # * allow disabling main in the .cpp amalgamation